From 62e4130b5f56d415a30f1a6717622939b8428fdb Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Thu, 24 Oct 2024 17:38:26 -0700 Subject: [PATCH] ociindex: allow readonly access 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 --- client/ociindex/ociindex.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/client/ociindex/ociindex.go b/client/ociindex/ociindex.go index 5321f773d70e..e427f6550806 100644 --- a/client/ociindex/ociindex.go +++ b/client/ociindex/ociindex.go @@ -5,6 +5,7 @@ import ( "io" "os" "path" + "syscall" "github.com/gofrs/flock" ocispecs "github.com/opencontainers/image-spec/specs-go/v1" @@ -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 {