mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-23 03:18:59 +00:00
e32a516b5f
* lib/db: Add ExpirePendingFolders(). Use-case is to drop any no-longer-pending folders for a specific device when parsing its ClusterConfig message where previously offered folders are not mentioned any more. The timestamp in ObservedFolder is stored with only second precision, so round to seconds here as well. This allows calling the function within the same second of adding or updating entries. * lib/model: Weed out pending folders when receiving ClusterConfig. Filter the entries by timestamp, which must be newer than or equal to the reception time of the ClusterConfig. For just mentioned ones, this assumption will hold as AddOrUpdatePendingFolder() updates the timestamp. * lib/model, gui: Notify when one or more pending folders expired. Introduce new event type FolderOfferCancelled and use it to trigger a complete refreshCluster() cycle. Listing individual entries would be much more code and probably just as much work to answer the API request. * lib/model: Add comment and rename ExpirePendingFolders(). * lib/events: Rename FolderOfferCancelled to ClusterPendingChanged. * lib/model: Reuse ClusterPendingChanged event for cleanPending() Changing the config does not necessarily mean that the /resut/cluster/pending endpoints need to be refreshed, but only if something was actually removed. Detect this and indicate it through the ClusterPendingChanged event, which is already hooked up to requery respective endpoints within the GUI. No more need for a separate refreshCluster() in reaction to ConfigSaved event or calling refreshConfig(). * lib/model: Gofmt. * lib/db: Warn instead of info log for failed removal. * gui: Fix pending notifications not loading on GUI start. * lib/db: Use short device ID in log message. * lib/db: Return list of expired folder IDs after deleting them. * lib/model: Refactor Pending...Changed events. * lib/model: Adjust format of removed pending folders enumeration. Use an array of objects with device / folder ID properties, matching the other places where it's used. * lib/db: Drop invalid entries in RemovePendingFoldersBeforeTime(). * lib/model: Gofmt. My local gofmt did not complain here, strangely... * gui: Handle PendingDevicesChanged event. Even though it currently only holds one device at a time, wrap the contents in an array under the "added" property name. * lib/model: Fix null values in PendingFoldersChanged removed member. * gui: Handle PendingFoldersChanged event. * lib/model: Simplify construction of expiredPendingList. * lib/model: Reduce code duplication in cleanPending(). Use goto and a label for the common parts of calling the DB removal function and building the event data part. * lib/events, gui: Mark ...Rejected events deprecated. Extend comments explaining the conditions when the replacement event types are emitted. * lib/model: Wrap removed devices in array of objects as well. * lib/db: Use iter.Value() instead of needless db.Get(iter.Key()) * lib/db: Add comment explaining RemovePendingFoldersBeforeTime(). * lib/model: Rename fields folderID and deviceID in event data. * lib/db: Only list actually expired IDs as removed. Skip entries where Delete() failed as well as invalid entries that got removed automatically. * lib/model: Gofmt
209 lines
6.3 KiB
Go
209 lines
6.3 KiB
Go
// Copyright (C) 2020 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package db
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
)
|
|
|
|
func (db *Lowlevel) AddOrUpdatePendingDevice(device protocol.DeviceID, name, address string) error {
|
|
key := db.keyer.GeneratePendingDeviceKey(nil, device[:])
|
|
od := ObservedDevice{
|
|
Time: time.Now().Round(time.Second),
|
|
Name: name,
|
|
Address: address,
|
|
}
|
|
bs, err := od.Marshal()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Put(key, bs)
|
|
}
|
|
|
|
func (db *Lowlevel) RemovePendingDevice(device protocol.DeviceID) {
|
|
key := db.keyer.GeneratePendingDeviceKey(nil, device[:])
|
|
if err := db.Delete(key); err != nil {
|
|
l.Warnf("Failed to remove pending device entry: %v", err)
|
|
}
|
|
}
|
|
|
|
// PendingDevices enumerates all entries. Invalid ones are dropped from the database
|
|
// after a warning log message, as a side-effect.
|
|
func (db *Lowlevel) PendingDevices() (map[protocol.DeviceID]ObservedDevice, error) {
|
|
iter, err := db.NewPrefixIterator([]byte{KeyTypePendingDevice})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer iter.Release()
|
|
res := make(map[protocol.DeviceID]ObservedDevice)
|
|
for iter.Next() {
|
|
keyDev := db.keyer.DeviceFromPendingDeviceKey(iter.Key())
|
|
deviceID, err := protocol.DeviceIDFromBytes(keyDev)
|
|
var od ObservedDevice
|
|
if err != nil {
|
|
goto deleteKey
|
|
}
|
|
if err = od.Unmarshal(iter.Value()); err != nil {
|
|
goto deleteKey
|
|
}
|
|
res[deviceID] = od
|
|
continue
|
|
deleteKey:
|
|
// Deleting invalid entries is the only possible "repair" measure and
|
|
// appropriate for the importance of pending entries. They will come back
|
|
// soon if still relevant.
|
|
l.Infof("Invalid pending device entry, deleting from database: %x", iter.Key())
|
|
if err := db.Delete(iter.Key()); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (db *Lowlevel) AddOrUpdatePendingFolder(id, label string, device protocol.DeviceID) error {
|
|
key, err := db.keyer.GeneratePendingFolderKey(nil, device[:], []byte(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
of := ObservedFolder{
|
|
Time: time.Now().Round(time.Second),
|
|
Label: label,
|
|
}
|
|
bs, err := of.Marshal()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Put(key, bs)
|
|
}
|
|
|
|
// RemovePendingFolderForDevice removes entries for specific folder / device combinations.
|
|
func (db *Lowlevel) RemovePendingFolderForDevice(id string, device protocol.DeviceID) {
|
|
key, err := db.keyer.GeneratePendingFolderKey(nil, device[:], []byte(id))
|
|
if err != nil {
|
|
return
|
|
}
|
|
if err := db.Delete(key); err != nil {
|
|
l.Warnf("Failed to remove pending folder entry: %v", err)
|
|
}
|
|
}
|
|
|
|
// RemovePendingFolder removes all entries matching a specific folder ID.
|
|
func (db *Lowlevel) RemovePendingFolder(id string) {
|
|
iter, err := db.NewPrefixIterator([]byte{KeyTypePendingFolder})
|
|
if err != nil {
|
|
l.Infof("Could not iterate through pending folder entries: %v", err)
|
|
return
|
|
}
|
|
defer iter.Release()
|
|
for iter.Next() {
|
|
if id != string(db.keyer.FolderFromPendingFolderKey(iter.Key())) {
|
|
continue
|
|
}
|
|
if err := db.Delete(iter.Key()); err != nil {
|
|
l.Warnf("Failed to remove pending folder entry: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// RemovePendingFoldersBeforeTime removes entries for a specific device which are older
|
|
// than a given timestamp or invalid. It returns only the valid removed folder IDs.
|
|
func (db *Lowlevel) RemovePendingFoldersBeforeTime(device protocol.DeviceID, oldest time.Time) ([]string, error) {
|
|
prefixKey, err := db.keyer.GeneratePendingFolderKey(nil, device[:], nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
iter, err := db.NewPrefixIterator(prefixKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer iter.Release()
|
|
oldest = oldest.Round(time.Second)
|
|
var res []string
|
|
for iter.Next() {
|
|
var of ObservedFolder
|
|
var folderID string
|
|
if err = of.Unmarshal(iter.Value()); err != nil {
|
|
l.Infof("Invalid pending folder entry, deleting from database: %x", iter.Key())
|
|
} else if of.Time.Before(oldest) {
|
|
folderID = string(db.keyer.FolderFromPendingFolderKey(iter.Key()))
|
|
l.Infof("Removing stale pending folder %s (%s) from device %s, last seen %v",
|
|
folderID, of.Label, device.Short(), of.Time)
|
|
} else {
|
|
// Keep entries younger or equal to the given timestamp
|
|
continue
|
|
}
|
|
if err := db.Delete(iter.Key()); err != nil {
|
|
l.Warnf("Failed to remove pending folder entry: %v", err)
|
|
} else if len(folderID) > 0 {
|
|
res = append(res, folderID)
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// Consolidated information about a pending folder
|
|
type PendingFolder struct {
|
|
OfferedBy map[protocol.DeviceID]ObservedFolder `json:"offeredBy"`
|
|
}
|
|
|
|
func (db *Lowlevel) PendingFolders() (map[string]PendingFolder, error) {
|
|
return db.PendingFoldersForDevice(protocol.EmptyDeviceID)
|
|
}
|
|
|
|
// PendingFoldersForDevice enumerates only entries matching the given device ID, unless it
|
|
// is EmptyDeviceID. Invalid ones are dropped from the database after a warning log
|
|
// message, as a side-effect.
|
|
func (db *Lowlevel) PendingFoldersForDevice(device protocol.DeviceID) (map[string]PendingFolder, error) {
|
|
var err error
|
|
prefixKey := []byte{KeyTypePendingFolder}
|
|
if device != protocol.EmptyDeviceID {
|
|
prefixKey, err = db.keyer.GeneratePendingFolderKey(nil, device[:], nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
iter, err := db.NewPrefixIterator(prefixKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer iter.Release()
|
|
res := make(map[string]PendingFolder)
|
|
for iter.Next() {
|
|
keyDev, ok := db.keyer.DeviceFromPendingFolderKey(iter.Key())
|
|
deviceID, err := protocol.DeviceIDFromBytes(keyDev)
|
|
var of ObservedFolder
|
|
var folderID string
|
|
if !ok || err != nil {
|
|
goto deleteKey
|
|
}
|
|
if folderID = string(db.keyer.FolderFromPendingFolderKey(iter.Key())); len(folderID) < 1 {
|
|
goto deleteKey
|
|
}
|
|
if err = of.Unmarshal(iter.Value()); err != nil {
|
|
goto deleteKey
|
|
}
|
|
if _, ok := res[folderID]; !ok {
|
|
res[folderID] = PendingFolder{
|
|
OfferedBy: map[protocol.DeviceID]ObservedFolder{},
|
|
}
|
|
}
|
|
res[folderID].OfferedBy[deviceID] = of
|
|
continue
|
|
deleteKey:
|
|
// Deleting invalid entries is the only possible "repair" measure and
|
|
// appropriate for the importance of pending entries. They will come back
|
|
// soon if still relevant.
|
|
l.Infof("Invalid pending folder entry, deleting from database: %x", iter.Key())
|
|
if err := db.Delete(iter.Key()); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|