aboutsummaryrefslogtreecommitdiff
path: root/weed/pb/filer_pb/filer_client.go
diff options
context:
space:
mode:
Diffstat (limited to 'weed/pb/filer_pb/filer_client.go')
-rw-r--r--weed/pb/filer_pb/filer_client.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/weed/pb/filer_pb/filer_client.go b/weed/pb/filer_pb/filer_client.go
index 80adab292..a4e875033 100644
--- a/weed/pb/filer_pb/filer_client.go
+++ b/weed/pb/filer_pb/filer_client.go
@@ -308,3 +308,58 @@ func DoRemove(ctx context.Context, client SeaweedFilerClient, parentDirectoryPat
return nil
}
+
+// DoDeleteEmptyParentDirectories recursively deletes empty parent directories.
+// It stops at root "/" or at stopAtPath.
+// For safety, dirPath must be under stopAtPath (when stopAtPath is provided).
+// The checked map tracks already-processed directories to avoid redundant work in batch operations.
+func DoDeleteEmptyParentDirectories(ctx context.Context, client SeaweedFilerClient, dirPath util.FullPath, stopAtPath util.FullPath, checked map[string]bool) {
+ if dirPath == "/" || dirPath == stopAtPath {
+ return
+ }
+
+ // Skip if already checked (for batch delete optimization)
+ dirPathStr := string(dirPath)
+ if checked != nil {
+ if checked[dirPathStr] {
+ return
+ }
+ checked[dirPathStr] = true
+ }
+
+ // Safety check: if stopAtPath is provided, dirPath must be under it
+ if stopAtPath != "" && !strings.HasPrefix(dirPathStr+"/", string(stopAtPath)+"/") {
+ glog.V(1).InfofCtx(ctx, "DoDeleteEmptyParentDirectories: %s is not under %s, skipping", dirPath, stopAtPath)
+ return
+ }
+
+ // Check if directory is empty by listing with limit 1
+ isEmpty := true
+ err := SeaweedList(ctx, client, dirPathStr, "", func(entry *Entry, isLast bool) error {
+ isEmpty = false
+ return io.EOF // Use sentinel error to explicitly stop iteration
+ }, "", false, 1)
+
+ if err != nil && err != io.EOF {
+ glog.V(3).InfofCtx(ctx, "DoDeleteEmptyParentDirectories: error checking %s: %v", dirPath, err)
+ return
+ }
+
+ if !isEmpty {
+ // Directory is not empty, stop checking upward
+ glog.V(3).InfofCtx(ctx, "DoDeleteEmptyParentDirectories: directory %s is not empty, stopping cleanup", dirPath)
+ return
+ }
+
+ // Directory is empty, try to delete it
+ glog.V(2).InfofCtx(ctx, "DoDeleteEmptyParentDirectories: deleting empty directory %s", dirPath)
+ parentDir, dirName := dirPath.DirAndName()
+
+ if err := DoRemove(ctx, client, parentDir, dirName, false, false, false, false, nil); err == nil {
+ // Successfully deleted, continue checking upwards
+ DoDeleteEmptyParentDirectories(ctx, client, util.FullPath(parentDir), stopAtPath, checked)
+ } else {
+ // Failed to delete, stop cleanup
+ glog.V(3).InfofCtx(ctx, "DoDeleteEmptyParentDirectories: failed to delete %s: %v", dirPath, err)
+ }
+}