diff --git a/global/deviceIDs.go b/global/deviceIDs.go index c4f32cf..2737365 100644 --- a/global/deviceIDs.go +++ b/global/deviceIDs.go @@ -7,19 +7,19 @@ import ( ) // GetCurrentDeviceID returns the current device ID or -// FSMisc if there is no device ID (e.g. first run). -func GetCurrentDeviceID() (string, error) { +// nil if there is no device ID (e.g. first run). +func GetCurrentDeviceID() (*string, error) { deviceIDList, err := GenDeviceIDList() if err != nil { - return "", errors.New("unable to generate device ID list: " + err.Error()) + return nil, errors.New("unable to generate device ID list: " + err.Error()) } var deviceID string if len(deviceIDList) > 0 { deviceID = (deviceIDList)[0].Name() } else { - deviceID = FSMisc // indicates to server that no device ID is being replaced + return nil, nil // nil device ID indicates to server that no device ID is being replaced } - return deviceID, nil + return &deviceID, nil } // GenDeviceIDList returns a slice of all registered device IDs. diff --git a/global/init.go b/global/init.go index 82f0fcf..47d5dcf 100644 --- a/global/init.go +++ b/global/init.go @@ -9,36 +9,35 @@ import ( // DirInit creates the libmutton directories. // Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID). -func DirInit(preserveOldCfgDir bool) (string, error) { +func DirInit(preserveOldCfgDir bool) (*string, error) { + var err error + // create EntryRoot - if err := os.MkdirAll(EntryRoot, 0700); err != nil { - return "", errors.New("unable to create \"" + EntryRoot + "\": " + err.Error()) + if err = os.MkdirAll(EntryRoot, 0700); err != nil { + return nil, errors.New("unable to create \"" + EntryRoot + "\": " + err.Error()) } // get old device ID before its potential removal - oldDeviceID, err := GetCurrentDeviceID() - if err != nil { - oldDeviceID = FSMisc - } + oldDeviceID, _ := GetCurrentDeviceID() // error ignored; oldDeviceID is set to nil on error, which is the correct assumption // remove existing config directory (if it exists and not in append mode) if !preserveOldCfgDir { isAccessible, _ := back.TargetIsFile(CfgDir, false) // error is ignored because dir/file status is irrelevant if isAccessible { if err = os.RemoveAll(CfgDir); err != nil { - return "", errors.New("unable to remove existing config directory: " + err.Error()) + return nil, errors.New("unable to remove existing config directory: " + err.Error()) } } } // create config directory w/devices subdirectory if err = os.MkdirAll(CfgDir+PathSeparator+"devices", 0700); err != nil { - return "", errors.New("unable to create \"" + CfgDir + "\": " + err.Error()) + return nil, errors.New("unable to create \"" + CfgDir + "\": " + err.Error()) } // create password age directory if err = os.MkdirAll(AgeDir, 0700); err != nil { - return "", errors.New("unable to create \"" + AgeDir + "\": " + err.Error()) + return nil, errors.New("unable to create \"" + AgeDir + "\": " + err.Error()) } return oldDeviceID, nil diff --git a/libmuttonserver.go b/libmuttonserver.go index 9566bc2..06dcb92 100644 --- a/libmuttonserver.go +++ b/libmuttonserver.go @@ -93,17 +93,22 @@ func main() { } case "register": // register a new device ID - // stdin[0] is expected to be the device ID - // stdin[1] is expected to be the old device ID (for removal) - f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"devices"+global.PathSeparator+stdin[0], os.O_CREATE|os.O_WRONLY, 0600) + // stdin[0] is expected to be JSON matching type synccommon.RegisterReqT + var registerReq synccommon.RegisterReqT + err := json.Unmarshal([]byte(stdin[0]), ®isterReq) + if err != nil { + fmt.Printf("{\"errMsg\":\"%s\"}", err.Error()) + return + } + f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"devices"+global.PathSeparator+registerReq.NewDeviceID, os.O_CREATE|os.O_WRONLY, 0600) if err != nil { fmt.Printf("{\"errMsg\":\"%s\"}", err.Error()) return } _ = f.Close() - if stdin[1] != global.FSMisc { // FSMisc is used to indicate that no device ID is being replaced + if registerReq.OldDeviceID != nil { // nil is used to indicate that no device ID is being replaced // remove the old device ID file - if err = os.RemoveAll(global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + stdin[1]); err != nil { + if err = os.RemoveAll(global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + *registerReq.OldDeviceID); err != nil { fmt.Printf("{\"errMsg\":\"%s\"}", err.Error()) return } @@ -116,8 +121,8 @@ func main() { } for _, deletion := range deletionsList { affectedIDVanityPath := strings.Split(deletion.Name(), global.FSSpace) - if affectedIDVanityPath[0] == stdin[1] { - if err = os.Rename(deletionsDirRoot+deletion.Name(), deletionsDirRoot+stdin[0]+global.FSSpace+affectedIDVanityPath[1]+global.FSSpace+affectedIDVanityPath[2]); err != nil { + if affectedIDVanityPath[0] == *registerReq.OldDeviceID { + if err = os.Rename(deletionsDirRoot+deletion.Name(), deletionsDirRoot+registerReq.NewDeviceID+global.FSSpace+affectedIDVanityPath[1]+global.FSSpace+affectedIDVanityPath[2]); err != nil { fmt.Printf("{\"errMsg\":\"%s\"}", err.Error()) return } @@ -126,8 +131,7 @@ func main() { } // print EntryRoot, AgeDir and bool indicating OS type to stdout for client to store in config - registerResp := synccommon.RegisterRespT{EntryRoot: global.EntryRoot, AgeDir: global.AgeDir, IsWindows: global.IsWindows} - registerRespBytes, err := json.Marshal(registerResp) + registerRespBytes, err := json.Marshal(synccommon.RegisterRespT{EntryRoot: global.EntryRoot, AgeDir: global.AgeDir, IsWindows: global.IsWindows}) if err != nil { fmt.Printf("{\"errMsg\":\"%s\"}", err.Error()) return diff --git a/syncclient/oneOff.go b/syncclient/oneOff.go index 537a3d7..21f00d9 100644 --- a/syncclient/oneOff.go +++ b/syncclient/oneOff.go @@ -164,7 +164,7 @@ end: // Device IDs are guaranteed unique as the current UNIX time is appended to them. // Leave prefix empty to use the current hostname as the prefix. // Returns: the remote EntryRoot, the remote AgeDir, and OS type indicator. -func GenDeviceID(oldDeviceID, prefix string) (string, string, bool, error) { +func GenDeviceID(oldDeviceID *string, prefix string) (string, string, bool, error) { // generate new device ID if prefix == "" { prefix, _ = os.Hostname() @@ -173,7 +173,10 @@ func GenDeviceID(oldDeviceID, prefix string) (string, string, bool, error) { // create new device ID file (locally) newDeviceIDPath := global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + newDeviceID - oldDeviceIDPath := global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID + var oldDeviceIDPath string + if oldDeviceID != nil { + oldDeviceIDPath = global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + *oldDeviceID + } f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600) if err != nil { return "", "", false, errors.New("unable to create local device ID file: " + err.Error()) @@ -183,7 +186,7 @@ func GenDeviceID(oldDeviceID, prefix string) (string, string, bool, error) { cleanupOnFail := func() { // remove new device ID file _ = os.RemoveAll(newDeviceIDPath) - if oldDeviceID != global.FSMisc { + if oldDeviceID != nil { // restore old device ID file (if it existed and 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) @@ -200,7 +203,11 @@ func GenDeviceID(oldDeviceID, prefix string) (string, string, bool, error) { cleanupOnFail() return "", "", false, errors.New("unable to connect to SSH client: " + err.Error()) } - output, err := GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID) + registerReqBytes, err := json.Marshal(synccommon.RegisterReqT{NewDeviceID: newDeviceID, OldDeviceID: oldDeviceID}) + if err != nil { + return "", "", false, errors.New("unable to marshal client register request: " + err.Error()) + } + output, err := GetSSHOutput(sshClient, "libmuttonserver register", string(registerReqBytes)) if err != nil { cleanupOnFail() return "", "", false, errors.New("unable to register device ID with server: " + err.Error()) @@ -217,9 +224,11 @@ func GenDeviceID(oldDeviceID, prefix string) (string, string, bool, error) { _ = sshClient.Close() // ignore error; non-critical/unlikely/not much could be done about it // remove old device ID file (locally; may not exist) - if err = os.RemoveAll(oldDeviceIDPath); err != nil { - cleanupOnFail() - return "", "", false, errors.New("unable to remove old device ID file (locally): " + err.Error()) + if oldDeviceID != nil { + if err = os.RemoveAll(oldDeviceIDPath); err != nil { + cleanupOnFail() + return "", "", false, errors.New("unable to remove old device ID file (locally): " + err.Error()) + } } return registerResp.EntryRoot, registerResp.AgeDir, registerResp.IsWindows, nil diff --git a/synccommon/common.go b/synccommon/common.go index 95583af..3bf9794 100644 --- a/synccommon/common.go +++ b/synccommon/common.go @@ -42,6 +42,11 @@ type RegisterRespT struct { IsWindows bool `json:"isWindows"` } +type RegisterReqT struct { + NewDeviceID string `json:"newDeviceID"` + OldDeviceID *string `json:"oldDeviceID"` // nil if not replacing an existing device ID +} + // GetAllEntryData returns a map of all vanity paths to // their respective containing folders and mod+age timestamps. func GetAllEntryData() (EntryMapT, error) {