diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 71564a2c4..da5536a08 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -109,6 +109,22 @@ func main() { confDir = expandTilde(confDir) + if _, err := os.Stat(confDir); err != nil && confDir == getDefaultConfDir() { + // We are supposed to use the default configuration directory. It + // doesn't exist. In the past our default has been ~/.syncthing, so if + // that directory exists we move it to the new default location and + // continue. We don't much care if this fails at this point, we will + // be checking that later. + + oldDefault := expandTilde("~/.syncthing") + if _, err := os.Stat(oldDefault); err == nil { + os.MkdirAll(filepath.Dir(confDir), 0700) + if err := os.Rename(oldDefault, confDir); err == nil { + infoln("Moved config dir", oldDefault, "to", confDir) + } + } + } + // Ensure that our home directory exists and that we have a certificate and key. ensureDir(confDir, 0700) @@ -560,39 +576,51 @@ func ensureDir(dir string, mode int) { } } +func getDefaultConfDir() string { + switch runtime.GOOS { + case "windows": + return filepath.Join(os.Getenv("AppData"), "Syncthing") + case "darwin": + return expandTilde("~/Library/Application Support/Syncthing") + default: + if xdgCfg := os.Getenv("XDG_CONFIG_HOME"); xdgCfg != "" { + return xdgCfg + "/syncthing" + } else { + return expandTilde("~/.config/syncthing") + } + } +} + func expandTilde(p string) string { - if runtime.GOOS == "windows" { + if !strings.HasPrefix(p, "~/") { return p } - if strings.HasPrefix(p, "~/") { - return strings.Replace(p, "~", getUnixHomeDir(), 1) - } - return p -} + switch runtime.GOOS { + case "windows": + return p -func getUnixHomeDir() string { - home := os.Getenv("HOME") - if home == "" { - fatalln("No home directory?") + default: + return filepath.Join(getHomeDir(), p[2:]) } - return home } func getHomeDir() string { - if runtime.GOOS == "windows" { - home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") - if home == "" { - home = os.Getenv("USERPROFILE") - } - return home - } - return getUnixHomeDir() -} + var home string -func getDefaultConfDir() string { - if runtime.GOOS == "windows" { - return filepath.Join(os.Getenv("AppData"), "syncthing") + switch runtime.GOOS { + case "windows": + home = os.Getenv("HomeDrive") + os.Getenv("HomePath") + if home == "" { + home = os.Getenv("UserProfile") + } + default: + home = os.Getenv("HOME") } - return expandTilde("~/.syncthing") + + if home == "" { + fatalln("No home directory found - set $HOME (or the platform equivalent).") + } + + return home }