mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-18 19:15:19 +00:00
63de838f27
* lib/locations: Fix enum values camelCase. * lib/locations: Remove unused FailuresFile. * cmd/syncthing: Turn around role of locations storage. Previously the locations package was used to provide default paths, possibly with an overridden home directory. Extra paths supplied on the command line were handled and passed around in the options object. To make the changed paths available to any other interested package, override the location setting from the option if supplied, instead of vice versa when not supplied. Adapt code using this to read from the locations package instead of passing through the options object. * lib/locations: Refactor showPaths to locations package. Generate a reusable string in locations.PrettyPrintPaths(). Enumerating all possible locations in different packages is error prone, so add a new public function to generate the listing as a string in the locations package. Adapt cmd/syncthing --paths to use that instead of its own console output. * lib/locations: Include CSRF token in pretty printed paths. * lib/api: New endpoint /rest/system/paths. The paths should be available for troubleshooting from a running instance. Using the --paths CLI option is not easy in some environments, so expose the locations mapping to a JSON endpoint. Add utility function ListExpandedPaths() that also filters out any entries which still contain variable placeholders. * gui: List runtime paths in separate log viewer tab. * Wrap paths. * lib/syncthing: Utilize locations.Get() instead of passing an arg. * Include base directories, move label to table caption. * gui: Switch to hard-coded paths instead of iterating over all. * gui: Break aboutModalView into tabs. Use tabs to separate authors from included third-party software. * gui: Move paths from log viewer to about modal. * lib/locations: Adjust pretty print output order to match GUI. * gui, authors: Remove additional bot names and fix indent. The indentation changed because of the tabbed about dialog, fix the authors script to respect that. Skip Syncthing*Automation in authors list as well. * Update AUTHORS list to remove bot names. * Revert AUTHORS email order change. * Do not emphasize DB and log file locations. * Review line wrapping. * review part 1: strings.Builder, naming * Rename and extend locations.Set() with error handling. Remodel the Override() function along the existing SetBaseDir() and rename it to simply Set(). Make sure to use absolute paths when given log file or GUI assets override options. Add proper error reporting if that goes wrong. * Remove obsolete comment about empty logfile option. * Don't filter out unexpanded baseDir placeholders, only ${timestamp}. * Restore behavior regarding special "-" logfile argument. If the option is given, but with empty value, assume the no log file (same as "-"). Don't try to convert the special value to an absolute path though and document this fact in a comment for the Set() function. * Use template to check for location key validity. * Don't filter out timestamp placeholders. * lib/api: Remove paths from /rest/system/status. * lib/ur: Properly initialize map in failure data (fixes #8479) Co-authored-by: Jakob Borg <jakob@kastelo.net>
243 lines
7.2 KiB
Go
243 lines
7.2 KiB
Go
// Copyright (C) 2019 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package locations
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
)
|
|
|
|
type LocationEnum string
|
|
|
|
// Use strings as keys to make printout and serialization of the locations map
|
|
// more meaningful.
|
|
const (
|
|
ConfigFile LocationEnum = "config"
|
|
CertFile LocationEnum = "certFile"
|
|
KeyFile LocationEnum = "keyFile"
|
|
HTTPSCertFile LocationEnum = "httpsCertFile"
|
|
HTTPSKeyFile LocationEnum = "httpsKeyFile"
|
|
Database LocationEnum = "database"
|
|
LogFile LocationEnum = "logFile"
|
|
CsrfTokens LocationEnum = "csrfTokens"
|
|
PanicLog LocationEnum = "panicLog"
|
|
AuditLog LocationEnum = "auditLog"
|
|
GUIAssets LocationEnum = "guiAssets"
|
|
DefFolder LocationEnum = "defFolder"
|
|
)
|
|
|
|
type BaseDirEnum string
|
|
|
|
const (
|
|
// Overridden by --home flag
|
|
ConfigBaseDir BaseDirEnum = "config"
|
|
DataBaseDir BaseDirEnum = "data"
|
|
// User's home directory, *not* --home flag
|
|
UserHomeBaseDir BaseDirEnum = "userHome"
|
|
|
|
LevelDBDir = "index-v0.14.0.db"
|
|
)
|
|
|
|
// Platform dependent directories
|
|
var baseDirs = make(map[BaseDirEnum]string, 3)
|
|
|
|
func init() {
|
|
userHome := userHomeDir()
|
|
config := defaultConfigDir(userHome)
|
|
baseDirs[UserHomeBaseDir] = userHome
|
|
baseDirs[ConfigBaseDir] = config
|
|
baseDirs[DataBaseDir] = defaultDataDir(userHome, config)
|
|
|
|
err := expandLocations()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
panic("Failed to expand locations at init time")
|
|
}
|
|
}
|
|
|
|
// Set overrides a location to the given path, making sure to it points to an
|
|
// absolute path first. Only the special "-" value will be used verbatim.
|
|
func Set(locationName LocationEnum, path string) error {
|
|
if !filepath.IsAbs(path) && path != "-" {
|
|
var err error
|
|
path, err = filepath.Abs(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
_, ok := locationTemplates[locationName]
|
|
if !ok {
|
|
return fmt.Errorf("unknown location: %s", locationName)
|
|
}
|
|
locations[locationName] = filepath.Clean(path)
|
|
return nil
|
|
}
|
|
|
|
func SetBaseDir(baseDirName BaseDirEnum, path string) error {
|
|
if !filepath.IsAbs(path) {
|
|
var err error
|
|
path, err = filepath.Abs(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
_, ok := baseDirs[baseDirName]
|
|
if !ok {
|
|
return fmt.Errorf("unknown base dir: %s", baseDirName)
|
|
}
|
|
baseDirs[baseDirName] = filepath.Clean(path)
|
|
return expandLocations()
|
|
}
|
|
|
|
func Get(location LocationEnum) string {
|
|
return locations[location]
|
|
}
|
|
|
|
func GetBaseDir(baseDir BaseDirEnum) string {
|
|
return baseDirs[baseDir]
|
|
}
|
|
|
|
// Use the variables from baseDirs here
|
|
var locationTemplates = map[LocationEnum]string{
|
|
ConfigFile: "${config}/config.xml",
|
|
CertFile: "${config}/cert.pem",
|
|
KeyFile: "${config}/key.pem",
|
|
HTTPSCertFile: "${config}/https-cert.pem",
|
|
HTTPSKeyFile: "${config}/https-key.pem",
|
|
Database: "${data}/" + LevelDBDir,
|
|
LogFile: "${data}/syncthing.log", // --logfile on Windows
|
|
CsrfTokens: "${data}/csrftokens.txt",
|
|
PanicLog: "${data}/panic-${timestamp}.log",
|
|
AuditLog: "${data}/audit-${timestamp}.log",
|
|
GUIAssets: "${config}/gui",
|
|
DefFolder: "${userHome}/Sync",
|
|
}
|
|
|
|
var locations = make(map[LocationEnum]string)
|
|
|
|
// expandLocations replaces the variables in the locations map with actual
|
|
// directory locations.
|
|
func expandLocations() error {
|
|
newLocations := make(map[LocationEnum]string)
|
|
for key, dir := range locationTemplates {
|
|
for varName, value := range baseDirs {
|
|
dir = strings.ReplaceAll(dir, "${"+string(varName)+"}", value)
|
|
}
|
|
var err error
|
|
dir, err = fs.ExpandTilde(dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newLocations[key] = filepath.Clean(dir)
|
|
}
|
|
locations = newLocations
|
|
return nil
|
|
}
|
|
|
|
// ListExpandedPaths returns a machine-readable mapping of the currently configured locations.
|
|
func ListExpandedPaths() map[string]string {
|
|
res := make(map[string]string, len(locations))
|
|
for key, path := range baseDirs {
|
|
res["baseDir-"+string(key)] = path
|
|
}
|
|
for key, path := range locations {
|
|
res[string(key)] = path
|
|
}
|
|
return res
|
|
}
|
|
|
|
// PrettyPaths returns a nicely formatted, human-readable listing
|
|
func PrettyPaths() string {
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, "Configuration file:\n\t%s\n\n", Get(ConfigFile))
|
|
fmt.Fprintf(&b, "Device private key & certificate files:\n\t%s\n\t%s\n\n", Get(KeyFile), Get(CertFile))
|
|
fmt.Fprintf(&b, "GUI / API HTTPS private key & certificate files:\n\t%s\n\t%s\n\n", Get(HTTPSKeyFile), Get(HTTPSCertFile))
|
|
fmt.Fprintf(&b, "Database location:\n\t%s\n\n", Get(Database))
|
|
fmt.Fprintf(&b, "Log file:\n\t%s\n\n", Get(LogFile))
|
|
fmt.Fprintf(&b, "GUI override directory:\n\t%s\n\n", Get(GUIAssets))
|
|
fmt.Fprintf(&b, "CSRF tokens file:\n\t%s\n\n", Get(CsrfTokens))
|
|
fmt.Fprintf(&b, "Default sync folder directory:\n\t%s\n\n", Get(DefFolder))
|
|
return b.String()
|
|
}
|
|
|
|
// defaultConfigDir returns the default configuration directory, as figured
|
|
// out by various the environment variables present on each platform, or dies
|
|
// trying.
|
|
func defaultConfigDir(userHome string) string {
|
|
switch runtime.GOOS {
|
|
case build.Windows:
|
|
if p := os.Getenv("LocalAppData"); p != "" {
|
|
return filepath.Join(p, "Syncthing")
|
|
}
|
|
return filepath.Join(os.Getenv("AppData"), "Syncthing")
|
|
|
|
case build.Darwin:
|
|
return filepath.Join(userHome, "Library/Application Support/Syncthing")
|
|
|
|
default:
|
|
if xdgCfg := os.Getenv("XDG_CONFIG_HOME"); xdgCfg != "" {
|
|
return filepath.Join(xdgCfg, "syncthing")
|
|
}
|
|
return filepath.Join(userHome, ".config/syncthing")
|
|
}
|
|
}
|
|
|
|
// defaultDataDir returns the default data directory, which usually is the
|
|
// config directory but might be something else.
|
|
func defaultDataDir(userHome, config string) string {
|
|
if build.IsWindows || build.IsDarwin {
|
|
return config
|
|
}
|
|
|
|
// If a database exists at the "normal" location, use that anyway.
|
|
if _, err := os.Lstat(filepath.Join(config, LevelDBDir)); err == nil {
|
|
return config
|
|
}
|
|
// Always use this env var, as it's explicitly set by the user
|
|
if xdgHome := os.Getenv("XDG_DATA_HOME"); xdgHome != "" {
|
|
return filepath.Join(xdgHome, "syncthing")
|
|
}
|
|
// Only use the XDG default, if a syncthing specific dir already
|
|
// exists. Existence of ~/.local/share is not deemed enough, as
|
|
// it may also exist erroneously on non-XDG systems.
|
|
xdgDefault := filepath.Join(userHome, ".local/share/syncthing")
|
|
if _, err := os.Lstat(xdgDefault); err == nil {
|
|
return xdgDefault
|
|
}
|
|
// FYI: XDG_DATA_DIRS is not relevant, as it is for system-wide
|
|
// data dirs, not user specific ones.
|
|
return config
|
|
}
|
|
|
|
// userHomeDir returns the user's home directory, or dies trying.
|
|
func userHomeDir() string {
|
|
userHome, err := fs.ExpandTilde("~")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
panic("Failed to get user home dir")
|
|
}
|
|
return userHome
|
|
}
|
|
|
|
func GetTimestamped(key LocationEnum) string {
|
|
// We take the roundtrip via "${timestamp}" instead of passing the path
|
|
// directly through time.Format() to avoid issues when the path we are
|
|
// expanding contains numbers; otherwise for example
|
|
// /home/user2006/.../panic-20060102-150405.log would get both instances of
|
|
// 2006 replaced by 2015...
|
|
tpl := locations[key]
|
|
now := time.Now().Format("20060102-150405")
|
|
return strings.ReplaceAll(tpl, "${timestamp}", now)
|
|
}
|