Use nil pointer to indicate missing device ID (rather than FSMisc)

This commit is contained in:
2026-01-21 18:49:16 -05:00
parent 48e084ac0f
commit 409db78d83
5 changed files with 48 additions and 31 deletions
+5 -5
View File
@@ -7,19 +7,19 @@ import (
) )
// GetCurrentDeviceID returns the current device ID or // GetCurrentDeviceID returns the current device ID or
// FSMisc if there is no device ID (e.g. first run). // nil if there is no device ID (e.g. first run).
func GetCurrentDeviceID() (string, error) { func GetCurrentDeviceID() (*string, error) {
deviceIDList, err := GenDeviceIDList() deviceIDList, err := GenDeviceIDList()
if err != nil { 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 var deviceID string
if len(deviceIDList) > 0 { if len(deviceIDList) > 0 {
deviceID = (deviceIDList)[0].Name() deviceID = (deviceIDList)[0].Name()
} else { } 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. // GenDeviceIDList returns a slice of all registered device IDs.
+9 -10
View File
@@ -9,36 +9,35 @@ import (
// DirInit creates the libmutton directories. // DirInit creates the libmutton directories.
// Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID). // 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 // create EntryRoot
if err := os.MkdirAll(EntryRoot, 0700); err != nil { if err = os.MkdirAll(EntryRoot, 0700); err != nil {
return "", errors.New("unable to create \"" + EntryRoot + "\": " + err.Error()) return nil, errors.New("unable to create \"" + EntryRoot + "\": " + err.Error())
} }
// get old device ID before its potential removal // get old device ID before its potential removal
oldDeviceID, err := GetCurrentDeviceID() oldDeviceID, _ := GetCurrentDeviceID() // error ignored; oldDeviceID is set to nil on error, which is the correct assumption
if err != nil {
oldDeviceID = FSMisc
}
// remove existing config directory (if it exists and not in append mode) // remove existing config directory (if it exists and not in append mode)
if !preserveOldCfgDir { if !preserveOldCfgDir {
isAccessible, _ := back.TargetIsFile(CfgDir, false) // error is ignored because dir/file status is irrelevant isAccessible, _ := back.TargetIsFile(CfgDir, false) // error is ignored because dir/file status is irrelevant
if isAccessible { if isAccessible {
if err = os.RemoveAll(CfgDir); err != nil { 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 // create config directory w/devices subdirectory
if err = os.MkdirAll(CfgDir+PathSeparator+"devices", 0700); err != nil { 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 // create password age directory
if err = os.MkdirAll(AgeDir, 0700); err != nil { 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 return oldDeviceID, nil
+13 -9
View File
@@ -93,17 +93,22 @@ func main() {
} }
case "register": case "register":
// register a new device ID // register a new device ID
// stdin[0] is expected to be the device ID // stdin[0] is expected to be JSON matching type synccommon.RegisterReqT
// stdin[1] is expected to be the old device ID (for removal) var registerReq synccommon.RegisterReqT
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"devices"+global.PathSeparator+stdin[0], os.O_CREATE|os.O_WRONLY, 0600) err := json.Unmarshal([]byte(stdin[0]), &registerReq)
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 { if err != nil {
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error()) fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
return return
} }
_ = f.Close() _ = 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 // 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()) fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
return return
} }
@@ -116,8 +121,8 @@ func main() {
} }
for _, deletion := range deletionsList { for _, deletion := range deletionsList {
affectedIDVanityPath := strings.Split(deletion.Name(), global.FSSpace) affectedIDVanityPath := strings.Split(deletion.Name(), global.FSSpace)
if affectedIDVanityPath[0] == stdin[1] { if affectedIDVanityPath[0] == *registerReq.OldDeviceID {
if err = os.Rename(deletionsDirRoot+deletion.Name(), deletionsDirRoot+stdin[0]+global.FSSpace+affectedIDVanityPath[1]+global.FSSpace+affectedIDVanityPath[2]); err != nil { 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()) fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
return return
} }
@@ -126,8 +131,7 @@ func main() {
} }
// print EntryRoot, AgeDir and bool indicating OS type to stdout for client to store in config // 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(synccommon.RegisterRespT{EntryRoot: global.EntryRoot, AgeDir: global.AgeDir, IsWindows: global.IsWindows})
registerRespBytes, err := json.Marshal(registerResp)
if err != nil { if err != nil {
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error()) fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
return return
+16 -7
View File
@@ -164,7 +164,7 @@ end:
// Device IDs are guaranteed unique as the current UNIX time is appended to them. // 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. // Leave prefix empty to use the current hostname as the prefix.
// Returns: the remote EntryRoot, the remote AgeDir, and OS type indicator. // 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 // generate new device ID
if prefix == "" { if prefix == "" {
prefix, _ = os.Hostname() prefix, _ = os.Hostname()
@@ -173,7 +173,10 @@ func GenDeviceID(oldDeviceID, prefix string) (string, string, bool, error) {
// create new device ID file (locally) // create new device ID file (locally)
newDeviceIDPath := global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + newDeviceID 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) f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil { if err != nil {
return "", "", false, errors.New("unable to create local device ID file: " + err.Error()) 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() { cleanupOnFail := func() {
// remove new device ID file // remove new device ID file
_ = os.RemoveAll(newDeviceIDPath) _ = 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) // restore old device ID file (if it existed and has already been removed due to DirInit)
if isAccessible, _ := back.TargetIsFile(oldDeviceIDPath, true); !isAccessible { if isAccessible, _ := back.TargetIsFile(oldDeviceIDPath, true); !isAccessible {
f, _ := os.OpenFile(oldDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600) 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() cleanupOnFail()
return "", "", false, errors.New("unable to connect to SSH client: " + err.Error()) 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 { if err != nil {
cleanupOnFail() cleanupOnFail()
return "", "", false, errors.New("unable to register device ID with server: " + err.Error()) 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 _ = sshClient.Close() // ignore error; non-critical/unlikely/not much could be done about it
// remove old device ID file (locally; may not exist) // remove old device ID file (locally; may not exist)
if err = os.RemoveAll(oldDeviceIDPath); err != nil { if oldDeviceID != nil {
cleanupOnFail() if err = os.RemoveAll(oldDeviceIDPath); err != nil {
return "", "", false, errors.New("unable to remove old device ID file (locally): " + err.Error()) cleanupOnFail()
return "", "", false, errors.New("unable to remove old device ID file (locally): " + err.Error())
}
} }
return registerResp.EntryRoot, registerResp.AgeDir, registerResp.IsWindows, nil return registerResp.EntryRoot, registerResp.AgeDir, registerResp.IsWindows, nil
+5
View File
@@ -42,6 +42,11 @@ type RegisterRespT struct {
IsWindows bool `json:"isWindows"` 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 // GetAllEntryData returns a map of all vanity paths to
// their respective containing folders and mod+age timestamps. // their respective containing folders and mod+age timestamps.
func GetAllEntryData() (EntryMapT, error) { func GetAllEntryData() (EntryMapT, error) {