2014-11-16 21:13:20 +01:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 21:43:32 +02:00
//
2015-03-07 21:36:35 +01:00
// 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 http://mozilla.org/MPL/2.0/.
2014-06-01 22:50:14 +02:00
2014-05-15 05:40:17 +02:00
// Package config implements reading and writing of the syncthing configuration file.
2014-05-15 02:18:09 +02:00
package config
2014-03-02 23:58:14 +01:00
import (
"encoding/xml"
2015-06-23 14:55:30 +02:00
"fmt"
2014-10-06 09:25:45 +02:00
"io"
2014-12-29 13:48:26 +01:00
"math/rand"
2014-04-22 11:46:08 +02:00
"os"
2014-10-11 17:55:11 +02:00
"path/filepath"
2014-03-02 23:58:14 +01:00
"reflect"
2015-04-05 22:52:22 +02:00
"runtime"
2014-03-02 23:58:14 +01:00
"sort"
"strconv"
2015-03-07 20:48:53 +01:00
"strings"
2014-04-19 13:33:51 +02:00
2015-01-13 13:22:56 +01:00
"github.com/syncthing/protocol"
2015-08-06 11:29:25 +02:00
"github.com/syncthing/syncthing/lib/osutil"
2014-11-30 00:17:00 +01:00
"golang.org/x/crypto/bcrypt"
2014-03-02 23:58:14 +01:00
)
2015-04-04 21:49:15 +02:00
const (
OldestHandledVersion = 5
2015-06-23 14:55:30 +02:00
CurrentVersion = 12
2015-06-20 20:21:05 +02:00
MaxRescanIntervalS = 365 * 24 * 60 * 60
2015-04-04 21:49:15 +02:00
)
2014-10-08 13:52:05 +02:00
2014-03-02 23:58:14 +01:00
type Configuration struct {
2015-03-10 23:45:43 +01:00
Version int ` xml:"version,attr" json:"version" `
Folders [ ] FolderConfiguration ` xml:"folder" json:"folders" `
Devices [ ] DeviceConfiguration ` xml:"device" json:"devices" `
GUI GUIConfiguration ` xml:"gui" json:"gui" `
Options OptionsConfiguration ` xml:"options" json:"options" `
IgnoredDevices [ ] protocol . DeviceID ` xml:"ignoredDevice" json:"ignoredDevices" `
2014-12-28 00:12:12 +01:00
XMLName xml . Name ` xml:"configuration" json:"-" `
2014-09-28 14:22:39 +02:00
2015-04-04 21:49:15 +02:00
OriginalVersion int ` xml:"-" json:"-" ` // The version we read from disk, before any conversion
2014-03-02 23:58:14 +01:00
}
2015-04-28 22:32:10 +02:00
func ( cfg Configuration ) Copy ( ) Configuration {
newCfg := cfg
2015-04-05 17:36:52 +02:00
// Deep copy FolderConfigurations
2015-04-28 22:32:10 +02:00
newCfg . Folders = make ( [ ] FolderConfiguration , len ( cfg . Folders ) )
for i := range newCfg . Folders {
newCfg . Folders [ i ] = cfg . Folders [ i ] . Copy ( )
2015-04-05 17:36:52 +02:00
}
// Deep copy DeviceConfigurations
2015-04-28 22:32:10 +02:00
newCfg . Devices = make ( [ ] DeviceConfiguration , len ( cfg . Devices ) )
for i := range newCfg . Devices {
newCfg . Devices [ i ] = cfg . Devices [ i ] . Copy ( )
2015-04-05 17:36:52 +02:00
}
2015-04-28 22:32:10 +02:00
newCfg . Options = cfg . Options . Copy ( )
2015-04-05 17:36:52 +02:00
// DeviceIDs are values
2015-04-28 22:32:10 +02:00
newCfg . IgnoredDevices = make ( [ ] protocol . DeviceID , len ( cfg . IgnoredDevices ) )
copy ( newCfg . IgnoredDevices , cfg . IgnoredDevices )
2015-04-05 17:36:52 +02:00
2015-04-28 22:32:10 +02:00
return newCfg
2015-04-05 17:36:52 +02:00
}
2014-09-28 13:00:38 +02:00
type FolderConfiguration struct {
2015-08-27 00:49:06 +02:00
ID string ` xml:"id,attr" json:"id" `
RawPath string ` xml:"path,attr" json:"path" `
Devices [ ] FolderDeviceConfiguration ` xml:"device" json:"devices" `
ReadOnly bool ` xml:"ro,attr" json:"readOnly" `
RescanIntervalS int ` xml:"rescanIntervalS,attr" json:"rescanIntervalS" `
IgnorePerms bool ` xml:"ignorePerms,attr" json:"ignorePerms" `
AutoNormalize bool ` xml:"autoNormalize,attr" json:"autoNormalize" `
MinDiskFreePct int ` xml:"minDiskFreePct" json:"minDiskFreePct" `
Versioning VersioningConfiguration ` xml:"versioning" json:"versioning" `
Copiers int ` xml:"copiers" json:"copiers" ` // This defines how many files are handled concurrently.
Pullers int ` xml:"pullers" json:"pullers" ` // Defines how many blocks are fetched at the same time, possibly between separate copier routines.
Hashers int ` xml:"hashers" json:"hashers" ` // Less than one sets the value to the number of cores. These are CPU bound due to hashing.
Order PullOrder ` xml:"order" json:"order" `
IgnoreDelete bool ` xml:"ignoreDelete" json:"ignoreDelete" `
ScanProgressIntervalS int ` xml:"scanProgressInterval" json:"scanProgressInterval" ` // Set to a negative value to disable. Value of 0 will get replaced with value of 2 (default value)
2015-03-10 23:45:43 +01:00
Invalid string ` xml:"-" json:"invalid" ` // Set at runtime when there is an error, not saved
2014-05-25 20:49:08 +02:00
}
2015-04-28 22:32:10 +02:00
func ( f FolderConfiguration ) Copy ( ) FolderConfiguration {
c := f
c . Devices = make ( [ ] FolderDeviceConfiguration , len ( f . Devices ) )
copy ( c . Devices , f . Devices )
2015-04-05 17:36:52 +02:00
return c
}
2015-04-05 22:52:22 +02:00
func ( f FolderConfiguration ) Path ( ) string {
// This is intentionally not a pointer method, because things like
// cfg.Folders["default"].Path() should be valid.
// Attempt tilde expansion; leave unchanged in case of error
if path , err := osutil . ExpandTilde ( f . RawPath ) ; err == nil {
f . RawPath = path
}
// Attempt absolutification; leave unchanged in case of error
if ! filepath . IsAbs ( f . RawPath ) {
// Abs() looks like a fairly expensive syscall on Windows, while
// IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
// somewhat faster in the general case, hence the outer if...
if path , err := filepath . Abs ( f . RawPath ) ; err == nil {
f . RawPath = path
}
}
// Attempt to enable long filename support on Windows. We may still not
// have an absolute path here if the previous steps failed.
if runtime . GOOS == "windows" && filepath . IsAbs ( f . RawPath ) && ! strings . HasPrefix ( f . RawPath , ` \\ ` ) {
return ` \\?\ ` + f . RawPath
}
return f . RawPath
}
2014-10-11 17:55:11 +02:00
func ( f * FolderConfiguration ) CreateMarker ( ) error {
if ! f . HasMarker ( ) {
2015-04-05 22:52:22 +02:00
marker := filepath . Join ( f . Path ( ) , ".stfolder" )
2014-10-11 17:55:11 +02:00
fd , err := os . Create ( marker )
if err != nil {
return err
}
fd . Close ( )
osutil . HideFile ( marker )
}
return nil
}
func ( f * FolderConfiguration ) HasMarker ( ) bool {
2015-04-05 22:52:22 +02:00
_ , err := os . Stat ( filepath . Join ( f . Path ( ) , ".stfolder" ) )
2014-10-11 17:55:11 +02:00
if err != nil {
return false
}
return true
}
2014-12-08 16:36:15 +01:00
func ( f * FolderConfiguration ) DeviceIDs ( ) [ ] protocol . DeviceID {
2015-07-22 09:02:55 +02:00
deviceIDs := make ( [ ] protocol . DeviceID , len ( f . Devices ) )
for i , n := range f . Devices {
deviceIDs [ i ] = n . DeviceID
2014-10-06 09:25:45 +02:00
}
2015-07-22 09:02:55 +02:00
return deviceIDs
2014-10-06 09:25:45 +02:00
}
2014-05-25 20:49:08 +02:00
type VersioningConfiguration struct {
2015-03-10 23:45:43 +01:00
Type string ` xml:"type,attr" json:"type" `
Params map [ string ] string ` json:"params" `
2014-05-25 20:49:08 +02:00
}
type InternalVersioningConfiguration struct {
Type string ` xml:"type,attr,omitempty" `
Params [ ] InternalParam ` xml:"param" `
}
type InternalParam struct {
Key string ` xml:"key,attr" `
Val string ` xml:"val,attr" `
}
func ( c * VersioningConfiguration ) MarshalXML ( e * xml . Encoder , start xml . StartElement ) error {
var tmp InternalVersioningConfiguration
tmp . Type = c . Type
for k , v := range c . Params {
tmp . Params = append ( tmp . Params , InternalParam { k , v } )
}
return e . EncodeElement ( tmp , start )
}
func ( c * VersioningConfiguration ) UnmarshalXML ( d * xml . Decoder , start xml . StartElement ) error {
var tmp InternalVersioningConfiguration
err := d . DecodeElement ( & tmp , & start )
if err != nil {
return err
}
c . Type = tmp . Type
c . Params = make ( map [ string ] string , len ( tmp . Params ) )
for _ , p := range tmp . Params {
c . Params [ p . Key ] = p . Val
}
return nil
2014-04-08 13:45:18 +02:00
}
2014-09-28 13:00:38 +02:00
type DeviceConfiguration struct {
2015-03-10 23:45:43 +01:00
DeviceID protocol . DeviceID ` xml:"id,attr" json:"deviceID" `
Name string ` xml:"name,attr,omitempty" json:"name" `
Addresses [ ] string ` xml:"address,omitempty" json:"addresses" `
Compression protocol . Compression ` xml:"compression,attr" json:"compression" `
CertName string ` xml:"certName,attr,omitempty" json:"certName" `
Introducer bool ` xml:"introducer,attr" json:"introducer" `
2014-03-02 23:58:14 +01:00
}
2015-04-05 17:36:52 +02:00
func ( orig DeviceConfiguration ) Copy ( ) DeviceConfiguration {
c := orig
c . Addresses = make ( [ ] string , len ( orig . Addresses ) )
copy ( c . Addresses , orig . Addresses )
return c
}
2014-09-28 13:00:38 +02:00
type FolderDeviceConfiguration struct {
2015-03-10 23:45:43 +01:00
DeviceID protocol . DeviceID ` xml:"id,attr" json:"deviceID" `
2014-08-17 00:20:21 +02:00
}
2014-03-02 23:58:14 +01:00
type OptionsConfiguration struct {
2015-06-23 14:55:30 +02:00
ListenAddress [ ] string ` xml:"listenAddress" json:"listenAddress" default:"tcp://0.0.0.0:22000" `
GlobalAnnServers [ ] string ` xml:"globalAnnounceServer" json:"globalAnnounceServers" json:"globalAnnounceServer" default:"udp4://announce.syncthing.net:22027, udp6://announce-v6.syncthing.net:22027" `
2015-03-10 23:45:43 +01:00
GlobalAnnEnabled bool ` xml:"globalAnnounceEnabled" json:"globalAnnounceEnabled" default:"true" `
LocalAnnEnabled bool ` xml:"localAnnounceEnabled" json:"localAnnounceEnabled" default:"true" `
2015-08-27 16:04:13 +02:00
LocalAnnPort int ` xml:"localAnnouncePort" json:"localAnnouncePort" default:"21027" `
2015-08-23 14:59:38 +02:00
LocalAnnMCAddr string ` xml:"localAnnounceMCAddr" json:"localAnnounceMCAddr" default:"[ff12::8384]:21027" `
2015-07-23 21:39:44 +02:00
RelayServers [ ] string ` xml:"relayServer" json:"relayServers" default:"dynamic+https://relays.syncthing.net" `
2015-03-10 23:45:43 +01:00
MaxSendKbps int ` xml:"maxSendKbps" json:"maxSendKbps" `
MaxRecvKbps int ` xml:"maxRecvKbps" json:"maxRecvKbps" `
ReconnectIntervalS int ` xml:"reconnectionIntervalS" json:"reconnectionIntervalS" default:"60" `
2015-07-24 21:07:26 +02:00
RelaysEnabled bool ` xml:"relaysEnabled" json:"relaysEnabled" default:"true" `
2015-06-28 22:09:03 +02:00
RelayReconnectIntervalM int ` xml:"relayReconnectIntervalM" json:"relayReconnectIntervalM" default:"10" `
2015-07-24 20:55:52 +02:00
RelayWithoutGlobalAnn bool ` xml:"relayWithoutGlobalAnn" json:"relayWithoutGlobalAnn" default:"false" `
2015-03-10 23:45:43 +01:00
StartBrowser bool ` xml:"startBrowser" json:"startBrowser" default:"true" `
UPnPEnabled bool ` xml:"upnpEnabled" json:"upnpEnabled" default:"true" `
2015-05-09 22:17:53 +02:00
UPnPLeaseM int ` xml:"upnpLeaseMinutes" json:"upnpLeaseMinutes" default:"60" `
2015-04-16 01:36:27 +02:00
UPnPRenewalM int ` xml:"upnpRenewalMinutes" json:"upnpRenewalMinutes" default:"30" `
2015-04-25 13:52:07 +02:00
UPnPTimeoutS int ` xml:"upnpTimeoutSeconds" json:"upnpTimeoutSeconds" default:"10" `
2015-03-10 23:45:43 +01:00
URAccepted int ` xml:"urAccepted" json:"urAccepted" ` // Accepted usage reporting version; 0 for off (undecided), -1 for off (permanently)
URUniqueID string ` xml:"urUniqueID" json:"urUniqueId" ` // Unique ID for reporting purposes, regenerated when UR is turned on.
RestartOnWakeup bool ` xml:"restartOnWakeup" json:"restartOnWakeup" default:"true" `
AutoUpgradeIntervalH int ` xml:"autoUpgradeIntervalH" json:"autoUpgradeIntervalH" default:"12" ` // 0 for off
KeepTemporariesH int ` xml:"keepTemporariesH" json:"keepTemporariesH" default:"24" ` // 0 for off
CacheIgnoredFiles bool ` xml:"cacheIgnoredFiles" json:"cacheIgnoredFiles" default:"true" `
ProgressUpdateIntervalS int ` xml:"progressUpdateIntervalS" json:"progressUpdateIntervalS" default:"5" `
SymlinksEnabled bool ` xml:"symlinksEnabled" json:"symlinksEnabled" default:"true" `
LimitBandwidthInLan bool ` xml:"limitBandwidthInLan" json:"limitBandwidthInLan" default:"false" `
2015-05-11 09:01:09 +02:00
DatabaseBlockCacheMiB int ` xml:"databaseBlockCacheMiB" json:"databaseBlockCacheMiB" default:"0" `
2015-06-27 13:34:41 +02:00
PingTimeoutS int ` xml:"pingTimeoutS" json:"pingTimeoutS" default:"30" `
PingIdleTimeS int ` xml:"pingIdleTimeS" json:"pingIdleTimeS" default:"60" `
2015-08-09 10:35:48 +02:00
MinHomeDiskFreePct int ` xml:"minHomeDiskFreePct" json:"minHomeDiskFreePct" default:"1" `
2014-04-08 15:56:12 +02:00
}
2015-04-05 17:36:52 +02:00
func ( orig OptionsConfiguration ) Copy ( ) OptionsConfiguration {
c := orig
c . ListenAddress = make ( [ ] string , len ( orig . ListenAddress ) )
copy ( c . ListenAddress , orig . ListenAddress )
c . GlobalAnnServers = make ( [ ] string , len ( orig . GlobalAnnServers ) )
copy ( c . GlobalAnnServers , orig . GlobalAnnServers )
return c
}
2014-04-08 15:56:12 +02:00
type GUIConfiguration struct {
2015-03-10 23:45:43 +01:00
Enabled bool ` xml:"enabled,attr" json:"enabled" default:"true" `
2015-03-26 21:36:06 +01:00
Address string ` xml:"address" json:"address" default:"127.0.0.1:8384" `
2015-03-10 23:45:43 +01:00
User string ` xml:"user,omitempty" json:"user" `
Password string ` xml:"password,omitempty" json:"password" `
UseTLS bool ` xml:"tls,attr" json:"useTLS" `
APIKey string ` xml:"apikey,omitempty" json:"apiKey" `
2014-03-02 23:58:14 +01:00
}
2014-10-06 09:25:45 +02:00
func New ( myID protocol . DeviceID ) Configuration {
var cfg Configuration
2014-10-08 13:52:05 +02:00
cfg . Version = CurrentVersion
cfg . OriginalVersion = CurrentVersion
2014-10-06 09:25:45 +02:00
setDefaults ( & cfg )
setDefaults ( & cfg . Options )
setDefaults ( & cfg . GUI )
cfg . prepare ( myID )
return cfg
}
func ReadXML ( r io . Reader , myID protocol . DeviceID ) ( Configuration , error ) {
var cfg Configuration
setDefaults ( & cfg )
setDefaults ( & cfg . Options )
setDefaults ( & cfg . GUI )
err := xml . NewDecoder ( r ) . Decode ( & cfg )
2014-10-08 13:52:05 +02:00
cfg . OriginalVersion = cfg . Version
2014-10-06 09:25:45 +02:00
cfg . prepare ( myID )
return cfg , err
}
func ( cfg * Configuration ) WriteXML ( w io . Writer ) error {
e := xml . NewEncoder ( w )
e . Indent ( "" , " " )
err := e . Encode ( cfg )
if err != nil {
return err
}
_ , err = w . Write ( [ ] byte ( "\n" ) )
return err
}
2014-09-28 13:00:38 +02:00
func ( cfg * Configuration ) prepare ( myID protocol . DeviceID ) {
2014-03-04 22:29:43 +01:00
fillNilSlices ( & cfg . Options )
2014-03-04 11:25:10 +01:00
2014-12-28 00:12:12 +01:00
// Initialize an empty slices
2014-09-28 13:00:38 +02:00
if cfg . Folders == nil {
cfg . Folders = [ ] FolderConfiguration { }
2014-05-13 04:57:38 +02:00
}
2014-12-28 00:12:12 +01:00
if cfg . IgnoredDevices == nil {
cfg . IgnoredDevices = [ ] protocol . DeviceID { }
}
2014-05-13 04:57:38 +02:00
2014-09-28 13:00:38 +02:00
// Check for missing, bad or duplicate folder ID:s
var seenFolders = map [ string ] * FolderConfiguration { }
for i := range cfg . Folders {
folder := & cfg . Folders [ i ]
2014-04-27 21:53:27 +02:00
2015-04-05 22:52:22 +02:00
if len ( folder . RawPath ) == 0 {
2014-09-28 13:00:38 +02:00
folder . Invalid = "no directory configured"
2014-05-13 02:30:04 +02:00
continue
}
2015-02-10 00:56:16 +01:00
// The reason it's done like this:
// C: -> C:\ -> C:\ (issue that this is trying to fix)
// C:\somedir -> C:\somedir\ -> C:\somedir
// C:\somedir\ -> C:\somedir\\ -> C:\somedir
// This way in the tests, we get away without OS specific separators
// in the test configs.
2015-04-05 22:52:22 +02:00
folder . RawPath = filepath . Dir ( folder . RawPath + string ( filepath . Separator ) )
2015-01-23 00:22:30 +01:00
2014-09-28 13:00:38 +02:00
if folder . ID == "" {
folder . ID = "default"
2014-03-29 18:53:48 +01:00
}
2015-06-20 20:21:05 +02:00
if folder . RescanIntervalS > MaxRescanIntervalS {
folder . RescanIntervalS = MaxRescanIntervalS
} else if folder . RescanIntervalS < 0 {
folder . RescanIntervalS = 0
}
2014-09-28 13:00:38 +02:00
if seen , ok := seenFolders [ folder . ID ] ; ok {
l . Warnf ( "Multiple folders with ID %q; disabling" , folder . ID )
seen . Invalid = "duplicate folder ID"
folder . Invalid = "duplicate folder ID"
2014-04-27 21:53:27 +02:00
} else {
2014-09-28 13:00:38 +02:00
seenFolders [ folder . ID ] = folder
2014-03-29 18:53:48 +01:00
}
}
2015-06-23 14:55:30 +02:00
cfg . Options . ListenAddress = uniqueStrings ( cfg . Options . ListenAddress )
cfg . Options . GlobalAnnServers = uniqueStrings ( cfg . Options . GlobalAnnServers )
2015-04-04 21:49:15 +02:00
if cfg . Version < OldestHandledVersion {
l . Warnf ( "Configuration version %d is deprecated. Attempting best effort conversion, but please verify manually." , cfg . Version )
2014-06-17 23:22:19 +02:00
}
2015-03-07 20:48:53 +01:00
// Upgrade configuration versions as appropriate
2015-04-04 21:49:15 +02:00
if cfg . Version <= 5 {
2014-10-11 17:55:11 +02:00
convertV5V6 ( cfg )
}
2014-11-20 22:24:11 +01:00
if cfg . Version == 6 {
convertV6V7 ( cfg )
}
2015-03-07 20:48:53 +01:00
if cfg . Version == 7 {
convertV7V8 ( cfg )
}
2015-02-23 09:44:10 +01:00
if cfg . Version == 8 {
convertV8V9 ( cfg )
}
2015-03-21 15:33:31 +01:00
if cfg . Version == 9 {
convertV9V10 ( cfg )
}
2015-07-16 12:52:36 +02:00
if cfg . Version == 10 {
convertV10V11 ( cfg )
}
2015-06-23 14:55:30 +02:00
if cfg . Version == 11 {
convertV11V12 ( cfg )
}
2014-11-20 22:24:11 +01:00
2014-04-22 11:46:08 +02:00
// Hash old cleartext passwords
2014-04-19 13:33:51 +02:00
if len ( cfg . GUI . Password ) > 0 && cfg . GUI . Password [ 0 ] != '$' {
hash , err := bcrypt . GenerateFromPassword ( [ ] byte ( cfg . GUI . Password ) , 0 )
if err != nil {
2014-08-17 10:28:36 +02:00
l . Warnln ( "bcrypting password:" , err )
2014-04-19 13:33:51 +02:00
} else {
cfg . GUI . Password = string ( hash )
}
}
2014-09-28 13:00:38 +02:00
// Build a list of available devices
existingDevices := make ( map [ protocol . DeviceID ] bool )
for _ , device := range cfg . Devices {
existingDevices [ device . DeviceID ] = true
2014-07-22 23:27:00 +02:00
}
2014-10-06 21:43:38 +02:00
// Ensure this device is present in the config
if ! existingDevices [ myID ] {
2014-08-17 00:20:21 +02:00
myName , _ := os . Hostname ( )
2014-09-28 13:00:38 +02:00
cfg . Devices = append ( cfg . Devices , DeviceConfiguration {
DeviceID : myID ,
2014-09-28 13:05:25 +02:00
Name : myName ,
2014-08-17 00:20:21 +02:00
} )
2014-10-06 21:43:38 +02:00
existingDevices [ myID ] = true
2014-08-17 00:20:21 +02:00
}
2014-10-06 21:28:16 +02:00
2014-09-28 13:00:38 +02:00
sort . Sort ( DeviceConfigurationList ( cfg . Devices ) )
// Ensure that any loose devices are not present in the wrong places
// Ensure that there are no duplicate devices
2014-11-23 19:43:49 +01:00
// Ensure that puller settings are sane
2014-09-28 13:00:38 +02:00
for i := range cfg . Folders {
cfg . Folders [ i ] . Devices = ensureDevicePresent ( cfg . Folders [ i ] . Devices , myID )
cfg . Folders [ i ] . Devices = ensureExistingDevices ( cfg . Folders [ i ] . Devices , existingDevices )
cfg . Folders [ i ] . Devices = ensureNoDuplicates ( cfg . Folders [ i ] . Devices )
sort . Sort ( FolderDeviceConfigurationList ( cfg . Folders [ i ] . Devices ) )
2014-04-22 11:46:08 +02:00
}
// An empty address list is equivalent to a single "dynamic" entry
2014-09-28 13:00:38 +02:00
for i := range cfg . Devices {
n := & cfg . Devices [ i ]
2014-04-22 11:46:08 +02:00
if len ( n . Addresses ) == 0 || len ( n . Addresses ) == 1 && n . Addresses [ 0 ] == "" {
n . Addresses = [ ] string { "dynamic" }
}
}
2014-11-18 23:57:21 +01:00
2015-03-26 13:57:27 +01:00
// Very short reconnection intervals are annoying
if cfg . Options . ReconnectIntervalS < 5 {
cfg . Options . ReconnectIntervalS = 5
}
2014-12-29 13:48:26 +01:00
if cfg . GUI . APIKey == "" {
cfg . GUI . APIKey = randomString ( 32 )
}
2014-09-06 14:11:18 +02:00
}
2014-09-20 18:42:09 +02:00
// ChangeRequiresRestart returns true if updating the configuration requires a
// complete restart.
func ChangeRequiresRestart ( from , to Configuration ) bool {
2014-09-28 13:00:38 +02:00
// Adding, removing or changing folders requires restart
2014-10-06 21:28:16 +02:00
if ! reflect . DeepEqual ( from . Folders , to . Folders ) {
2014-09-20 18:42:09 +02:00
return true
}
2014-10-06 21:28:16 +02:00
// Removing a device requres restart
toDevs := make ( map [ protocol . DeviceID ] bool , len ( from . Devices ) )
for _ , dev := range to . Devices {
toDevs [ dev . DeviceID ] = true
}
for _ , dev := range from . Devices {
if _ , ok := toDevs [ dev . DeviceID ] ; ! ok {
2014-09-20 18:42:09 +02:00
return true
}
}
2014-11-27 10:00:07 +01:00
// Changing usage reporting to on or off does not require a restart.
to . Options . URAccepted = from . Options . URAccepted
to . Options . URUniqueID = from . Options . URUniqueID
2014-09-20 18:42:09 +02:00
// All of the generic options require restart
if ! reflect . DeepEqual ( from . Options , to . Options ) || ! reflect . DeepEqual ( from . GUI , to . GUI ) {
return true
}
return false
}
2015-07-16 12:52:36 +02:00
func convertV10V11 ( cfg * Configuration ) {
// Set minimum disk free of existing folders to 1%
for i := range cfg . Folders {
cfg . Folders [ i ] . MinDiskFreePct = 1
}
cfg . Version = 11
}
2015-06-23 14:55:30 +02:00
func convertV11V12 ( cfg * Configuration ) {
// Change listen address schema
for i , addr := range cfg . Options . ListenAddress {
if len ( addr ) > 0 && ! strings . HasPrefix ( addr , "tcp://" ) {
cfg . Options . ListenAddress [ i ] = fmt . Sprintf ( "tcp://%s" , addr )
}
}
for i , device := range cfg . Devices {
for j , addr := range device . Addresses {
if addr != "dynamic" && addr != "" {
cfg . Devices [ i ] . Addresses [ j ] = fmt . Sprintf ( "tcp://%s" , addr )
}
}
}
// Use new discovery server
for i , addr := range cfg . Options . GlobalAnnServers {
if addr == "udp4://announce.syncthing.net:22026" {
cfg . Options . GlobalAnnServers [ i ] = "udp4://announce.syncthing.net:22027"
} else if addr == "udp6://announce-v6.syncthing.net:22026" {
cfg . Options . GlobalAnnServers [ i ] = "udp6://announce-v6.syncthing.net:22027"
} else if addr == "udp4://194.126.249.5:22026" {
cfg . Options . GlobalAnnServers [ i ] = "udp4://194.126.249.5:22027"
} else if addr == "udp6://[2001:470:28:4d6::5]:22026" {
cfg . Options . GlobalAnnServers [ i ] = "udp6://[2001:470:28:4d6::5]:22027"
}
}
2015-08-23 14:59:38 +02:00
// Use new multicast group
if cfg . Options . LocalAnnMCAddr == "[ff32::5222]:21026" {
cfg . Options . LocalAnnMCAddr = "[ff12::8384]:21027"
}
2015-08-27 16:04:13 +02:00
// Use new local discovery port
if cfg . Options . LocalAnnPort == 21025 {
cfg . Options . LocalAnnPort = 21027
}
2015-06-23 14:55:30 +02:00
cfg . Version = 12
}
2015-03-21 15:33:31 +01:00
func convertV9V10 ( cfg * Configuration ) {
// Enable auto normalization on existing folders.
for i := range cfg . Folders {
cfg . Folders [ i ] . AutoNormalize = true
}
cfg . Version = 10
}
2015-02-23 09:44:10 +01:00
func convertV8V9 ( cfg * Configuration ) {
// Compression is interpreted and serialized differently, but no enforced
// changes. Still need a new version number since the compression stuff
// isn't understandable by earlier versions.
cfg . Version = 9
}
2015-03-07 20:48:53 +01:00
func convertV7V8 ( cfg * Configuration ) {
// Add IPv6 announce server
if len ( cfg . Options . GlobalAnnServers ) == 1 && cfg . Options . GlobalAnnServers [ 0 ] == "udp4://announce.syncthing.net:22026" {
cfg . Options . GlobalAnnServers = append ( cfg . Options . GlobalAnnServers , "udp6://announce-v6.syncthing.net:22026" )
}
cfg . Version = 8
}
2014-11-20 22:24:11 +01:00
func convertV6V7 ( cfg * Configuration ) {
// Migrate announce server addresses to the new URL based format
for i := range cfg . Options . GlobalAnnServers {
cfg . Options . GlobalAnnServers [ i ] = "udp4://" + cfg . Options . GlobalAnnServers [ i ]
}
cfg . Version = 7
}
2014-10-11 17:55:11 +02:00
func convertV5V6 ( cfg * Configuration ) {
// Added ".stfolder" file at folder roots to identify mount issues
// Doesn't affect the config itself, but uses config migrations to identify
// the migration point.
2014-10-12 15:09:25 +02:00
for _ , folder := range Wrap ( "" , * cfg ) . Folders ( ) {
2014-10-15 11:20:40 +02:00
// Best attempt, if it fails, it fails, the user will have to fix
// it up manually, as the repo will not get started.
folder . CreateMarker ( )
2014-10-11 17:55:11 +02:00
}
cfg . Version = 6
}
2014-10-06 09:25:45 +02:00
func setDefaults ( data interface { } ) error {
s := reflect . ValueOf ( data ) . Elem ( )
t := s . Type ( )
2014-04-08 13:45:18 +02:00
2014-10-06 09:25:45 +02:00
for i := 0 ; i < s . NumField ( ) ; i ++ {
f := s . Field ( i )
tag := t . Field ( i ) . Tag
2014-03-02 23:58:14 +01:00
2014-10-06 09:25:45 +02:00
v := tag . Get ( "default" )
if len ( v ) > 0 {
switch f . Interface ( ) . ( type ) {
case string :
f . SetString ( v )
2014-03-02 23:58:14 +01:00
2014-10-06 09:25:45 +02:00
case int :
i , err := strconv . ParseInt ( v , 10 , 64 )
if err != nil {
return err
}
f . SetInt ( i )
2014-08-17 00:20:21 +02:00
2014-10-06 09:25:45 +02:00
case bool :
f . SetBool ( v == "true" )
case [ ] string :
// We don't do anything with string slices here. Any default
// we set will be appended to by the XML decoder, so we fill
// those after decoding.
default :
panic ( f . Type ( ) )
}
}
}
return nil
2014-08-17 00:20:21 +02:00
}
2014-10-06 09:25:45 +02:00
// fillNilSlices sets default value on slices that are still nil.
func fillNilSlices ( data interface { } ) error {
s := reflect . ValueOf ( data ) . Elem ( )
t := s . Type ( )
for i := 0 ; i < s . NumField ( ) ; i ++ {
f := s . Field ( i )
tag := t . Field ( i ) . Tag
v := tag . Get ( "default" )
if len ( v ) > 0 {
switch f . Interface ( ) . ( type ) {
case [ ] string :
if f . IsNil ( ) {
2015-03-07 20:48:53 +01:00
// Treat the default as a comma separated slice
vs := strings . Split ( v , "," )
for i := range vs {
vs [ i ] = strings . TrimSpace ( vs [ i ] )
}
rv := reflect . MakeSlice ( reflect . TypeOf ( [ ] string { } ) , len ( vs ) , len ( vs ) )
for i , v := range vs {
rv . Index ( i ) . SetString ( v )
}
2014-10-06 09:25:45 +02:00
f . Set ( rv )
}
}
}
}
return nil
2014-08-17 00:20:21 +02:00
}
2014-10-06 09:25:45 +02:00
func uniqueStrings ( ss [ ] string ) [ ] string {
var m = make ( map [ string ] bool , len ( ss ) )
for _ , s := range ss {
2015-06-25 17:48:41 +02:00
m [ strings . Trim ( s , " " ) ] = true
2014-10-06 09:25:45 +02:00
}
var us = make ( [ ] string , 0 , len ( m ) )
for k := range m {
us = append ( us , k )
}
2015-03-07 21:05:30 +01:00
sort . Strings ( us )
2014-10-06 09:25:45 +02:00
return us
2014-08-17 00:20:21 +02:00
}
2014-09-28 13:00:38 +02:00
func ensureDevicePresent ( devices [ ] FolderDeviceConfiguration , myID protocol . DeviceID ) [ ] FolderDeviceConfiguration {
for _ , device := range devices {
if device . DeviceID . Equals ( myID ) {
return devices
2014-03-02 23:58:14 +01:00
}
}
2014-09-28 13:00:38 +02:00
devices = append ( devices , FolderDeviceConfiguration {
DeviceID : myID ,
2014-07-28 01:20:36 +02:00
} )
2014-03-02 23:58:14 +01:00
2014-09-28 13:00:38 +02:00
return devices
2014-03-02 23:58:14 +01:00
}
2014-07-22 23:27:00 +02:00
2014-09-28 13:00:38 +02:00
func ensureExistingDevices ( devices [ ] FolderDeviceConfiguration , existingDevices map [ protocol . DeviceID ] bool ) [ ] FolderDeviceConfiguration {
count := len ( devices )
2014-07-22 23:27:00 +02:00
i := 0
2014-07-28 01:08:15 +02:00
loop :
for i < count {
2014-09-28 13:00:38 +02:00
if _ , ok := existingDevices [ devices [ i ] . DeviceID ] ; ! ok {
devices [ i ] = devices [ count - 1 ]
2014-07-28 01:08:15 +02:00
count --
continue loop
2014-07-22 23:27:00 +02:00
}
2014-07-28 01:08:15 +02:00
i ++
2014-07-22 23:27:00 +02:00
}
2014-09-28 13:00:38 +02:00
return devices [ 0 : count ]
2014-07-22 23:27:00 +02:00
}
2014-07-28 01:15:16 +02:00
2014-09-28 13:00:38 +02:00
func ensureNoDuplicates ( devices [ ] FolderDeviceConfiguration ) [ ] FolderDeviceConfiguration {
count := len ( devices )
2014-07-28 01:15:16 +02:00
i := 0
2014-09-28 13:00:38 +02:00
seenDevices := make ( map [ protocol . DeviceID ] bool )
2014-07-28 01:15:16 +02:00
loop :
for i < count {
2014-09-28 13:00:38 +02:00
id := devices [ i ] . DeviceID
if _ , ok := seenDevices [ id ] ; ok {
devices [ i ] = devices [ count - 1 ]
2014-07-28 01:15:16 +02:00
count --
continue loop
}
2014-09-28 13:00:38 +02:00
seenDevices [ id ] = true
2014-07-28 01:15:16 +02:00
i ++
}
2014-09-28 13:00:38 +02:00
return devices [ 0 : count ]
2014-07-28 01:15:16 +02:00
}
2014-10-06 09:25:45 +02:00
type DeviceConfigurationList [ ] DeviceConfiguration
func ( l DeviceConfigurationList ) Less ( a , b int ) bool {
return l [ a ] . DeviceID . Compare ( l [ b ] . DeviceID ) == - 1
}
func ( l DeviceConfigurationList ) Swap ( a , b int ) {
l [ a ] , l [ b ] = l [ b ] , l [ a ]
}
func ( l DeviceConfigurationList ) Len ( ) int {
return len ( l )
}
type FolderDeviceConfigurationList [ ] FolderDeviceConfiguration
func ( l FolderDeviceConfigurationList ) Less ( a , b int ) bool {
return l [ a ] . DeviceID . Compare ( l [ b ] . DeviceID ) == - 1
}
func ( l FolderDeviceConfigurationList ) Swap ( a , b int ) {
l [ a ] , l [ b ] = l [ b ] , l [ a ]
}
func ( l FolderDeviceConfigurationList ) Len ( ) int {
return len ( l )
}
2014-12-29 13:48:26 +01:00
// randomCharset contains the characters that can make up a randomString().
const randomCharset = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
// randomString returns a string of random characters (taken from
// randomCharset) of the specified length.
func randomString ( l int ) string {
bs := make ( [ ] byte , l )
for i := range bs {
bs [ i ] = randomCharset [ rand . Intn ( len ( randomCharset ) ) ]
}
return string ( bs )
}
2015-04-25 07:13:53 +02:00
type PullOrder int
const (
OrderRandom PullOrder = iota // default is random
OrderAlphabetic
OrderSmallestFirst
OrderLargestFirst
OrderOldestFirst
OrderNewestFirst
)
func ( o PullOrder ) String ( ) string {
switch o {
case OrderRandom :
return "random"
case OrderAlphabetic :
return "alphabetic"
case OrderSmallestFirst :
return "smallestFirst"
case OrderLargestFirst :
return "largestFirst"
case OrderOldestFirst :
return "oldestFirst"
case OrderNewestFirst :
return "newestFirst"
default :
return "unknown"
}
}
func ( o PullOrder ) MarshalText ( ) ( [ ] byte , error ) {
return [ ] byte ( o . String ( ) ) , nil
}
func ( o * PullOrder ) UnmarshalText ( bs [ ] byte ) error {
switch string ( bs ) {
case "random" :
* o = OrderRandom
case "alphabetic" :
* o = OrderAlphabetic
case "smallestFirst" :
* o = OrderSmallestFirst
case "largestFirst" :
* o = OrderLargestFirst
case "oldestFirst" :
* o = OrderOldestFirst
case "newestFirst" :
* o = OrderNewestFirst
default :
* o = OrderRandom
}
return nil
}