-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9483 from dragonchaser/remove-empty-trashs
Delete empty trash dirs
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |