lib/model: Minor cleanup to not fondle cfg.Raw things in handleDeintroductions

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3739
This commit is contained in:
Jakob Borg 2016-11-17 08:56:55 +00:00 committed by Audrius Butkevicius
parent faee1d5a8d
commit a8a0bc356a
2 changed files with 36 additions and 24 deletions

View File

@ -209,6 +209,27 @@ func (w *Wrapper) SetDevice(dev DeviceConfiguration) error {
return w.replaceLocked(newCfg)
}
// RemoveDevice removes the device from the configuration
func (w *Wrapper) RemoveDevice(id protocol.DeviceID) error {
w.mut.Lock()
defer w.mut.Unlock()
newCfg := w.cfg.Copy()
removed := false
for i := range newCfg.Devices {
if newCfg.Devices[i].DeviceID == id {
newCfg.Devices = append(newCfg.Devices[:i], newCfg.Devices[i+1:]...)
removed = true
break
}
}
if !removed {
return nil
}
return w.replaceLocked(newCfg)
}
// Folders returns a map of folders. Folder structures should not be changed,
// other than for the purpose of updating via SetFolder().
func (w *Wrapper) Folders() map[string]FolderConfiguration {

View File

@ -949,7 +949,8 @@ func (m *Model) handleDeintroductions(introducerCfg config.DeviceConfiguration,
for i := 0; i < len(folderCfg.Devices); i++ {
if folderCfg.Devices[i].IntroducedBy == introducerCfg.DeviceID {
if !foldersDevices.has(folderCfg.Devices[i].DeviceID, folderCfg.ID) {
// We could not find that folder shared on the introducer with the device that was introduced to us.
// We could not find that folder shared on the
// introducer with the device that was introduced to us.
// We should follow and unshare aswell.
l.Infof("Unsharing folder %q with %v as introducer %v no longer shares the folder with that device", folderCfg.ID, folderCfg.Devices[i].DeviceID, folderCfg.Devices[i].IntroducedBy)
folderCfg.Devices = append(folderCfg.Devices[:i], folderCfg.Devices[i+1:]...)
@ -968,36 +969,26 @@ func (m *Model) handleDeintroductions(introducerCfg config.DeviceConfiguration,
}
}
// Check if we should remove some devices, if the introducer no longer shares any folder with them.
// Yet do not remove if we share other folders that haven't been introduced by the introducer.
raw := m.cfg.RawCopy()
deviceChanged := false
for i := 0; i < len(raw.Devices); i++ {
if raw.Devices[i].IntroducedBy == introducerCfg.DeviceID {
if !foldersDevices.hasDevice(raw.Devices[i].DeviceID) {
if foldersIntroducedByOthers.hasDevice(raw.Devices[i].DeviceID) {
l.Infof("Would have removed %v as %v no longer shares any folders, yet there are other folders that are shared with this device that haven't been introduced by this introducer.", raw.Devices[i].DeviceID, raw.Devices[i].IntroducedBy)
// Check if we should remove some devices, if the introducer no longer
// shares any folder with them. Yet do not remove if we share other
// folders that haven't been introduced by the introducer.
for _, device := range m.cfg.Devices() {
if device.IntroducedBy == introducerCfg.DeviceID {
if !foldersDevices.hasDevice(device.DeviceID) {
if foldersIntroducedByOthers.hasDevice(device.DeviceID) {
l.Infof("Would have removed %v as %v no longer shares any folders, yet there are other folders that are shared with this device that haven't been introduced by this introducer.", device.DeviceID, device.IntroducedBy)
continue
}
// The introducer no longer shares any folder with the device, remove the device.
l.Infof("Removing device %v as introducer %v no longer shares any folders with that device", raw.Devices[i].DeviceID, raw.Devices[i].IntroducedBy)
raw.Devices = append(raw.Devices[:i], raw.Devices[i+1:]...)
i--
deviceChanged = true
// The introducer no longer shares any folder with the
// device, remove the device.
l.Infof("Removing device %v as introducer %v no longer shares any folders with that device", device.DeviceID, device.IntroducedBy)
m.cfg.RemoveDevice(device.DeviceID)
changed = true
}
}
}
// We've removed a device, replace the config.
if deviceChanged {
if err := m.cfg.Replace(raw); err != nil {
l.Warnln("Failed to save config", err)
}
changed = true
}
return changed
}
func (m *Model) introduceDevice(device protocol.Device, introducerCfg config.DeviceConfiguration) {