-
Notifications
You must be signed in to change notification settings - Fork 0
/
appdir.go
106 lines (97 loc) · 3.26 KB
/
appdir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2018 Shift Devices AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"os"
"path/filepath"
"runtime"
"sync"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
)
// appFolder is what AppDir always returns. It is initialized only once
// in the initAppFolder func.
//
// It is tempting to initialize appFolder in an init func but because
// the Go code is called from C/C++ using CGO, we take a safer approach
// and do it in a sync.Once call to make it more future proof and avoid
// potential bugs where an init is potentially called only after appFolder
// is being accessed.
//
// When replacing the value in tests, make sure initAppFolderOnce below
// has already been called.
var mu sync.RWMutex
var appFolder string
// SetAppDir sets the app folder (retrieved by AppDir()) and can only be called once.
func SetAppDir(folder string) {
mu.Lock()
defer mu.Unlock()
if appFolder != "" {
panic("app folder already set")
}
appFolder = folder
}
func defaultAppFolder() string {
var folder string
switch runtime.GOOS {
case "darwin":
// Usually /Users/$USER/Library/Application Support.
folder = os.Getenv("HOME") + "/Library/Application Support"
case "windows":
folder = os.Getenv("APPDATA")
case "linux":
// Previously, we always used $HOME/.config/bitbox to store paired channel data.
// Because XDG_CONFIG_HOME is preferred over HOME env var in default setup,
// existing users may suffer if they loose existing pairing channel info,
// especially when 2FA is enabled, requiring full device reset.
// So, check for the existing dir first. If that fails, use the regular approach.
// For most users, it is a noop.
// See https://github.com/digitalbitbox/bitbox-wallet-app/pull/16 for more details.
folder = filepath.Join(os.Getenv("HOME"), ".config")
if fi, err := os.Stat(folder + "/bitbox"); err == nil && fi.IsDir() {
break
}
// Usually /home/$USER/.config.
folder = os.Getenv("XDG_CONFIG_HOME")
if folder == "" {
folder = filepath.Join(os.Getenv("HOME"), ".config")
}
}
folder = filepath.Join(folder, "bitbox")
return folder
}
// AppDir returns the absolute path to the default BitBox desktop app directory
// in the user standard config location.
func AppDir() string {
mu.RLock()
folder := appFolder
mu.RUnlock()
if folder != "" {
return folder
}
mu.Lock()
appFolder = defaultAppFolder()
mu.Unlock()
return appFolder
}
// DownloadsDir returns the absolute path to the Downloads folder in the home folder.
func DownloadsDir() (string, error) {
homeFolder := os.Getenv("HOME")
if runtime.GOOS == "windows" && homeFolder == "" {
homeFolder = os.Getenv("USERPROFILE")
}
if homeFolder == "" {
return "", errp.New("can't find home directory")
}
return filepath.Join(homeFolder, "Downloads"), nil
}