From 09832abe50ba08664b900a865cd6e8fa23ff636e Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 4 May 2016 11:26:36 +0000 Subject: [PATCH] lib/config: Change folder type attribute to a FolderType type GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3032 --- cmd/syncthing/main.go | 2 +- lib/config/config.go | 3 --- lib/config/folderconfiguration.go | 5 ++-- lib/config/foldertype.go | 41 +++++++++++++++++++++++++++++++ lib/model/model.go | 4 +-- lib/model/model_test.go | 2 +- test/override_test.go | 2 +- 7 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 lib/config/foldertype.go diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 3ac19dd7..0d6dd6ff 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 1e2699ba..b6de510c 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 99834c2f..07d4670a 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 00000000..61759a86 --- /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 2bb503fe..f3bd1c8a 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 18d2b6ae..8483542e 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 4076b8fb..b95c90c7 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")