From 83aa4ee4974c04ab87ebd1105fafe3ee31e0144d Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Fri, 19 Jul 2024 13:48:14 -0400 Subject: [PATCH] Fix rename prompting for new location before verifying old one exists --- libmuttonserver.go | 2 +- mutn.go | 3 ++- src/sync/client.go | 2 +- src/sync/common.go | 10 ++++++---- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/libmuttonserver.go b/libmuttonserver.go index 97d8645..6f2c313 100644 --- a/libmuttonserver.go +++ b/libmuttonserver.go @@ -41,7 +41,7 @@ func main() { // stdin[0] is evaluated after fallthrough // stdin[1] is expected to be the OLD incomplete target location with FSPath representing path separators - always pass in UNIX format // stdin[2] is expected to be the NEW incomplete target location with FSPath representing path separators - always pass in UNIX format - sync.RenameLocal(strings.ReplaceAll(stdin[1], sync.FSPath, "/"), strings.ReplaceAll(stdin[2], sync.FSPath, "/")) + sync.RenameLocal(strings.ReplaceAll(stdin[1], sync.FSPath, "/"), strings.ReplaceAll(stdin[2], sync.FSPath, "/"), true) fallthrough // fallthrough to add the old entry to the deletions directory case "shear": // shear an entry from the server and add it to the deletions directory diff --git a/mutn.go b/mutn.go index e658f7a..538533d 100644 --- a/mutn.go +++ b/mutn.go @@ -98,7 +98,8 @@ func main() { case "note", "-n": field = 4 case "rename", "-r": - cli.RenameCli(args[1]) // pass the incomplete path as the server and all clients (reading from the deletions directory) will have a different home directory + backend.TargetIsFile(targetLocation, true, 0) // ensure location exists before prompting for new location + cli.RenameCli(args[1]) // pass the incomplete path as the server and all clients (reading from the deletions directory) will have a different home directory default: cli.HelpEdit() } diff --git a/src/sync/client.go b/src/sync/client.go index 76f4df5..2c2b2a0 100644 --- a/src/sync/client.go +++ b/src/sync/client.go @@ -414,7 +414,7 @@ func ShearRemoteFromClient(targetLocationIncomplete string) { // RenameRemoteFromClient renames oldLocationIncomplete to newLocationIncomplete on the local system and calls the server to perform the rename remotely and add the old target to the deletions list // can safely be called in offline mode, as well, so this is the intended interface for renaming (RenameLocal should only be used directly by the server binary) func RenameRemoteFromClient(oldLocationIncomplete, newLocationIncomplete string) { - RenameLocal(oldLocationIncomplete, newLocationIncomplete) // move the target on the local system + RenameLocal(oldLocationIncomplete, newLocationIncomplete, false) // move the target on the local system deviceIDList := genDeviceIDList() if len(*deviceIDList) > 0 { // ensure a device ID exists (online mode) diff --git a/src/sync/common.go b/src/sync/common.go index 99fc303..04fb6c8 100644 --- a/src/sync/common.go +++ b/src/sync/common.go @@ -2,10 +2,11 @@ package sync import ( "fmt" - "github.com/rwinkhart/MUTN/src/backend" "io/fs" "os" "strings" + + "github.com/rwinkhart/MUTN/src/backend" ) // getModTimes returns a list of all entry modification times @@ -78,13 +79,14 @@ func ShearLocal(targetLocationIncomplete, clientDeviceID string) string { // RenameLocal renames oldLocationIncomplete to newLocationIncomplete on the local system // this function should only be used directly by the server binary -func RenameLocal(oldLocationIncomplete, newLocationIncomplete string) { +func RenameLocal(oldLocationIncomplete, newLocationIncomplete string, verifyOldLocationExists bool) { // get full paths for both locations oldLocation := backend.TargetLocationFormat(oldLocationIncomplete) newLocation := backend.TargetLocationFormat(newLocationIncomplete) - // ensure oldLocation exists - backend.TargetIsFile(oldLocation, true, 0) + if verifyOldLocationExists { + backend.TargetIsFile(oldLocation, true, 0) + } // ensure newLocation does not exist _, isAccessible := backend.TargetIsFile(newLocation, false, 0)