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 (
|
2018-08-19 21:34:26 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-08-27 13:54:00 +00:00
|
|
|
"path/filepath"
|
2018-07-12 08:15:57 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2021-06-03 13:08:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2018-07-12 08:15:57 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2018-08-19 21:34:26 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/scanner"
|
2018-07-12 08:15:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecvOnlyRevertDeletes(t *testing.T) {
|
|
|
|
// Make sure that we delete extraneous files and directories when we hit
|
|
|
|
// Revert.
|
|
|
|
|
2019-03-20 17:38:03 +00:00
|
|
|
// Get us a model up and running
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2022-04-10 18:55:05 +00:00
|
|
|
ffs := f.Filesystem(nil)
|
2020-02-12 06:47:05 +00:00
|
|
|
defer cleanupModel(m)
|
2023-07-29 08:24:44 +00:00
|
|
|
conn := addFakeConn(m, device1, f.ID)
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2019-03-20 17:38:03 +00:00
|
|
|
// Create some test data
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2019-03-20 17:38:03 +00:00
|
|
|
for _, dir := range []string{".stfolder", "ignDir", "unknownDir"} {
|
2023-05-03 08:25:36 +00:00
|
|
|
must(t, ffs.MkdirAll(dir, 0o755))
|
2019-03-20 17:38:03 +00:00
|
|
|
}
|
2023-05-03 08:25:36 +00:00
|
|
|
writeFilePerm(t, ffs, "ignDir/ignFile", []byte("hello\n"), 0o644)
|
|
|
|
writeFilePerm(t, ffs, "unknownDir/unknownFile", []byte("hello\n"), 0o644)
|
|
|
|
writeFilePerm(t, ffs, ".stignore", []byte("ignDir\n"), 0o644)
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2019-03-20 17:38:03 +00:00
|
|
|
knownFiles := setupKnownFiles(t, ffs, []byte("hello\n"))
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
// Send and index update for the known stuff
|
|
|
|
|
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
|
|
|
must(t, m.Index(conn, &protocol.Index{Folder: "ro", Files: knownFiles}))
|
2019-04-07 11:29:17 +00:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2020-07-30 11:49:14 +00:00
|
|
|
size := globalSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
// Scan, should discover the other stuff in the folder
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2020-07-30 11:49:14 +00:00
|
|
|
// We should now have two files and two directories, with global state unchanged.
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
size = globalSize(t, m, "ro")
|
2020-07-30 11:49:14 +00:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
2018-07-12 08:15:57 +00:00
|
|
|
t.Fatalf("Global: expected 2 files and 2 directories: %+v", size)
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 2 || size.Directories != 2 {
|
|
|
|
t.Fatalf("Local: expected 2 files and 2 directories: %+v", size)
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files+size.Directories == 0 {
|
|
|
|
t.Fatalf("ROChanged: expected something: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revert should delete the unknown stuff
|
|
|
|
|
|
|
|
m.Revert("ro")
|
|
|
|
|
|
|
|
// These should still exist
|
2019-03-20 17:38:03 +00:00
|
|
|
for _, p := range []string{"knownDir/knownFile", "ignDir/ignFile"} {
|
|
|
|
if _, err := ffs.Stat(p); err != nil {
|
2018-07-12 08:15:57 +00:00
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These should have been removed
|
2019-03-20 17:38:03 +00:00
|
|
|
for _, p := range []string{"unknownDir", "unknownDir/unknownFile"} {
|
|
|
|
if _, err := ffs.Stat(p); !fs.IsNotExist(err) {
|
2018-07-12 08:15:57 +00:00
|
|
|
t.Error("Unexpected existing thing:", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should now have one file and directory again.
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
size = globalSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 files and 1 directories: %+v", size)
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 files and 1 directories: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecvOnlyRevertNeeds(t *testing.T) {
|
|
|
|
// Make sure that a new file gets picked up and considered latest, then
|
|
|
|
// gets considered old when we hit Revert.
|
|
|
|
|
2019-03-20 17:38:03 +00:00
|
|
|
// Get us a model up and running
|
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2022-04-10 18:55:05 +00:00
|
|
|
ffs := f.Filesystem(nil)
|
2020-02-12 06:47:05 +00:00
|
|
|
defer cleanupModel(m)
|
2023-07-29 08:24:44 +00:00
|
|
|
conn := addFakeConn(m, device1, f.ID)
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0o755))
|
2018-07-12 08:15:57 +00:00
|
|
|
oldData := []byte("hello\n")
|
2019-03-20 17:38:03 +00:00
|
|
|
knownFiles := setupKnownFiles(t, ffs, oldData)
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
// Send and index update for the known stuff
|
|
|
|
|
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
|
|
|
must(t, m.Index(conn, &protocol.Index{Folder: "ro", Files: knownFiles}))
|
2019-04-07 11:29:17 +00:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
// Scan the folder.
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
// Everything should be in sync.
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
size := globalSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2021-06-05 15:01:23 +00:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("ROChanged: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the file.
|
|
|
|
|
|
|
|
newData := []byte("totally different data\n")
|
2023-05-03 08:25:36 +00:00
|
|
|
writeFilePerm(t, ffs, "knownDir/knownFile", newData, 0o644)
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
// Rescan.
|
|
|
|
|
2019-03-09 18:45:36 +00:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
// We now have a newer file than the rest of the cluster. Global state should reflect this.
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
size = globalSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
const sizeOfDir = 128
|
2018-10-10 10:43:07 +00:00
|
|
|
if size.Files != 1 || size.Bytes != sizeOfDir+int64(len(oldData)) {
|
|
|
|
t.Fatalf("Global: expected no change due to the new file: %+v", size)
|
2018-07-12 08:15:57 +00:00
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 1 || size.Bytes != sizeOfDir+int64(len(newData)) {
|
|
|
|
t.Fatalf("Local: expected the new file to be reflected: %+v", size)
|
|
|
|
}
|
2021-06-05 15:01:23 +00:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files+size.Directories == 0 {
|
|
|
|
t.Fatalf("ROChanged: expected something: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We hit the Revert button. The file that was new should become old.
|
|
|
|
|
|
|
|
m.Revert("ro")
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
size = globalSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 1 || size.Bytes != sizeOfDir+int64(len(oldData)) {
|
|
|
|
t.Fatalf("Global: expected the global size to revert: %+v", size)
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 1 || size.Bytes != sizeOfDir+int64(len(newData)) {
|
|
|
|
t.Fatalf("Local: expected the local size to remain: %+v", size)
|
|
|
|
}
|
2021-06-05 15:01:23 +00:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2018-07-12 08:15:57 +00:00
|
|
|
if size.Files != 1 || size.Bytes != int64(len(oldData)) {
|
|
|
|
t.Fatalf("Local: expected to need the old file data: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 10:43:07 +00:00
|
|
|
func TestRecvOnlyUndoChanges(t *testing.T) {
|
2019-03-20 17:38:03 +00:00
|
|
|
// Get us a model up and running
|
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2022-04-10 18:55:05 +00:00
|
|
|
ffs := f.Filesystem(nil)
|
2020-02-12 06:47:05 +00:00
|
|
|
defer cleanupModel(m)
|
2023-07-29 08:24:44 +00:00
|
|
|
conn := addFakeConn(m, device1, f.ID)
|
2018-10-10 10:43:07 +00:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0o755))
|
2018-10-10 10:43:07 +00:00
|
|
|
oldData := []byte("hello\n")
|
2019-03-20 17:38:03 +00:00
|
|
|
knownFiles := setupKnownFiles(t, ffs, oldData)
|
2018-10-10 10:43:07 +00:00
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
// Send an index update for the known stuff
|
2018-10-10 10:43:07 +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
|
|
|
must(t, m.Index(conn, &protocol.Index{Folder: "ro", Files: knownFiles}))
|
2019-04-07 11:29:17 +00:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
2018-10-10 10:43:07 +00:00
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
// Scan the folder.
|
2018-10-10 10:43:07 +00:00
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-10-10 10:43:07 +00:00
|
|
|
|
|
|
|
// Everything should be in sync.
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
size := globalSize(t, m, "ro")
|
2018-10-10 10:43:07 +00:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = localSize(t, m, "ro")
|
2018-10-10 10:43:07 +00:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2021-06-05 15:01:23 +00:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2018-10-10 10:43:07 +00:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-10-10 10:43:07 +00:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("ROChanged: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a file and modify another
|
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
const file = "foo"
|
2023-05-03 08:25:36 +00:00
|
|
|
writeFilePerm(t, ffs, file, []byte("hello\n"), 0o644)
|
|
|
|
writeFilePerm(t, ffs, "knownDir/knownFile", []byte("bye\n"), 0o644)
|
2018-10-10 10:43:07 +00:00
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-10-10 10:43:07 +00:00
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-10-10 10:43:07 +00:00
|
|
|
if size.Files != 2 {
|
|
|
|
t.Fatalf("Receive only: expected 2 files: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the file again and undo the modification
|
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
must(t, ffs.Remove(file))
|
2023-05-03 08:25:36 +00:00
|
|
|
writeFilePerm(t, ffs, "knownDir/knownFile", oldData, 0o644)
|
2020-02-12 06:47:05 +00:00
|
|
|
must(t, ffs.Chtimes("knownDir/knownFile", knownFiles[1].ModTime(), knownFiles[1].ModTime()))
|
2018-10-10 10:43:07 +00:00
|
|
|
|
2020-02-12 06:47:05 +00:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-10-10 10:43:07 +00:00
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-10-10 10:43:07 +00:00
|
|
|
if size.Files+size.Directories+size.Deleted != 0 {
|
|
|
|
t.Fatalf("Receive only: expected all zero: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:14:25 +00:00
|
|
|
func TestRecvOnlyDeletedRemoteDrop(t *testing.T) {
|
|
|
|
// Get us a model up and running
|
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2022-04-10 18:55:05 +00:00
|
|
|
ffs := f.Filesystem(nil)
|
2020-04-02 14:14:25 +00:00
|
|
|
defer cleanupModel(m)
|
2023-07-29 08:24:44 +00:00
|
|
|
conn := addFakeConn(m, device1, f.ID)
|
2020-04-02 14:14:25 +00:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0o755))
|
2020-04-02 14:14:25 +00:00
|
|
|
oldData := []byte("hello\n")
|
|
|
|
knownFiles := setupKnownFiles(t, ffs, oldData)
|
|
|
|
|
|
|
|
// Send an index update for the known stuff
|
|
|
|
|
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
|
|
|
must(t, m.Index(conn, &protocol.Index{Folder: "ro", Files: knownFiles}))
|
2020-04-02 14:14:25 +00:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
|
|
|
|
|
|
|
// Scan the folder.
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
// Everything should be in sync.
|
|
|
|
|
|
|
|
size := globalSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
|
|
|
size = localSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2021-06-05 15:01:23 +00:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2020-04-02 14:14:25 +00:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("ROChanged: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete our file
|
|
|
|
|
|
|
|
must(t, ffs.Remove(knownFiles[1].Name))
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Deleted != 1 {
|
|
|
|
t.Fatalf("Receive only: expected 1 deleted: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the remote
|
|
|
|
|
|
|
|
f.fset.Drop(device1)
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Deleted != 0 {
|
|
|
|
t.Fatalf("Receive only: expected no deleted: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 13:54:00 +00:00
|
|
|
func TestRecvOnlyRemoteUndoChanges(t *testing.T) {
|
|
|
|
// Get us a model up and running
|
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2022-04-10 18:55:05 +00:00
|
|
|
ffs := f.Filesystem(nil)
|
2020-08-27 13:54:00 +00:00
|
|
|
defer cleanupModel(m)
|
2023-07-29 08:24:44 +00:00
|
|
|
conn := addFakeConn(m, device1, f.ID)
|
2020-08-27 13:54:00 +00:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0o755))
|
2020-08-27 13:54:00 +00:00
|
|
|
oldData := []byte("hello\n")
|
|
|
|
knownFiles := setupKnownFiles(t, ffs, oldData)
|
|
|
|
|
|
|
|
// Send an index update for the known stuff
|
|
|
|
|
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
|
|
|
must(t, m.Index(conn, &protocol.Index{Folder: "ro", Files: knownFiles}))
|
2020-08-27 13:54:00 +00:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
|
|
|
|
|
|
|
// Scan the folder.
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
// Everything should be in sync.
|
|
|
|
|
|
|
|
size := globalSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
|
|
|
size = localSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2021-06-05 15:01:23 +00:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2020-08-27 13:54:00 +00:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("ROChanged: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a file and modify another
|
|
|
|
|
|
|
|
const file = "foo"
|
|
|
|
knownFile := filepath.Join("knownDir", "knownFile")
|
2023-05-03 08:25:36 +00:00
|
|
|
writeFilePerm(t, ffs, file, []byte("hello\n"), 0o644)
|
|
|
|
writeFilePerm(t, ffs, knownFile, []byte("bye\n"), 0o644)
|
2020-08-27 13:54:00 +00:00
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files != 2 {
|
|
|
|
t.Fatalf("Receive only: expected 2 files: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the same changes on the remote
|
|
|
|
|
|
|
|
files := make([]protocol.FileInfo, 0, 2)
|
2021-03-07 12:43:22 +00:00
|
|
|
snap := fsetSnapshot(t, f.fset)
|
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(f protocol.FileInfo) bool {
|
|
|
|
if f.Name != file && f.Name != knownFile {
|
2020-08-27 13:54:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
f.LocalFlags = 0
|
|
|
|
f.Version = protocol.Vector{}.Update(device1.Short())
|
|
|
|
files = append(files, f)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
snap.Release()
|
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
|
|
|
must(t, m.IndexUpdate(conn, &protocol.IndexUpdate{Folder: "ro", Files: files}))
|
2020-08-27 13:54:00 +00:00
|
|
|
|
2020-12-20 17:13:35 +00:00
|
|
|
// Ensure the pull to resolve conflicts (content identical) happened
|
|
|
|
must(t, f.doInSync(func() error {
|
|
|
|
f.pull()
|
|
|
|
return nil
|
|
|
|
}))
|
2020-08-27 13:54:00 +00:00
|
|
|
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files+size.Directories+size.Deleted != 0 {
|
|
|
|
t.Fatalf("Receive only: expected all zero: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 13:08:56 +00:00
|
|
|
func TestRecvOnlyRevertOwnID(t *testing.T) {
|
|
|
|
// If the folder was receive-only in the past, the global item might have
|
|
|
|
// only our id in the version vector and be valid. There was a bug based on
|
|
|
|
// the incorrect assumption that this can never happen.
|
|
|
|
|
|
|
|
// Get us a model up and running
|
|
|
|
|
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2022-04-10 18:55:05 +00:00
|
|
|
ffs := f.Filesystem(nil)
|
2021-06-03 13:08:56 +00:00
|
|
|
defer cleanupModel(m)
|
2023-07-29 08:24:44 +00:00
|
|
|
conn := addFakeConn(m, device1, f.ID)
|
2021-06-03 13:08:56 +00:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0o755))
|
2021-06-03 13:08:56 +00:00
|
|
|
data := []byte("hello\n")
|
|
|
|
name := "foo"
|
2023-05-03 08:25:36 +00:00
|
|
|
writeFilePerm(t, ffs, name, data, 0o644)
|
2021-06-03 13:08:56 +00:00
|
|
|
|
|
|
|
// Make sure the file is scanned and locally changed
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
fi, ok := m.testCurrentFolderFile(f.ID, name)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("File missing")
|
|
|
|
} else if !fi.IsReceiveOnlyChanged() {
|
|
|
|
t.Fatal("File should be receiveonly changed")
|
|
|
|
}
|
|
|
|
fi.LocalFlags = 0
|
|
|
|
v := fi.Version.Counters[0].Value
|
|
|
|
fi.Version.Counters[0].Value = uint64(time.Unix(int64(v), 0).Add(-10 * time.Second).Unix())
|
|
|
|
|
|
|
|
// Monitor the outcome
|
|
|
|
sub := f.evLogger.Subscribe(events.LocalIndexUpdated)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-sub.C():
|
|
|
|
if file, _ := m.testCurrentFolderFile(f.ID, name); file.Deleted {
|
|
|
|
t.Error("local file was deleted")
|
|
|
|
cancel()
|
|
|
|
} else if file.IsEquivalent(fi, f.modTimeWindow) {
|
|
|
|
cancel() // That's what we are waiting for
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Receive an index update with an older version, but valid and then revert
|
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
|
|
|
must(t, m.Index(conn, &protocol.Index{Folder: f.ID, Files: []protocol.FileInfo{fi}}))
|
2021-06-03 13:08:56 +00:00
|
|
|
f.Revert()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 09:29:20 +00:00
|
|
|
func TestRecvOnlyLocalChangeDoesNotCauseConflict(t *testing.T) {
|
|
|
|
// Get us a model up and running
|
|
|
|
|
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
|
|
|
ffs := f.Filesystem(nil)
|
|
|
|
defer cleanupModel(m)
|
|
|
|
conn := addFakeConn(m, device1, f.ID)
|
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0o755))
|
|
|
|
oldData := []byte("hello\n")
|
|
|
|
knownFiles := setupKnownFiles(t, ffs, oldData)
|
|
|
|
|
|
|
|
// Send an index update for the known stuff
|
|
|
|
|
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
|
|
|
must(t, m.Index(conn, &protocol.Index{Folder: "ro", Files: knownFiles}))
|
2024-01-08 09:29:20 +00:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
|
|
|
|
|
|
|
// Scan the folder.
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
// Everything should be in sync.
|
|
|
|
|
|
|
|
size := globalSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
|
|
|
size = localSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
|
|
|
size = needSizeLocal(t, m, "ro")
|
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("ROChanged: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify the file
|
|
|
|
|
|
|
|
writeFilePerm(t, ffs, "knownDir/knownFile", []byte("change1\n"), 0o644)
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files != 1 {
|
|
|
|
t.Fatalf("Receive only: expected 1 file: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform another modification. This should not cause the file to be needed.
|
|
|
|
// This is a regression test: Previously on scan the file version was changed to conflict with the global
|
|
|
|
// version, thus being needed and creating a conflict copy on next pull.
|
|
|
|
|
|
|
|
writeFilePerm(t, ffs, "knownDir/knownFile", []byte("change2\n"), 0o644)
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
size = needSizeLocal(t, m, "ro")
|
|
|
|
if size.Files != 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 17:38:03 +00:00
|
|
|
func setupKnownFiles(t *testing.T, ffs fs.Filesystem, data []byte) []protocol.FileInfo {
|
|
|
|
t.Helper()
|
2019-01-11 12:56:05 +00:00
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
must(t, ffs.MkdirAll("knownDir", 0o755))
|
|
|
|
writeFilePerm(t, ffs, "knownDir/knownFile", data, 0o644)
|
2018-07-12 08:15:57 +00:00
|
|
|
|
|
|
|
t0 := time.Now().Add(-1 * time.Minute)
|
2019-03-20 17:38:03 +00:00
|
|
|
must(t, ffs.Chtimes("knownDir/knownFile", t0, t0))
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2019-03-20 17:38:03 +00:00
|
|
|
fi, err := ffs.Stat("knownDir/knownFile")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-08-19 21:34:26 +00:00
|
|
|
blocks, _ := scanner.Blocks(context.TODO(), bytes.NewReader(data), protocol.BlockSize(int64(len(data))), int64(len(data)), nil, true)
|
2018-07-12 08:15:57 +00:00
|
|
|
knownFiles := []protocol.FileInfo{
|
|
|
|
{
|
|
|
|
Name: "knownDir",
|
|
|
|
Type: protocol.FileInfoTypeDirectory,
|
2023-05-03 08:25:36 +00:00
|
|
|
Permissions: 0o755,
|
2018-07-12 08:15:57 +00:00
|
|
|
Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 42}}},
|
|
|
|
Sequence: 42,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "knownDir/knownFile",
|
|
|
|
Type: protocol.FileInfoTypeFile,
|
2023-05-03 08:25:36 +00:00
|
|
|
Permissions: 0o644,
|
2018-07-12 08:15:57 +00:00
|
|
|
Size: fi.Size(),
|
|
|
|
ModifiedS: fi.ModTime().Unix(),
|
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
|
|
|
ModifiedNs: int32(fi.ModTime().Nanosecond()),
|
2018-07-12 08:15:57 +00:00
|
|
|
Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 42}}},
|
|
|
|
Sequence: 42,
|
2018-08-19 21:34:26 +00:00
|
|
|
Blocks: blocks,
|
2018-07-12 08:15:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return knownFiles
|
|
|
|
}
|
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
func setupROFolder(t *testing.T) (*testModel, *receiveOnlyFolder, context.CancelFunc) {
|
2020-02-12 06:47:05 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
w, cancel := newConfigWrapper(defaultCfg)
|
2020-04-02 14:14:25 +00:00
|
|
|
cfg := w.RawCopy()
|
2023-05-03 08:25:36 +00:00
|
|
|
fcfg := newFolderConfig()
|
2019-03-20 17:38:03 +00:00
|
|
|
fcfg.ID = "ro"
|
2020-02-12 06:47:05 +00:00
|
|
|
fcfg.Label = "ro"
|
2018-07-12 08:15:57 +00:00
|
|
|
fcfg.Type = config.FolderTypeReceiveOnly
|
2020-04-02 14:14:25 +00:00
|
|
|
cfg.Folders = []config.FolderConfiguration{fcfg}
|
2021-01-15 14:43:34 +00:00
|
|
|
replace(t, w, cfg)
|
2019-11-29 08:29:59 +00:00
|
|
|
|
2023-09-06 10:52:01 +00:00
|
|
|
m := newModel(t, w, myID, nil)
|
2019-11-22 20:30:16 +00:00
|
|
|
m.ServeBackground()
|
2020-11-17 12:19:04 +00:00
|
|
|
<-m.started
|
2020-02-12 06:47:05 +00:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2023-12-11 21:06:45 +00:00
|
|
|
m.mut.RLock()
|
|
|
|
defer m.mut.RUnlock()
|
2023-09-02 14:42:46 +00:00
|
|
|
r, _ := m.folderRunners.Get("ro")
|
|
|
|
f := r.(*receiveOnlyFolder)
|
2019-03-20 17:38:03 +00:00
|
|
|
|
2021-01-15 14:43:34 +00:00
|
|
|
return m, f, cancel
|
2019-11-29 08:29:59 +00:00
|
|
|
}
|