paths: fall back to XDG_DATA_HOME for non-root users' state dir
So peerapi has a default state directory, mostly for netstack mode
testing.
package paths
import (
+ "os"
"path/filepath"
"runtime"
@@ -45,8 +46,17 @@ func stateFileUnix() string {
try = filepath.Dir(try)
}
- // TODO: try some $HOME/.tailscale or XDG path? But will it
- // even work usefully enough as non-root? Probably not. Maybe
- // best to require it be explicit in that case.
+ if os.Getuid() == 0 {
return ""
}
+
+ // For non-root users, fall back to $XDG_DATA_HOME/tailscale/*.
+ return filepath.Join(xdgDataHome(), "tailscale", "tailscaled.state")
+}
+
+func xdgDataHome() string {
+ if e := os.Getenv("XDG_DATA_HOME"); e != "" {
+ return e
+ }
+ return filepath.Join(os.Getenv("HOME"), ".local/share")
+}
