fs: Allow sync of a file and a directory with the same name

When sorting fs.DirEntries we sort by DirEntry type and
when synchronizing files let the directories be before objects,
so when the destintation fs doesn't support duplicate names,
we will only lose duplicated object instead of whole directory.

The enables synchronisation to work with a file and a directory of the same name
which is reasonably common on bucket based remotes.
This commit is contained in:
forgems 2019-06-09 16:57:05 +02:00 committed by Nick Craig-Wood
parent fb6966b5fe
commit 4b27c6719b
4 changed files with 161 additions and 36 deletions

View file

@ -17,7 +17,7 @@ func (ds DirEntries) Swap(i, j int) {
// Less is part of sort.Interface.
func (ds DirEntries) Less(i, j int) bool {
return ds[i].Remote() < ds[j].Remote()
return CompareDirEntries(ds[i], ds[j]) < 0
}
// ForObject runs the function supplied on every object in the entries
@ -79,3 +79,28 @@ func DirEntryType(d DirEntry) string {
}
return fmt.Sprintf("unknown type %T", d)
}
// CompareDirEntries returns 1 if a > b, 0 if a == b and -1 if a < b
// If two dir entries have the same name, compare their types (directories are before objects)
func CompareDirEntries(a, b DirEntry) int {
aName := a.Remote()
bName := b.Remote()
if aName > bName {
return 1
} else if aName < bName {
return -1
}
typeA := DirEntryType(a)
typeB := DirEntryType(b)
// same name, compare types
if typeA > typeB {
return 1
} else if typeA < typeB {
return -1
}
return 0
}