Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 359e474

Browse files
committed
Merge pull request #1386 from lowzj/fix-gc-directory
fix: delete empty directory in local storage backport to 1.0.x
2 parents 6eb2e96 + 4bbbc80 commit 359e474

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

pkg/fileutils/fileutils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,17 @@ func GetFreeSpace(path string) (Fsize, error) {
256256

257257
return Fsize(fs.Bavail * uint64(fs.Bsize)), nil
258258
}
259+
260+
// IsEmptyDir check whether the directory is empty.
261+
func IsEmptyDir(path string) (bool, error) {
262+
f, err := os.Open(path)
263+
if err != nil {
264+
return false, err
265+
}
266+
defer f.Close()
267+
268+
if _, err = f.Readdirnames(1); err == io.EOF {
269+
return true, nil
270+
}
271+
return false, err
272+
}

pkg/fileutils/fileutils_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ func (s *FileUtilTestSuite) TestDeleteFiles(c *check.C) {
114114
f1 := filepath.Join(s.tmpDir, "TestDeleteFile001")
115115
f2 := filepath.Join(s.tmpDir, "TestDeleteFile002")
116116
os.Create(f1)
117-
//os.Create(f2)
118117
DeleteFiles(f1, f2)
119118
c.Assert(PathExist(f1) || PathExist(f2), check.Equals, false)
120119
}
@@ -322,3 +321,33 @@ func (s *FileUtilTestSuite) TestIsRegularFile(c *check.C) {
322321
c.Assert(IsRegularFile(pathStr), check.Equals, false)
323322
os.Remove(pathStr)
324323
}
324+
325+
func (s *FileUtilTestSuite) TestIsEmptyDir(c *check.C) {
326+
pathStr := filepath.Join(s.tmpDir, "TestIsEmptyDir")
327+
328+
// not exist
329+
empty, err := IsEmptyDir(pathStr)
330+
c.Assert(empty, check.Equals, false)
331+
c.Assert(err, check.NotNil)
332+
333+
// not a directory
334+
_, _ = os.Create(pathStr)
335+
empty, err = IsEmptyDir(pathStr)
336+
c.Assert(empty, check.Equals, false)
337+
c.Assert(err, check.NotNil)
338+
_ = os.Remove(pathStr)
339+
340+
// empty
341+
_ = os.Mkdir(pathStr, 0755)
342+
empty, err = IsEmptyDir(pathStr)
343+
c.Assert(empty, check.Equals, true)
344+
c.Assert(err, check.IsNil)
345+
346+
// not empty
347+
childPath := filepath.Join(pathStr, "child")
348+
_ = os.Mkdir(childPath, 0755)
349+
empty, err = IsEmptyDir(pathStr)
350+
c.Assert(empty, check.Equals, false)
351+
c.Assert(err, check.IsNil)
352+
_ = os.Remove(pathStr)
353+
}

supernode/daemon/mgr/cdn/path_util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"path"
2222

23+
"github.com/sirupsen/logrus"
24+
2325
"github.com/dragonflyoss/Dragonfly/pkg/stringutils"
2426
"github.com/dragonflyoss/Dragonfly/supernode/config"
2527
"github.com/dragonflyoss/Dragonfly/supernode/store"
@@ -69,6 +71,13 @@ func getMd5DataRaw(taskID string) *store.Raw {
6971
}
7072
}
7173

74+
func getParentRaw(taskID string) *store.Raw {
75+
return &store.Raw{
76+
Bucket: config.DownloadHome,
77+
Key: getParentKey(taskID),
78+
}
79+
}
80+
7281
func getHomeRaw() *store.Raw {
7382
return &store.Raw{
7483
Bucket: config.DownloadHome,
@@ -91,5 +100,11 @@ func deleteTaskFiles(ctx context.Context, cacheStore *store.Store, taskID string
91100
return err
92101
}
93102

103+
// try to clean the parent bucket
104+
if err := cacheStore.Remove(ctx, getParentRaw(taskID)); err != nil &&
105+
!store.IsKeyNotFound(err) {
106+
logrus.Warnf("taskID:%s failed remove parent bucket:%v", taskID, err)
107+
}
108+
94109
return nil
95110
}

supernode/store/local_storage.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,24 @@ func (ls *localStorage) Stat(ctx context.Context, raw *Raw) (*StorageInfo, error
260260
}
261261

262262
// Remove deletes a file or dir.
263+
// It will force delete the file or dir when the raw.Trunc is true.
263264
func (ls *localStorage) Remove(ctx context.Context, raw *Raw) error {
264-
path, _, err := ls.statPath(raw.Bucket, raw.Key)
265+
path, info, err := ls.statPath(raw.Bucket, raw.Key)
265266
if err != nil {
266267
return err
267268
}
268269

269270
lock(path, -1, false)
270271
defer unLock(path, -1, false)
271272

272-
return os.RemoveAll(path)
273+
if raw.Trunc || !info.IsDir() {
274+
return os.RemoveAll(path)
275+
}
276+
empty, err := fileutils.IsEmptyDir(path)
277+
if empty {
278+
return os.RemoveAll(path)
279+
}
280+
return err
273281
}
274282

275283
// GetAvailSpace returns the available disk space in B.

0 commit comments

Comments
 (0)