Skip to content

Commit

Permalink
Merge pull request #9483 from dragonchaser/remove-empty-trashs
Browse files Browse the repository at this point in the history
Delete empty trash dirs
  • Loading branch information
dragonchaser committed Jun 28, 2024
2 parents 548084e + 1697637 commit 90d0008
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
7 changes: 7 additions & 0 deletions changelog/unreleased/empty-trash-dirs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: Empty trash directories

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
https://github.com/owncloud/ocis/issues/9271
67 changes: 67 additions & 0 deletions ocis/pkg/command/trash.go
Original file line number Diff line number Diff line change
@@ -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{
TrashPurgeEmptyDirsCommand(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 TrashPurgeEmptyDirsCommand(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "purge-empty-dirs",
Usage: "purge empty 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, "trash")
}

if err := trash.PurgeTrashEmptyPaths(basePath, c.Bool("dry-run")); err != nil {
fmt.Println(err)
return err
}

return nil
},
}
}

func init() {
register.AddCommand(TrashCommand)
}
57 changes: 57 additions & 0 deletions ocis/pkg/trash/trash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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)
}

0 comments on commit 90d0008

Please sign in to comment.