util.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "log"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "github.com/syncthing/syncthing/lib/osutil"
  13. )
  14. func nulString(bs []byte) string {
  15. for i := range bs {
  16. if bs[i] == 0 {
  17. return string(bs[:i])
  18. }
  19. }
  20. return string(bs)
  21. }
  22. func defaultConfigDir() string {
  23. switch runtime.GOOS {
  24. case "windows":
  25. if p := os.Getenv("LocalAppData"); p != "" {
  26. return filepath.Join(p, "Syncthing")
  27. }
  28. return filepath.Join(os.Getenv("AppData"), "Syncthing")
  29. case "darwin":
  30. dir, err := osutil.ExpandTilde("~/Library/Application Support/Syncthing")
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. return dir
  35. default:
  36. if xdgCfg := os.Getenv("XDG_CONFIG_HOME"); xdgCfg != "" {
  37. return filepath.Join(xdgCfg, "syncthing")
  38. }
  39. dir, err := osutil.ExpandTilde("~/.config/syncthing")
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. return dir
  44. }
  45. }