Repository: containerd/containerd
File: plugins/content/local/store.go (Go)

Code before:
// stored under a verifiable digest.
//
// Store can generally support multi-reader, single-writer ingest of data,
// including resumable ingest.
type store struct {
	root               string
	ls                 LabelStore
	integritySupported bool

	locksMu              sync.Mutex
	locks                map[string]*lock
	ensureIngestRootOnce func() error
}

// NewStore returns a local content store
func NewStore(root string) (content.Store, error) {
	return NewLabeledStore(root, nil)
}

// NewLabeledStore returns a new content store using the provided label store
//
// Note: content stores which are used underneath a metadata store may not
// require labels and should use `NewStore`. `NewLabeledStore` is primarily
// useful for tests or standalone implementations.
func NewLabeledStore(root string, ls LabelStore) (content.Store, error) {
	if err := os.MkdirAll(root, 0755); err != nil {
		return nil, err
	}
	supported, err := fsverity.IsSupported(root)
	if err != nil {
		log.G(context.Background()).WithField("root", root).WithError(err).Warnf("failed to check fsverity support")
	}
	s := &store{
		root:               root,
		ls:                 ls,
		integritySupported: supported,
		locks:              map[string]*lock{},
	}
	s.ensureIngestRootOnce = sync.OnceValue(s.ensureIngestRoot)
	return s, nil
}

func (s *store) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
	p, err := s.blobPath(dgst)
	if err != nil {
		return content.Info{}, fmt.Errorf("calculating blob info path: %w", err)
	}

	fi, err := os.Stat(p)
	if err != nil {
		if os.IsNotExist(err) {

Review comment (bug):
Should we use stat first?

e.g. something like:
```
			if _, err := os.Stat(root); err != nil {
				if !os.IsNotExist(err) {
					return fmt.Errorf("failed to stat %q: %w", root, err)
				}
				if err := os.MkdirAll(root, 0755); err != nil {
					return fmt.Errorf("failed to mkdir %q: %w", root, err)
				}
			}
```

at min should probably be checking if the error is already exists before erroring out

Maybe add a test case for the changes? 

Code after:

// Store is digest-keyed store for content. All data written into the store is
// stored under a verifiable digest.
//
// Store can generally support multi-reader, single-writer ingest of data,
// including resumable ingest.
type store struct {
	root               string
	ls                 LabelStore
	integritySupported bool

	locksMu              sync.Mutex
	locks                map[string]*lock
	ensureIngestRootOnce func() error
}

// NewStore returns a local content store
func NewStore(root string) (content.Store, error) {
	return NewLabeledStore(root, nil)
}

// NewLabeledStore returns a new content store using the provided label store
//
// Note: content stores which are used underneath a metadata store may not
// require labels and should use `NewStore`. `NewLabeledStore` is primarily
// useful for tests or standalone implementations.
func NewLabeledStore(root string, ls LabelStore) (content.Store, error) {
	if _, err := os.Stat(root); err != nil {
		if !errors.Is(err, fs.ErrNotExist) {
			return nil, fmt.Errorf("failed to stat %q: %w", root, err)
		}
		if err := os.MkdirAll(root, 0755); err != nil {
			return nil, fmt.Errorf("failed to mkdir %q: %w", root, err)
		}
	}
	supported, err := fsverity.IsSupported(root)
	if err != nil {
		log.L.WithError(err).WithField("path", root).Warnf("failed check for fsverity support")
	}
	s := &store{
		root:               root,
		ls:                 ls,
		integritySupported: supported,
		locks:              map[string]*lock{},
	}
	s.ensureIngestRootOnce = sync.OnceValue(s.ensureIngestRoot)
	return s, nil
}

func (s *store) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
	p, err := s.blobPath(dgst)