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
// 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.
+9 -10
View File
@@ -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