From 5305a0d02ecc4371d9ad234b37905cd82bce2bf0 Mon Sep 17 00:00:00 2001 From: Changxin Miao Date: Wed, 11 Dec 2024 22:29:07 +0800 Subject: [PATCH] Also clean up expired tmp dirs Signed-off-by: Changxin Miao --- pkg/gateway/gateway.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pkg/gateway/gateway.go b/pkg/gateway/gateway.go index 214737316e28..2f85d0514c67 100644 --- a/pkg/gateway/gateway.go +++ b/pkg/gateway/gateway.go @@ -24,6 +24,7 @@ import ( "net/http" "os" "path" + "path/filepath" "sort" "strconv" "strings" @@ -1172,6 +1173,7 @@ func (n *jfsObjects) cleanup() { if errno != 0 { continue } + deleted := 0 entries, _ := f.ReaddirPlus(mctx, 0) for _, entry := range entries { if _, err := uuid.Parse(string(entry.Name)); err != nil { @@ -1183,11 +1185,41 @@ func (n *jfsObjects) cleanup() { logger.Errorf("failed to delete expired temporary files path: %s,", p) } else { logger.Infof("delete expired temporary files path: %s, mtime: %s", p, time.Unix(entry.Attr.Mtime, 0).Format(time.RFC3339)) + deleted += 1 } } } + f.Close(mctx) + if deleted == len(entries) { // try to clean up to .sys + dir = strings.TrimRight(dir, "/") + for { + if errno := n.rmdirIfEmpty(mctx, dir); errno != 0 { + break + } + dir = filepath.Dir(dir) + if !strings.HasPrefix(dir, metaBucket) { + break + } + } + } + } + } +} + +func (n *jfsObjects) rmdirIfEmpty(ctx meta.Context, dir string) syscall.Errno { + f, errno := n.fs.Open(ctx, dir, 0) + if errno != 0 { // reopen to avoid reading from dir cache + logger.Warnf("Failed to reopen %s: %v", dir, errno) + return errno + } + defer f.Close(ctx) + if entries, errno := f.ReaddirPlus(ctx, 0); errno == 0 && len(entries) == 0 { + if errno = n.fs.Delete(ctx, dir); errno != 0 && !fs.IsNotEmpty(errno) { + logger.Warnf("Failed to delete empty %s: %v", dir, errno) + return errno } } + return 0 } type jfsFLock struct {