From 71ba42bec8fee21007354ce2400e013fbe5e4eea Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 4 Mar 2017 11:26:58 +0100 Subject: [PATCH] Bugfix: Error while deleting remote dirs --- Sync/ByLength.go | 16 ++++++++++++++++ Sync/Synchronise.go | 8 ++++---- 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 Sync/ByLength.go diff --git a/Sync/ByLength.go b/Sync/ByLength.go new file mode 100644 index 0000000..f861af0 --- /dev/null +++ b/Sync/ByLength.go @@ -0,0 +1,16 @@ +package Sync + +type ByLength []string + +func (s ByLength) Len() int { + return len(s) +} + +func (s ByLength) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +// Longer strings will appear first. +func (s ByLength) Less(i, j int) bool { + return len(s[i]) > len(s[j]) +} diff --git a/Sync/Synchronise.go b/Sync/Synchronise.go index 4f8f8c9..d5aa51c 100644 --- a/Sync/Synchronise.go +++ b/Sync/Synchronise.go @@ -2,8 +2,6 @@ package Sync import ( "fmt" - "github.com/pkg/sftp" - "golang.org/x/crypto/ssh" "io" "log" "os" @@ -11,6 +9,9 @@ import ( "sort" "strings" "time" + + "github.com/pkg/sftp" + "golang.org/x/crypto/ssh" ) func Synchronise(ssh *ssh.Client, supervised, pushOnly bool, localDir, remoteDir string) { @@ -156,7 +157,7 @@ func Synchronise(ssh *ssh.Client, supervised, pushOnly bool, localDir, remoteDir log.Printf("Found %d remote files to delete.\n", len(deleteRemoteFiles)) if len(deleteRemoteFiles) > 0 { - sort.Strings(deleteRemoteFiles) + sort.Sort(ByLength(deleteRemoteFiles)) shouldDeleteRemoteFiles := true if supervised { fmt.Println(`=================================================================`) @@ -193,7 +194,6 @@ func Synchronise(ssh *ssh.Client, supervised, pushOnly bool, localDir, remoteDir continue } - // TODO: Does not work: File an issue! removeError := sftp.Remove(normalised2remoteFiles[remoteFileNormalised] + `/`) if removeError != nil { log.Printf("Was not able to delete the remote directory %s: %s\n", normalised2remoteFiles[remoteFileNormalised], removeError.Error())