From 51bb67f3150e70c2c8e8dc3cc72de5666d7c058a Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Wed, 19 Mar 2025 23:35:50 -0400 Subject: [PATCH] Break current device ID retrieval out into exported function GetCurrentDeviceID(); fix duplicate device IDs being generated by not deleting the old one before a potentially failed registration --- core/init.go | 21 ++++++++++++++------- sync/init.go | 12 ++++++------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/core/init.go b/core/init.go index f23afd0..8830f78 100644 --- a/core/init.go +++ b/core/init.go @@ -60,13 +60,7 @@ func DirInit(preserveOldConfigDir bool) string { } // get old device ID before its potential removal - oldDeviceIDList := GenDeviceIDList(false) // errorOnFail is false so that nil is received when the devices directory does not exist - var oldDeviceID string - if oldDeviceIDList != nil && len(*oldDeviceIDList) > 0 { // ensure not derferencing nil, which occurs when the devices directory does not exist - oldDeviceID = (*oldDeviceIDList)[0].Name() - } else { - oldDeviceID = FSMisc // indicates to server that no device ID is being replaced - } + oldDeviceID := GetCurrentDeviceID() // remove existing config directory (if it exists and not in append mode) if !preserveOldConfigDir { @@ -87,3 +81,16 @@ func DirInit(preserveOldConfigDir bool) string { return oldDeviceID } + +// GetOldDeviceID returns the current device ID or +// FSMisc if there is no device ID (e.g. first run). +func GetCurrentDeviceID() string { + deviceIDList := GenDeviceIDList(false) // errorOnFail is false so that nil is received when the devices directory does not exist + var deviceID string + if deviceIDList != nil && len(*deviceIDList) > 0 { // ensure not derferencing nil, which occurs when the devices directory does not exist + deviceID = (*deviceIDList)[0].Name() + } else { + deviceID = FSMisc // indicates to server that no device ID is being replaced + } + return deviceID +} diff --git a/sync/init.go b/sync/init.go index cdccd9d..ce0eeb8 100644 --- a/sync/init.go +++ b/sync/init.go @@ -27,6 +27,12 @@ func DeviceIDGen(oldDeviceID string) (string, string) { } _ = fileToClose.Close() // error ignored; if the file could be created, it can probably be closed + // remove old device ID file (locally; may not exist) + err = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + oldDeviceID) + if err != nil { + core.PrintError("Failed to remove old device ID file (locally): "+err.Error(), core.ErrorWrite, true) + } + // register new device ID with server and fetch remote EntryRoot and OS type // also removes the old device ID file (remotely) // manualSync is true so the user is alerted if device ID registration fails @@ -37,11 +43,5 @@ func DeviceIDGen(oldDeviceID string) (string, string) { core.PrintError("Init failed - Unable to close SSH client: "+err.Error(), core.ErrorServerConnection, true) } - // remove old device ID file (locally; may not exist) - err = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + oldDeviceID) - if err != nil { - core.PrintError("Failed to remove old device ID file (locally): "+err.Error(), core.ErrorWrite, true) - } - return sshEntryRootSSHIsWindows[0], sshEntryRootSSHIsWindows[1] }