-
Notifications
You must be signed in to change notification settings - Fork 0
fs storage: remove empty directories after object deletion #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,13 +54,46 @@ func (folder *Folder) ListFolder() (objects []storage.Object, subFolders []stora | |
|
|
||
| func (folder *Folder) DeleteObjects(objectsWithRelativePaths []storage.Object) error { | ||
| for _, object := range objectsWithRelativePaths { | ||
| err := os.RemoveAll(folder.GetFilePath(object.GetName())) | ||
| filePath := folder.GetFilePath(object.GetName()) | ||
| err := os.RemoveAll(filePath) | ||
| if os.IsNotExist(err) { | ||
| continue | ||
| } | ||
| if err != nil { | ||
| return fmt.Errorf("unable to delete file %q: %w", object.GetName(), err) | ||
| } | ||
| if err = folder.cleanupEmptyFolders(path.Dir(filePath)); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // cleanupEmptyFolders removes empty parent directories up to the root folder path. | ||
| func (folder *Folder) cleanupEmptyFolders(dirPath string) error { | ||
| rootPath := path.Clean(folder.rootPath) | ||
| for { | ||
| dirPath = path.Clean(dirPath) | ||
| if dirPath == rootPath || !strings.HasPrefix(dirPath, rootPath) { | ||
| break | ||
| } | ||
|
||
| entries, err := os.ReadDir(dirPath) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| break | ||
| } | ||
| return fmt.Errorf("unable to read directory %q: %w", dirPath, err) | ||
| } | ||
| if len(entries) > 0 { | ||
| break | ||
| } | ||
| if err = os.Remove(dirPath); err != nil { | ||
| if os.IsNotExist(err) { | ||
| break | ||
| } | ||
| return fmt.Errorf("unable to remove empty directory %q: %w", dirPath, err) | ||
| } | ||
|
Comment on lines
+90
to
+99
|
||
| dirPath = path.Dir(dirPath) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
path.Dir/path.Cleanoperate on slash-separated paths (URL paths), not OS filesystem paths. On Windows (and even in some edge cases on Unix when separators differ), this can yield incorrect parent traversal and root comparisons, preventing cleanup or cleaning the wrong directories. Usefilepath.Dirandfilepath.Cleanfor filesystem paths, and avoidstrings.HasPrefixfor path containment checks (it can misclassify/tmp/root2as within/tmp/root); instead preferfilepath.Rel(rootPath, dirPath)and stop when the rel path is.or starts with..(or is..).