2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// 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,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-30 08:56:02 -07:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2016-04-26 07:01:46 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2018-02-25 01:39:00 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2019-08-15 07:29:37 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2019-03-10 23:28:54 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore"
|
2018-02-25 01:39:00 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2016-05-04 03:47:33 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/versioner"
|
2014-09-30 08:56:02 -07:00
|
|
|
)
|
|
|
|
|
2016-05-04 03:47:33 -07:00
|
|
|
func init() {
|
2016-12-16 15:23:35 -07:00
|
|
|
folderFactories[config.FolderTypeSendOnly] = newSendOnlyFolder
|
2016-05-04 03:47:33 -07:00
|
|
|
}
|
|
|
|
|
2016-12-16 15:23:35 -07:00
|
|
|
type sendOnlyFolder struct {
|
2016-04-26 07:01:46 -07:00
|
|
|
folder
|
2014-09-30 08:56:02 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 14:26:25 -07:00
|
|
|
func newSendOnlyFolder(model *model, fset *db.FileSet, ignores *ignore.Matcher, cfg config.FolderConfiguration, _ versioner.Versioner, evLogger events.Logger, ioLimiter *byteSemaphore) service {
|
2018-05-11 01:45:13 -07:00
|
|
|
f := &sendOnlyFolder{
|
2020-07-14 01:48:50 -07:00
|
|
|
folder: newFolder(model, fset, ignores, cfg, evLogger, ioLimiter, nil),
|
2014-09-30 08:56:02 -07:00
|
|
|
}
|
2018-05-11 01:45:13 -07:00
|
|
|
f.folder.puller = f
|
|
|
|
return f
|
2014-09-30 08:56:02 -07:00
|
|
|
}
|
|
|
|
|
2018-01-14 10:01:06 -07:00
|
|
|
func (f *sendOnlyFolder) PullErrors() []FileError {
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-25 01:39:00 -07:00
|
|
|
|
|
|
|
// pull checks need for files that only differ by metadata (no changes on disk)
|
2018-05-11 01:45:13 -07:00
|
|
|
func (f *sendOnlyFolder) pull() bool {
|
2018-02-25 01:39:00 -07:00
|
|
|
batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
|
|
|
|
batchSizeBytes := 0
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
snap := f.fset.Snapshot()
|
|
|
|
defer snap.Release()
|
2020-05-30 00:50:23 -07:00
|
|
|
snap.WithNeed(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
|
2018-02-25 01:39:00 -07:00
|
|
|
if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
|
2019-04-07 04:29:17 -07:00
|
|
|
f.updateLocalsFromPulling(batch)
|
2018-02-25 01:39:00 -07:00
|
|
|
batch = batch[:0]
|
|
|
|
batchSizeBytes = 0
|
|
|
|
}
|
|
|
|
|
2019-03-10 23:28:54 -07:00
|
|
|
if f.ignores.ShouldIgnore(intf.FileName()) {
|
2018-02-25 01:39:00 -07:00
|
|
|
file := intf.(protocol.FileInfo)
|
2021-02-08 07:30:39 -07:00
|
|
|
file.SetIgnored()
|
2018-02-25 01:39:00 -07:00
|
|
|
batch = append(batch, file)
|
|
|
|
batchSizeBytes += file.ProtoSize()
|
|
|
|
l.Debugln(f, "Handling ignored file", file)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
curFile, ok := snap.Get(protocol.LocalDeviceID, intf.FileName())
|
2018-02-25 01:39:00 -07:00
|
|
|
if !ok {
|
|
|
|
if intf.IsDeleted() {
|
2019-07-23 01:51:16 -07:00
|
|
|
l.Debugln("Should never get a deleted file as needed when we don't have it")
|
2020-10-07 01:05:13 -07:00
|
|
|
f.evLogger.Log(events.Failure, "got deleted file that doesn't exist locally as needed when pulling on send-only")
|
2018-02-25 01:39:00 -07:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
file := intf.(protocol.FileInfo)
|
2020-08-24 23:11:14 -07:00
|
|
|
if !file.IsEquivalentOptional(curFile, f.modTimeWindow, f.IgnorePerms, false, 0) {
|
2018-02-25 01:39:00 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
batch = append(batch, file)
|
|
|
|
batchSizeBytes += file.ProtoSize()
|
|
|
|
l.Debugln(f, "Merging versions of identical file", file)
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(batch) > 0 {
|
2019-04-07 04:29:17 -07:00
|
|
|
f.updateLocalsFromPulling(batch)
|
2018-02-25 01:39:00 -07:00
|
|
|
}
|
2018-05-11 01:45:13 -07:00
|
|
|
|
|
|
|
return true
|
2018-02-25 01:39:00 -07:00
|
|
|
}
|
2018-05-20 23:56:24 -07:00
|
|
|
|
2019-04-07 04:29:17 -07:00
|
|
|
func (f *sendOnlyFolder) Override() {
|
2020-03-27 05:05:09 -07:00
|
|
|
f.doInSync(func() error { f.override(); return nil })
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *sendOnlyFolder) override() {
|
2020-12-12 04:32:24 -07:00
|
|
|
l.Infoln("Overriding global state on folder", f.Description())
|
2020-03-24 04:51:17 -07:00
|
|
|
|
2018-05-20 23:56:24 -07:00
|
|
|
f.setState(FolderScanning)
|
2020-03-27 05:05:09 -07:00
|
|
|
defer f.setState(FolderIdle)
|
|
|
|
|
2018-05-20 23:56:24 -07:00
|
|
|
batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
|
|
|
|
batchSizeBytes := 0
|
2020-01-21 10:23:08 -07:00
|
|
|
snap := f.fset.Snapshot()
|
|
|
|
defer snap.Release()
|
2020-05-30 00:50:23 -07:00
|
|
|
snap.WithNeed(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
|
2018-05-20 23:56:24 -07:00
|
|
|
need := fi.(protocol.FileInfo)
|
|
|
|
if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
|
2019-04-07 04:29:17 -07:00
|
|
|
f.updateLocalsFromScanning(batch)
|
2018-05-20 23:56:24 -07:00
|
|
|
batch = batch[:0]
|
|
|
|
batchSizeBytes = 0
|
|
|
|
}
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
have, ok := snap.Get(protocol.LocalDeviceID, need.Name)
|
2018-05-20 23:56:24 -07:00
|
|
|
// Don't override files that are in a bad state (ignored,
|
|
|
|
// unsupported, must rescan, ...).
|
|
|
|
if ok && have.IsInvalid() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !ok || have.Name != need.Name {
|
|
|
|
// We are missing the file
|
2020-02-19 08:58:09 -07:00
|
|
|
need.SetDeleted(f.shortID)
|
2018-05-20 23:56:24 -07:00
|
|
|
} else {
|
|
|
|
// We have the file, replace with our version
|
|
|
|
have.Version = have.Version.Merge(need.Version).Update(f.shortID)
|
|
|
|
need = have
|
|
|
|
}
|
|
|
|
need.Sequence = 0
|
|
|
|
batch = append(batch, need)
|
|
|
|
batchSizeBytes += need.ProtoSize()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if len(batch) > 0 {
|
2019-04-07 04:29:17 -07:00
|
|
|
f.updateLocalsFromScanning(batch)
|
2018-05-20 23:56:24 -07:00
|
|
|
}
|
|
|
|
}
|