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
+6 -2
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. // 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 { func GenDeviceIDList(errorOnFail bool) *[]fs.DirEntry {
// create a slice of all registered devices // create a slice of all registered devices
deviceIDList, err := os.ReadDir(ConfigDir + PathSeparator + "devices") deviceIDList, err := os.ReadDir(ConfigDir + PathSeparator + "devices")
if err != nil && errorOnFail { if err != nil {
if errorOnFail {
fmt.Println(AnsiError+"Failed to read the devices directory:", err.Error()+AnsiReset) fmt.Println(AnsiError+"Failed to read the devices directory:", err.Error()+AnsiReset)
os.Exit(101) os.Exit(101)
} else {
return nil // a nil return value indicates that the devices directory could not be read/does not exist
}
} }
return &deviceIDList return &deviceIDList
} }
+8 -2
View File
@@ -53,7 +53,7 @@ func GpgKeyGen() string {
} }
// DirInit creates the libmutton directories. // 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 { func DirInit(preserveOldConfigDir bool) string {
// create EntryRoot // create EntryRoot
err := os.MkdirAll(EntryRoot, 0700) err := os.MkdirAll(EntryRoot, 0700)
@@ -63,7 +63,13 @@ func DirInit(preserveOldConfigDir bool) string {
} }
// get old device ID before its potential removal // 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) // remove existing config directory (if it exists and not in append mode)
if !preserveOldConfigDir { if !preserveOldConfigDir {
+1 -2
View File
@@ -20,9 +20,8 @@ func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8)
if errorOnFail { if errorOnFail {
fmt.Println(AnsiError + "Failed to access \"" + targetLocation + "\" - Ensure it exists and has the correct permissions" + AnsiReset) fmt.Println(AnsiError + "Failed to access \"" + targetLocation + "\" - Ensure it exists and has the correct permissions" + AnsiReset)
os.Exit(105) os.Exit(105)
} else {
return false, false
} }
return false, false
} }
if targetInfo.IsDir() { if targetInfo.IsDir() {
if errorOnFail && failCondition == 2 { if errorOnFail && failCondition == 2 {
+2
View File
@@ -59,7 +59,9 @@ func main() {
// stdin[1] is expected to be the old device ID (for removal) // stdin[1] is expected to be the old device ID (for removal)
fileToClose, _ := os.OpenFile(core.ConfigDir+core.PathSeparator+"devices"+core.PathSeparator+stdin[0], os.O_CREATE|os.O_WRONLY, 0600) // errors ignored; failure unlikely to occur if init was successful; "register" is not a user-facing argument and thus the error would not be visible fileToClose, _ := os.OpenFile(core.ConfigDir+core.PathSeparator+"devices"+core.PathSeparator+stdin[0], os.O_CREATE|os.O_WRONLY, 0600) // errors ignored; failure unlikely to occur if init was successful; "register" is not a user-facing argument and thus the error would not be visible
_ = fileToClose.Close() _ = fileToClose.Close()
if stdin[1] != sync.FSMisc { // sync.FSMisc is used to indicate that no device ID is being replaced
_ = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + stdin[1]) _ = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + stdin[1])
}
// print EntryRoot and bool indicating OS type to stdout for client to store in config // print EntryRoot and bool indicating OS type to stdout for client to store in config
fmt.Print(core.EntryRoot + sync.FSSpace + strconv.FormatBool(core.IsWindows)) fmt.Print(core.EntryRoot + sync.FSSpace + strconv.FormatBool(core.IsWindows))
case "init": case "init":
+1 -4
View File
@@ -9,7 +9,6 @@ import (
"time" "time"
"github.com/rwinkhart/libmutton/core" "github.com/rwinkhart/libmutton/core"
"golang.org/x/crypto/ssh"
) )
// DeviceIDGen generates a new client device ID and registers it with the server (will replace existing one). // DeviceIDGen generates a new client device ID and registers it with the server (will replace existing one).
@@ -34,14 +33,12 @@ func DeviceIDGen(oldDeviceID string) (string, string) {
// also removes the old device ID file (remotely) // also removes the old device ID file (remotely)
// manualSync is true so the user is alerted if device ID registration fails // manualSync is true so the user is alerted if device ID registration fails
sshClient, _, _ := GetSSHClient(true) sshClient, _, _ := GetSSHClient(true)
defer func(sshClient *ssh.Client) { sshEntryRootSSHIsWindows := strings.Split(GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID), FSSpace)
err = sshClient.Close() err = sshClient.Close()
if err != nil { if err != nil {
fmt.Println(core.AnsiError+"Init failed - Unable to close SSH client:", err.Error()+core.AnsiReset) fmt.Println(core.AnsiError+"Init failed - Unable to close SSH client:", err.Error()+core.AnsiReset)
os.Exit(104) os.Exit(104)
} }
}(sshClient)
sshEntryRootSSHIsWindows := strings.Split(GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID), FSSpace)
// remove old device ID file (locally; may not exist) // remove old device ID file (locally; may not exist)
err = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + oldDeviceID) err = os.RemoveAll(core.ConfigDir + core.PathSeparator + "devices" + core.PathSeparator + oldDeviceID)