Rename Repository -> Folder, Node -> Device (fixes #739)
This commit is contained in:
+141
-141
@@ -25,24 +25,24 @@ var l = logger.DefaultLogger
|
||||
type Configuration struct {
|
||||
Location string `xml:"-" json:"-"`
|
||||
Version int `xml:"version,attr" default:"3"`
|
||||
Repositories []RepositoryConfiguration `xml:"repository"`
|
||||
Nodes []NodeConfiguration `xml:"node"`
|
||||
Folders []FolderConfiguration `xml:"folder"`
|
||||
Devices []DeviceConfiguration `xml:"device"`
|
||||
GUI GUIConfiguration `xml:"gui"`
|
||||
Options OptionsConfiguration `xml:"options"`
|
||||
XMLName xml.Name `xml:"configuration" json:"-"`
|
||||
}
|
||||
|
||||
type RepositoryConfiguration struct {
|
||||
type FolderConfiguration struct {
|
||||
ID string `xml:"id,attr"`
|
||||
Directory string `xml:"directory,attr"`
|
||||
Nodes []RepositoryNodeConfiguration `xml:"node"`
|
||||
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"`
|
||||
|
||||
nodeIDs []protocol.NodeID
|
||||
deviceIDs []protocol.DeviceID
|
||||
}
|
||||
|
||||
type VersioningConfiguration struct {
|
||||
@@ -86,17 +86,17 @@ func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartEl
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RepositoryConfiguration) NodeIDs() []protocol.NodeID {
|
||||
if r.nodeIDs == nil {
|
||||
for _, n := range r.Nodes {
|
||||
r.nodeIDs = append(r.nodeIDs, n.NodeID)
|
||||
func (r *FolderConfiguration) DeviceIDs() []protocol.DeviceID {
|
||||
if r.deviceIDs == nil {
|
||||
for _, n := range r.Devices {
|
||||
r.deviceIDs = append(r.deviceIDs, n.DeviceID)
|
||||
}
|
||||
}
|
||||
return r.nodeIDs
|
||||
return r.deviceIDs
|
||||
}
|
||||
|
||||
type NodeConfiguration struct {
|
||||
NodeID protocol.NodeID `xml:"id,attr"`
|
||||
type DeviceConfiguration struct {
|
||||
DeviceID protocol.DeviceID `xml:"id,attr"`
|
||||
Name string `xml:"name,attr,omitempty"`
|
||||
Addresses []string `xml:"address,omitempty"`
|
||||
Compression bool `xml:"compression,attr"`
|
||||
@@ -104,8 +104,8 @@ type NodeConfiguration struct {
|
||||
Introducer bool `xml:"introducer,attr"`
|
||||
}
|
||||
|
||||
type RepositoryNodeConfiguration struct {
|
||||
NodeID protocol.NodeID `xml:"id,attr"`
|
||||
type FolderDeviceConfiguration struct {
|
||||
DeviceID protocol.DeviceID `xml:"id,attr"`
|
||||
|
||||
Deprecated_Name string `xml:"name,attr,omitempty" json:"-"`
|
||||
Deprecated_Addresses []string `xml:"address,omitempty" json:"-"`
|
||||
@@ -145,35 +145,35 @@ type GUIConfiguration struct {
|
||||
APIKey string `xml:"apikey,omitempty"`
|
||||
}
|
||||
|
||||
func (cfg *Configuration) NodeMap() map[protocol.NodeID]NodeConfiguration {
|
||||
m := make(map[protocol.NodeID]NodeConfiguration, len(cfg.Nodes))
|
||||
for _, n := range cfg.Nodes {
|
||||
m[n.NodeID] = n
|
||||
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
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (cfg *Configuration) GetNodeConfiguration(nodeID protocol.NodeID) *NodeConfiguration {
|
||||
for i, node := range cfg.Nodes {
|
||||
if node.NodeID == nodeID {
|
||||
return &cfg.Nodes[i]
|
||||
func (cfg *Configuration) GetDeviceConfiguration(deviceID protocol.DeviceID) *DeviceConfiguration {
|
||||
for i, device := range cfg.Devices {
|
||||
if device.DeviceID == deviceID {
|
||||
return &cfg.Devices[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *Configuration) GetRepoConfiguration(repoID string) *RepositoryConfiguration {
|
||||
for i, repo := range cfg.Repositories {
|
||||
if repo.ID == repoID {
|
||||
return &cfg.Repositories[i]
|
||||
func (cfg *Configuration) GetFolderConfiguration(folderID string) *FolderConfiguration {
|
||||
for i, folder := range cfg.Folders {
|
||||
if folder.ID == folderID {
|
||||
return &cfg.Folders[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *Configuration) RepoMap() map[string]RepositoryConfiguration {
|
||||
m := make(map[string]RepositoryConfiguration, len(cfg.Repositories))
|
||||
for _, r := range cfg.Repositories {
|
||||
func (cfg *Configuration) FolderMap() map[string]FolderConfiguration {
|
||||
m := make(map[string]FolderConfiguration, len(cfg.Folders))
|
||||
for _, r := range cfg.Folders {
|
||||
m[r.ID] = r
|
||||
}
|
||||
return m
|
||||
@@ -290,44 +290,44 @@ func uniqueStrings(ss []string) []string {
|
||||
return us
|
||||
}
|
||||
|
||||
func (cfg *Configuration) prepare(myID protocol.NodeID) {
|
||||
func (cfg *Configuration) prepare(myID protocol.DeviceID) {
|
||||
fillNilSlices(&cfg.Options)
|
||||
|
||||
cfg.Options.ListenAddress = uniqueStrings(cfg.Options.ListenAddress)
|
||||
|
||||
// Initialize an empty slice for repositories if the config has none
|
||||
if cfg.Repositories == nil {
|
||||
cfg.Repositories = []RepositoryConfiguration{}
|
||||
// Initialize an empty slice for folders if the config has none
|
||||
if cfg.Folders == nil {
|
||||
cfg.Folders = []FolderConfiguration{}
|
||||
}
|
||||
|
||||
// Check for missing, bad or duplicate repository ID:s
|
||||
var seenRepos = map[string]*RepositoryConfiguration{}
|
||||
// Check for missing, bad or duplicate folder ID:s
|
||||
var seenFolders = map[string]*FolderConfiguration{}
|
||||
var uniqueCounter int
|
||||
for i := range cfg.Repositories {
|
||||
repo := &cfg.Repositories[i]
|
||||
for i := range cfg.Folders {
|
||||
folder := &cfg.Folders[i]
|
||||
|
||||
if len(repo.Directory) == 0 {
|
||||
repo.Invalid = "no directory configured"
|
||||
if len(folder.Directory) == 0 {
|
||||
folder.Invalid = "no directory configured"
|
||||
continue
|
||||
}
|
||||
|
||||
if repo.ID == "" {
|
||||
repo.ID = "default"
|
||||
if folder.ID == "" {
|
||||
folder.ID = "default"
|
||||
}
|
||||
|
||||
if seen, ok := seenRepos[repo.ID]; ok {
|
||||
l.Warnf("Multiple repositories with ID %q; disabling", repo.ID)
|
||||
if seen, ok := seenFolders[folder.ID]; ok {
|
||||
l.Warnf("Multiple folders with ID %q; disabling", folder.ID)
|
||||
|
||||
seen.Invalid = "duplicate repository ID"
|
||||
if seen.ID == repo.ID {
|
||||
seen.Invalid = "duplicate folder ID"
|
||||
if seen.ID == folder.ID {
|
||||
uniqueCounter++
|
||||
seen.ID = fmt.Sprintf("%s~%d", repo.ID, uniqueCounter)
|
||||
seen.ID = fmt.Sprintf("%s~%d", folder.ID, uniqueCounter)
|
||||
}
|
||||
repo.Invalid = "duplicate repository ID"
|
||||
folder.Invalid = "duplicate folder ID"
|
||||
uniqueCounter++
|
||||
repo.ID = fmt.Sprintf("%s~%d", repo.ID, uniqueCounter)
|
||||
folder.ID = fmt.Sprintf("%s~%d", folder.ID, uniqueCounter)
|
||||
} else {
|
||||
seenRepos[repo.ID] = repo
|
||||
seenFolders[folder.ID] = folder
|
||||
}
|
||||
}
|
||||
|
||||
@@ -362,42 +362,42 @@ func (cfg *Configuration) prepare(myID protocol.NodeID) {
|
||||
}
|
||||
}
|
||||
|
||||
// Build a list of available nodes
|
||||
existingNodes := make(map[protocol.NodeID]bool)
|
||||
existingNodes[myID] = true
|
||||
for _, node := range cfg.Nodes {
|
||||
existingNodes[node.NodeID] = true
|
||||
// Build a list of available devices
|
||||
existingDevices := make(map[protocol.DeviceID]bool)
|
||||
existingDevices[myID] = true
|
||||
for _, device := range cfg.Devices {
|
||||
existingDevices[device.DeviceID] = true
|
||||
}
|
||||
|
||||
// Ensure this node is present in all relevant places
|
||||
me := cfg.GetNodeConfiguration(myID)
|
||||
// Ensure this device is present in all relevant places
|
||||
me := cfg.GetDeviceConfiguration(myID)
|
||||
if me == nil {
|
||||
myName, _ := os.Hostname()
|
||||
cfg.Nodes = append(cfg.Nodes, NodeConfiguration{
|
||||
NodeID: myID,
|
||||
cfg.Devices = append(cfg.Devices, DeviceConfiguration{
|
||||
DeviceID: myID,
|
||||
Name: myName,
|
||||
})
|
||||
}
|
||||
sort.Sort(NodeConfigurationList(cfg.Nodes))
|
||||
// Ensure that any loose nodes are not present in the wrong places
|
||||
// Ensure that there are no duplicate nodes
|
||||
for i := range cfg.Repositories {
|
||||
cfg.Repositories[i].Nodes = ensureNodePresent(cfg.Repositories[i].Nodes, myID)
|
||||
cfg.Repositories[i].Nodes = ensureExistingNodes(cfg.Repositories[i].Nodes, existingNodes)
|
||||
cfg.Repositories[i].Nodes = ensureNoDuplicates(cfg.Repositories[i].Nodes)
|
||||
sort.Sort(RepositoryNodeConfigurationList(cfg.Repositories[i].Nodes))
|
||||
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))
|
||||
}
|
||||
|
||||
// An empty address list is equivalent to a single "dynamic" entry
|
||||
for i := range cfg.Nodes {
|
||||
n := &cfg.Nodes[i]
|
||||
for i := range cfg.Devices {
|
||||
n := &cfg.Devices[i]
|
||||
if len(n.Addresses) == 0 || len(n.Addresses) == 1 && n.Addresses[0] == "" {
|
||||
n.Addresses = []string{"dynamic"}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func New(location string, myID protocol.NodeID) Configuration {
|
||||
func New(location string, myID protocol.DeviceID) Configuration {
|
||||
var cfg Configuration
|
||||
|
||||
cfg.Location = location
|
||||
@@ -411,7 +411,7 @@ func New(location string, myID protocol.NodeID) Configuration {
|
||||
return cfg
|
||||
}
|
||||
|
||||
func Load(location string, myID protocol.NodeID) (Configuration, error) {
|
||||
func Load(location string, myID protocol.DeviceID) (Configuration, error) {
|
||||
var cfg Configuration
|
||||
|
||||
cfg.Location = location
|
||||
@@ -435,24 +435,24 @@ func Load(location string, myID protocol.NodeID) (Configuration, error) {
|
||||
// ChangeRequiresRestart returns true if updating the configuration requires a
|
||||
// complete restart.
|
||||
func ChangeRequiresRestart(from, to Configuration) bool {
|
||||
// Adding, removing or changing repos requires restart
|
||||
if len(from.Repositories) != len(to.Repositories) {
|
||||
// Adding, removing or changing folders requires restart
|
||||
if len(from.Folders) != len(to.Folders) {
|
||||
return true
|
||||
}
|
||||
fromRepos := from.RepoMap()
|
||||
toRepos := to.RepoMap()
|
||||
for id := range fromRepos {
|
||||
if !reflect.DeepEqual(fromRepos[id], toRepos[id]) {
|
||||
fromFolders := from.FolderMap()
|
||||
toFolders := to.FolderMap()
|
||||
for id := range fromFolders {
|
||||
if !reflect.DeepEqual(fromFolders[id], toFolders[id]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Removing a node requires a restart. Adding one does not. Changing
|
||||
// Removing a device requires a restart. Adding one does not. Changing
|
||||
// address or name does not.
|
||||
fromNodes := from.NodeMap()
|
||||
toNodes := to.NodeMap()
|
||||
for nodeID := range fromNodes {
|
||||
if _, ok := toNodes[nodeID]; !ok {
|
||||
fromDevices := from.DeviceMap()
|
||||
toDevices := to.DeviceMap()
|
||||
for deviceID := range fromDevices {
|
||||
if _, ok := toDevices[deviceID]; !ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -466,22 +466,22 @@ func ChangeRequiresRestart(from, to Configuration) bool {
|
||||
}
|
||||
|
||||
func convertV3V4(cfg *Configuration) {
|
||||
// In previous versions, rescan interval was common for each repository.
|
||||
// In previous versions, rescan interval was common for each folder.
|
||||
// From now, it can be set independently. We have to make sure, that after upgrade
|
||||
// the individual rescan interval will be defined for every existing repository.
|
||||
for i := range cfg.Repositories {
|
||||
cfg.Repositories[i].RescanIntervalS = cfg.Options.Deprecated_RescanIntervalS
|
||||
// the individual rescan interval will be defined for every existing folder.
|
||||
for i := range cfg.Folders {
|
||||
cfg.Folders[i].RescanIntervalS = cfg.Options.Deprecated_RescanIntervalS
|
||||
}
|
||||
|
||||
cfg.Options.Deprecated_RescanIntervalS = 0
|
||||
|
||||
// In previous versions, repositories held full node configurations.
|
||||
// Since that's the only place where node configs were in V1, we still have
|
||||
// In previous versions, folders held full device configurations.
|
||||
// Since that's the only place where device configs were in V1, we still have
|
||||
// to define the deprecated fields to be able to upgrade from V1 to V4.
|
||||
for i, repo := range cfg.Repositories {
|
||||
for i, folder := range cfg.Folders {
|
||||
|
||||
for j := range repo.Nodes {
|
||||
rncfg := cfg.Repositories[i].Nodes[j]
|
||||
for j := range folder.Devices {
|
||||
rncfg := cfg.Folders[i].Devices[j]
|
||||
rncfg.Deprecated_Name = ""
|
||||
rncfg.Deprecated_Addresses = nil
|
||||
}
|
||||
@@ -492,10 +492,10 @@ func convertV3V4(cfg *Configuration) {
|
||||
|
||||
func convertV2V3(cfg *Configuration) {
|
||||
// In previous versions, compression was always on. When upgrading, enable
|
||||
// compression on all existing new. New nodes will get compression on by
|
||||
// compression on all existing new. New devices will get compression on by
|
||||
// default by the GUI.
|
||||
for i := range cfg.Nodes {
|
||||
cfg.Nodes[i].Compression = true
|
||||
for i := range cfg.Devices {
|
||||
cfg.Devices[i].Compression = true
|
||||
}
|
||||
|
||||
// The global discovery format and port number changed in v0.9. Having the
|
||||
@@ -508,31 +508,31 @@ func convertV2V3(cfg *Configuration) {
|
||||
}
|
||||
|
||||
func convertV1V2(cfg *Configuration) {
|
||||
// Collect the list of nodes.
|
||||
// Replace node configs inside repositories with only a reference to the nide ID.
|
||||
// Set all repositories to read only if the global read only flag is set.
|
||||
var nodes = map[string]RepositoryNodeConfiguration{}
|
||||
for i, repo := range cfg.Repositories {
|
||||
cfg.Repositories[i].ReadOnly = cfg.Options.Deprecated_ReadOnly
|
||||
for j, node := range repo.Nodes {
|
||||
id := node.NodeID.String()
|
||||
if _, ok := nodes[id]; !ok {
|
||||
nodes[id] = node
|
||||
// Collect the list of devices.
|
||||
// Replace device configs inside folders with only a reference to the nide ID.
|
||||
// Set all folders to read only if the global read only flag is set.
|
||||
var devices = map[string]FolderDeviceConfiguration{}
|
||||
for i, folder := range cfg.Folders {
|
||||
cfg.Folders[i].ReadOnly = cfg.Options.Deprecated_ReadOnly
|
||||
for j, device := range folder.Devices {
|
||||
id := device.DeviceID.String()
|
||||
if _, ok := devices[id]; !ok {
|
||||
devices[id] = device
|
||||
}
|
||||
cfg.Repositories[i].Nodes[j] = RepositoryNodeConfiguration{NodeID: node.NodeID}
|
||||
cfg.Folders[i].Devices[j] = FolderDeviceConfiguration{DeviceID: device.DeviceID}
|
||||
}
|
||||
}
|
||||
cfg.Options.Deprecated_ReadOnly = false
|
||||
|
||||
// Set and sort the list of nodes.
|
||||
for _, node := range nodes {
|
||||
cfg.Nodes = append(cfg.Nodes, NodeConfiguration{
|
||||
NodeID: node.NodeID,
|
||||
Name: node.Deprecated_Name,
|
||||
Addresses: node.Deprecated_Addresses,
|
||||
// Set and sort the list of devices.
|
||||
for _, device := range devices {
|
||||
cfg.Devices = append(cfg.Devices, DeviceConfiguration{
|
||||
DeviceID: device.DeviceID,
|
||||
Name: device.Deprecated_Name,
|
||||
Addresses: device.Deprecated_Addresses,
|
||||
})
|
||||
}
|
||||
sort.Sort(NodeConfigurationList(cfg.Nodes))
|
||||
sort.Sort(DeviceConfigurationList(cfg.Devices))
|
||||
|
||||
// GUI
|
||||
cfg.GUI.Address = cfg.Options.Deprecated_GUIAddress
|
||||
@@ -543,73 +543,73 @@ func convertV1V2(cfg *Configuration) {
|
||||
cfg.Version = 2
|
||||
}
|
||||
|
||||
type NodeConfigurationList []NodeConfiguration
|
||||
type DeviceConfigurationList []DeviceConfiguration
|
||||
|
||||
func (l NodeConfigurationList) Less(a, b int) bool {
|
||||
return l[a].NodeID.Compare(l[b].NodeID) == -1
|
||||
func (l DeviceConfigurationList) Less(a, b int) bool {
|
||||
return l[a].DeviceID.Compare(l[b].DeviceID) == -1
|
||||
}
|
||||
func (l NodeConfigurationList) Swap(a, b int) {
|
||||
func (l DeviceConfigurationList) Swap(a, b int) {
|
||||
l[a], l[b] = l[b], l[a]
|
||||
}
|
||||
func (l NodeConfigurationList) Len() int {
|
||||
func (l DeviceConfigurationList) Len() int {
|
||||
return len(l)
|
||||
}
|
||||
|
||||
type RepositoryNodeConfigurationList []RepositoryNodeConfiguration
|
||||
type FolderDeviceConfigurationList []FolderDeviceConfiguration
|
||||
|
||||
func (l RepositoryNodeConfigurationList) Less(a, b int) bool {
|
||||
return l[a].NodeID.Compare(l[b].NodeID) == -1
|
||||
func (l FolderDeviceConfigurationList) Less(a, b int) bool {
|
||||
return l[a].DeviceID.Compare(l[b].DeviceID) == -1
|
||||
}
|
||||
func (l RepositoryNodeConfigurationList) Swap(a, b int) {
|
||||
func (l FolderDeviceConfigurationList) Swap(a, b int) {
|
||||
l[a], l[b] = l[b], l[a]
|
||||
}
|
||||
func (l RepositoryNodeConfigurationList) Len() int {
|
||||
func (l FolderDeviceConfigurationList) Len() int {
|
||||
return len(l)
|
||||
}
|
||||
|
||||
func ensureNodePresent(nodes []RepositoryNodeConfiguration, myID protocol.NodeID) []RepositoryNodeConfiguration {
|
||||
for _, node := range nodes {
|
||||
if node.NodeID.Equals(myID) {
|
||||
return nodes
|
||||
func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
|
||||
for _, device := range devices {
|
||||
if device.DeviceID.Equals(myID) {
|
||||
return devices
|
||||
}
|
||||
}
|
||||
|
||||
nodes = append(nodes, RepositoryNodeConfiguration{
|
||||
NodeID: myID,
|
||||
devices = append(devices, FolderDeviceConfiguration{
|
||||
DeviceID: myID,
|
||||
})
|
||||
|
||||
return nodes
|
||||
return devices
|
||||
}
|
||||
|
||||
func ensureExistingNodes(nodes []RepositoryNodeConfiguration, existingNodes map[protocol.NodeID]bool) []RepositoryNodeConfiguration {
|
||||
count := len(nodes)
|
||||
func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
|
||||
count := len(devices)
|
||||
i := 0
|
||||
loop:
|
||||
for i < count {
|
||||
if _, ok := existingNodes[nodes[i].NodeID]; !ok {
|
||||
nodes[i] = nodes[count-1]
|
||||
if _, ok := existingDevices[devices[i].DeviceID]; !ok {
|
||||
devices[i] = devices[count-1]
|
||||
count--
|
||||
continue loop
|
||||
}
|
||||
i++
|
||||
}
|
||||
return nodes[0:count]
|
||||
return devices[0:count]
|
||||
}
|
||||
|
||||
func ensureNoDuplicates(nodes []RepositoryNodeConfiguration) []RepositoryNodeConfiguration {
|
||||
count := len(nodes)
|
||||
func ensureNoDuplicates(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
|
||||
count := len(devices)
|
||||
i := 0
|
||||
seenNodes := make(map[protocol.NodeID]bool)
|
||||
seenDevices := make(map[protocol.DeviceID]bool)
|
||||
loop:
|
||||
for i < count {
|
||||
id := nodes[i].NodeID
|
||||
if _, ok := seenNodes[id]; ok {
|
||||
nodes[i] = nodes[count-1]
|
||||
id := devices[i].DeviceID
|
||||
if _, ok := seenDevices[id]; ok {
|
||||
devices[i] = devices[count-1]
|
||||
count--
|
||||
continue loop
|
||||
}
|
||||
seenNodes[id] = true
|
||||
seenDevices[id] = true
|
||||
i++
|
||||
}
|
||||
return nodes[0:count]
|
||||
return devices[0:count]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user