From 309ef8480381a8cfacf321b22d5484c92880f4a2 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Thu, 16 May 2024 17:46:03 -0400 Subject: [PATCH] Parse deletions per-deviceID --- libmuttonserver.go | 2 +- src/sync/client.go | 3 ++- src/sync/common.go | 2 +- src/sync/server.go | 12 ++++++++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/libmuttonserver.go b/libmuttonserver.go index 344212c..11c94aa 100644 --- a/libmuttonserver.go +++ b/libmuttonserver.go @@ -18,7 +18,7 @@ func main() { switch args[1] { case "fetch": // print all information needed for syncing to stdout for interpretation by the client - sync.GetRemoteDataFromServer() + sync.GetRemoteDataFromServer(args[2]) case "shear": // shear an entry from the server and add it to the deletions directory deviceIDTargetLocation := strings.Split(args[2], "\x1d") diff --git a/src/sync/client.go b/src/sync/client.go index 0c8272f..58825f6 100644 --- a/src/sync/client.go +++ b/src/sync/client.go @@ -102,7 +102,8 @@ func GetSSHOutput(cmd string, manualSync bool) string { // getRemoteDataFromClient returns a map of remote entries to their modification times, a list of remote folders, and a list of queued deletions func getRemoteDataFromClient(manualSync bool) (map[string]int64, []string, []string) { // get remote output over SSH - output := GetSSHOutput("libmuttonserver fetch", manualSync) + clientDeviceID, _ := os.ReadDir(backend.ConfigDir + backend.PathSeparator + "devices") + output := GetSSHOutput("libmuttonserver fetch "+clientDeviceID[0].Name(), manualSync) // split output into slice based on occurrences of "\x1d" outputSlice := strings.Split(output, "\x1d") diff --git a/src/sync/common.go b/src/sync/common.go index 6554d3f..7964954 100644 --- a/src/sync/common.go +++ b/src/sync/common.go @@ -62,7 +62,7 @@ func getModTimes(entryList []string) []int64 { // 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 + // 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 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) diff --git a/src/sync/server.go b/src/sync/server.go index 8ad3f77..5a33c95 100644 --- a/src/sync/server.go +++ b/src/sync/server.go @@ -4,12 +4,13 @@ import ( "fmt" "github.com/rwinkhart/MUTN/src/backend" "os" + "strings" ) // GetRemoteDataFromServer prints to stdout the remote entries, mod times, folders, and deletions // lists in output are separated by "\x1d" // output is meant to be captured over SSH for interpretation by the client -func GetRemoteDataFromServer() { +func GetRemoteDataFromServer(clientDeviceID string) { entryList, dirList := WalkEntryDir() modList := getModTimes(entryList) deletionsList, err := os.ReadDir(backend.ConfigDir + backend.PathSeparator + "deletions") @@ -33,6 +34,13 @@ func GetRemoteDataFromServer() { } fmt.Print("\x1d") for _, deletion := range deletionsList { - fmt.Print("\x1f" + deletion.Name()) + // print deletion if it is relevant to the current client device + affectedIDTargetLocationIncomplete := strings.Split(deletion.Name(), "\x1d") + if affectedIDTargetLocationIncomplete[0] == clientDeviceID { + fmt.Print("\x1f" + strings.ReplaceAll(affectedIDTargetLocationIncomplete[1], "\x1e", backend.PathSeparator)) + + // assume successful client deletion and remove deletions file (if assumption is somehow false, worst case scenario is that the client will re-upload the deleted entry) + os.Remove(backend.ConfigDir + backend.PathSeparator + "deletions" + backend.PathSeparator + deletion.Name()) + } } }