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) 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, // Folders returns a map of folders. Folder structures should not be changed,
// other than for the purpose of updating via SetFolder(). // other than for the purpose of updating via SetFolder().
func (w *Wrapper) Folders() map[string]FolderConfiguration { 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++ { for i := 0; i < len(folderCfg.Devices); i++ {
if folderCfg.Devices[i].IntroducedBy == introducerCfg.DeviceID { if folderCfg.Devices[i].IntroducedBy == introducerCfg.DeviceID {
if !foldersDevices.has(folderCfg.Devices[i].DeviceID, folderCfg.ID) { 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. // 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) 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:]...) 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. // Check if we should remove some devices, if the introducer no longer
// Yet do not remove if we share other folders that haven't been introduced by the introducer. // shares any folder with them. Yet do not remove if we share other
raw := m.cfg.RawCopy() // folders that haven't been introduced by the introducer.
deviceChanged := false for _, device := range m.cfg.Devices() {
for i := 0; i < len(raw.Devices); i++ { if device.IntroducedBy == introducerCfg.DeviceID {
if raw.Devices[i].IntroducedBy == introducerCfg.DeviceID { if !foldersDevices.hasDevice(device.DeviceID) {
if !foldersDevices.hasDevice(raw.Devices[i].DeviceID) { if foldersIntroducedByOthers.hasDevice(device.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.", device.DeviceID, device.IntroducedBy)
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)
continue continue
} }
// The introducer no longer shares any folder with the device, remove the device. // The introducer no longer shares any folder with the
l.Infof("Removing device %v as introducer %v no longer shares any folders with that device", raw.Devices[i].DeviceID, raw.Devices[i].IntroducedBy) // device, remove the device.
raw.Devices = append(raw.Devices[:i], raw.Devices[i+1:]...) l.Infof("Removing device %v as introducer %v no longer shares any folders with that device", device.DeviceID, device.IntroducedBy)
i-- m.cfg.RemoveDevice(device.DeviceID)
deviceChanged = true 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 return changed
} }
func (m *Model) introduceDevice(device protocol.Device, introducerCfg config.DeviceConfiguration) { func (m *Model) introduceDevice(device protocol.Device, introducerCfg config.DeviceConfiguration) {