2019-05-10 11:33:45 +00:00
|
|
|
// Copyright (C) 2016 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 (
|
2020-11-17 12:19:04 +00:00
|
|
|
"context"
|
2019-05-19 12:29:07 +00:00
|
|
|
"os"
|
2020-01-21 17:23:08 +00:00
|
|
|
"testing"
|
2019-05-18 06:52:50 +00:00
|
|
|
"time"
|
2019-05-10 11:33:45 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2019-11-29 08:11:52 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db/backend"
|
2019-08-15 14:29:37 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2019-05-10 11:33:45 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2020-05-06 06:34:54 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore"
|
2019-05-10 11:33:45 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2023-07-29 08:24:44 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol/mocks"
|
2020-02-12 06:47:05 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2019-05-10 11:33:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-01-15 14:43:34 +00:00
|
|
|
myID, device1, device2 protocol.DeviceID
|
|
|
|
defaultCfgWrapper config.Wrapper
|
|
|
|
defaultCfgWrapperCancel context.CancelFunc
|
|
|
|
defaultFolderConfig config.FolderConfiguration
|
|
|
|
defaultCfg config.Configuration
|
|
|
|
defaultAutoAcceptCfg config.Configuration
|
2023-07-29 08:24:44 +00:00
|
|
|
device1Conn = &mocks.Connection{}
|
|
|
|
device2Conn = &mocks.Connection{}
|
2019-05-10 11:33:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
myID, _ = protocol.DeviceIDFromString("ZNWFSWE-RWRV2BD-45BLMCV-LTDE2UR-4LJDW6J-R5BPWEB-TXD27XJ-IZF5RA4")
|
|
|
|
device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
|
|
|
|
device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
|
2023-07-29 08:24:44 +00:00
|
|
|
device1Conn.DeviceIDReturns(device1)
|
2023-09-06 10:52:01 +00:00
|
|
|
device1Conn.ConnectionIDReturns(rand.String(16))
|
2023-07-29 08:24:44 +00:00
|
|
|
device2Conn.DeviceIDReturns(device2)
|
2023-09-06 10:52:01 +00:00
|
|
|
device2Conn.ConnectionIDReturns(rand.String(16))
|
2019-05-10 11:33:45 +00:00
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
cfg := config.New(myID)
|
|
|
|
cfg.Options.MinHomeDiskFree.Value = 0 // avoids unnecessary free space checks
|
|
|
|
defaultCfgWrapper, defaultCfgWrapperCancel = newConfigWrapper(cfg)
|
2021-02-04 20:10:41 +00:00
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
defaultFolderConfig = newFolderConfig()
|
2019-05-10 11:33:45 +00:00
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
waiter, _ := defaultCfgWrapper.Modify(func(cfg *config.Configuration) {
|
2021-02-04 20:10:41 +00:00
|
|
|
cfg.SetDevice(newDeviceConfiguration(cfg.Defaults.Device, device1, "device1"))
|
2021-01-15 14:43:34 +00:00
|
|
|
cfg.SetFolder(defaultFolderConfig)
|
|
|
|
cfg.Options.KeepTemporariesH = 1
|
|
|
|
})
|
|
|
|
waiter.Wait()
|
2019-05-10 11:33:45 +00:00
|
|
|
|
|
|
|
defaultCfg = defaultCfgWrapper.RawCopy()
|
|
|
|
|
|
|
|
defaultAutoAcceptCfg = config.Configuration{
|
2020-11-09 14:33:32 +00:00
|
|
|
Version: config.CurrentVersion,
|
2019-05-10 11:33:45 +00:00
|
|
|
Devices: []config.DeviceConfiguration{
|
|
|
|
{
|
|
|
|
DeviceID: myID, // self
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DeviceID: device1,
|
|
|
|
AutoAcceptFolders: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DeviceID: device2,
|
|
|
|
AutoAcceptFolders: true,
|
|
|
|
},
|
|
|
|
},
|
2021-02-04 20:10:41 +00:00
|
|
|
Defaults: config.Defaults{
|
|
|
|
Folder: config.FolderConfiguration{
|
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
|
|
|
FilesystemType: config.FilesystemTypeFake,
|
2023-05-03 08:25:36 +00:00
|
|
|
Path: rand.String(32),
|
2021-02-04 20:10:41 +00:00
|
|
|
},
|
2019-05-10 11:33:45 +00:00
|
|
|
},
|
2023-05-03 08:25:36 +00:00
|
|
|
Options: config.OptionsConfiguration{
|
|
|
|
MinHomeDiskFree: config.Size{}, // avoids unnecessary free space checks
|
|
|
|
},
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
func newConfigWrapper(cfg config.Configuration) (config.Wrapper, context.CancelFunc) {
|
|
|
|
wrapper := config.Wrap("", cfg, myID, events.NoopLogger)
|
2021-03-11 13:51:00 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go wrapper.Serve(ctx)
|
|
|
|
return wrapper, cancel
|
2021-01-15 14:43:34 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
func newDefaultCfgWrapper() (config.Wrapper, config.FolderConfiguration, context.CancelFunc) {
|
|
|
|
w, cancel := newConfigWrapper(defaultCfgWrapper.RawCopy())
|
|
|
|
fcfg := newFolderConfig()
|
2021-01-15 14:43:34 +00:00
|
|
|
_, _ = w.Modify(func(cfg *config.Configuration) {
|
|
|
|
cfg.SetFolder(fcfg)
|
|
|
|
})
|
|
|
|
return w, fcfg, cancel
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
func newFolderConfig() config.FolderConfiguration {
|
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
|
|
|
cfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", config.FilesystemTypeFake, rand.String(32)+"?content=true")
|
2020-02-12 06:47:05 +00:00
|
|
|
cfg.FSWatcherEnabled = false
|
|
|
|
cfg.Devices = append(cfg.Devices, config.FolderDeviceConfiguration{DeviceID: device1})
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
func setupModelWithConnection(t testing.TB) (*testModel, *fakeConnection, config.FolderConfiguration, context.CancelFunc) {
|
2020-12-21 11:59:22 +00:00
|
|
|
t.Helper()
|
2023-05-03 08:25:36 +00:00
|
|
|
w, fcfg, cancel := newDefaultCfgWrapper()
|
2020-12-21 11:59:22 +00:00
|
|
|
m, fc := setupModelWithConnectionFromWrapper(t, w)
|
2021-01-15 14:43:34 +00:00
|
|
|
return m, fc, fcfg, cancel
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 11:59:22 +00:00
|
|
|
func setupModelWithConnectionFromWrapper(t testing.TB, w config.Wrapper) (*testModel, *fakeConnection) {
|
|
|
|
t.Helper()
|
|
|
|
m := setupModel(t, w)
|
2019-05-10 11:33:45 +00:00
|
|
|
|
2021-05-16 15:23:27 +00:00
|
|
|
fc := addFakeConn(m, device1, "default")
|
2019-05-10 11:33:45 +00:00
|
|
|
fc.folder = "default"
|
|
|
|
|
|
|
|
_ = m.ScanFolder("default")
|
|
|
|
|
|
|
|
return m, fc
|
|
|
|
}
|
|
|
|
|
2020-12-21 11:59:22 +00:00
|
|
|
func setupModel(t testing.TB, w config.Wrapper) *testModel {
|
|
|
|
t.Helper()
|
2023-09-06 10:52:01 +00:00
|
|
|
m := newModel(t, w, myID, nil)
|
2019-05-10 11:33:45 +00:00
|
|
|
m.ServeBackground()
|
2020-11-17 12:19:04 +00:00
|
|
|
<-m.started
|
2019-05-10 11:33:45 +00:00
|
|
|
|
|
|
|
m.ScanFolders()
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
type testModel struct {
|
|
|
|
*model
|
2021-03-07 12:43:22 +00:00
|
|
|
t testing.TB
|
2020-11-17 12:19:04 +00:00
|
|
|
cancel context.CancelFunc
|
|
|
|
evCancel context.CancelFunc
|
|
|
|
stopped chan struct{}
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:52:01 +00:00
|
|
|
func newModel(t testing.TB, cfg config.Wrapper, id protocol.DeviceID, protectedFiles []string) *testModel {
|
2020-12-21 11:59:22 +00:00
|
|
|
t.Helper()
|
2019-08-15 14:29:37 +00:00
|
|
|
evLogger := events.NewLogger()
|
2020-12-21 11:59:22 +00:00
|
|
|
ldb, err := db.NewLowlevel(backend.OpenMemory(), evLogger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-09-06 10:52:01 +00:00
|
|
|
m := NewModel(cfg, id, ldb, protectedFiles, evLogger, protocol.NewKeyGenerator()).(*model)
|
2020-11-17 12:19:04 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go evLogger.Serve(ctx)
|
|
|
|
return &testModel{
|
|
|
|
model: m,
|
|
|
|
evCancel: cancel,
|
|
|
|
stopped: make(chan struct{}),
|
2021-03-07 12:43:22 +00:00
|
|
|
t: t,
|
2020-11-17 12:19:04 +00:00
|
|
|
}
|
2019-05-19 12:29:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func (m *testModel) ServeBackground() {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
m.cancel = cancel
|
|
|
|
go func() {
|
|
|
|
m.model.Serve(ctx)
|
|
|
|
close(m.stopped)
|
|
|
|
}()
|
|
|
|
<-m.started
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:43:22 +00:00
|
|
|
func (m *testModel) testAvailability(folder string, file protocol.FileInfo, block protocol.BlockInfo) []Availability {
|
|
|
|
av, err := m.model.Availability(folder, file, block)
|
|
|
|
must(m.t, err)
|
|
|
|
return av
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *testModel) testCurrentFolderFile(folder string, file string) (protocol.FileInfo, bool) {
|
|
|
|
f, ok, err := m.model.CurrentFolderFile(folder, file)
|
|
|
|
must(m.t, err)
|
|
|
|
return f, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *testModel) testCompletion(device protocol.DeviceID, folder string) FolderCompletion {
|
2021-06-05 15:01:23 +00:00
|
|
|
comp, err := m.Completion(device, folder)
|
2021-03-07 12:43:22 +00:00
|
|
|
must(m.t, err)
|
|
|
|
return comp
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func cleanupModel(m *testModel) {
|
|
|
|
if m.cancel != nil {
|
|
|
|
m.cancel()
|
|
|
|
<-m.stopped
|
|
|
|
}
|
|
|
|
m.evCancel()
|
2019-05-19 12:29:07 +00:00
|
|
|
m.db.Close()
|
|
|
|
os.Remove(m.cfg.ConfigPath())
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func cleanupModelAndRemoveDir(m *testModel, dir string) {
|
2019-05-19 12:29:07 +00:00
|
|
|
cleanupModel(m)
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
|
2019-05-18 06:52:50 +00:00
|
|
|
type alwaysChangedKey struct {
|
|
|
|
fs fs.Filesystem
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// alwaysChanges is an ignore.ChangeDetector that always returns true on Changed()
|
|
|
|
type alwaysChanged struct {
|
|
|
|
seen map[alwaysChangedKey]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAlwaysChanged() *alwaysChanged {
|
|
|
|
return &alwaysChanged{
|
|
|
|
seen: make(map[alwaysChangedKey]struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *alwaysChanged) Remember(fs fs.Filesystem, name string, _ time.Time) {
|
|
|
|
c.seen[alwaysChangedKey{fs, name}] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *alwaysChanged) Reset() {
|
|
|
|
c.seen = make(map[alwaysChangedKey]struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *alwaysChanged) Seen(fs fs.Filesystem, name string) bool {
|
|
|
|
_, ok := c.seen[alwaysChangedKey{fs, name}]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:34:50 +00:00
|
|
|
func (*alwaysChanged) Changed() bool {
|
2019-05-18 06:52:50 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
|
|
|
|
func localSize(t *testing.T, m Model, folder string) db.Counts {
|
|
|
|
t.Helper()
|
|
|
|
snap := dbSnapshot(t, m, folder)
|
|
|
|
defer snap.Release()
|
|
|
|
return snap.LocalSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func globalSize(t *testing.T, m Model, folder string) db.Counts {
|
|
|
|
t.Helper()
|
|
|
|
snap := dbSnapshot(t, m, folder)
|
|
|
|
defer snap.Release()
|
|
|
|
return snap.GlobalSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func receiveOnlyChangedSize(t *testing.T, m Model, folder string) db.Counts {
|
|
|
|
t.Helper()
|
|
|
|
snap := dbSnapshot(t, m, folder)
|
|
|
|
defer snap.Release()
|
|
|
|
return snap.ReceiveOnlyChangedSize()
|
|
|
|
}
|
|
|
|
|
2021-06-05 15:01:23 +00:00
|
|
|
func needSizeLocal(t *testing.T, m Model, folder string) db.Counts {
|
2020-01-21 17:23:08 +00:00
|
|
|
t.Helper()
|
|
|
|
snap := dbSnapshot(t, m, folder)
|
|
|
|
defer snap.Release()
|
2020-05-11 13:07:06 +00:00
|
|
|
return snap.NeedSize(protocol.LocalDeviceID)
|
2020-01-21 17:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dbSnapshot(t *testing.T, m Model, folder string) *db.Snapshot {
|
|
|
|
t.Helper()
|
|
|
|
snap, err := m.DBSnapshot(folder)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return snap
|
|
|
|
}
|
2020-05-06 06:34:54 +00:00
|
|
|
|
2021-03-07 12:43:22 +00:00
|
|
|
func fsetSnapshot(t *testing.T, fset *db.FileSet) *db.Snapshot {
|
|
|
|
t.Helper()
|
|
|
|
snap, err := fset.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return snap
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:34:54 +00:00
|
|
|
// Reach in and update the ignore matcher to one that always does
|
|
|
|
// reloads when asked to, instead of checking file mtimes. This is
|
|
|
|
// because we will be changing the files on disk often enough that the
|
|
|
|
// mtimes will be unreliable to determine change status.
|
2020-12-21 11:59:22 +00:00
|
|
|
func folderIgnoresAlwaysReload(t testing.TB, m *testModel, fcfg config.FolderConfiguration) {
|
|
|
|
t.Helper()
|
2020-05-06 06:34:54 +00:00
|
|
|
m.removeFolder(fcfg)
|
2022-01-31 09:12:52 +00:00
|
|
|
fset := newFileSet(t, fcfg.ID, m.db)
|
2022-04-10 18:55:05 +00:00
|
|
|
ignores := ignore.New(fcfg.Filesystem(nil), ignore.WithCache(true), ignore.WithChangeDetector(newAlwaysChanged()))
|
2023-12-11 21:06:45 +00:00
|
|
|
m.mut.Lock()
|
2020-05-06 06:34:54 +00:00
|
|
|
m.addAndStartFolderLockedWithIgnores(fcfg, fset, ignores)
|
2023-12-11 21:06:45 +00:00
|
|
|
m.mut.Unlock()
|
2020-05-06 06:34:54 +00:00
|
|
|
}
|
2020-10-21 09:51:53 +00:00
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func basicClusterConfig(local, remote protocol.DeviceID, folders ...string) *protocol.ClusterConfig {
|
2020-10-21 09:51:53 +00:00
|
|
|
var cc protocol.ClusterConfig
|
|
|
|
for _, folder := range folders {
|
|
|
|
cc.Folders = append(cc.Folders, protocol.Folder{
|
|
|
|
ID: folder,
|
|
|
|
Devices: []protocol.Device{
|
|
|
|
{
|
|
|
|
ID: local,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: remote,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
return &cc
|
2020-10-21 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func localIndexUpdate(m *testModel, folder string, fs []protocol.FileInfo) {
|
2023-12-11 21:06:45 +00:00
|
|
|
m.mut.RLock()
|
2020-10-21 09:51:53 +00:00
|
|
|
fset := m.folderFiles[folder]
|
2023-12-11 21:06:45 +00:00
|
|
|
m.mut.RUnlock()
|
2020-10-21 09:51:53 +00:00
|
|
|
|
|
|
|
fset.Update(protocol.LocalDeviceID, fs)
|
|
|
|
seq := fset.Sequence(protocol.LocalDeviceID)
|
|
|
|
filenames := make([]string, len(fs))
|
|
|
|
for i, file := range fs {
|
|
|
|
filenames[i] = file.Name
|
|
|
|
}
|
|
|
|
m.evLogger.Log(events.LocalIndexUpdated, map[string]interface{}{
|
|
|
|
"folder": folder,
|
|
|
|
"items": len(fs),
|
|
|
|
"filenames": filenames,
|
|
|
|
"sequence": seq,
|
|
|
|
"version": seq, // legacy for sequence
|
|
|
|
})
|
|
|
|
}
|
2020-12-21 11:59:22 +00:00
|
|
|
|
2021-02-04 20:10:41 +00:00
|
|
|
func newDeviceConfiguration(defaultCfg config.DeviceConfiguration, id protocol.DeviceID, name string) config.DeviceConfiguration {
|
|
|
|
cfg := defaultCfg.Copy()
|
|
|
|
cfg.DeviceID = id
|
|
|
|
cfg.Name = name
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2022-01-31 09:12:52 +00:00
|
|
|
func newFileSet(t testing.TB, folder string, ldb *db.Lowlevel) *db.FileSet {
|
2020-12-21 11:59:22 +00:00
|
|
|
t.Helper()
|
2022-01-31 09:12:52 +00:00
|
|
|
fset, err := db.NewFileSet(folder, ldb)
|
2020-12-21 11:59:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return fset
|
|
|
|
}
|
2021-01-15 14:43:34 +00:00
|
|
|
|
|
|
|
func replace(t testing.TB, w config.Wrapper, to config.Configuration) {
|
|
|
|
t.Helper()
|
|
|
|
waiter, err := w.Modify(func(cfg *config.Configuration) {
|
|
|
|
*cfg = to
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
waiter.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func pauseFolder(t testing.TB, w config.Wrapper, id string, paused bool) {
|
|
|
|
t.Helper()
|
|
|
|
waiter, err := w.Modify(func(cfg *config.Configuration) {
|
|
|
|
_, i, _ := cfg.Folder(id)
|
|
|
|
cfg.Folders[i].Paused = paused
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
waiter.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFolder(t testing.TB, w config.Wrapper, fcfg config.FolderConfiguration) {
|
|
|
|
t.Helper()
|
|
|
|
waiter, err := w.Modify(func(cfg *config.Configuration) {
|
|
|
|
cfg.SetFolder(fcfg)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
waiter.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func pauseDevice(t testing.TB, w config.Wrapper, id protocol.DeviceID, paused bool) {
|
|
|
|
t.Helper()
|
|
|
|
waiter, err := w.Modify(func(cfg *config.Configuration) {
|
|
|
|
_, i, _ := cfg.Device(id)
|
|
|
|
cfg.Devices[i].Paused = paused
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
waiter.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setDevice(t testing.TB, w config.Wrapper, device config.DeviceConfiguration) {
|
|
|
|
t.Helper()
|
|
|
|
waiter, err := w.Modify(func(cfg *config.Configuration) {
|
|
|
|
cfg.SetDevice(device)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
waiter.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addDevice2(t testing.TB, w config.Wrapper, fcfg config.FolderConfiguration) {
|
|
|
|
waiter, err := w.Modify(func(cfg *config.Configuration) {
|
2021-02-04 20:10:41 +00:00
|
|
|
cfg.SetDevice(newDeviceConfiguration(cfg.Defaults.Device, device2, "device2"))
|
2021-01-15 14:43:34 +00:00
|
|
|
fcfg.Devices = append(fcfg.Devices, config.FolderDeviceConfiguration{DeviceID: device2})
|
|
|
|
cfg.SetFolder(fcfg)
|
|
|
|
})
|
|
|
|
must(t, err)
|
|
|
|
waiter.Wait()
|
|
|
|
}
|
2021-11-10 08:46:21 +00:00
|
|
|
|
|
|
|
func writeFile(t testing.TB, filesystem fs.Filesystem, name string, data []byte) {
|
|
|
|
t.Helper()
|
|
|
|
fd, err := filesystem.Create(name)
|
|
|
|
must(t, err)
|
|
|
|
defer fd.Close()
|
|
|
|
_, err = fd.Write(data)
|
|
|
|
must(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFilePerm(t testing.TB, filesystem fs.Filesystem, name string, data []byte, perm fs.FileMode) {
|
|
|
|
t.Helper()
|
|
|
|
writeFile(t, filesystem, name, data)
|
|
|
|
must(t, filesystem.Chmod(name, perm))
|
|
|
|
}
|