Rework getting data path.

This commit is contained in:
Martin Dosch 2024-04-07 09:11:03 +02:00
parent 080100486e
commit c7376832ce
No known key found for this signature in database
GPG Key ID: 52A57CFCE13D657D
2 changed files with 25 additions and 64 deletions

View File

@ -56,14 +56,14 @@ func getClientID() (string, error) {
var clientID string
clientIDLoc, err := getClientIDLoc()
if err != nil {
return "", err
return strError, err
}
buf, err := readFile(clientIDLoc)
if err != nil {
clientID = uuid.NewString()
file, err := os.Create(clientIDLoc)
if err != nil {
return "", fmt.Errorf("getClientID: failed to create clientid file: %w", err)
return strError, fmt.Errorf("getClientID: failed to create clientid file: %w", err)
}
defer file.Close()
if runtime.GOOS != "windows" {
@ -73,7 +73,7 @@ func getClientID() (string, error) {
}
_, err = file.Write([]byte(clientID))
if err != nil {
return "", fmt.Errorf("getClientID: failed to write client id file: %w", err)
return strError, fmt.Errorf("getClientID: failed to write client id file: %w", err)
}
} else {
clientID = buf.String()
@ -81,9 +81,7 @@ func getClientID() (string, error) {
return clientID, nil
}
// TODO: Dedup code by putting detection of dataDir in a separate function and
// remove it from func oxGetPubKeyLoc oxGetPrivKeyLoc and getClientIDLoc
func getClientIDLoc() (string, error) {
func getDataPath(folder string) (string, error) {
var err error
var homeDir, dataDir string
switch {
@ -98,20 +96,31 @@ func getClientIDLoc() (string, error) {
default:
homeDir, err = os.UserHomeDir()
if err != nil {
return strError, fmt.Errorf("getClientIDLoc: failed to determine user dir: %w", err)
return strError, fmt.Errorf("getDataPath: failed to determine user dir: %w", err)
}
if homeDir == "" {
return strError, errors.New("getClientIDLoc: received empty string for home directory")
return strError, errors.New("getDataPath: received empty string for home directory")
}
dataDir = homeDir + "/.local/share"
}
dataDir += "/go-sendxmpp/"
if folder != "" && !strings.HasSuffix(folder, "/") {
folder = fmt.Sprintf("%s/", folder)
}
dataDir = fmt.Sprintf("%s/go-sendxmpp/%s", dataDir, folder)
if _, err = os.Stat(dataDir); os.IsNotExist(err) {
err = os.MkdirAll(dataDir, defaultDirRights)
if err != nil {
return strError, fmt.Errorf("getClientIDLoc: could not create folder: %w", err)
return strError, fmt.Errorf("getDataPath: could not create folder: %w", err)
}
}
return dataDir, nil
}
func getClientIDLoc() (string, error) {
dataDir, err := getDataPath("")
if err != nil {
return strError, fmt.Errorf("getClientIDLoc: %w", err)
}
dataFile := dataDir + "clientid"
return dataFile, nil
}

60
ox.go
View File

@ -316,33 +316,9 @@ func oxPublishPubKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ, pubKey *
}
func oxGetPrivKeyLoc(jid string) (string, error) {
var err error
var homeDir, dataDir string
switch {
case os.Getenv("$XDG_DATA_HOME") != "":
dataDir = os.Getenv("$XDG_DATA_HOME")
case os.Getenv("$XDG_HOME") != "":
homeDir = os.Getenv("$XDG_HOME")
dataDir = homeDir + "/.local/share"
case os.Getenv("$HOME") != "":
homeDir = os.Getenv("$HOME")
dataDir = homeDir + "/.local/share"
default:
homeDir, err = os.UserHomeDir()
if err != nil {
return strError, fmt.Errorf("oxGetPrivKeyLoc: failed to determine user dir: %w", err)
}
if homeDir == "" {
return strError, errors.New("oxGetPrivKeyLoc: received empty string for home directory")
}
dataDir = homeDir + "/.local/share"
}
dataDir += "/go-sendxmpp/oxprivkeys/"
if _, err = os.Stat(dataDir); os.IsNotExist(err) {
err = os.MkdirAll(dataDir, defaultDirRights)
if err != nil {
return strError, fmt.Errorf("oxGetPrivKeyLoc: could not create folder for private keys: %w", err)
}
dataDir, err := getDataPath("oxprivkeys/")
if err != nil {
return strError, fmt.Errorf("oxGetPubKeyLoc: %w", err)
}
// TODO: Remove handling of oldDataFile in a later version when it's very likely that there are no
// more versions in use using the oldDataFile (<0.8.3).
@ -365,33 +341,9 @@ func oxGetPrivKeyLoc(jid string) (string, error) {
}
func oxGetPubKeyLoc(fingerprint string) (string, error) {
var err error
var homeDir, dataDir string
switch {
case os.Getenv("$XDG_DATA_HOME") != "":
dataDir = os.Getenv("$XDG_DATA_HOME")
case os.Getenv("$XDG_HOME") != "":
homeDir = os.Getenv("$XDG_HOME")
dataDir = homeDir + "/.local/share"
case os.Getenv("$HOME") != "":
homeDir = os.Getenv("$HOME")
dataDir = homeDir + "/.local/share"
default:
homeDir, err = os.UserHomeDir()
if err != nil {
return strError, fmt.Errorf("oxGetPubKeyLoc: failed to determine user dir: %w", err)
}
if homeDir == "" {
return strError, errors.New("oxGetPubKeyLoc: received empty string for home directory")
}
dataDir = homeDir + "/.local/share"
}
dataDir += "/go-sendxmpp/oxpubkeys/"
if _, err = os.Stat(dataDir); os.IsNotExist(err) {
err = os.MkdirAll(dataDir, defaultDirRights)
if err != nil {
return strError, fmt.Errorf("oxGetPubKeyLoc: could not create folder for public keys: %w", err)
}
dataDir, err := getDataPath("oxpubkeys/")
if err != nil {
return strError, fmt.Errorf("oxGetPubKeyLoc: %w", err)
}
dataFile := dataDir + fingerprint
return dataFile, nil