From 23593c3d2009ca87072c73f0d5050f9b2de638d4 Mon Sep 17 00:00:00 2001 From: filoozom Date: Sun, 2 Mar 2014 16:07:12 +0100 Subject: [PATCH 1/4] Update getHomeDir() to use "os/user" os.Getenv("HOME") doesn't work properly on Windows (and maybe other systems?) and the package "os/user" gives us a convenient way to find the home directory for every OS. --- main.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 069c6dd65..f9c7416e5 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( _ "net/http/pprof" "os" "os/exec" + "os/user" "path" "runtime" "runtime/debug" @@ -561,9 +562,13 @@ func expandTilde(p string) string { } func getHomeDir() string { - home := os.Getenv("HOME") - if home == "" { + usr, err := user.Current() + if err != nil { + fatalln(err) + } + + if usr.HomeDir == "" { fatalln("No home directory?") } - return home + return usr.HomeDir } From 043dea760fd02bb712ce15e8dd4023d83d782379 Mon Sep 17 00:00:00 2001 From: filoozom Date: Sun, 2 Mar 2014 23:49:51 +0100 Subject: [PATCH 2/4] Set the right config and home dir for each OS Use %AppData%\syncthing for the config files and %USERPROFILE%\Sync as sync folder for Windows. --- main.go | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index f9c7416e5..172331470 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( _ "net/http/pprof" "os" "os/exec" - "os/user" "path" "runtime" "runtime/debug" @@ -41,7 +40,7 @@ var ( ) func main() { - flag.StringVar(&confDir, "home", "~/.syncthing", "Set configuration directory") + flag.StringVar(&confDir, "home", getDefaultConfDir(), "Set configuration directory") flag.StringVar(&trace, "debug.trace", "", "(connect,net,idx,file,pull)") flag.StringVar(&profiler, "debug.profiler", "", "(addr)") flag.BoolVar(&showVersion, "version", false, "Show version") @@ -139,7 +138,7 @@ func main() { cfg, err = readConfigXML(nil) cfg.Repositories = []RepositoryConfiguration{ { - Directory: "~/Sync", + Directory: path.Join(getHomeDir(), "Sync"), Nodes: []NodeConfiguration{ {NodeID: myID, Addresses: []string{"dynamic"}}, }, @@ -555,20 +554,38 @@ func ensureDir(dir string, mode int) { } func expandTilde(p string) string { + if runtime.GOOS == "windows" { + return p + } + if strings.HasPrefix(p, "~/") { - return strings.Replace(p, "~", getHomeDir(), 1) + return strings.Replace(p, "~", getUnixHomeDir(), 1) } return p } -func getHomeDir() string { - usr, err := user.Current() - if err != nil { - fatalln(err) - } - - if usr.HomeDir == "" { +func getUnixHomeDir() string { + home := os.Getenv("HOME") + if home == "" { fatalln("No home directory?") } - return usr.HomeDir + 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() +} + +func getDefaultConfDir() string { + if runtime.GOOS == "windows" { + return path.Join(os.Getenv("AppData"), "syncthing") + } + return expandTilde("~/.syncthing") } From 8f41d90ab1e16a09e3f222a73557cbcc256e3b0a Mon Sep 17 00:00:00 2001 From: filoozom Date: Mon, 3 Mar 2014 08:46:20 +0100 Subject: [PATCH 3/4] Delete cfgFile before renaming it on Windows --- main.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 172331470..4e4400e2a 100644 --- a/main.go +++ b/main.go @@ -328,6 +328,13 @@ func saveConfigLoop(cfgFile string) { continue } + if runtime.GOOS == "windows" { + err := os.Remove(cfgFile) + if err != nil && !os.IsNotExist(err) { + warnln(err) + } + } + err = os.Rename(cfgFile+".tmp", cfgFile) if err != nil { warnln(err) @@ -557,7 +564,7 @@ func expandTilde(p string) string { if runtime.GOOS == "windows" { return p } - + if strings.HasPrefix(p, "~/") { return strings.Replace(p, "~", getUnixHomeDir(), 1) } From b4a1aadd1b5509f292982446617d6d6709ddfd9f Mon Sep 17 00:00:00 2001 From: filoozom Date: Mon, 3 Mar 2014 08:47:52 +0100 Subject: [PATCH 4/4] Fix divided by zero when the sync folder is empty (tot = 0) --- model.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/model.go b/model.go index 2617fb42a..75fd8c78e 100644 --- a/model.go +++ b/model.go @@ -191,7 +191,10 @@ func (m *Model) ConnectionStats() map[string]ConnectionInfo { } } - ci.Completion = int(100 * have / tot) + ci.Completion = 100 + if tot != 0 { + ci.Completion = int(100 * have / tot) + } res[node] = ci }