lib/model: Refactor conflict name handling (#5572)
This commit is contained in:
committed by
Audrius Butkevicius
parent
e2e8f6e940
commit
43bcb3d5a5
@@ -1736,8 +1736,8 @@ func removeAvailability(availabilities []Availability, availability Availability
|
|||||||
return availabilities
|
return availabilities
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *sendReceiveFolder) moveForConflict(name string, lastModBy string, scanChan chan<- string) error {
|
func (f *sendReceiveFolder) moveForConflict(name, lastModBy string, scanChan chan<- string) error {
|
||||||
if strings.Contains(filepath.Base(name), ".sync-conflict-") {
|
if isConflict(name) {
|
||||||
l.Infoln("Conflict for", name, "which is already a conflict copy; not copying again.")
|
l.Infoln("Conflict for", name, "which is already a conflict copy; not copying again.")
|
||||||
if err := f.fs.Remove(name); err != nil && !fs.IsNotExist(err) {
|
if err := f.fs.Remove(name); err != nil && !fs.IsNotExist(err) {
|
||||||
return err
|
return err
|
||||||
@@ -1752,9 +1752,7 @@ func (f *sendReceiveFolder) moveForConflict(name string, lastModBy string, scanC
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ext := filepath.Ext(name)
|
newName := conflictName(name, lastModBy)
|
||||||
withoutExt := name[:len(name)-len(ext)]
|
|
||||||
newName := withoutExt + time.Now().Format(".sync-conflict-20060102-150405-") + lastModBy + ext
|
|
||||||
err := f.fs.Rename(name, newName)
|
err := f.fs.Rename(name, newName)
|
||||||
if fs.IsNotExist(err) {
|
if fs.IsNotExist(err) {
|
||||||
// We were supposed to move a file away but it does not exist. Either
|
// We were supposed to move a file away but it does not exist. Either
|
||||||
@@ -1764,17 +1762,14 @@ func (f *sendReceiveFolder) moveForConflict(name string, lastModBy string, scanC
|
|||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
if f.MaxConflicts > -1 {
|
if f.MaxConflicts > -1 {
|
||||||
matches, gerr := f.fs.Glob(withoutExt + ".sync-conflict-????????-??????*" + ext)
|
matches := existingConflicts(name, f.fs)
|
||||||
if gerr == nil && len(matches) > f.MaxConflicts {
|
if len(matches) > f.MaxConflicts {
|
||||||
sort.Sort(sort.Reverse(sort.StringSlice(matches)))
|
sort.Sort(sort.Reverse(sort.StringSlice(matches)))
|
||||||
for _, match := range matches[f.MaxConflicts:] {
|
for _, match := range matches[f.MaxConflicts:] {
|
||||||
gerr = f.fs.Remove(match)
|
if gerr := f.fs.Remove(match); gerr != nil {
|
||||||
if gerr != nil {
|
|
||||||
l.Debugln(f, "removing extra conflict", gerr)
|
l.Debugln(f, "removing extra conflict", gerr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if gerr != nil {
|
|
||||||
l.Debugln(f, "globbing for conflicts", gerr)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -1970,3 +1965,21 @@ func componentCount(name string) int {
|
|||||||
}
|
}
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func conflictName(name, lastModBy string) string {
|
||||||
|
ext := filepath.Ext(name)
|
||||||
|
return name[:len(name)-len(ext)] + time.Now().Format(".sync-conflict-20060102-150405-") + lastModBy + ext
|
||||||
|
}
|
||||||
|
|
||||||
|
func isConflict(name string) bool {
|
||||||
|
return strings.Contains(filepath.Base(name), ".sync-conflict-")
|
||||||
|
}
|
||||||
|
|
||||||
|
func existingConflicts(name string, fs fs.Filesystem) []string {
|
||||||
|
ext := filepath.Ext(name)
|
||||||
|
matches, err := fs.Glob(name[:len(name)-len(ext)] + ".sync-conflict-????????-??????*" + ext)
|
||||||
|
if err != nil {
|
||||||
|
l.Debugln("globbing for conflicts", err)
|
||||||
|
}
|
||||||
|
return matches
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user