diff --git a/pkg/cache/file/file.go b/pkg/cache/file/file.go index 423913f..d7f6022 100644 --- a/pkg/cache/file/file.go +++ b/pkg/cache/file/file.go @@ -39,6 +39,7 @@ func NewFileCache(dir string) (cache.Cache, error) { } // Store stores the object and returns the metadata +// Fails if the object already exists func (f *Cache) Store(_ context.Context, id string, content io.Reader) (cache.Object, error) { if id == "" { return cache.Object{}, fmt.Errorf("%w id cannot be empty", cache.ErrCreatingObject) @@ -49,6 +50,11 @@ func (f *Cache) Store(_ context.Context, id string, content io.Reader) (cache.Ob } objectDir := filepath.Join(f.dir, id) + + if _, err := os.Stat(objectDir); !errors.Is(err, os.ErrNotExist) { + return cache.Object{}, fmt.Errorf("%w: object already exists %q", cache.ErrCreatingObject, id) + } + // TODO: check permissions err := os.MkdirAll(objectDir, 0o750) if err != nil { diff --git a/pkg/cache/file/file_test.go b/pkg/cache/file/file_test.go index 4353119..ad82486 100644 --- a/pkg/cache/file/file_test.go +++ b/pkg/cache/file/file_test.go @@ -56,8 +56,9 @@ func TestFileCacheStoreObject(t *testing.T) { content: []byte("content"), }, }, - id: "object", - content: []byte("new content"), + id: "object", + content: []byte("new content"), + expectErr: cache.ErrCreatingObject, }, { title: "store empty object",