Skip to content

Commit 64e6ccc

Browse files
committed
Fix a race in fs-based DeleteBucket
If a new object is created while RemoveAll is removing files, it might prevent it from finishing successfully. So take a global lock while doing so.
1 parent d085b6e commit 64e6ccc

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

internal/backend/fs.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,17 @@ func getBucketAttributes(path string) (BucketAttrs, error) {
196196

197197
// DeleteBucket removes the bucket from the backend.
198198
func (s *storageFS) DeleteBucket(name string) error {
199-
objs, err := s.ListObjects(name, "", false)
199+
s.mtx.Lock()
200+
defer s.mtx.Unlock()
201+
202+
objs, err := s.listObjects(name, "", false)
200203
if err != nil {
201204
return BucketNotFound
202205
}
203206
if len(objs) > 0 {
204207
return BucketNotEmpty
205208
}
206209

207-
s.mtx.Lock()
208-
defer s.mtx.Unlock()
209210
return os.RemoveAll(filepath.Join(s.rootDir, url.PathEscape(name)))
210211
}
211212

@@ -314,7 +315,10 @@ func (s *storageFS) createObject(obj StreamingObject, conditions Conditions) (St
314315
func (s *storageFS) ListObjects(bucketName string, prefix string, versions bool) ([]ObjectAttrs, error) {
315316
s.mtx.RLock()
316317
defer s.mtx.RUnlock()
318+
return s.listObjects(bucketName, prefix, versions)
319+
}
317320

321+
func (s *storageFS) listObjects(bucketName string, prefix string, versions bool) ([]ObjectAttrs, error) {
318322
objects := []ObjectAttrs{}
319323
bucketPath := filepath.Join(s.rootDir, url.PathEscape(bucketName))
320324
if err := filepath.Walk(bucketPath, func(path string, info fs.FileInfo, err error) error {

0 commit comments

Comments
 (0)