forked from TrueCloudLab/rclone
copy: create (pseudo copy) empty source directories to destination - fixes #1837
This commit is contained in:
parent
737aed8412
commit
d758e1908e
2 changed files with 66 additions and 4 deletions
|
@ -4,8 +4,8 @@ package sync
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/ncw/rclone/fs"
|
"github.com/ncw/rclone/fs"
|
||||||
|
@ -489,10 +489,44 @@ func deleteEmptyDirectories(f fs.Fs, entriesMap map[string]fs.DirEntry) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This copies the empty directories in the slice passed in and logs
|
||||||
|
// any errors copying the directories
|
||||||
|
func copyEmptyDirectories(f fs.Fs, entries map[string]fs.DirEntry) error {
|
||||||
|
if len(entries) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var okCount int
|
||||||
|
for _, entry := range entries {
|
||||||
|
dir, ok := entry.(fs.Directory)
|
||||||
|
if ok {
|
||||||
|
err := f.Mkdir(dir.Remote())
|
||||||
|
if err != nil {
|
||||||
|
fs.Errorf(fs.LogDirName(f, dir.Remote()), "Failed to Mkdir: %v", err)
|
||||||
|
accounting.Stats.Error(err)
|
||||||
|
} else {
|
||||||
|
okCount++
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fs.Errorf(f, "Not a directory: %v", entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if accounting.Stats.Errored() {
|
||||||
|
fs.Debugf(f, "failed to copy %d directories", accounting.Stats.GetErrors())
|
||||||
|
}
|
||||||
|
|
||||||
|
if okCount > 0 {
|
||||||
|
fs.Debugf(f, "copied %d directories", okCount)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func parentDirCheck(entries map[string]fs.DirEntry, entry fs.DirEntry) {
|
func parentDirCheck(entries map[string]fs.DirEntry, entry fs.DirEntry) {
|
||||||
path := strings.Split(entry.Remote(), "/")
|
parentDir := path.Dir(entry.Remote())
|
||||||
path = path[:len(path)-1]
|
if parentDir == "." {
|
||||||
parentDir := strings.Join(path, "/")
|
parentDir = ""
|
||||||
|
}
|
||||||
if _, ok := entries[parentDir]; ok {
|
if _, ok := entries[parentDir]; ok {
|
||||||
delete(entries, parentDir)
|
delete(entries, parentDir)
|
||||||
}
|
}
|
||||||
|
@ -660,6 +694,8 @@ func (s *syncCopyMove) run() error {
|
||||||
s.stopTransfers()
|
s.stopTransfers()
|
||||||
s.stopDeleters()
|
s.stopDeleters()
|
||||||
|
|
||||||
|
s.processError(copyEmptyDirectories(s.fdst, s.srcEmptyDirs))
|
||||||
|
|
||||||
// Delete files after
|
// Delete files after
|
||||||
if s.deleteMode == fs.DeleteModeAfter {
|
if s.deleteMode == fs.DeleteModeAfter {
|
||||||
if s.currentError() != nil && !fs.Config.IgnoreErrors {
|
if s.currentError() != nil && !fs.Config.IgnoreErrors {
|
||||||
|
|
|
@ -79,6 +79,32 @@ func TestCopyWithDepth(t *testing.T) {
|
||||||
fstest.CheckItems(t, r.Fremote, file2)
|
fstest.CheckItems(t, r.Fremote, file2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test copy empty directories
|
||||||
|
func TestCopyEmptyDirectories(t *testing.T) {
|
||||||
|
r := fstest.NewRun(t)
|
||||||
|
defer r.Finalise()
|
||||||
|
file1 := r.WriteFile("sub dir/hello world", "hello world", t1)
|
||||||
|
err := operations.Mkdir(r.Flocal, "sub dir2")
|
||||||
|
require.NoError(t, err)
|
||||||
|
r.Mkdir(r.Fremote)
|
||||||
|
|
||||||
|
err = CopyDir(r.Fremote, r.Flocal)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
fstest.CheckListingWithPrecision(
|
||||||
|
t,
|
||||||
|
r.Fremote,
|
||||||
|
[]fstest.Item{
|
||||||
|
file1,
|
||||||
|
},
|
||||||
|
[]string{
|
||||||
|
"sub dir",
|
||||||
|
"sub dir2",
|
||||||
|
},
|
||||||
|
fs.Config.ModifyWindow,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Test a server side copy if possible, or the backup path if not
|
// Test a server side copy if possible, or the backup path if not
|
||||||
func TestServerSideCopy(t *testing.T) {
|
func TestServerSideCopy(t *testing.T) {
|
||||||
r := fstest.NewRun(t)
|
r := fstest.NewRun(t)
|
||||||
|
|
Loading…
Reference in a new issue