summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakob Borg <jakob@nym.se>2016-05-04 11:26:36 +0000
committerAudrius Butkevicius <audrius.butkevicius@gmail.com>2016-05-04 11:26:36 +0000
commit09832abe50ba08664b900a865cd6e8fa23ff636e (patch)
treef115361c7b690ff8df3b7fd0496ee813f0f8d6c1
parenteabd2fc936be685a0b1207fde49dba77a012c2ff (diff)
lib/config: Change folder type attribute to a FolderType type
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3032
-rw-r--r--cmd/syncthing/main.go2
-rw-r--r--lib/config/config.go3
-rw-r--r--lib/config/folderconfiguration.go5
-rw-r--r--lib/config/foldertype.go41
-rw-r--r--lib/model/model.go4
-rw-r--r--lib/model/model_test.go2
-rw-r--r--test/override_test.go2
7 files changed, 48 insertions, 11 deletions
diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go
index 3ac19dd735..0d6dd6ff3a 100644
--- a/cmd/syncthing/main.go
+++ b/cmd/syncthing/main.go
@@ -988,7 +988,7 @@ func defaultConfig(myName string) config.Configuration {
if !noDefaultFolder {
l.Infoln("Default folder created and/or linked to new config")
- defaultFolder = config.NewFolderConfiguration("default", locations[locDefFolder], config.FolderTypeReadWrite)
+ defaultFolder = config.NewFolderConfiguration("default", locations[locDefFolder])
defaultFolder.RescanIntervalS = 60
defaultFolder.MinDiskFreePct = 1
defaultFolder.Devices = []config.FolderDeviceConfiguration{{DeviceID: myID}}
diff --git a/lib/config/config.go b/lib/config/config.go
index 1e2699ba6e..b6de510cac 100644
--- a/lib/config/config.go
+++ b/lib/config/config.go
@@ -23,9 +23,6 @@ const (
OldestHandledVersion = 10
CurrentVersion = 13
MaxRescanIntervalS = 365 * 24 * 60 * 60
-
- FolderTypeReadWrite = "readwrite"
- FolderTypeReadOnly = "readonly"
)
var (
diff --git a/lib/config/folderconfiguration.go b/lib/config/folderconfiguration.go
index 99834c2f9a..07d4670a9d 100644
--- a/lib/config/folderconfiguration.go
+++ b/lib/config/folderconfiguration.go
@@ -20,7 +20,7 @@ type FolderConfiguration struct {
ID string `xml:"id,attr" json:"id"`
Label string `xml:"label,attr" json:"label"`
RawPath string `xml:"path,attr" json:"path"`
- Type string `xml:"type,attr" json:"type"`
+ Type FolderType `xml:"type,attr" json:"type"`
Devices []FolderDeviceConfiguration `xml:"device" json:"devices"`
RescanIntervalS int `xml:"rescanIntervalS,attr" json:"rescanIntervalS"`
IgnorePerms bool `xml:"ignorePerms,attr" json:"ignorePerms"`
@@ -49,11 +49,10 @@ type FolderDeviceConfiguration struct {
DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
}
-func NewFolderConfiguration(id, path, foldertype string) FolderConfiguration {
+func NewFolderConfiguration(id, path string) FolderConfiguration {
f := FolderConfiguration{
ID: id,
RawPath: path,
- Type: foldertype,
}
f.prepare()
return f
diff --git a/lib/config/foldertype.go b/lib/config/foldertype.go
new file mode 100644
index 0000000000..61759a86cc
--- /dev/null
+++ b/lib/config/foldertype.go
@@ -0,0 +1,41 @@
+// Copyright (C) 2016 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 http://mozilla.org/MPL/2.0/.
+
+package config
+
+type FolderType int
+
+const (
+ FolderTypeReadWrite FolderType = iota // default is readwrite
+ FolderTypeReadOnly
+)
+
+func (t FolderType) String() string {
+ switch t {
+ case FolderTypeReadWrite:
+ return "readwrite"
+ case FolderTypeReadOnly:
+ return "readonly"
+ default:
+ return "unknown"
+ }
+}
+
+func (t FolderType) MarshalText() ([]byte, error) {
+ return []byte(t.String()), nil
+}
+
+func (t *FolderType) UnmarshalText(bs []byte) error {
+ switch string(bs) {
+ case "readwrite":
+ *t = FolderTypeReadWrite
+ case "readonly":
+ *t = FolderTypeReadOnly
+ default:
+ *t = FolderTypeReadWrite
+ }
+ return nil
+}
diff --git a/lib/model/model.go b/lib/model/model.go
index 2bb503fe8b..f3bd1c8a6b 100644
--- a/lib/model/model.go
+++ b/lib/model/model.go
@@ -104,7 +104,7 @@ type folderFactory func(*Model, config.FolderConfiguration, versioner.Versioner)
var (
symlinkWarning = stdsync.Once{}
- folderFactories = make(map[string]folderFactory, 0)
+ folderFactories = make(map[config.FolderType]folderFactory, 0)
)
// NewModel creates and starts a new model. The model starts in read-only mode,
@@ -176,7 +176,7 @@ func (m *Model) StartFolder(folder string) {
folderFactory, ok := folderFactories[cfg.Type]
if !ok {
- panic("unknown folder type " + cfg.Type)
+ panic(fmt.Sprintf("unknown folder type 0x%x", cfg.Type))
}
var ver versioner.Versioner
diff --git a/lib/model/model_test.go b/lib/model/model_test.go
index 18d2b6ae2a..8483542e96 100644
--- a/lib/model/model_test.go
+++ b/lib/model/model_test.go
@@ -34,7 +34,7 @@ func init() {
device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
- defaultFolderConfig = config.NewFolderConfiguration("default", "testdata", config.FolderTypeReadWrite)
+ defaultFolderConfig = config.NewFolderConfiguration("default", "testdata")
defaultFolderConfig.Devices = []config.FolderDeviceConfiguration{{DeviceID: device1}}
_defaultConfig := config.Configuration{
Folders: []config.FolderConfiguration{defaultFolderConfig},
diff --git a/test/override_test.go b/test/override_test.go
index 4076b8fb31..b95c90c7f7 100644
--- a/test/override_test.go
+++ b/test/override_test.go
@@ -27,7 +27,7 @@ func TestOverride(t *testing.T) {
id, _ := protocol.DeviceIDFromString(id1)
cfg, _ := config.Load("h1/config.xml", id)
fld := cfg.Folders()["default"]
- fld.ReadOnly = true
+ fld.Type = config.FolderTypeReadOnly
cfg.SetFolder(fld)
os.Rename("h1/config.xml", "h1/config.xml.orig")
defer osutil.Rename("h1/config.xml.orig", "h1/config.xml")