mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-28 12:56:31 -04:00
61 lines
1.9 KiB
Go
61 lines
1.9 KiB
Go
package core
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/rwinkhart/rcw/wrappers"
|
|
)
|
|
|
|
// RCWSanityCheckGen generates the RCW sanity check file for libmutton.
|
|
func RCWSanityCheckGen(passphrase []byte) {
|
|
err := wrappers.GenSanityCheck(ConfigDir+"/sanity.rcw", passphrase)
|
|
if err != nil {
|
|
PrintError("Failed to generate sanity check file: "+err.Error(), ErrorWrite, true)
|
|
}
|
|
}
|
|
|
|
// DirInit creates the libmutton directories.
|
|
// Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID).
|
|
func DirInit(preserveOldConfigDir bool) string {
|
|
// create EntryRoot
|
|
err := os.MkdirAll(EntryRoot, 0700)
|
|
if err != nil {
|
|
PrintError("Failed to create \""+EntryRoot+"\": "+err.Error(), ErrorWrite, true)
|
|
}
|
|
|
|
// get old device ID before its potential removal
|
|
oldDeviceID := GetCurrentDeviceID()
|
|
|
|
// remove existing config directory (if it exists and not in append mode)
|
|
if !preserveOldConfigDir {
|
|
_, isAccessible := TargetIsFile(ConfigDir, false, 1)
|
|
if isAccessible {
|
|
err = os.RemoveAll(ConfigDir)
|
|
if err != nil {
|
|
PrintError("Failed to remove existing config directory: "+err.Error(), ErrorWrite, true)
|
|
}
|
|
}
|
|
}
|
|
|
|
// create config directory w/devices subdirectory
|
|
err = os.MkdirAll(ConfigDir+PathSeparator+"devices", 0700)
|
|
if err != nil {
|
|
PrintError("Failed to create \""+ConfigDir+"\": "+err.Error(), ErrorWrite, true)
|
|
}
|
|
|
|
return oldDeviceID
|
|
}
|
|
|
|
// GetOldDeviceID returns the current device ID or
|
|
// FSMisc if there is no device ID (e.g. first run).
|
|
func GetCurrentDeviceID() string {
|
|
deviceIDList := GenDeviceIDList(false) // errorOnFail is false so that nil is received when the devices directory does not exist
|
|
var deviceID string
|
|
if deviceIDList != nil && len(*deviceIDList) > 0 { // ensure not derferencing nil, which occurs when the devices directory does not exist
|
|
deviceID = (*deviceIDList)[0].Name()
|
|
} else {
|
|
deviceID = FSMisc // indicates to server that no device ID is being replaced
|
|
}
|
|
return deviceID
|
|
}
|