Skip to content

Commit

Permalink
ociindex: allow readonly access
Browse files Browse the repository at this point in the history
If file lock can't be taken because of a permission or readonly
error then assume that the index is immutable and locks are not needed.

Writer side is unchanged as if lock would be skipped then the write
is assumed to fail anyway.

Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Oct 25, 2024
1 parent 076d69e commit 62e4130
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions client/ociindex/ociindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"path"
"syscall"

"github.com/gofrs/flock"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -36,15 +37,18 @@ func (s StoreIndex) Read() (*ocispecs.Index, error) {
lock := flock.New(s.lockPath)
locked, err := lock.TryRLock()
if err != nil {
return nil, errors.Wrapf(err, "could not lock %s", s.lockPath)
}
if !locked {
return nil, errors.Errorf("could not lock %s", s.lockPath)
if !errors.Is(err, syscall.EPERM) && !errors.Is(err, syscall.EROFS) {
return nil, errors.Wrapf(err, "could not lock %s", s.lockPath)
}
} else {
if !locked {
return nil, errors.Errorf("could not lock %s", s.lockPath)
}
defer func() {
lock.Unlock()
os.RemoveAll(s.lockPath)
}()
}
defer func() {
lock.Unlock()
os.RemoveAll(s.lockPath)
}()

b, err := os.ReadFile(s.indexPath)
if err != nil {
Expand Down

0 comments on commit 62e4130

Please sign in to comment.