diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index d5d94bbe7..39d8fa3f6 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -795,7 +795,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) { ldb.ConvertSymlinkTypes() } - m := model.NewModel(cfg, myID, myDeviceName(cfg), "syncthing", Version, ldb, protectedFiles) + m := model.NewModel(cfg, myID, "syncthing", Version, ldb, protectedFiles) if t := os.Getenv("STDEADLOCKTIMEOUT"); len(t) > 0 { it, err := strconv.Atoi(t) @@ -964,15 +964,6 @@ func syncthingMain(runtimeOptions RuntimeOptions) { os.Exit(code) } -func myDeviceName(cfg *config.Wrapper) string { - devices := cfg.Devices() - myName := devices[myID].Name - if myName == "" { - myName, _ = os.Hostname() - } - return myName -} - func setupSignalHandling() { // Exit cleanly with "restarting" code on SIGHUP. diff --git a/lib/config/config.go b/lib/config/config.go index e31af4a03..6d10bc037 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -157,7 +157,8 @@ type Configuration struct { IgnoredDevices []protocol.DeviceID `xml:"ignoredDevice" json:"ignoredDevices"` XMLName xml.Name `xml:"configuration" json:"-"` - OriginalVersion int `xml:"-" json:"-"` // The version we read from disk, before any conversion + MyID protocol.DeviceID `xml:"-" json:"-"` // Provided by the instantiator. + OriginalVersion int `xml:"-" json:"-"` // The version we read from disk, before any conversion } func (cfg Configuration) Copy() Configuration { @@ -198,6 +199,8 @@ func (cfg *Configuration) WriteXML(w io.Writer) error { func (cfg *Configuration) prepare(myID protocol.DeviceID) error { var myName string + cfg.MyID = myID + // Ensure this device is present in the config for _, device := range cfg.Devices { if device.DeviceID == myID { diff --git a/lib/config/wrapper.go b/lib/config/wrapper.go index 82553b460..eeb70e69f 100644 --- a/lib/config/wrapper.go +++ b/lib/config/wrapper.go @@ -431,3 +431,11 @@ func (w *Wrapper) StunServers() []string { return addresses } + +func (w *Wrapper) MyName() string { + w.mut.Lock() + myID := w.cfg.MyID + w.mut.Unlock() + cfg, _ := w.Device(myID) + return cfg.Name +} diff --git a/lib/model/model.go b/lib/model/model.go index dfc7a40c3..027f503af 100644 --- a/lib/model/model.go +++ b/lib/model/model.go @@ -77,7 +77,6 @@ type Model struct { cacheIgnoredFiles bool protectedFiles []string - deviceName string clientName string clientVersion string @@ -123,7 +122,7 @@ var ( // NewModel creates and starts a new model. The model starts in read-only mode, // where it sends index information to connected peers and responds to requests // for file data without altering the local folder in any way. -func NewModel(cfg *config.Wrapper, id protocol.DeviceID, deviceName, clientName, clientVersion string, ldb *db.Instance, protectedFiles []string) *Model { +func NewModel(cfg *config.Wrapper, id protocol.DeviceID, clientName, clientVersion string, ldb *db.Instance, protectedFiles []string) *Model { m := &Model{ Supervisor: suture.New("model", suture.Spec{ Log: func(line string) { @@ -138,7 +137,6 @@ func NewModel(cfg *config.Wrapper, id protocol.DeviceID, deviceName, clientName, shortID: id.Short(), cacheIgnoredFiles: cfg.Options().CacheIgnoredFiles, protectedFiles: protectedFiles, - deviceName: deviceName, clientName: clientName, clientVersion: clientVersion, folderCfgs: make(map[string]config.FolderConfiguration), @@ -1320,9 +1318,13 @@ func (m *Model) OnHello(remoteID protocol.DeviceID, addr net.Addr, hello protoco } // GetHello is called when we are about to connect to some remote device. -func (m *Model) GetHello(protocol.DeviceID) protocol.HelloIntf { +func (m *Model) GetHello(id protocol.DeviceID) protocol.HelloIntf { + name := "" + if _, ok := m.cfg.Device(id); ok { + name = m.cfg.MyName() + } return &protocol.Hello{ - DeviceName: m.deviceName, + DeviceName: name, ClientName: m.clientName, ClientVersion: m.clientVersion, } diff --git a/lib/model/model_test.go b/lib/model/model_test.go index e6fe48ab1..1c44ff172 100644 --- a/lib/model/model_test.go +++ b/lib/model/model_test.go @@ -88,7 +88,7 @@ func init() { func TestRequest(t *testing.T) { db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) // device1 shares default, but device2 doesn't m.AddFolder(defaultFolderConfig) @@ -166,7 +166,7 @@ func BenchmarkIndex_100(b *testing.B) { func benchmarkIndex(b *testing.B, nfiles int) { db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) m.StartFolder("default") m.ServeBackground() @@ -196,7 +196,7 @@ func BenchmarkIndexUpdate_10000_1(b *testing.B) { func benchmarkIndexUpdate(b *testing.B, nfiles, nufiles int) { db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) m.StartFolder("default") m.ServeBackground() @@ -356,7 +356,7 @@ func (f *fakeConnection) sendIndexUpdate() { func BenchmarkRequestOut(b *testing.B) { db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) m.ServeBackground() defer m.Stop() @@ -386,7 +386,7 @@ func BenchmarkRequestOut(b *testing.B) { func BenchmarkRequestInSingleFile(b *testing.B) { db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) m.ServeBackground() defer m.Stop() @@ -426,7 +426,7 @@ func TestDeviceRename(t *testing.T) { cfg := config.Wrap("tmpconfig.xml", rawCfg) db := db.OpenMemory() - m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(cfg, protocol.LocalDeviceID, "syncthing", "dev", db, nil) if cfg.Devices()[device1].Name != "" { t.Errorf("Device already has a name") @@ -512,7 +512,7 @@ func TestClusterConfig(t *testing.T) { db := db.OpenMemory() - m := NewModel(config.Wrap("/tmp/test", cfg), protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(config.Wrap("/tmp/test", cfg), protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(cfg.Folders[0]) m.AddFolder(cfg.Folders[1]) m.ServeBackground() @@ -586,7 +586,7 @@ func TestIntroducer(t *testing.T) { wcfg := config.Wrap("/tmp/test", cfg) - m := NewModel(wcfg, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(wcfg, protocol.LocalDeviceID, "syncthing", "dev", db, nil) for _, folder := range cfg.Folders { m.AddFolder(folder) } @@ -1002,7 +1002,7 @@ func TestIgnores(t *testing.T) { ioutil.WriteFile("testdata/.stignore", []byte(".*\nquux\n"), 0644) db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.ServeBackground() defer m.Stop() @@ -1073,7 +1073,7 @@ func TestROScanRecovery(t *testing.T) { os.RemoveAll(fcfg.RawPath) - m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", ldb, nil) + m := NewModel(cfg, protocol.LocalDeviceID, "syncthing", "dev", ldb, nil) m.AddFolder(fcfg) m.StartFolder("default") m.ServeBackground() @@ -1160,7 +1160,7 @@ func TestRWScanRecovery(t *testing.T) { os.RemoveAll(fcfg.RawPath) - m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", ldb, nil) + m := NewModel(cfg, protocol.LocalDeviceID, "syncthing", "dev", ldb, nil) m.AddFolder(fcfg) m.StartFolder("default") m.ServeBackground() @@ -1225,7 +1225,7 @@ func TestRWScanRecovery(t *testing.T) { func TestGlobalDirectoryTree(t *testing.T) { db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) m.ServeBackground() defer m.Stop() @@ -1477,7 +1477,7 @@ func TestGlobalDirectoryTree(t *testing.T) { func TestGlobalDirectorySelfFixing(t *testing.T) { db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) m.ServeBackground() @@ -1652,7 +1652,7 @@ func BenchmarkTree_100_10(b *testing.B) { func benchmarkTree(b *testing.B, n1, n2 int) { db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) m.ServeBackground() @@ -1787,7 +1787,7 @@ func TestIssue3028(t *testing.T) { // Create a model and default folder db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) defCfg := defaultFolderConfig.Copy() defCfg.RescanIntervalS = 86400 m.AddFolder(defCfg) @@ -1865,7 +1865,7 @@ func TestScanNoDatabaseWrite(t *testing.T) { // something actually changed. db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) m.StartFolder("default") m.ServeBackground() @@ -1949,7 +1949,7 @@ func TestIssue2782(t *testing.T) { defer os.RemoveAll(testDir) db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(config.NewFolderConfiguration("default", "~/"+testName+"/synclink/")) m.StartFolder("default") m.ServeBackground() @@ -1975,7 +1975,7 @@ func TestIndexesForUnknownDevicesDropped(t *testing.T) { t.Error("expected two devices") } - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", dbi, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil) m.AddFolder(defaultFolderConfig) m.StartFolder("default") @@ -2009,7 +2009,7 @@ func TestSharedWithClearedOnDisconnect(t *testing.T) { wcfg := config.Wrap("/tmp/test", cfg) - m := NewModel(wcfg, protocol.LocalDeviceID, "device", "syncthing", "dev", dbi, nil) + m := NewModel(wcfg, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil) m.AddFolder(fcfg) m.StartFolder(fcfg.ID) m.ServeBackground() @@ -2123,7 +2123,7 @@ func TestIssue3496(t *testing.T) { // checks on the completion calculation stuff. dbi := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", dbi, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil) m.AddFolder(defaultFolderConfig) m.StartFolder("default") m.ServeBackground() @@ -2196,7 +2196,7 @@ func TestIssue3496(t *testing.T) { func TestIssue3804(t *testing.T) { dbi := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", dbi, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil) m.AddFolder(defaultFolderConfig) m.StartFolder("default") m.ServeBackground() @@ -2211,7 +2211,7 @@ func TestIssue3804(t *testing.T) { func TestIssue3829(t *testing.T) { dbi := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", dbi, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil) m.AddFolder(defaultFolderConfig) m.StartFolder("default") m.ServeBackground() @@ -2248,7 +2248,7 @@ func TestNoRequestsFromPausedDevices(t *testing.T) { wcfg := config.Wrap("/tmp/test", cfg) - m := NewModel(wcfg, protocol.LocalDeviceID, "device", "syncthing", "dev", dbi, nil) + m := NewModel(wcfg, protocol.LocalDeviceID, "syncthing", "dev", dbi, nil) m.AddFolder(fcfg) m.StartFolder(fcfg.ID) m.ServeBackground() diff --git a/lib/model/requests_test.go b/lib/model/requests_test.go index 6bbb3c93e..20749d78f 100644 --- a/lib/model/requests_test.go +++ b/lib/model/requests_test.go @@ -215,7 +215,7 @@ func setupModelWithConnection() (*Model, *fakeConnection) { w := config.Wrap("/tmp/cfg", cfg) db := db.OpenMemory() - m := NewModel(w, device1, "device", "syncthing", "dev", db, nil) + m := NewModel(w, device1, "syncthing", "dev", db, nil) m.AddFolder(cfg.Folders[0]) m.ServeBackground() m.StartFolder("default") diff --git a/lib/model/rwfolder_test.go b/lib/model/rwfolder_test.go index 10ef435db..10b48795e 100644 --- a/lib/model/rwfolder_test.go +++ b/lib/model/rwfolder_test.go @@ -71,7 +71,7 @@ func setUpFile(filename string, blockNumbers []int) protocol.FileInfo { func setUpModel(file protocol.FileInfo) *Model { db := db.OpenMemory() - model := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + model := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) model.AddFolder(defaultFolderConfig) // Update index model.updateLocalsFromScanning("default", []protocol.FileInfo{file}) @@ -476,7 +476,7 @@ func TestDeregisterOnFailInCopy(t *testing.T) { db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) f := setUpSendReceiveFolder(m) @@ -549,7 +549,7 @@ func TestDeregisterOnFailInPull(t *testing.T) { defer os.Remove("testdata/" + ignore.TempName("filex")) db := db.OpenMemory() - m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil) + m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil) m.AddFolder(defaultFolderConfig) f := setUpSendReceiveFolder(m)