Skip to content

Commit

Permalink
Also clean up expired tmp dirs
Browse files Browse the repository at this point in the history
Signed-off-by: Changxin Miao <[email protected]>
  • Loading branch information
polyrabbit committed Dec 11, 2024
1 parent 66bb3e7 commit 693eac6
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -1172,6 +1173,7 @@ func (n *jfsObjects) cleanup() {
if errno != 0 {
continue
}
deletedOnce := false
entries, _ := f.ReaddirPlus(mctx, 0)
for _, entry := range entries {
if _, err := uuid.Parse(string(entry.Name)); err != nil {
Expand All @@ -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))
deletedOnce = true
}
}
}
f.Close(mctx)
if deletedOnce { // 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 {
Expand Down

0 comments on commit 693eac6

Please sign in to comment.