mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-29 13:26:38 -04:00
Implement initial server-side shearing logic
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// WalkEntryDir walks the entry directory and returns lists of all files and directories found (two separate lists)
|
||||
@@ -56,3 +57,47 @@ 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
|
||||
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)
|
||||
}
|
||||
|
||||
// read the devices directory
|
||||
var deviceIDList []os.DirEntry
|
||||
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...
|
||||
for _, device := range deviceIDList {
|
||||
if device.Name() != deviceID {
|
||||
_, 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
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
backend.ReadConfig([]string{"sshUser"}, "0") // exits if value does not exist (indicates offline mode)
|
||||
// if running on the client in online mode...
|
||||
// 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"), true)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user