Fix init if devices directory does not exist/is empty

This commit is contained in:
2024-08-13 20:21:11 -04:00
parent b1fc8e3c4d
commit be46b0d6b2
5 changed files with 25 additions and 17 deletions
+8 -4
View File
@@ -51,13 +51,17 @@ func ParseConfig(valuesRequested [][2]string, missingValueError string) []string
}
// GenDeviceIDList returns a pointer to a slice of all registered device IDs.
// Requires: errorOnFail (set to true to throw an error if the device ID list cannot be generated)
// Requires: errorOnFail (set to true to throw an error if the devices directory cannot be read/does not exist)
func GenDeviceIDList(errorOnFail bool) *[]fs.DirEntry {
// create a slice of all registered devices
deviceIDList, err := os.ReadDir(ConfigDir + PathSeparator + "devices")
if err != nil && errorOnFail {
fmt.Println(AnsiError+"Failed to read the devices directory:", err.Error()+AnsiReset)
os.Exit(101)
if err != nil {
if errorOnFail {
fmt.Println(AnsiError+"Failed to read the devices directory:", err.Error()+AnsiReset)
os.Exit(101)
} else {
return nil // a nil return value indicates that the devices directory could not be read/does not exist
}
}
return &deviceIDList
}
+8 -2
View File
@@ -53,7 +53,7 @@ func GpgKeyGen() string {
}
// DirInit creates the libmutton directories.
// Returns: oldDeviceID (from before the directory reset).
// Returns: oldDeviceID (from before the directory reset; will be sync.FSMisc if there is no pre-existing ID).
func DirInit(preserveOldConfigDir bool) string {
// create EntryRoot
err := os.MkdirAll(EntryRoot, 0700)
@@ -63,7 +63,13 @@ func DirInit(preserveOldConfigDir bool) string {
}
// get old device ID before its potential removal
oldDeviceID := (*GenDeviceIDList(false))[0].Name() // errorOnFail set to false to ignore error if device ID directory does not exist (error non-critical for this function)
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 = "\u259f" // sync.FSMisc cannot be used directly due to import cycle; indicates to server that no device ID is being replaced
}
// remove existing config directory (if it exists and not in append mode)
if !preserveOldConfigDir {
+1 -2
View File
@@ -20,9 +20,8 @@ func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8)
if errorOnFail {
fmt.Println(AnsiError + "Failed to access \"" + targetLocation + "\" - Ensure it exists and has the correct permissions" + AnsiReset)
os.Exit(105)
} else {
return false, false
}
return false, false
}
if targetInfo.IsDir() {
if errorOnFail && failCondition == 2 {