2014-07-12 15:45:33 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-05-14 20:40:17 -07:00
|
|
|
// Package config implements reading and writing of the syncthing configuration file.
|
2014-05-14 17:18:09 -07:00
|
|
|
package config
|
2014-03-02 15:58:14 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2014-05-14 03:58:33 -07:00
|
|
|
"fmt"
|
2014-04-22 02:46:08 -07:00
|
|
|
"os"
|
2014-03-02 15:58:14 -07:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2014-04-19 04:33:51 -07:00
|
|
|
|
|
|
|
"code.google.com/p/go.crypto/bcrypt"
|
2014-09-22 12:42:11 -07:00
|
|
|
"github.com/syncthing/syncthing/internal/events"
|
|
|
|
"github.com/syncthing/syncthing/internal/logger"
|
|
|
|
"github.com/syncthing/syncthing/internal/osutil"
|
|
|
|
"github.com/syncthing/syncthing/internal/protocol"
|
2014-03-02 15:58:14 -07:00
|
|
|
)
|
|
|
|
|
2014-05-14 17:18:09 -07:00
|
|
|
var l = logger.DefaultLogger
|
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
type Configuration struct {
|
2014-09-28 04:05:25 -07:00
|
|
|
Location string `xml:"-" json:"-"`
|
2014-09-29 11:22:16 -07:00
|
|
|
Version int `xml:"version,attr" default:"5"`
|
2014-09-28 04:05:25 -07:00
|
|
|
Folders []FolderConfiguration `xml:"folder"`
|
|
|
|
Devices []DeviceConfiguration `xml:"device"`
|
|
|
|
GUI GUIConfiguration `xml:"gui"`
|
|
|
|
Options OptionsConfiguration `xml:"options"`
|
|
|
|
XMLName xml.Name `xml:"configuration" json:"-"`
|
2014-09-28 05:22:39 -07:00
|
|
|
|
|
|
|
Deprecated_Repositories []FolderConfiguration `xml:"repository" json:"-"`
|
|
|
|
Deprecated_Nodes []DeviceConfiguration `xml:"node" json:"-"`
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
type FolderConfiguration struct {
|
2014-09-28 04:05:25 -07:00
|
|
|
ID string `xml:"id,attr"`
|
2014-09-28 04:56:35 -07:00
|
|
|
Path string `xml:"path,attr"`
|
2014-09-28 04:05:25 -07:00
|
|
|
Devices []FolderDeviceConfiguration `xml:"device"`
|
|
|
|
ReadOnly bool `xml:"ro,attr"`
|
|
|
|
RescanIntervalS int `xml:"rescanIntervalS,attr" default:"60"`
|
|
|
|
IgnorePerms bool `xml:"ignorePerms,attr"`
|
|
|
|
Invalid string `xml:"-"` // Set at runtime when there is an error, not saved
|
|
|
|
Versioning VersioningConfiguration `xml:"versioning"`
|
2014-05-25 11:49:08 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
deviceIDs []protocol.DeviceID
|
2014-09-28 05:22:39 -07:00
|
|
|
|
2014-09-29 11:07:31 -07:00
|
|
|
Deprecated_Directory string `xml:"directory,omitempty,attr" json:"-"`
|
|
|
|
Deprecated_Nodes []FolderDeviceConfiguration `xml:"node" json:"-"`
|
2014-05-25 11:49:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type VersioningConfiguration struct {
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Params map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
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 04:45:18 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (r *FolderConfiguration) DeviceIDs() []protocol.DeviceID {
|
|
|
|
if r.deviceIDs == nil {
|
|
|
|
for _, n := range r.Devices {
|
|
|
|
r.deviceIDs = append(r.deviceIDs, n.DeviceID)
|
2014-04-08 04:45:18 -07:00
|
|
|
}
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
return r.deviceIDs
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
type DeviceConfiguration struct {
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID protocol.DeviceID `xml:"id,attr"`
|
|
|
|
Name string `xml:"name,attr,omitempty"`
|
|
|
|
Addresses []string `xml:"address,omitempty"`
|
|
|
|
Compression bool `xml:"compression,attr"`
|
|
|
|
CertName string `xml:"certName,attr,omitempty"`
|
|
|
|
Introducer bool `xml:"introducer,attr"`
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
type FolderDeviceConfiguration struct {
|
|
|
|
DeviceID protocol.DeviceID `xml:"id,attr"`
|
2014-08-16 15:20:21 -07:00
|
|
|
|
|
|
|
Deprecated_Name string `xml:"name,attr,omitempty" json:"-"`
|
|
|
|
Deprecated_Addresses []string `xml:"address,omitempty" json:"-"`
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
type OptionsConfiguration struct {
|
2014-09-25 15:51:12 -07:00
|
|
|
ListenAddress []string `xml:"listenAddress" default:"0.0.0.0:22000"`
|
|
|
|
GlobalAnnServer string `xml:"globalAnnounceServer" default:"announce.syncthing.net:22026"`
|
|
|
|
GlobalAnnEnabled bool `xml:"globalAnnounceEnabled" default:"true"`
|
|
|
|
LocalAnnEnabled bool `xml:"localAnnounceEnabled" default:"true"`
|
|
|
|
LocalAnnPort int `xml:"localAnnouncePort" default:"21025"`
|
|
|
|
LocalAnnMCAddr string `xml:"localAnnounceMCAddr" default:"[ff32::5222]:21026"`
|
|
|
|
MaxSendKbps int `xml:"maxSendKbps"`
|
|
|
|
MaxRecvKbps int `xml:"maxRecvKbps"`
|
|
|
|
ReconnectIntervalS int `xml:"reconnectionIntervalS" default:"60"`
|
|
|
|
StartBrowser bool `xml:"startBrowser" default:"true"`
|
|
|
|
UPnPEnabled bool `xml:"upnpEnabled" default:"true"`
|
|
|
|
UPnPLease int `xml:"upnpLeaseMinutes" default:"0"`
|
|
|
|
UPnPRenewal int `xml:"upnpRenewalMinutes" default:"30"`
|
|
|
|
URAccepted int `xml:"urAccepted"` // Accepted usage reporting version; 0 for off (undecided), -1 for off (permanently)
|
|
|
|
RestartOnWakeup bool `xml:"restartOnWakeup" default:"true"`
|
|
|
|
AutoUpgradeIntervalH int `xml:"autoUpgradeIntervalH" default:"12"` // 0 for off
|
2014-10-03 15:15:54 -07:00
|
|
|
KeepTemporariesH int `xml:"keepTemporariesH" default:"24"` // 0 for off
|
2014-04-08 06:56:12 -07:00
|
|
|
|
2014-08-18 14:05:47 -07:00
|
|
|
Deprecated_RescanIntervalS int `xml:"rescanIntervalS,omitempty" json:"-"`
|
|
|
|
Deprecated_UREnabled bool `xml:"urEnabled,omitempty" json:"-"`
|
|
|
|
Deprecated_URDeclined bool `xml:"urDeclined,omitempty" json:"-"`
|
|
|
|
Deprecated_ReadOnly bool `xml:"readOnly,omitempty" json:"-"`
|
|
|
|
Deprecated_GUIEnabled bool `xml:"guiEnabled,omitempty" json:"-"`
|
|
|
|
Deprecated_GUIAddress string `xml:"guiAddress,omitempty" json:"-"`
|
2014-04-08 06:56:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type GUIConfiguration struct {
|
|
|
|
Enabled bool `xml:"enabled,attr" default:"true"`
|
|
|
|
Address string `xml:"address" default:"127.0.0.1:8080"`
|
|
|
|
User string `xml:"user,omitempty"`
|
|
|
|
Password string `xml:"password,omitempty"`
|
2014-05-21 05:04:16 -07:00
|
|
|
UseTLS bool `xml:"tls,attr"`
|
2014-06-04 13:00:55 -07:00
|
|
|
APIKey string `xml:"apikey,omitempty"`
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (cfg *Configuration) DeviceMap() map[protocol.DeviceID]DeviceConfiguration {
|
|
|
|
m := make(map[protocol.DeviceID]DeviceConfiguration, len(cfg.Devices))
|
|
|
|
for _, n := range cfg.Devices {
|
|
|
|
m[n.DeviceID] = n
|
2014-06-06 19:00:46 -07:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (cfg *Configuration) GetDeviceConfiguration(deviceID protocol.DeviceID) *DeviceConfiguration {
|
|
|
|
for i, device := range cfg.Devices {
|
|
|
|
if device.DeviceID == deviceID {
|
|
|
|
return &cfg.Devices[i]
|
2014-08-14 15:15:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (cfg *Configuration) GetFolderConfiguration(folderID string) *FolderConfiguration {
|
|
|
|
for i, folder := range cfg.Folders {
|
|
|
|
if folder.ID == folderID {
|
|
|
|
return &cfg.Folders[i]
|
2014-09-23 07:04:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (cfg *Configuration) FolderMap() map[string]FolderConfiguration {
|
|
|
|
m := make(map[string]FolderConfiguration, len(cfg.Folders))
|
|
|
|
for _, r := range cfg.Folders {
|
2014-06-06 19:00:46 -07:00
|
|
|
m[r.ID] = r
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-03-04 14:29:43 -07:00
|
|
|
func setDefaults(data interface{}) error {
|
2014-03-02 15:58:14 -07:00
|
|
|
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:
|
|
|
|
f.SetString(v)
|
|
|
|
|
|
|
|
case int:
|
|
|
|
i, err := strconv.ParseInt(v, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.SetInt(i)
|
|
|
|
|
|
|
|
case bool:
|
|
|
|
f.SetBool(v == "true")
|
|
|
|
|
2014-03-04 14:29:43 -07:00
|
|
|
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.
|
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
default:
|
|
|
|
panic(f.Type())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-04 14:29:43 -07: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() {
|
|
|
|
rv := reflect.MakeSlice(reflect.TypeOf([]string{}), 1, 1)
|
|
|
|
rv.Index(0).SetString(v)
|
|
|
|
f.Set(rv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-06 05:11:18 -07:00
|
|
|
func (cfg *Configuration) Save() error {
|
|
|
|
fd, err := os.Create(cfg.Location + ".tmp")
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("Saving config:", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
e := xml.NewEncoder(fd)
|
2014-03-02 15:58:14 -07:00
|
|
|
e.Indent("", " ")
|
2014-09-06 05:11:18 -07:00
|
|
|
err = e.Encode(cfg)
|
|
|
|
if err != nil {
|
|
|
|
fd.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fd.Write([]byte("\n"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("Saving config:", err)
|
|
|
|
fd.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fd.Close()
|
2014-03-02 15:58:14 -07:00
|
|
|
if err != nil {
|
2014-09-06 05:11:18 -07:00
|
|
|
l.Warnln("Saving config:", err)
|
2014-03-02 15:58:14 -07:00
|
|
|
return err
|
|
|
|
}
|
2014-09-06 05:11:18 -07:00
|
|
|
|
|
|
|
err = osutil.Rename(cfg.Location+".tmp", cfg.Location)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("Saving config:", err)
|
|
|
|
}
|
2014-09-06 08:31:23 -07:00
|
|
|
events.Default.Log(events.ConfigSaved, cfg)
|
2014-03-02 15:58:14 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func uniqueStrings(ss []string) []string {
|
|
|
|
var m = make(map[string]bool, len(ss))
|
|
|
|
for _, s := range ss {
|
|
|
|
m[s] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var us = make([]string, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
us = append(us, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return us
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (cfg *Configuration) prepare(myID protocol.DeviceID) {
|
2014-03-04 14:29:43 -07:00
|
|
|
fillNilSlices(&cfg.Options)
|
2014-03-04 03:25:10 -07:00
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
cfg.Options.ListenAddress = uniqueStrings(cfg.Options.ListenAddress)
|
2014-03-29 10:53:48 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Initialize an empty slice for folders if the config has none
|
|
|
|
if cfg.Folders == nil {
|
|
|
|
cfg.Folders = []FolderConfiguration{}
|
2014-05-12 19:57:38 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Check for missing, bad or duplicate folder ID:s
|
|
|
|
var seenFolders = map[string]*FolderConfiguration{}
|
2014-05-14 03:58:33 -07:00
|
|
|
var uniqueCounter int
|
2014-09-28 04:00:38 -07:00
|
|
|
for i := range cfg.Folders {
|
|
|
|
folder := &cfg.Folders[i]
|
2014-04-27 12:53:27 -07:00
|
|
|
|
2014-09-28 04:56:35 -07:00
|
|
|
if len(folder.Path) == 0 {
|
2014-09-28 04:00:38 -07:00
|
|
|
folder.Invalid = "no directory configured"
|
2014-05-12 17:30:04 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
if folder.ID == "" {
|
|
|
|
folder.ID = "default"
|
2014-03-29 10:53:48 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
if seen, ok := seenFolders[folder.ID]; ok {
|
|
|
|
l.Warnf("Multiple folders with ID %q; disabling", folder.ID)
|
2014-05-14 03:58:33 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
seen.Invalid = "duplicate folder ID"
|
|
|
|
if seen.ID == folder.ID {
|
2014-05-14 03:58:33 -07:00
|
|
|
uniqueCounter++
|
2014-09-28 04:00:38 -07:00
|
|
|
seen.ID = fmt.Sprintf("%s~%d", folder.ID, uniqueCounter)
|
2014-05-14 03:58:33 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
folder.Invalid = "duplicate folder ID"
|
2014-05-14 03:58:33 -07:00
|
|
|
uniqueCounter++
|
2014-09-28 04:00:38 -07:00
|
|
|
folder.ID = fmt.Sprintf("%s~%d", folder.ID, uniqueCounter)
|
2014-04-27 12:53:27 -07:00
|
|
|
} else {
|
2014-09-28 04:00:38 -07:00
|
|
|
seenFolders[folder.ID] = folder
|
2014-03-29 10:53:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:22:19 -07:00
|
|
|
if cfg.Options.Deprecated_URDeclined {
|
|
|
|
cfg.Options.URAccepted = -1
|
|
|
|
}
|
|
|
|
cfg.Options.Deprecated_URDeclined = false
|
|
|
|
cfg.Options.Deprecated_UREnabled = false
|
|
|
|
|
2014-04-22 02:46:08 -07:00
|
|
|
// Upgrade to v2 configuration if appropriate
|
2014-04-08 04:45:18 -07:00
|
|
|
if cfg.Version == 1 {
|
2014-09-06 05:11:18 -07:00
|
|
|
convertV1V2(cfg)
|
2014-04-08 04:45:18 -07:00
|
|
|
}
|
|
|
|
|
2014-08-01 07:48:06 -07:00
|
|
|
// Upgrade to v3 configuration if appropriate
|
|
|
|
if cfg.Version == 2 {
|
2014-09-06 05:11:18 -07:00
|
|
|
convertV2V3(cfg)
|
2014-08-01 07:48:06 -07:00
|
|
|
}
|
|
|
|
|
2014-08-16 15:20:21 -07:00
|
|
|
// Upgrade to v4 configuration if appropriate
|
|
|
|
if cfg.Version == 3 {
|
2014-09-06 05:11:18 -07:00
|
|
|
convertV3V4(cfg)
|
2014-08-16 15:20:21 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 05:22:39 -07:00
|
|
|
// Upgrade to v5 configuration if appropriate
|
|
|
|
if cfg.Version == 4 {
|
|
|
|
convertV4V5(cfg)
|
|
|
|
}
|
|
|
|
|
2014-04-22 02:46:08 -07:00
|
|
|
// Hash old cleartext passwords
|
2014-04-19 04:33:51 -07: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 01:28:36 -07:00
|
|
|
l.Warnln("bcrypting password:", err)
|
2014-04-19 04:33:51 -07:00
|
|
|
} else {
|
|
|
|
cfg.GUI.Password = string(hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Build a list of available devices
|
|
|
|
existingDevices := make(map[protocol.DeviceID]bool)
|
|
|
|
existingDevices[myID] = true
|
|
|
|
for _, device := range cfg.Devices {
|
|
|
|
existingDevices[device.DeviceID] = true
|
2014-07-22 14:27:00 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Ensure this device is present in all relevant places
|
|
|
|
me := cfg.GetDeviceConfiguration(myID)
|
2014-08-16 15:20:21 -07:00
|
|
|
if me == nil {
|
|
|
|
myName, _ := os.Hostname()
|
2014-09-28 04:00:38 -07:00
|
|
|
cfg.Devices = append(cfg.Devices, DeviceConfiguration{
|
|
|
|
DeviceID: myID,
|
2014-09-28 04:05:25 -07:00
|
|
|
Name: myName,
|
2014-08-16 15:20:21 -07:00
|
|
|
})
|
|
|
|
}
|
2014-09-28 04:00:38 -07: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
|
|
|
|
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 02:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// An empty address list is equivalent to a single "dynamic" entry
|
2014-09-28 04:00:38 -07:00
|
|
|
for i := range cfg.Devices {
|
|
|
|
n := &cfg.Devices[i]
|
2014-04-22 02:46:08 -07:00
|
|
|
if len(n.Addresses) == 0 || len(n.Addresses) == 1 && n.Addresses[0] == "" {
|
|
|
|
n.Addresses = []string{"dynamic"}
|
|
|
|
}
|
|
|
|
}
|
2014-09-06 05:11:18 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func New(location string, myID protocol.DeviceID) Configuration {
|
2014-09-06 05:11:18 -07:00
|
|
|
var cfg Configuration
|
|
|
|
|
|
|
|
cfg.Location = location
|
|
|
|
|
|
|
|
setDefaults(&cfg)
|
|
|
|
setDefaults(&cfg.Options)
|
|
|
|
setDefaults(&cfg.GUI)
|
|
|
|
|
|
|
|
cfg.prepare(myID)
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func Load(location string, myID protocol.DeviceID) (Configuration, error) {
|
2014-09-06 05:11:18 -07:00
|
|
|
var cfg Configuration
|
|
|
|
|
|
|
|
cfg.Location = location
|
|
|
|
|
|
|
|
setDefaults(&cfg)
|
|
|
|
setDefaults(&cfg.Options)
|
|
|
|
setDefaults(&cfg.GUI)
|
|
|
|
|
|
|
|
fd, err := os.Open(location)
|
|
|
|
if err != nil {
|
|
|
|
return Configuration{}, err
|
|
|
|
}
|
|
|
|
err = xml.NewDecoder(fd).Decode(&cfg)
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
cfg.prepare(myID)
|
2014-04-22 02:46:08 -07:00
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2014-09-20 09:42:09 -07:00
|
|
|
// ChangeRequiresRestart returns true if updating the configuration requires a
|
|
|
|
// complete restart.
|
|
|
|
func ChangeRequiresRestart(from, to Configuration) bool {
|
2014-09-28 04:00:38 -07:00
|
|
|
// Adding, removing or changing folders requires restart
|
|
|
|
if len(from.Folders) != len(to.Folders) {
|
2014-09-20 09:42:09 -07:00
|
|
|
return true
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
fromFolders := from.FolderMap()
|
|
|
|
toFolders := to.FolderMap()
|
|
|
|
for id := range fromFolders {
|
|
|
|
if !reflect.DeepEqual(fromFolders[id], toFolders[id]) {
|
2014-09-20 09:42:09 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Removing a device requires a restart. Adding one does not. Changing
|
2014-09-20 09:42:09 -07:00
|
|
|
// address or name does not.
|
2014-09-28 04:00:38 -07:00
|
|
|
fromDevices := from.DeviceMap()
|
|
|
|
toDevices := to.DeviceMap()
|
|
|
|
for deviceID := range fromDevices {
|
|
|
|
if _, ok := toDevices[deviceID]; !ok {
|
2014-09-20 09:42:09 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All of the generic options require restart
|
|
|
|
if !reflect.DeepEqual(from.Options, to.Options) || !reflect.DeepEqual(from.GUI, to.GUI) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-09-28 05:22:39 -07:00
|
|
|
func convertV4V5(cfg *Configuration) {
|
|
|
|
// Renamed a bunch of fields in the structs.
|
|
|
|
if cfg.Deprecated_Nodes == nil {
|
|
|
|
cfg.Deprecated_Nodes = []DeviceConfiguration{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Deprecated_Repositories == nil {
|
|
|
|
cfg.Deprecated_Repositories = []FolderConfiguration{}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Devices = cfg.Deprecated_Nodes
|
|
|
|
cfg.Folders = cfg.Deprecated_Repositories
|
|
|
|
|
|
|
|
for i := range cfg.Folders {
|
2014-09-29 11:07:31 -07:00
|
|
|
cfg.Folders[i].Path = cfg.Folders[i].Deprecated_Directory
|
|
|
|
cfg.Folders[i].Deprecated_Directory = ""
|
|
|
|
cfg.Folders[i].Devices = cfg.Folders[i].Deprecated_Nodes
|
|
|
|
cfg.Folders[i].Deprecated_Nodes = nil
|
2014-09-28 05:22:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Deprecated_Nodes = nil
|
|
|
|
cfg.Deprecated_Repositories = nil
|
|
|
|
|
|
|
|
cfg.Version = 5
|
|
|
|
}
|
|
|
|
|
2014-08-16 15:20:21 -07:00
|
|
|
func convertV3V4(cfg *Configuration) {
|
2014-09-28 04:00:38 -07:00
|
|
|
// In previous versions, rescan interval was common for each folder.
|
2014-08-18 14:05:47 -07:00
|
|
|
// From now, it can be set independently. We have to make sure, that after upgrade
|
2014-09-28 04:00:38 -07:00
|
|
|
// the individual rescan interval will be defined for every existing folder.
|
2014-09-28 05:22:39 -07:00
|
|
|
for i := range cfg.Deprecated_Repositories {
|
|
|
|
cfg.Deprecated_Repositories[i].RescanIntervalS = cfg.Options.Deprecated_RescanIntervalS
|
2014-08-18 14:05:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Options.Deprecated_RescanIntervalS = 0
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// In previous versions, folders held full device configurations.
|
|
|
|
// Since that's the only place where device configs were in V1, we still have
|
2014-08-16 15:20:21 -07:00
|
|
|
// to define the deprecated fields to be able to upgrade from V1 to V4.
|
2014-09-28 05:22:39 -07:00
|
|
|
for i, folder := range cfg.Deprecated_Repositories {
|
2014-08-18 14:05:47 -07:00
|
|
|
|
2014-09-29 11:07:31 -07:00
|
|
|
for j := range folder.Deprecated_Nodes {
|
|
|
|
rncfg := cfg.Deprecated_Repositories[i].Deprecated_Nodes[j]
|
2014-08-16 15:20:21 -07:00
|
|
|
rncfg.Deprecated_Name = ""
|
|
|
|
rncfg.Deprecated_Addresses = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Version = 4
|
|
|
|
}
|
|
|
|
|
2014-08-01 07:48:06 -07:00
|
|
|
func convertV2V3(cfg *Configuration) {
|
|
|
|
// In previous versions, compression was always on. When upgrading, enable
|
2014-09-28 04:00:38 -07:00
|
|
|
// compression on all existing new. New devices will get compression on by
|
2014-08-01 07:48:06 -07:00
|
|
|
// default by the GUI.
|
2014-09-28 05:22:39 -07:00
|
|
|
for i := range cfg.Deprecated_Nodes {
|
|
|
|
cfg.Deprecated_Nodes[i].Compression = true
|
2014-08-01 07:48:06 -07:00
|
|
|
}
|
2014-08-01 23:37:10 -07:00
|
|
|
|
|
|
|
// The global discovery format and port number changed in v0.9. Having the
|
|
|
|
// default announce server but old port number is guaranteed to be legacy.
|
|
|
|
if cfg.Options.GlobalAnnServer == "announce.syncthing.net:22025" {
|
|
|
|
cfg.Options.GlobalAnnServer = "announce.syncthing.net:22026"
|
|
|
|
}
|
|
|
|
|
2014-08-01 07:48:06 -07:00
|
|
|
cfg.Version = 3
|
|
|
|
}
|
|
|
|
|
2014-04-08 04:45:18 -07:00
|
|
|
func convertV1V2(cfg *Configuration) {
|
2014-09-28 04:00:38 -07:00
|
|
|
// Collect the list of devices.
|
2014-09-28 04:39:39 -07:00
|
|
|
// Replace device configs inside folders with only a reference to the
|
|
|
|
// device ID. Set all folders to read only if the global read only flag is
|
|
|
|
// set.
|
2014-09-28 04:00:38 -07:00
|
|
|
var devices = map[string]FolderDeviceConfiguration{}
|
2014-09-28 05:22:39 -07:00
|
|
|
for i, folder := range cfg.Deprecated_Repositories {
|
|
|
|
cfg.Deprecated_Repositories[i].ReadOnly = cfg.Options.Deprecated_ReadOnly
|
2014-09-29 11:07:31 -07:00
|
|
|
for j, device := range folder.Deprecated_Nodes {
|
2014-09-28 04:00:38 -07:00
|
|
|
id := device.DeviceID.String()
|
|
|
|
if _, ok := devices[id]; !ok {
|
|
|
|
devices[id] = device
|
2014-04-08 04:45:18 -07:00
|
|
|
}
|
2014-09-29 11:07:31 -07:00
|
|
|
cfg.Deprecated_Repositories[i].Deprecated_Nodes[j] = FolderDeviceConfiguration{DeviceID: device.DeviceID}
|
2014-04-08 04:45:18 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-08 06:56:12 -07:00
|
|
|
cfg.Options.Deprecated_ReadOnly = false
|
2014-04-08 04:45:18 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Set and sort the list of devices.
|
|
|
|
for _, device := range devices {
|
2014-09-28 05:22:39 -07:00
|
|
|
cfg.Deprecated_Nodes = append(cfg.Deprecated_Nodes, DeviceConfiguration{
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID: device.DeviceID,
|
2014-09-28 04:00:38 -07:00
|
|
|
Name: device.Deprecated_Name,
|
|
|
|
Addresses: device.Deprecated_Addresses,
|
2014-08-16 15:20:21 -07:00
|
|
|
})
|
2014-04-08 04:45:18 -07:00
|
|
|
}
|
2014-09-28 05:22:39 -07:00
|
|
|
sort.Sort(DeviceConfigurationList(cfg.Deprecated_Nodes))
|
2014-04-08 04:45:18 -07:00
|
|
|
|
2014-04-08 06:56:12 -07:00
|
|
|
// GUI
|
|
|
|
cfg.GUI.Address = cfg.Options.Deprecated_GUIAddress
|
|
|
|
cfg.GUI.Enabled = cfg.Options.Deprecated_GUIEnabled
|
|
|
|
cfg.Options.Deprecated_GUIEnabled = false
|
|
|
|
cfg.Options.Deprecated_GUIAddress = ""
|
|
|
|
|
2014-04-08 04:45:18 -07:00
|
|
|
cfg.Version = 2
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
type DeviceConfigurationList []DeviceConfiguration
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (l DeviceConfigurationList) Less(a, b int) bool {
|
|
|
|
return l[a].DeviceID.Compare(l[b].DeviceID) == -1
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
func (l DeviceConfigurationList) Swap(a, b int) {
|
2014-03-02 15:58:14 -07:00
|
|
|
l[a], l[b] = l[b], l[a]
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
func (l DeviceConfigurationList) Len() int {
|
2014-03-02 15:58:14 -07:00
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
type FolderDeviceConfigurationList []FolderDeviceConfiguration
|
2014-08-16 15:20:21 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (l FolderDeviceConfigurationList) Less(a, b int) bool {
|
|
|
|
return l[a].DeviceID.Compare(l[b].DeviceID) == -1
|
2014-08-16 15:20:21 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
func (l FolderDeviceConfigurationList) Swap(a, b int) {
|
2014-08-16 15:20:21 -07:00
|
|
|
l[a], l[b] = l[b], l[a]
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
func (l FolderDeviceConfigurationList) Len() int {
|
2014-08-16 15:20:21 -07:00
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
|
|
|
|
for _, device := range devices {
|
|
|
|
if device.DeviceID.Equals(myID) {
|
|
|
|
return devices
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
devices = append(devices, FolderDeviceConfiguration{
|
|
|
|
DeviceID: myID,
|
2014-07-27 16:20:36 -07:00
|
|
|
})
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
return devices
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
2014-07-22 14:27:00 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
|
|
|
|
count := len(devices)
|
2014-07-22 14:27:00 -07:00
|
|
|
i := 0
|
2014-07-27 16:08:15 -07:00
|
|
|
loop:
|
|
|
|
for i < count {
|
2014-09-28 04:00:38 -07:00
|
|
|
if _, ok := existingDevices[devices[i].DeviceID]; !ok {
|
|
|
|
devices[i] = devices[count-1]
|
2014-07-27 16:08:15 -07:00
|
|
|
count--
|
|
|
|
continue loop
|
2014-07-22 14:27:00 -07:00
|
|
|
}
|
2014-07-27 16:08:15 -07:00
|
|
|
i++
|
2014-07-22 14:27:00 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
return devices[0:count]
|
2014-07-22 14:27:00 -07:00
|
|
|
}
|
2014-07-27 16:15:16 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ensureNoDuplicates(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
|
|
|
|
count := len(devices)
|
2014-07-27 16:15:16 -07:00
|
|
|
i := 0
|
2014-09-28 04:00:38 -07:00
|
|
|
seenDevices := make(map[protocol.DeviceID]bool)
|
2014-07-27 16:15:16 -07:00
|
|
|
loop:
|
|
|
|
for i < count {
|
2014-09-28 04:00:38 -07:00
|
|
|
id := devices[i].DeviceID
|
|
|
|
if _, ok := seenDevices[id]; ok {
|
|
|
|
devices[i] = devices[count-1]
|
2014-07-27 16:15:16 -07:00
|
|
|
count--
|
|
|
|
continue loop
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
seenDevices[id] = true
|
2014-07-27 16:15:16 -07:00
|
|
|
i++
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
return devices[0:count]
|
2014-07-27 16:15:16 -07:00
|
|
|
}
|