lib/config: Change folder type attribute to a FolderType type
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3032
This commit is contained in:
parent
eabd2fc936
commit
09832abe50
@ -988,7 +988,7 @@ func defaultConfig(myName string) config.Configuration {
|
|||||||
if !noDefaultFolder {
|
if !noDefaultFolder {
|
||||||
l.Infoln("Default folder created and/or linked to new config")
|
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.RescanIntervalS = 60
|
||||||
defaultFolder.MinDiskFreePct = 1
|
defaultFolder.MinDiskFreePct = 1
|
||||||
defaultFolder.Devices = []config.FolderDeviceConfiguration{{DeviceID: myID}}
|
defaultFolder.Devices = []config.FolderDeviceConfiguration{{DeviceID: myID}}
|
||||||
|
|||||||
@ -23,9 +23,6 @@ const (
|
|||||||
OldestHandledVersion = 10
|
OldestHandledVersion = 10
|
||||||
CurrentVersion = 13
|
CurrentVersion = 13
|
||||||
MaxRescanIntervalS = 365 * 24 * 60 * 60
|
MaxRescanIntervalS = 365 * 24 * 60 * 60
|
||||||
|
|
||||||
FolderTypeReadWrite = "readwrite"
|
|
||||||
FolderTypeReadOnly = "readonly"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@ -20,7 +20,7 @@ type FolderConfiguration struct {
|
|||||||
ID string `xml:"id,attr" json:"id"`
|
ID string `xml:"id,attr" json:"id"`
|
||||||
Label string `xml:"label,attr" json:"label"`
|
Label string `xml:"label,attr" json:"label"`
|
||||||
RawPath string `xml:"path,attr" json:"path"`
|
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"`
|
Devices []FolderDeviceConfiguration `xml:"device" json:"devices"`
|
||||||
RescanIntervalS int `xml:"rescanIntervalS,attr" json:"rescanIntervalS"`
|
RescanIntervalS int `xml:"rescanIntervalS,attr" json:"rescanIntervalS"`
|
||||||
IgnorePerms bool `xml:"ignorePerms,attr" json:"ignorePerms"`
|
IgnorePerms bool `xml:"ignorePerms,attr" json:"ignorePerms"`
|
||||||
@ -49,11 +49,10 @@ type FolderDeviceConfiguration struct {
|
|||||||
DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
|
DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFolderConfiguration(id, path, foldertype string) FolderConfiguration {
|
func NewFolderConfiguration(id, path string) FolderConfiguration {
|
||||||
f := FolderConfiguration{
|
f := FolderConfiguration{
|
||||||
ID: id,
|
ID: id,
|
||||||
RawPath: path,
|
RawPath: path,
|
||||||
Type: foldertype,
|
|
||||||
}
|
}
|
||||||
f.prepare()
|
f.prepare()
|
||||||
return f
|
return f
|
||||||
|
|||||||
41
lib/config/foldertype.go
Normal file
41
lib/config/foldertype.go
Normal file
@ -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
|
||||||
|
}
|
||||||
@ -104,7 +104,7 @@ type folderFactory func(*Model, config.FolderConfiguration, versioner.Versioner)
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
symlinkWarning = stdsync.Once{}
|
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,
|
// 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]
|
folderFactory, ok := folderFactories[cfg.Type]
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("unknown folder type " + cfg.Type)
|
panic(fmt.Sprintf("unknown folder type 0x%x", cfg.Type))
|
||||||
}
|
}
|
||||||
|
|
||||||
var ver versioner.Versioner
|
var ver versioner.Versioner
|
||||||
|
|||||||
@ -34,7 +34,7 @@ func init() {
|
|||||||
device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
|
device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
|
||||||
device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
|
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}}
|
defaultFolderConfig.Devices = []config.FolderDeviceConfiguration{{DeviceID: device1}}
|
||||||
_defaultConfig := config.Configuration{
|
_defaultConfig := config.Configuration{
|
||||||
Folders: []config.FolderConfiguration{defaultFolderConfig},
|
Folders: []config.FolderConfiguration{defaultFolderConfig},
|
||||||
|
|||||||
@ -27,7 +27,7 @@ func TestOverride(t *testing.T) {
|
|||||||
id, _ := protocol.DeviceIDFromString(id1)
|
id, _ := protocol.DeviceIDFromString(id1)
|
||||||
cfg, _ := config.Load("h1/config.xml", id)
|
cfg, _ := config.Load("h1/config.xml", id)
|
||||||
fld := cfg.Folders()["default"]
|
fld := cfg.Folders()["default"]
|
||||||
fld.ReadOnly = true
|
fld.Type = config.FolderTypeReadOnly
|
||||||
cfg.SetFolder(fld)
|
cfg.SetFolder(fld)
|
||||||
os.Rename("h1/config.xml", "h1/config.xml.orig")
|
os.Rename("h1/config.xml", "h1/config.xml.orig")
|
||||||
defer osutil.Rename("h1/config.xml.orig", "h1/config.xml")
|
defer osutil.Rename("h1/config.xml.orig", "h1/config.xml")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user