From 423a2d8e624223b7e6f57e33b02b89c2a965a508 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Thu, 27 Jun 2024 12:54:51 +0200 Subject: [PATCH 1/7] Delete empty trash dirs Co-authored-by: Julian Koberg Signed-off-by: Christian Richter --- ocis/pkg/command/trash.go | 67 +++++++++++++++++++++++++++++++++++++++ ocis/pkg/trash/trash.go | 54 +++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 ocis/pkg/command/trash.go create mode 100644 ocis/pkg/trash/trash.go diff --git a/ocis/pkg/command/trash.go b/ocis/pkg/command/trash.go new file mode 100644 index 0000000000..9594cddcb1 --- /dev/null +++ b/ocis/pkg/command/trash.go @@ -0,0 +1,67 @@ +package command + +import ( + "fmt" + "github.com/owncloud/ocis/v2/ocis/pkg/trash" + + "github.com/owncloud/ocis/v2/ocis-pkg/config" + "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/config/parser" + "github.com/owncloud/ocis/v2/ocis/pkg/register" + "github.com/urfave/cli/v2" +) + +func TrashCommand(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "trash", + Usage: "ocis trash functionality", + Subcommands: []*cli.Command{ + TrashPurgeOrphanedDirsCommand(cfg), + }, + Before: func(c *cli.Context) error { + return configlog.ReturnError(parser.ParseConfig(cfg, true)) + }, + Action: func(_ *cli.Context) error { + fmt.Println("Read the docs") + return nil + }, + } +} + +func TrashPurgeOrphanedDirsCommand(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "purge-orphaned-dirs", + Usage: "purge orphaned directories", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "basepath", + Aliases: []string{"p"}, + Usage: "the basepath of the decomposedfs (e.g. /var/tmp/ocis/storage/users)", + Required: true, + }, + &cli.BoolFlag{ + Name: "dry-run", + Usage: "do not delete anything, just print what would be deleted", + Value: true, + }, + }, + Action: func(c *cli.Context) error { + basePath := c.String("basepath") + if basePath == "" { + fmt.Println("basepath is required") + return cli.ShowCommandHelp(c, "consistency") + } + + if err := trash.PurgeTrashOrphanedPaths(basePath, c.Bool("dry-run")); err != nil { + fmt.Println(err) + return err + } + + return nil + }, + } +} + +func init() { + register.AddCommand(TrashCommand) +} diff --git a/ocis/pkg/trash/trash.go b/ocis/pkg/trash/trash.go new file mode 100644 index 0000000000..4ff4161dd1 --- /dev/null +++ b/ocis/pkg/trash/trash.go @@ -0,0 +1,54 @@ +package trash + +import ( + "errors" + "fmt" + "os" + "path/filepath" +) + +const ( + // _trashGlobPattern is the glob pattern to find all trash items + _trashGlobPattern = "storage/users/spaces/*/*/trash/*/*/*/*" +) + +// PurgeTrashOrphanedPaths purges orphaned paths in the trash +func PurgeTrashOrphanedPaths(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 { + return nil + } + nd := filepath.Dir(path) + if filepath.Base(nd) == "trash" { + return nil + } + return removeEmptyFolder(nd, dryRun) +} From 0dd7eb19fc45beb7d27ceb738fbb56018256065d Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Thu, 27 Jun 2024 13:00:18 +0200 Subject: [PATCH 2/7] add changelog Signed-off-by: Christian Richter --- changelog/unreleased/empty-trash-dirs.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 changelog/unreleased/empty-trash-dirs.md diff --git a/changelog/unreleased/empty-trash-dirs.md b/changelog/unreleased/empty-trash-dirs.md new file mode 100644 index 0000000000..4545a752b8 --- /dev/null +++ b/changelog/unreleased/empty-trash-dirs.md @@ -0,0 +1,7 @@ +Enhancement: Empty trash directories + +We have added a cli-command that allows cleaning up emppty directories in the trashbins folder structure in decomposedFS. + +https://github.com/owncloud/ocis/pull/9483 +https://github.com/owncloud/ocis/issues/9393 +https://github.com/owncloud/ocis/issues/9271 From 707199ce99dae65d940e8d0b8e963997754277a8 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Thu, 27 Jun 2024 13:53:23 +0200 Subject: [PATCH 3/7] add explanation Signed-off-by: Christian Richter --- ocis/pkg/trash/trash.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ocis/pkg/trash/trash.go b/ocis/pkg/trash/trash.go index 4ff4161dd1..ded6176044 100644 --- a/ocis/pkg/trash/trash.go +++ b/ocis/pkg/trash/trash.go @@ -44,6 +44,9 @@ func removeEmptyFolder(path string, dryRun bool) error { 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) From 97fe0b06fb2c3be2519aaeb59aa2e99d176b3b98 Mon Sep 17 00:00:00 2001 From: Christian Richter <1058116+dragonchaser@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:57:18 +0000 Subject: [PATCH 4/7] Update ocis/pkg/command/trash.go Co-authored-by: kobergj --- ocis/pkg/command/trash.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocis/pkg/command/trash.go b/ocis/pkg/command/trash.go index 9594cddcb1..6149b509ed 100644 --- a/ocis/pkg/command/trash.go +++ b/ocis/pkg/command/trash.go @@ -49,7 +49,7 @@ func TrashPurgeOrphanedDirsCommand(cfg *config.Config) *cli.Command { basePath := c.String("basepath") if basePath == "" { fmt.Println("basepath is required") - return cli.ShowCommandHelp(c, "consistency") + return cli.ShowCommandHelp(c, "trash") } if err := trash.PurgeTrashOrphanedPaths(basePath, c.Bool("dry-run")); err != nil { From ba970523af8b16b9e3da8b53be7311c9c47d08b3 Mon Sep 17 00:00:00 2001 From: Christian Richter <1058116+dragonchaser@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:57:34 +0000 Subject: [PATCH 5/7] Update ocis/pkg/command/trash.go Co-authored-by: kobergj --- ocis/pkg/command/trash.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocis/pkg/command/trash.go b/ocis/pkg/command/trash.go index 6149b509ed..9674bfd809 100644 --- a/ocis/pkg/command/trash.go +++ b/ocis/pkg/command/trash.go @@ -30,7 +30,7 @@ func TrashCommand(cfg *config.Config) *cli.Command { func TrashPurgeOrphanedDirsCommand(cfg *config.Config) *cli.Command { return &cli.Command{ - Name: "purge-orphaned-dirs", + Name: "purge-empty-dirs", Usage: "purge orphaned directories", Flags: []cli.Flag{ &cli.StringFlag{ From 86529c87f1248253b1b04f72caac92d40f2e4744 Mon Sep 17 00:00:00 2001 From: Christian Richter <1058116+dragonchaser@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:57:43 +0000 Subject: [PATCH 6/7] Update changelog/unreleased/empty-trash-dirs.md Co-authored-by: kobergj --- changelog/unreleased/empty-trash-dirs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/empty-trash-dirs.md b/changelog/unreleased/empty-trash-dirs.md index 4545a752b8..89c5f54b51 100644 --- a/changelog/unreleased/empty-trash-dirs.md +++ b/changelog/unreleased/empty-trash-dirs.md @@ -1,6 +1,6 @@ Enhancement: Empty trash directories -We have added a cli-command that allows cleaning up emppty directories in the trashbins folder structure in decomposedFS. +We have added a cli-command that allows cleaning up empty directories in the trashbins folder structure in decomposedFS. https://github.com/owncloud/ocis/pull/9483 https://github.com/owncloud/ocis/issues/9393 From 169763735861ca05d05f8f00fdfed46ac396627d Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Thu, 27 Jun 2024 17:15:00 +0200 Subject: [PATCH 7/7] incorporate requested changes Signed-off-by: Christian Richter --- ocis/pkg/command/trash.go | 8 ++++---- ocis/pkg/trash/trash.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ocis/pkg/command/trash.go b/ocis/pkg/command/trash.go index 9674bfd809..a68b66e6d0 100644 --- a/ocis/pkg/command/trash.go +++ b/ocis/pkg/command/trash.go @@ -16,7 +16,7 @@ func TrashCommand(cfg *config.Config) *cli.Command { Name: "trash", Usage: "ocis trash functionality", Subcommands: []*cli.Command{ - TrashPurgeOrphanedDirsCommand(cfg), + TrashPurgeEmptyDirsCommand(cfg), }, Before: func(c *cli.Context) error { return configlog.ReturnError(parser.ParseConfig(cfg, true)) @@ -28,10 +28,10 @@ func TrashCommand(cfg *config.Config) *cli.Command { } } -func TrashPurgeOrphanedDirsCommand(cfg *config.Config) *cli.Command { +func TrashPurgeEmptyDirsCommand(cfg *config.Config) *cli.Command { return &cli.Command{ Name: "purge-empty-dirs", - Usage: "purge orphaned directories", + Usage: "purge empty directories", Flags: []cli.Flag{ &cli.StringFlag{ Name: "basepath", @@ -52,7 +52,7 @@ func TrashPurgeOrphanedDirsCommand(cfg *config.Config) *cli.Command { return cli.ShowCommandHelp(c, "trash") } - if err := trash.PurgeTrashOrphanedPaths(basePath, c.Bool("dry-run")); err != nil { + if err := trash.PurgeTrashEmptyPaths(basePath, c.Bool("dry-run")); err != nil { fmt.Println(err) return err } diff --git a/ocis/pkg/trash/trash.go b/ocis/pkg/trash/trash.go index ded6176044..985960c06e 100644 --- a/ocis/pkg/trash/trash.go +++ b/ocis/pkg/trash/trash.go @@ -12,8 +12,8 @@ const ( _trashGlobPattern = "storage/users/spaces/*/*/trash/*/*/*/*" ) -// PurgeTrashOrphanedPaths purges orphaned paths in the trash -func PurgeTrashOrphanedPaths(p string, dryRun bool) error { +// 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 {