Do not determine online/offline mode based on device ID presence

This commit is contained in:
2025-05-30 16:02:46 +00:00
parent c2fc911693
commit 5666974a7f
2 changed files with 79 additions and 74 deletions
+77 -72
View File
@@ -9,40 +9,44 @@ import (
"github.com/rwinkhart/libmutton/synccommon" "github.com/rwinkhart/libmutton/synccommon"
) )
// 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. // ShearRemoteFromClient removes the target file or directory from
// 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). // 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) error { func ShearRemoteFromClient(targetLocationIncomplete string) error {
deviceID, isDir, err := synccommon.ShearLocal(targetLocationIncomplete, "") // remove the target from the local system and get the device ID of the client deviceID, isDir, err := synccommon.ShearLocal(targetLocationIncomplete, "") // remove the target from the local system and get the device ID of the client
if err != nil { if err != nil {
return errors.New("unable to shear target locally: " + err.Error()) return errors.New("unable to shear target locally: " + err.Error())
} }
if deviceID != "" { // ensure a device ID exists (online mode) sshClient, offlineMode, _, _, err := GetSSHClient()
// create an SSH client if offlineMode {
sshClient, offlineMode, _, _, err := GetSSHClient() goto end
if offlineMode { }
goto end if err != nil {
} return errors.New("unable to connect to SSH client: " + err.Error())
if err != nil { }
return errors.New("unable to connect to SSH client: " + err.Error()) if deviceID == "" {
} return errors.New("unable to shear target remotely: no device ID found")
}
// ensure targetLocationIncomplete ends with a slash if it is a directory (for clarity in shear message) // ensure targetLocationIncomplete ends with a slash if it is a directory (for clarity in shear message)
if isDir && !strings.HasSuffix(targetLocationIncomplete, "/") { if isDir && !strings.HasSuffix(targetLocationIncomplete, "/") {
targetLocationIncomplete += "/" targetLocationIncomplete += "/"
} }
// call the server to remotely shear the target and add it to the deletions list // call the server to remotely shear the target and add it to the deletions list
_, err = GetSSHOutput(sshClient, "libmuttonserver shear", deviceID+"\n"+strings.ReplaceAll(targetLocationIncomplete, global.PathSeparator, global.FSPath)) _, err = GetSSHOutput(sshClient, "libmuttonserver shear", deviceID+"\n"+strings.ReplaceAll(targetLocationIncomplete, global.PathSeparator, global.FSPath))
if err != nil { if err != nil {
return errors.New("unable to shear target remotely: " + err.Error()) return errors.New("unable to shear target remotely: " + err.Error())
} }
// close the SSH client // close the SSH client
err = sshClient.Close() err = sshClient.Close()
if err != nil { if err != nil {
return errors.New("unable to close SSH client: " + err.Error()) return errors.New("unable to close SSH client: " + err.Error())
}
} }
end: end:
@@ -50,8 +54,11 @@ end:
return nil return nil
} }
// 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. // RenameRemoteFromClient renames oldLocationIncomplete to newLocationIncomplete on
// It 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). // the local system and calls the server to perform the rename remotely and add the
// old target to the deletions list.
// It 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) error { func RenameRemoteFromClient(oldLocationIncomplete, newLocationIncomplete string) error {
err := synccommon.RenameLocal(oldLocationIncomplete, newLocationIncomplete, false) // move the target on the local system err := synccommon.RenameLocal(oldLocationIncomplete, newLocationIncomplete, false) // move the target on the local system
if err != nil { if err != nil {
@@ -62,30 +69,31 @@ func RenameRemoteFromClient(oldLocationIncomplete, newLocationIncomplete string)
if err != nil { if err != nil {
return errors.New("unable to generate device ID list: " + err.Error()) return errors.New("unable to generate device ID list: " + err.Error())
} }
if len(deviceIDList) > 0 { // ensure a device ID exists (online mode) // create an SSH client
// create an SSH client sshClient, offlineMode, _, _, err := GetSSHClient()
sshClient, offlineMode, _, _, err := GetSSHClient() if offlineMode {
if offlineMode { goto end
goto end }
} if err != nil {
if err != nil { return errors.New("unable to connect to SSH client: " + err.Error())
return errors.New("unable to connect to SSH client: " + err.Error()) }
} if deviceIDList[0].Name() == "" {
return errors.New("unable to rename target remotely: no device ID found")
}
// call the server to move the target on the remote system and add the old target to the deletions list // call the server to move the target on the remote system and add the old target to the deletions list
_, err = GetSSHOutput(sshClient, "libmuttonserver rename", _, err = GetSSHOutput(sshClient, "libmuttonserver rename",
(deviceIDList)[0].Name()+"\n"+ (deviceIDList)[0].Name()+"\n"+
strings.ReplaceAll(oldLocationIncomplete, global.PathSeparator, global.FSPath)+"\n"+ strings.ReplaceAll(oldLocationIncomplete, global.PathSeparator, global.FSPath)+"\n"+
strings.ReplaceAll(newLocationIncomplete, global.PathSeparator, global.FSPath)) strings.ReplaceAll(newLocationIncomplete, global.PathSeparator, global.FSPath))
if err != nil { if err != nil {
return errors.New("unable to rename target remotely: " + err.Error()) return errors.New("unable to rename target remotely: " + err.Error())
} }
// close the SSH client // close the SSH client
err = sshClient.Close() err = sshClient.Close()
if err != nil { if err != nil {
return errors.New("unable to close SSH client: " + err.Error()) return errors.New("unable to close SSH client: " + err.Error())
}
} }
end: end:
@@ -93,39 +101,36 @@ end:
return nil return nil
} }
// AddFolderRemoteFromClient creates a new entry-containing directory on the local system and calls the server to create the folder remotely. // AddFolderRemoteFromClient creates a new entry-containing directory
// It can safely be called in offline mode, as well, so this is the intended interface for adding folders (AddFolderLocal should only be used directly by the server binary). // on the local system and calls the server to create the folder remotely.
// It can safely be called in offline mode, as well, so this is the
// intended interface for adding folders (AddFolderLocal should only be
// used directly by the server binary).
func AddFolderRemoteFromClient(targetLocationIncomplete string) error { func AddFolderRemoteFromClient(targetLocationIncomplete string) error {
err := synccommon.AddFolderLocal(targetLocationIncomplete) // add the folder on the local system err := synccommon.AddFolderLocal(targetLocationIncomplete) // add the folder on the local system
if err != nil { if err != nil {
return errors.New("unable to add folder locally: " + err.Error()) return errors.New("unable to add folder locally: " + err.Error())
} }
deviceIDList, err := global.GenDeviceIDList() // create an SSH client
sshClient, offlineMode, _, _, err := GetSSHClient()
if offlineMode {
goto end
}
if err != nil { if err != nil {
return errors.New("unable to generate device ID list: " + err.Error()) return errors.New("unable to connect to SSH client: " + err.Error())
} }
if len(deviceIDList) > 0 { // ensure a device ID exists (online mode)
// create an SSH client
sshClient, offlineMode, _, _, err := GetSSHClient()
if offlineMode {
goto end
}
if err != nil {
return errors.New("unable to connect to SSH client: " + err.Error())
}
// call the server to create the folder remotely // call the server to create the folder remotely
_, err = GetSSHOutput(sshClient, "libmuttonserver addfolder", strings.ReplaceAll(targetLocationIncomplete, global.PathSeparator, global.FSPath)) // call the server to create the folder remotely _, err = GetSSHOutput(sshClient, "libmuttonserver addfolder", strings.ReplaceAll(targetLocationIncomplete, global.PathSeparator, global.FSPath)) // call the server to create the folder remotely
if err != nil { if err != nil {
return errors.New("unable to add folder remotely: " + err.Error()) return errors.New("unable to add folder remotely: " + err.Error())
} }
// close the SSH client // close the SSH client
err = sshClient.Close() err = sshClient.Close()
if err != nil { if err != nil {
return errors.New("unable to close SSH client: " + err.Error()) return errors.New("unable to close SSH client: " + err.Error())
}
} }
end: end:
+2 -2
View File
@@ -62,7 +62,7 @@ func ShearLocal(targetLocationIncomplete, clientDeviceID string) (string, bool,
} }
} }
// get the full targetLocation path and remove the target // remove the target locally
targetLocationComplete := global.TargetLocationFormat(targetLocationIncomplete) targetLocationComplete := global.TargetLocationFormat(targetLocationIncomplete)
var isFile bool 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 if !onServer { // error if target does not exist on client, needed because os.RemoveAll does not return an error if target does not exist
@@ -112,7 +112,7 @@ func RenameLocal(oldLocationIncomplete, newLocationIncomplete string, verifyOldL
// AddFolderLocal creates a new entry-containing directory on the local system. // AddFolderLocal creates a new entry-containing directory on the local system.
// This function should only be used directly by the server binary. // This function should only be used directly by the server binary.
func AddFolderLocal(targetLocationIncomplete string) error { func AddFolderLocal(targetLocationIncomplete string) error {
// get the full targetLocation path and create the target // create the target locally
targetLocationComplete := global.TargetLocationFormat(targetLocationIncomplete) targetLocationComplete := global.TargetLocationFormat(targetLocationIncomplete)
err := os.Mkdir(targetLocationComplete, 0700) err := os.Mkdir(targetLocationComplete, 0700)
if err != nil { if err != nil {