rmdirs: add --leave-root flag - fixes #1874
This commit is contained in:
+3
-3
@@ -1183,7 +1183,7 @@ func Purge(f Fs) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = Rmdirs(f, "")
|
||||
err = Rmdirs(f, "", false)
|
||||
}
|
||||
if err != nil {
|
||||
Stats.Error(err)
|
||||
@@ -1672,9 +1672,9 @@ func Rcat(fdst Fs, dstFileName string, in io.ReadCloser, modTime time.Time) (dst
|
||||
|
||||
// Rmdirs removes any empty directories (or directories only
|
||||
// containing empty directories) under f, including f.
|
||||
func Rmdirs(f Fs, dir string) error {
|
||||
func Rmdirs(f Fs, dir string, leaveRoot bool) error {
|
||||
dirEmpty := make(map[string]bool)
|
||||
dirEmpty[""] = true
|
||||
dirEmpty[""] = !leaveRoot
|
||||
err := Walk(f, dir, true, Config.MaxDepth, func(dirPath string, entries DirEntries, err error) error {
|
||||
if err != nil {
|
||||
Stats.Error(err)
|
||||
|
||||
+38
-2
@@ -522,7 +522,7 @@ func TestRcat(t *testing.T) {
|
||||
check(false)
|
||||
}
|
||||
|
||||
func TestRmdirs(t *testing.T) {
|
||||
func TestRmdirsNoLeaveRoot(t *testing.T) {
|
||||
r := fstest.NewRun(t)
|
||||
defer r.Finalise()
|
||||
r.Mkdir(r.Fremote)
|
||||
@@ -562,7 +562,7 @@ func TestRmdirs(t *testing.T) {
|
||||
fs.Config.ModifyWindow,
|
||||
)
|
||||
|
||||
require.NoError(t, fs.Rmdirs(r.Fremote, ""))
|
||||
require.NoError(t, fs.Rmdirs(r.Fremote, "", false))
|
||||
|
||||
fstest.CheckListingWithPrecision(
|
||||
t,
|
||||
@@ -580,6 +580,42 @@ func TestRmdirs(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestRmdirsLeaveRoot(t *testing.T) {
|
||||
r := fstest.NewRun(t)
|
||||
defer r.Finalise()
|
||||
r.Mkdir(r.Fremote)
|
||||
|
||||
r.ForceMkdir(r.Fremote)
|
||||
|
||||
require.NoError(t, fs.Mkdir(r.Fremote, "A1"))
|
||||
require.NoError(t, fs.Mkdir(r.Fremote, "A1/B1"))
|
||||
require.NoError(t, fs.Mkdir(r.Fremote, "A1/B1/C1"))
|
||||
|
||||
fstest.CheckListingWithPrecision(
|
||||
t,
|
||||
r.Fremote,
|
||||
[]fstest.Item{},
|
||||
[]string{
|
||||
"A1",
|
||||
"A1/B1",
|
||||
"A1/B1/C1",
|
||||
},
|
||||
fs.Config.ModifyWindow,
|
||||
)
|
||||
|
||||
require.NoError(t, fs.Rmdirs(r.Fremote, "/A1", true))
|
||||
|
||||
fstest.CheckListingWithPrecision(
|
||||
t,
|
||||
r.Fremote,
|
||||
[]fstest.Item{},
|
||||
[]string{
|
||||
"A1",
|
||||
},
|
||||
fs.Config.ModifyWindow,
|
||||
)
|
||||
}
|
||||
|
||||
func TestMoveFile(t *testing.T) {
|
||||
r := fstest.NewRun(t)
|
||||
defer r.Finalise()
|
||||
|
||||
Reference in New Issue
Block a user