Files
opencloud/ocis/pkg/trash/trash.go
Christian Richter 1697637358 incorporate requested changes
Signed-off-by: Christian Richter <crichter@owncloud.com>
2024-06-27 17:15:11 +02:00

58 lines
1.2 KiB
Go

package trash
import (
"errors"
"fmt"
"os"
"path/filepath"
)
const (
// _trashGlobPattern is the glob pattern to find all trash items
_trashGlobPattern = "storage/users/spaces/*/*/trash/*/*/*/*"
)
// PurgeTrashEmptyPaths purges empty paths in the trash
func PurgeTrashEmptyPaths(p string, dryRun bool) error {
// we have all trash nodes in all spaces now
dirs, err := filepath.Glob(filepath.Join(p, _trashGlobPattern))
if err != nil {
return err
}
if len(dirs) == 0 {
return errors.New("no trash found. Double check storage path")
}
for _, d := range dirs {
if err := removeEmptyFolder(d, dryRun); err != nil {
return err
}
}
return nil
}
func removeEmptyFolder(path string, dryRun bool) error {
if dryRun {
f, err := os.ReadDir(path)
if err != nil {
return err
}
if len(f) < 1 {
fmt.Println("would remove", path)
}
return nil
}
if err := os.Remove(path); err != nil {
// we do not really care about the error here
// if the folder is not empty we will get an error,
// this is our signal to break out of the recursion
return nil
}
nd := filepath.Dir(path)
if filepath.Base(nd) == "trash" {
return nil
}
return removeEmptyFolder(nd, dryRun)
}