-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.go
More file actions
47 lines (43 loc) · 1.43 KB
/
Copy pathpaths.go
File metadata and controls
47 lines (43 loc) · 1.43 KB
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
package main
import (
"os"
"path/filepath"
)
// Where the database lives, decided by one rule the user never has to read about:
//
// --data <dir> explicit, wins over everything
// <exe dir>/data exists → portable mode, keep everything beside the binary
// otherwise → the platform's system location
//
// The portable zip ships an empty data/ directory, the installer does not. That
// single difference is what makes an unzipped copy leave no trace outside its own
// folder while an installed service writes where a service should, with no flag
// and no explanation required of the user.
//
// The Unix system location stays "./data", which is what fogping always used: a
// tarball upgraded in place must keep finding its own history.
func resolveDataDir(override string) (dir string, portable bool) {
if override != "" {
return override, false
}
if d, ok := portableDir(); ok {
return d, true
}
return systemDataDir(), false
}
// portableDir reports the data directory next to the executable, if it exists.
// A symlinked or unreadable executable path simply means "not portable".
func portableDir() (string, bool) {
exe, err := os.Executable()
if err != nil {
return "", false
}
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
exe = resolved
}
d := filepath.Join(filepath.Dir(exe), "data")
if st, err := os.Stat(d); err == nil && st.IsDir() {
return d, true
}
return "", false
}