mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-27 20:36:29 -04:00
Reduce size of server binary by not compiling SSH
This commit is contained in:
@@ -13,5 +13,5 @@ var (
|
||||
const (
|
||||
AnsiError = "\033[38;5;9m"
|
||||
AnsiReset = "\033[0m"
|
||||
LibmuttonVersion = "0.2.X"
|
||||
LibmuttonVersion = "0.2.A" // untagged releases feature a letter suffix corresponding to the eventual release version, e.g "0.2.A" -> "0.2.0", "0.2.B" -> "0.2.1"
|
||||
)
|
||||
|
||||
@@ -275,6 +275,18 @@ func syncLists(localEntryModMap, remoteEntryModMap map[string]int64, manualSync
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// can safely be called in offline mode, as well, so this is the intended interface for shearing (ShearLocal should only be used directly in the server binary)
|
||||
func ShearRemoteFromClient(targetLocationIncomplete string) {
|
||||
deviceID := ShearLocal(targetLocationIncomplete, "") // remove the target from the local system and get the device ID of the client
|
||||
|
||||
// call the server to remotely shear the target and add it to the deletions list
|
||||
// deviceID and targetLocationIncomplete are separated by \x1d, path separators are replaced with \x1e, and spaces are replaced with \x1f TODO is there a need to combine deviceID and targetLocationIncomplete into one argument?
|
||||
GetSSHOutput("libmuttonserver shear "+deviceID+"\x1d"+strings.ReplaceAll(strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1e"), " ", "\x1f"), false)
|
||||
|
||||
os.Exit(0) // sync is not required after shearing since the target has already been removed from the local system
|
||||
}
|
||||
|
||||
// deletionSync removes entries from the client that have been deleted on the server (multi-client deletion)
|
||||
func deletionSync(deletions []string) {
|
||||
for _, deletion := range deletions {
|
||||
@@ -283,6 +295,14 @@ func deletionSync(deletions []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// AddFolderRemoteFromClient creates a new entry-containing directory on the local system and calls the server to create the folder remotely
|
||||
func AddFolderRemoteFromClient(targetLocationIncomplete string) {
|
||||
AddFolderLocal(targetLocationIncomplete) // add the folder on the local system
|
||||
GetSSHOutput("libmuttonserver addfolder "+strings.ReplaceAll(targetLocationIncomplete, " ", "\x1f"), false) // call the server to create the folder remotely
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// folderSync creates folders on the client (from the given list of folder names)
|
||||
func folderSync(folders []string) {
|
||||
for _, folder := range folders {
|
||||
|
||||
+35
-31
@@ -58,51 +58,59 @@ func getModTimes(entryList []string) []int64 {
|
||||
return modList
|
||||
}
|
||||
|
||||
// Shear removes the target file or directory from the system
|
||||
// if running on the server, it will also add the target to the deletions list
|
||||
// if running on the client, it will call the server to add the target to the deletions list
|
||||
func Shear(targetLocationIncomplete string, deviceID string) {
|
||||
// get the full targetLocation path and remove the target TODO move so that the target is not removed if the server fails to add it to the deletions list, on server do not error if target does not exist
|
||||
targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete[1:])
|
||||
backend.TargetIsFile(targetLocationComplete, true, 0) // needed because os.RemoveAll does not return an error if target does not exist
|
||||
err := os.RemoveAll(targetLocationComplete)
|
||||
if err != nil {
|
||||
fmt.Println(backend.AnsiError + "Failed to remove local target: " + err.Error() + backend.AnsiReset)
|
||||
os.Exit(1)
|
||||
// ShearLocal removes the target file or directory from the local system
|
||||
// returns: deviceID (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 {
|
||||
// determine if running on a server
|
||||
var onServer bool
|
||||
if clientDeviceID != "" {
|
||||
onServer = true
|
||||
}
|
||||
|
||||
// read the devices directory
|
||||
var deviceIDList []os.DirEntry
|
||||
deviceIDList, err = os.ReadDir(backend.ConfigDir + backend.PathSeparator + "devices")
|
||||
// create a slice of all registered devices
|
||||
deviceIDList, err := os.ReadDir(backend.ConfigDir + backend.PathSeparator + "devices")
|
||||
if err != nil {
|
||||
fmt.Println(backend.AnsiError + "Failed to read the devices directory: " + err.Error() + backend.AnsiReset)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// add the sheared target (incomplete, vanity) to the deletions list
|
||||
if deviceID != "" { // if running on the server...
|
||||
// add the sheared target (incomplete, vanity) to the deletions list (if running on a server)
|
||||
if onServer {
|
||||
for _, device := range deviceIDList {
|
||||
if device.Name() != deviceID {
|
||||
if device.Name() != clientDeviceID {
|
||||
_, err = os.Create(backend.ConfigDir + backend.PathSeparator + "deletions" + backend.PathSeparator + device.Name() + "\x1d" + strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1e"))
|
||||
if err != nil {
|
||||
// do not print error as there is currently no way of seeing server-side errors
|
||||
// failure to add the target to the deletions list will exit the program and result in a client re-uploading the target (non-critical)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else { // if running on the client... (online mode determined dynamically in GetSSHOutput, will silently exit if not in online mode) // TODO separate into own function to avoid server needing to build SSH
|
||||
// determine client device ID (to send to server, avoids creating a deletion file for the client device)
|
||||
deviceID = deviceIDList[0].Name()
|
||||
// below: deviceID and targetLocationIncomplete are separated by \x1d, path separators are replaced with \x1e, and spaces are replaced with \x1f
|
||||
GetSSHOutput("libmuttonserver shear "+deviceID+"\x1d"+strings.ReplaceAll(strings.ReplaceAll(targetLocationIncomplete, backend.PathSeparator, "\x1e"), " ", "\x1f"), false)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
// get the full targetLocation path and remove the target
|
||||
targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete[1:])
|
||||
if !onServer { // error if target does not exist on client, needed because os.RemoveAll does not return an error if target does not exist
|
||||
backend.TargetIsFile(targetLocationComplete, true, 0)
|
||||
}
|
||||
err = os.RemoveAll(targetLocationComplete)
|
||||
if err != nil {
|
||||
fmt.Println(backend.AnsiError + "Failed to remove local target: " + err.Error() + backend.AnsiReset)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !onServer { // return the device ID if running on the client
|
||||
return deviceIDList[0].Name()
|
||||
}
|
||||
return ""
|
||||
// do not exit program, as this function is used as part of ShearRemoteFromClient
|
||||
}
|
||||
|
||||
// AddFolder creates a new directory at targetLocation
|
||||
// if running on the client, it will also call the server to create the directory
|
||||
func AddFolder(targetLocationIncomplete string, onServer bool) {
|
||||
// AddFolderLocal creates a new entry-containing directory on the local system
|
||||
// this function should only be used directly by the server binary
|
||||
func AddFolderLocal(targetLocationIncomplete string) {
|
||||
// get the full targetLocation path and create the target
|
||||
targetLocationComplete := backend.TargetLocationFormat(targetLocationIncomplete[1:])
|
||||
err := os.Mkdir(targetLocationComplete, 0700)
|
||||
@@ -116,9 +124,5 @@ func AddFolder(targetLocationIncomplete string, onServer bool) {
|
||||
}
|
||||
}
|
||||
|
||||
if !onServer { // if running on the client... (online mode determined dynamically in GetSSHOutput, will silently exit if not in online mode) // TODO separate into own function to avoid server needing to build SSH
|
||||
GetSSHOutput("libmuttonserver addfolder "+strings.ReplaceAll(targetLocationIncomplete, " ", "\x1f"), false)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
// do not exit program, as this function is used as part of AddFolderRemoteFromClient
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user