Refactor node ID handling, use check digits (fixes #269)

New node ID:s contain four Luhn check digits and are grouped
differently. Code uses NodeID type instead of string, so it's formatted
homogenously everywhere.
This commit is contained in:
Jakob Borg
2014-06-30 01:42:03 +02:00
parent fee8289c0a
commit 8f3effed32
35 changed files with 563 additions and 478 deletions

View File

@@ -14,10 +14,10 @@ import (
"regexp"
"sort"
"strconv"
"strings"
"code.google.com/p/go.crypto/bcrypt"
"github.com/calmh/syncthing/logger"
"github.com/calmh/syncthing/protocol"
"github.com/calmh/syncthing/scanner"
)
@@ -69,7 +69,7 @@ type RepositoryConfiguration struct {
Versioning VersioningConfiguration `xml:"versioning"`
SyncOrderPatterns []SyncOrderPattern `xml:"syncorder>pattern"`
nodeIDs []string
nodeIDs []protocol.NodeID
}
type VersioningConfiguration struct {
@@ -113,7 +113,7 @@ func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartEl
return nil
}
func (r *RepositoryConfiguration) NodeIDs() []string {
func (r *RepositoryConfiguration) NodeIDs() []protocol.NodeID {
if r.nodeIDs == nil {
for _, n := range r.Nodes {
r.nodeIDs = append(r.nodeIDs, n.NodeID)
@@ -138,9 +138,9 @@ func (r RepositoryConfiguration) FileRanker() func(scanner.File) int {
}
type NodeConfiguration struct {
NodeID string `xml:"id,attr"`
Name string `xml:"name,attr,omitempty"`
Addresses []string `xml:"address,omitempty"`
NodeID protocol.NodeID `xml:"id,attr"`
Name string `xml:"name,attr,omitempty"`
Addresses []string `xml:"address,omitempty"`
}
type OptionsConfiguration struct {
@@ -174,8 +174,8 @@ type GUIConfiguration struct {
APIKey string `xml:"apikey,omitempty"`
}
func (cfg *Configuration) NodeMap() map[string]NodeConfiguration {
m := make(map[string]NodeConfiguration, len(cfg.Nodes))
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
}
@@ -276,7 +276,7 @@ func uniqueStrings(ss []string) []string {
return us
}
func Load(rd io.Reader, myID string) (Configuration, error) {
func Load(rd io.Reader, myID protocol.NodeID) (Configuration, error) {
var cfg Configuration
setDefaults(&cfg)
@@ -297,15 +297,6 @@ func Load(rd io.Reader, myID string) (Configuration, error) {
cfg.Repositories = []RepositoryConfiguration{}
}
// Sanitize node IDs
for i := range cfg.Nodes {
node := &cfg.Nodes[i]
// Strip spaces and dashes
node.NodeID = strings.Replace(node.NodeID, "-", "", -1)
node.NodeID = strings.Replace(node.NodeID, " ", "", -1)
node.NodeID = strings.ToUpper(node.NodeID)
}
// Check for missing, bad or duplicate repository ID:s
var seenRepos = map[string]*RepositoryConfiguration{}
var uniqueCounter int
@@ -321,13 +312,6 @@ func Load(rd io.Reader, myID string) (Configuration, error) {
repo.ID = "default"
}
for i := range repo.Nodes {
node := &repo.Nodes[i]
// Strip spaces and dashes
node.NodeID = strings.Replace(node.NodeID, "-", "", -1)
node.NodeID = strings.Replace(node.NodeID, " ", "", -1)
}
if seen, ok := seenRepos[repo.ID]; ok {
l.Warnf("Multiple repositories with ID %q; disabling", repo.ID)
@@ -390,8 +374,9 @@ func convertV1V2(cfg *Configuration) {
for i, repo := range cfg.Repositories {
cfg.Repositories[i].ReadOnly = cfg.Options.Deprecated_ReadOnly
for j, node := range repo.Nodes {
if _, ok := nodes[node.NodeID]; !ok {
nodes[node.NodeID] = node
id := node.NodeID.String()
if _, ok := nodes[id]; !ok {
nodes[id] = node
}
cfg.Repositories[i].Nodes[j] = NodeConfiguration{NodeID: node.NodeID}
}
@@ -416,7 +401,7 @@ func convertV1V2(cfg *Configuration) {
type NodeConfigurationList []NodeConfiguration
func (l NodeConfigurationList) Less(a, b int) bool {
return l[a].NodeID < l[b].NodeID
return l[a].NodeID.Compare(l[b].NodeID) == -1
}
func (l NodeConfigurationList) Swap(a, b int) {
l[a], l[b] = l[b], l[a]
@@ -425,10 +410,10 @@ func (l NodeConfigurationList) Len() int {
return len(l)
}
func ensureNodePresent(nodes []NodeConfiguration, myID string) []NodeConfiguration {
func ensureNodePresent(nodes []NodeConfiguration, myID protocol.NodeID) []NodeConfiguration {
var myIDExists bool
for _, node := range nodes {
if node.NodeID == myID {
if node.NodeID.Equals(myID) {
myIDExists = true
break
}