From 4689cae3f4a1e19486935f2da5b06bf0a90c731c Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sat, 1 Feb 2025 12:00:34 -0500 Subject: [PATCH] Ensure sheared folders always end in a trailing slash in the deletions directory; this improves clarity and reduces duplicate entries in the deletions directory --- sync/common.go | 12 +++++++----- sync/oneOff.go | 7 ++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/sync/common.go b/sync/common.go index a5df1f1..ba95037 100644 --- a/sync/common.go +++ b/sync/common.go @@ -20,10 +20,11 @@ func getModTimes(entryList []string) []int64 { } // ShearLocal removes the target file or directory from the local system. -// Returns: deviceID (only on client; for use in ShearRemoteFromClient). +// Returns: deviceID (only on client; for use in ShearRemoteFromClient), +// isDir (only on client; for use in ShearRemoteFromClient). // If the local system is a server, it will also add the target to the deletions list for all clients (except the requesting client). // This function should only be used directly by the server binary. -func ShearLocal(targetLocationIncomplete, clientDeviceID string) string { +func ShearLocal(targetLocationIncomplete, clientDeviceID string) (string, bool) { // determine if running on a server var onServer bool if clientDeviceID != "" { @@ -49,8 +50,9 @@ func ShearLocal(targetLocationIncomplete, clientDeviceID string) string { // get the full targetLocation path and remove the target targetLocationComplete := core.TargetLocationFormat(targetLocationIncomplete) + var isFile bool if !onServer { // error if target does not exist on client, needed because os.RemoveAll does not return an error if target does not exist - core.TargetIsFile(targetLocationComplete, true, 0) + isFile, _ = core.TargetIsFile(targetLocationComplete, true, 0) } err := os.RemoveAll(targetLocationComplete) if err != nil { @@ -59,9 +61,9 @@ func ShearLocal(targetLocationIncomplete, clientDeviceID string) string { } if !onServer && len(*deviceIDList) > 0 { // return the device ID if running on the client and a device ID exists (online mode) - return (*deviceIDList)[0].Name() + return (*deviceIDList)[0].Name(), !isFile } - return "" + return "", true // do not exit program, as this function is used as part of ShearRemoteFromClient } diff --git a/sync/oneOff.go b/sync/oneOff.go index b26610e..3017b19 100644 --- a/sync/oneOff.go +++ b/sync/oneOff.go @@ -11,12 +11,17 @@ import ( // ShearRemoteFromClient removes the target file or directory from the local system and calls the server to remove it remotely and add it to the deletions list. // It can safely be called in offline mode, as well, so this is the intended interface for shearing (ShearLocal should only be used directly by the server binary). func ShearRemoteFromClient(targetLocationIncomplete string, forceOffline bool) { - deviceID := ShearLocal(targetLocationIncomplete, "") // remove the target from the local system and get the device ID of the client + deviceID, isDir := ShearLocal(targetLocationIncomplete, "") // remove the target from the local system and get the device ID of the client if !forceOffline && deviceID != "" { // ensure a device ID exists (online mode) // create an SSH client; manualSync is false in case a device ID exists but SSH is not configured sshClient, _, _ := GetSSHClient(false) + // ensure targetLocationIncomplete ends with a slash if it is a directory (for clarity in shear message) + if isDir && !strings.HasSuffix(targetLocationIncomplete, "/") { + targetLocationIncomplete += "/" + } + // call the server to remotely shear the target and add it to the deletions list GetSSHOutput(sshClient, "libmuttonserver shear", deviceID+"\n"+strings.ReplaceAll(targetLocationIncomplete, core.PathSeparator, core.FSPath))