2018-07-12 08:15:57 +00:00
|
|
|
// Copyright (C) 2018 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 model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2019-08-15 14:29:37 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2018-07-12 08:15:57 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2023-08-21 17:44:33 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/semaphore"
|
2018-07-12 08:15:57 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/versioner"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
folderFactories[config.FolderTypeReceiveOnly] = newReceiveOnlyFolder
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
receiveOnlyFolder is a folder that does not propagate local changes outward.
|
|
|
|
It does this by the following general mechanism (not all of which is
|
2021-09-01 05:41:18 +00:00
|
|
|
implemented in this file):
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2022-08-03 13:43:26 +00:00
|
|
|
- Local changes are scanned and versioned as usual, but get the
|
|
|
|
FlagLocalReceiveOnly bit set.
|
|
|
|
|
|
|
|
- When changes are sent to the cluster this bit gets converted to the
|
|
|
|
Invalid bit (like all other local flags, currently) and also the Version
|
|
|
|
gets set to the empty version. The reason for clearing the Version is to
|
|
|
|
ensure that other devices will not consider themselves out of date due to
|
|
|
|
our change.
|
|
|
|
|
|
|
|
- The database layer accounts sizes per flag bit, so we can know how many
|
|
|
|
files have been changed locally. We use this to trigger a "Revert" option
|
|
|
|
on the folder when the amount of locally changed data is nonzero.
|
|
|
|
|
|
|
|
- To revert we take the files which have changed and reset their version
|
|
|
|
counter down to zero. The next pull will replace our changed version with
|
|
|
|
the globally latest. As this is a user-initiated operation we do not cause
|
|
|
|
conflict copies when reverting.
|
|
|
|
|
|
|
|
- When pulling normally (i.e., not in the revert case) with local changes,
|
|
|
|
normal conflict resolution will apply. Conflict copies will be created,
|
|
|
|
but not propagated outwards (because receive only, right).
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
Implementation wise a receiveOnlyFolder is just a sendReceiveFolder that
|
|
|
|
sets an extra bit on local changes and has a Revert method.
|
|
|
|
*/
|
|
|
|
type receiveOnlyFolder struct {
|
|
|
|
*sendReceiveFolder
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:44:33 +00:00
|
|
|
func newReceiveOnlyFolder(model *model, fset *db.FileSet, ignores *ignore.Matcher, cfg config.FolderConfiguration, ver versioner.Versioner, evLogger events.Logger, ioLimiter *semaphore.Semaphore) service {
|
2020-12-27 21:26:25 +00:00
|
|
|
sr := newSendReceiveFolder(model, fset, ignores, cfg, ver, evLogger, ioLimiter).(*sendReceiveFolder)
|
2018-07-12 08:15:57 +00:00
|
|
|
sr.localFlags = protocol.FlagLocalReceiveOnly // gets propagated to the scanner, and set on locally changed files
|
|
|
|
return &receiveOnlyFolder{sr}
|
|
|
|
}
|
|
|
|
|
2019-04-07 11:29:17 +00:00
|
|
|
func (f *receiveOnlyFolder) Revert() {
|
2021-03-07 12:43:22 +00:00
|
|
|
f.doInSync(f.revert)
|
2020-03-27 12:05:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 12:43:22 +00:00
|
|
|
func (f *receiveOnlyFolder) revert() error {
|
2021-11-17 11:10:06 +00:00
|
|
|
l.Infof("Reverting folder %v", f.Description())
|
2020-03-24 11:51:17 +00:00
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
f.setState(FolderScanning)
|
|
|
|
defer f.setState(FolderIdle)
|
|
|
|
|
2018-09-16 07:48:14 +00:00
|
|
|
scanChan := make(chan string)
|
|
|
|
go f.pullScannerRoutine(scanChan)
|
|
|
|
defer close(scanChan)
|
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
delQueue := &deleteQueue{
|
2019-03-07 14:15:14 +00:00
|
|
|
handler: f, // for the deleteItemOnDisk and deleteDirOnDisk methods
|
2019-03-11 06:28:54 +00:00
|
|
|
ignores: f.ignores,
|
2018-09-16 07:48:14 +00:00
|
|
|
scanChan: scanChan,
|
2018-07-12 08:15:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 20:01:46 +00:00
|
|
|
batch := db.NewFileInfoBatch(func(files []protocol.FileInfo) error {
|
|
|
|
f.updateLocalsFromScanning(files)
|
|
|
|
return nil
|
|
|
|
})
|
2021-03-07 12:43:22 +00:00
|
|
|
snap, err := f.dbSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
defer snap.Release()
|
refactor: use modern Protobuf encoder (#9817)
At a high level, this is what I've done and why:
- I'm moving the protobuf generation for the `protocol`, `discovery` and
`db` packages to the modern alternatives, and using `buf` to generate
because it's nice and simple.
- After trying various approaches on how to integrate the new types with
the existing code, I opted for splitting off our own data model types
from the on-the-wire generated types. This means we can have a
`FileInfo` type with nicer ergonomics and lots of methods, while the
protobuf generated type stays clean and close to the wire protocol. It
does mean copying between the two when required, which certainly adds a
small amount of inefficiency. If we want to walk this back in the future
and use the raw generated type throughout, that's possible, this however
makes the refactor smaller (!) as it doesn't change everything about the
type for everyone at the same time.
- I have simply removed in cold blood a significant number of old
database migrations. These depended on previous generations of generated
messages of various kinds and were annoying to support in the new
fashion. The oldest supported database version now is the one from
Syncthing 1.9.0 from Sep 7, 2020.
- I changed config structs to be regular manually defined structs.
For the sake of discussion, some things I tried that turned out not to
work...
### Embedding / wrapping
Embedding the protobuf generated structs in our existing types as a data
container and keeping our methods and stuff:
```
package protocol
type FileInfo struct {
*generated.FileInfo
}
```
This generates a lot of problems because the internal shape of the
generated struct is quite different (different names, different types,
more pointers), because initializing it doesn't work like you'd expect
(i.e., you end up with an embedded nil pointer and a panic), and because
the types of child types don't get wrapped. That is, even if we also
have a similar wrapper around a `Vector`, that's not the type you get
when accessing `someFileInfo.Version`, you get the `*generated.Vector`
that doesn't have methods, etc.
### Aliasing
```
package protocol
type FileInfo = generated.FileInfo
```
Doesn't help because you can't attach methods to it, plus all the above.
### Generating the types into the target package like we do now and
attaching methods
This fails because of the different shape of the generated type (as in
the embedding case above) plus the generated struct already has a bunch
of methods that we can't necessarily override properly (like `String()`
and a bunch of getters).
### Methods to functions
I considered just moving all the methods we attach to functions in a
specific package, so that for example
```
package protocol
func (f FileInfo) Equal(other FileInfo) bool
```
would become
```
package fileinfos
func Equal(a, b *generated.FileInfo) bool
```
and this would mostly work, but becomes quite verbose and cumbersome,
and somewhat limits discoverability (you can't see what methods are
available on the type in auto completions, etc). In the end I did this
in some cases, like in the database layer where a lot of things like
`func (fv *FileVersion) IsEmpty() bool` becomes `func fvIsEmpty(fv
*generated.FileVersion)` because they were anyway just internal methods.
Fixes #8247
2024-12-01 15:50:17 +00:00
|
|
|
snap.WithHave(protocol.LocalDeviceID, func(fi protocol.FileInfo) bool {
|
2018-07-12 08:15:57 +00:00
|
|
|
if !fi.IsReceiveOnlyChanged() {
|
|
|
|
// We're only interested in files that have changed locally in
|
|
|
|
// receive only mode.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-03-24 11:51:17 +00:00
|
|
|
fi.LocalFlags &^= protocol.FlagLocalReceiveOnly
|
2021-06-03 13:08:56 +00:00
|
|
|
|
|
|
|
switch gf, ok := snap.GetGlobal(fi.Name); {
|
|
|
|
case !ok:
|
|
|
|
msg := "Unexpected global file that we have locally"
|
|
|
|
l.Debugf("%v revert: %v: %v", f, msg, fi.Name)
|
|
|
|
f.evLogger.Log(events.Failure, msg)
|
|
|
|
return true
|
|
|
|
case gf.IsReceiveOnlyChanged():
|
|
|
|
// The global file is our own. A revert then means to delete it.
|
2018-07-12 08:15:57 +00:00
|
|
|
// We'll delete files directly, directories get queued and
|
|
|
|
// handled below.
|
2021-07-23 12:26:20 +00:00
|
|
|
if fi.Deleted {
|
|
|
|
fi.Version = protocol.Vector{} // if this file ever resurfaces anywhere we want our delete to be strictly older
|
|
|
|
break
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
handled, err := delQueue.handle(fi, snap)
|
2018-07-12 08:15:57 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Infof("Revert: deleting %s: %v\n", fi.Name, err)
|
|
|
|
return true // continue
|
|
|
|
}
|
|
|
|
if !handled {
|
|
|
|
return true // continue
|
|
|
|
}
|
2020-02-19 15:58:09 +00:00
|
|
|
fi.SetDeleted(f.shortID)
|
|
|
|
fi.Version = protocol.Vector{} // if this file ever resurfaces anywhere we want our delete to be strictly older
|
2022-07-26 06:24:58 +00:00
|
|
|
case gf.IsEquivalentOptional(fi, protocol.FileInfoComparison{
|
|
|
|
ModTimeWindow: f.modTimeWindow,
|
|
|
|
IgnoreFlags: protocol.FlagLocalReceiveOnly,
|
|
|
|
IgnoreOwnership: !f.SyncOwnership,
|
2022-09-14 07:50:55 +00:00
|
|
|
IgnoreXattrs: !f.SyncXattrs,
|
2022-07-26 06:24:58 +00:00
|
|
|
}):
|
2021-06-03 13:08:56 +00:00
|
|
|
// What we have locally is equivalent to the global file.
|
|
|
|
fi = gf
|
|
|
|
default:
|
2018-07-12 08:15:57 +00:00
|
|
|
// Revert means to throw away our local changes. We reset the
|
|
|
|
// version to the empty vector, which is strictly older than any
|
|
|
|
// other existing version. It is not in conflict with anything,
|
|
|
|
// either, so we will not create a conflict copy of our local
|
|
|
|
// changes.
|
|
|
|
fi.Version = protocol.Vector{}
|
|
|
|
}
|
|
|
|
|
2021-04-29 20:01:46 +00:00
|
|
|
batch.Append(fi)
|
|
|
|
_ = batch.FlushIfFull()
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
2021-04-29 20:01:46 +00:00
|
|
|
_ = batch.Flush()
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
// Handle any queued directories
|
2020-01-21 17:23:08 +00:00
|
|
|
deleted, err := delQueue.flush(snap)
|
2018-07-12 08:15:57 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Infoln("Revert:", err)
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
for _, dir := range deleted {
|
2021-04-29 20:01:46 +00:00
|
|
|
batch.Append(protocol.FileInfo{
|
2018-07-12 08:15:57 +00:00
|
|
|
Name: dir,
|
|
|
|
Type: protocol.FileInfoTypeDirectory,
|
|
|
|
ModifiedS: now.Unix(),
|
|
|
|
ModifiedBy: f.shortID,
|
|
|
|
Deleted: true,
|
|
|
|
Version: protocol.Vector{},
|
|
|
|
})
|
|
|
|
}
|
2021-04-29 20:01:46 +00:00
|
|
|
_ = batch.Flush()
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
// We will likely have changed our local index, but that won't trigger a
|
|
|
|
// pull by itself. Make sure we schedule one so that we start
|
|
|
|
// downloading files.
|
|
|
|
f.SchedulePull()
|
2021-03-07 12:43:22 +00:00
|
|
|
|
|
|
|
return nil
|
2018-07-12 08:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// deleteQueue handles deletes by delegating to a handler and queuing
|
|
|
|
// directories for last.
|
|
|
|
type deleteQueue struct {
|
|
|
|
handler interface {
|
2020-01-21 17:23:08 +00:00
|
|
|
deleteItemOnDisk(item protocol.FileInfo, snap *db.Snapshot, scanChan chan<- string) error
|
|
|
|
deleteDirOnDisk(dir string, snap *db.Snapshot, scanChan chan<- string) error
|
2018-07-12 08:15:57 +00:00
|
|
|
}
|
2018-09-16 07:48:14 +00:00
|
|
|
ignores *ignore.Matcher
|
|
|
|
dirs []string
|
|
|
|
scanChan chan<- string
|
2018-07-12 08:15:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
func (q *deleteQueue) handle(fi protocol.FileInfo, snap *db.Snapshot) (bool, error) {
|
2018-07-12 08:15:57 +00:00
|
|
|
// Things that are ignored but not marked deletable are not processed.
|
|
|
|
ign := q.ignores.Match(fi.Name)
|
|
|
|
if ign.IsIgnored() && !ign.IsDeletable() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directories are queued for later processing.
|
|
|
|
if fi.IsDirectory() {
|
|
|
|
q.dirs = append(q.dirs, fi.Name)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill it.
|
2020-01-21 17:23:08 +00:00
|
|
|
err := q.handler.deleteItemOnDisk(fi, snap, q.scanChan)
|
2018-07-12 08:15:57 +00:00
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
func (q *deleteQueue) flush(snap *db.Snapshot) ([]string, error) {
|
2018-07-12 08:15:57 +00:00
|
|
|
// Process directories from the leaves inward.
|
|
|
|
sort.Sort(sort.Reverse(sort.StringSlice(q.dirs)))
|
|
|
|
|
|
|
|
var firstError error
|
|
|
|
var deleted []string
|
|
|
|
|
|
|
|
for _, dir := range q.dirs {
|
2020-01-21 17:23:08 +00:00
|
|
|
if err := q.handler.deleteDirOnDisk(dir, snap, q.scanChan); err == nil {
|
2018-07-12 08:15:57 +00:00
|
|
|
deleted = append(deleted, dir)
|
refactor: use modern Protobuf encoder (#9817)
At a high level, this is what I've done and why:
- I'm moving the protobuf generation for the `protocol`, `discovery` and
`db` packages to the modern alternatives, and using `buf` to generate
because it's nice and simple.
- After trying various approaches on how to integrate the new types with
the existing code, I opted for splitting off our own data model types
from the on-the-wire generated types. This means we can have a
`FileInfo` type with nicer ergonomics and lots of methods, while the
protobuf generated type stays clean and close to the wire protocol. It
does mean copying between the two when required, which certainly adds a
small amount of inefficiency. If we want to walk this back in the future
and use the raw generated type throughout, that's possible, this however
makes the refactor smaller (!) as it doesn't change everything about the
type for everyone at the same time.
- I have simply removed in cold blood a significant number of old
database migrations. These depended on previous generations of generated
messages of various kinds and were annoying to support in the new
fashion. The oldest supported database version now is the one from
Syncthing 1.9.0 from Sep 7, 2020.
- I changed config structs to be regular manually defined structs.
For the sake of discussion, some things I tried that turned out not to
work...
### Embedding / wrapping
Embedding the protobuf generated structs in our existing types as a data
container and keeping our methods and stuff:
```
package protocol
type FileInfo struct {
*generated.FileInfo
}
```
This generates a lot of problems because the internal shape of the
generated struct is quite different (different names, different types,
more pointers), because initializing it doesn't work like you'd expect
(i.e., you end up with an embedded nil pointer and a panic), and because
the types of child types don't get wrapped. That is, even if we also
have a similar wrapper around a `Vector`, that's not the type you get
when accessing `someFileInfo.Version`, you get the `*generated.Vector`
that doesn't have methods, etc.
### Aliasing
```
package protocol
type FileInfo = generated.FileInfo
```
Doesn't help because you can't attach methods to it, plus all the above.
### Generating the types into the target package like we do now and
attaching methods
This fails because of the different shape of the generated type (as in
the embedding case above) plus the generated struct already has a bunch
of methods that we can't necessarily override properly (like `String()`
and a bunch of getters).
### Methods to functions
I considered just moving all the methods we attach to functions in a
specific package, so that for example
```
package protocol
func (f FileInfo) Equal(other FileInfo) bool
```
would become
```
package fileinfos
func Equal(a, b *generated.FileInfo) bool
```
and this would mostly work, but becomes quite verbose and cumbersome,
and somewhat limits discoverability (you can't see what methods are
available on the type in auto completions, etc). In the end I did this
in some cases, like in the database layer where a lot of things like
`func (fv *FileVersion) IsEmpty() bool` becomes `func fvIsEmpty(fv
*generated.FileVersion)` because they were anyway just internal methods.
Fixes #8247
2024-12-01 15:50:17 +00:00
|
|
|
} else if firstError == nil {
|
2018-07-12 08:15:57 +00:00
|
|
|
firstError = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return deleted, firstError
|
|
|
|
}
|