diff --git a/core/init.go b/core/init.go index c00c422..c9a86dc 100644 --- a/core/init.go +++ b/core/init.go @@ -74,7 +74,7 @@ func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData [][ return errors.New("unable to initialize libmutton directories: " + err.Error()) } // write config file - if len(clientSpecificIniData) > 0 { // TODO test passing empty clientSpecificIniData + if len(clientSpecificIniData) > 0 { err = cfg.WriteConfig(append(clientSpecificIniData, [][3]string{{"LIBMUTTON", "offlineMode", "true"}}...), nil, false) if err != nil { return errors.New("unable to write config file: " + err.Error()) diff --git a/synccycles/init.go b/synccycles/init.go index 7cd32cc..8a2a8a4 100644 --- a/synccycles/init.go +++ b/synccycles/init.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "github.com/rwinkhart/go-boilerplate/back" "github.com/rwinkhart/libmutton/global" "github.com/rwinkhart/libmutton/syncclient" ) @@ -23,32 +24,44 @@ func DeviceIDGen(oldDeviceID string) (string, string, error) { newDeviceID := deviceIDPrefix + "-" + deviceIDSuffix // create new device ID file (locally) - fileToClose, err := os.OpenFile(global.ConfigDir+global.PathSeparator+"devices"+global.PathSeparator+newDeviceID, os.O_CREATE|os.O_WRONLY, 0600) + newDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + newDeviceID + oldDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID + f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600) if err != nil { return "", "", errors.New("unable to create local device ID file: " + err.Error()) } - _ = fileToClose.Close() // error ignored; if the file could be created, it can probably be closed + _ = f.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(global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID) - if err != nil { - return "", "", errors.New("unable to remove old device ID file (locally): " + err.Error()) + cleanupOnFail := func() { + // remove new device ID file + os.RemoveAll(newDeviceIDPath) + // restore old device ID file (if it has already been removed due to DirInit) + if isAccessible, _ := back.TargetIsFile(oldDeviceIDPath, true); !isAccessible { + f, _ := os.OpenFile(oldDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600) + _ = f.Close() // error ignored; if the file could be created, it can probably be closed + } } // register new device ID with server and fetch remote EntryRoot and OS type // also removes the old device ID file (remotely) + // if registration fails, remove the new device ID file locally and return before removing the old one sshClient, _, _, _, err := syncclient.GetSSHClient() if err != nil { + cleanupOnFail() return "", "", errors.New("unable to connect to SSH client: " + err.Error()) } output, err := syncclient.GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID) if err != nil { + cleanupOnFail() return "", "", errors.New("unable to register device ID with server: " + err.Error()) } sshEntryRootSSHIsWindows := strings.Split(output, global.FSSpace) - err = sshClient.Close() + _ = sshClient.Close() // ignore error; non-critical/unlikely/not much could be done about it + + // remove old device ID file (locally; may not exist) + err = os.RemoveAll(oldDeviceIDPath) if err != nil { - return "", "", errors.New("unable to close SSH client: " + err.Error()) + return "", "", errors.New("unable to remove old device ID file (locally): " + err.Error()) } return sshEntryRootSSHIsWindows[0], sshEntryRootSSHIsWindows[1], nil