From 55ee4513e77d9b0aefb67bb86bb75519ee29aad3 Mon Sep 17 00:00:00 2001 From: chrislu Date: Wed, 5 Nov 2025 14:12:20 -0800 Subject: [PATCH] cleaner --- weed/filer/filer.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/weed/filer/filer.go b/weed/filer/filer.go index 75c50871e..69b57d749 100644 --- a/weed/filer/filer.go +++ b/weed/filer/filer.go @@ -420,13 +420,29 @@ func (f *Filer) doListDirectoryEntries(ctx context.Context, p util.FullPath, sta if !hasValidEntries && p != "/" && startFileName == "" { // Do a quick check to see if directory is truly empty now if isEmpty, checkErr := f.IsDirectoryEmpty(ctx, p); checkErr == nil && isEmpty { - glog.V(2).InfofCtx(ctx, "doListDirectoryEntries: deleting empty directory %s after expiring all entries", p) - parentDir, _ := p.DirAndName() - if dirEntry, findErr := f.FindEntry(ctx, p); findErr == nil { - // Delete the now-empty directory - if delErr := f.doDeleteEntryMetaAndData(ctx, dirEntry, false, false, nil); delErr == nil { - // Recursively try to delete parent directories if they become empty - f.DeleteEmptyParentDirectories(ctx, util.FullPath(parentDir), "") + // Safety: Always limit recursive deletion scope + // For S3 buckets, DirBucketsPath is always set (e.g., "/buckets") + // Don't delete bucket directories or the buckets path itself + stopAtPath := util.FullPath(f.DirBucketsPath) + + // Check if this is a bucket-level directory that should never be deleted + baseDepth := strings.Count(f.DirBucketsPath, "/") + dirDepth := strings.Count(string(p), "/") + + // If directory is at bucket level (e.g., /buckets/mybucket) or above, don't delete + if dirDepth <= baseDepth+1 { + glog.V(2).InfofCtx(ctx, "doListDirectoryEntries: skipping deletion of bucket-level directory %s", p) + } else { + // Safe to delete subdirectories within buckets + glog.V(2).InfofCtx(ctx, "doListDirectoryEntries: deleting empty directory %s after expiring all entries", p) + parentDir, _ := p.DirAndName() + if dirEntry, findErr := f.FindEntry(ctx, p); findErr == nil { + // Delete the now-empty directory + if delErr := f.doDeleteEntryMetaAndData(ctx, dirEntry, false, false, nil); delErr == nil { + // Recursively try to delete parent directories if they become empty + // Stop at the buckets path to prevent deleting bucket directories + f.DeleteEmptyParentDirectories(ctx, util.FullPath(parentDir), stopAtPath) + } } } }