2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00: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-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-30 15:56:02 +00:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2016-04-26 14:01:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2018-02-25 08:39:00 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2019-08-15 14:29:37 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2016-08-05 17:45:45 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2019-03-11 06:28:54 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore"
|
2018-02-25 08:39:00 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2019-07-09 09:40:30 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2016-05-04 10:47:33 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/versioner"
|
2014-09-30 15:56:02 +00:00
|
|
|
)
|
|
|
|
|
2016-05-04 10:47:33 +00:00
|
|
|
func init() {
|
2016-12-16 22:23:35 +00:00
|
|
|
folderFactories[config.FolderTypeSendOnly] = newSendOnlyFolder
|
2016-05-04 10:47:33 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 22:23:35 +00:00
|
|
|
type sendOnlyFolder struct {
|
2016-04-26 14:01:46 +00:00
|
|
|
folder
|
2014-09-30 15:56:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 07:02:18 +00:00
|
|
|
func newSendOnlyFolder(model *model, fset *db.FileSet, ignores *ignore.Matcher, cfg config.FolderConfiguration, _ versioner.Versioner, _ fs.Filesystem, evLogger events.Logger, ioLimiter *byteSemaphore) service {
|
2018-05-11 08:45:13 +00:00
|
|
|
f := &sendOnlyFolder{
|
2020-02-01 07:02:18 +00:00
|
|
|
folder: newFolder(model, fset, ignores, cfg, evLogger, ioLimiter),
|
2014-09-30 15:56:02 +00:00
|
|
|
}
|
2018-05-11 08:45:13 +00:00
|
|
|
f.folder.puller = f
|
2019-11-21 07:41:15 +00:00
|
|
|
f.folder.Service = util.AsService(f.serve, f.String())
|
2018-05-11 08:45:13 +00:00
|
|
|
return f
|
2014-09-30 15:56:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:01:06 +00:00
|
|
|
func (f *sendOnlyFolder) PullErrors() []FileError {
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-25 08:39:00 +00:00
|
|
|
|
|
|
|
// pull checks need for files that only differ by metadata (no changes on disk)
|
2018-05-11 08:45:13 +00:00
|
|
|
func (f *sendOnlyFolder) pull() bool {
|
2018-02-25 08:39:00 +00:00
|
|
|
select {
|
|
|
|
case <-f.initialScanFinished:
|
|
|
|
default:
|
|
|
|
// Once the initial scan finished, a pull will be scheduled
|
2018-05-11 08:45:13 +00:00
|
|
|
return false
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
|
|
|
|
batchSizeBytes := 0
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
snap := f.fset.Snapshot()
|
|
|
|
defer snap.Release()
|
2020-05-30 07:50:23 +00:00
|
|
|
snap.WithNeed(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
|
2018-02-25 08:39:00 +00:00
|
|
|
if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
|
2019-04-07 11:29:17 +00:00
|
|
|
f.updateLocalsFromPulling(batch)
|
2018-02-25 08:39:00 +00:00
|
|
|
batch = batch[:0]
|
|
|
|
batchSizeBytes = 0
|
|
|
|
}
|
|
|
|
|
2019-03-11 06:28:54 +00:00
|
|
|
if f.ignores.ShouldIgnore(intf.FileName()) {
|
2018-02-25 08:39:00 +00:00
|
|
|
file := intf.(protocol.FileInfo)
|
2018-06-24 07:50:18 +00:00
|
|
|
file.SetIgnored(f.shortID)
|
2018-02-25 08:39:00 +00:00
|
|
|
batch = append(batch, file)
|
|
|
|
batchSizeBytes += file.ProtoSize()
|
|
|
|
l.Debugln(f, "Handling ignored file", file)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
curFile, ok := snap.Get(protocol.LocalDeviceID, intf.FileName())
|
2018-02-25 08:39:00 +00:00
|
|
|
if !ok {
|
|
|
|
if intf.IsDeleted() {
|
2019-07-23 08:51:16 +00:00
|
|
|
l.Debugln("Should never get a deleted file as needed when we don't have it")
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
file := intf.(protocol.FileInfo)
|
2019-07-23 19:48:53 +00:00
|
|
|
if !file.IsEquivalentOptional(curFile, f.ModTimeWindow(), f.IgnorePerms, false, 0) {
|
2018-02-25 08:39:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
file.Version = file.Version.Merge(curFile.Version)
|
|
|
|
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 11:29:17 +00:00
|
|
|
f.updateLocalsFromPulling(batch)
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
2018-05-11 08:45:13 +00:00
|
|
|
|
|
|
|
return true
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
2018-05-21 06:56:24 +00:00
|
|
|
|
2019-04-07 11:29:17 +00:00
|
|
|
func (f *sendOnlyFolder) Override() {
|
2020-03-27 12:05:09 +00:00
|
|
|
f.doInSync(func() error { f.override(); return nil })
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *sendOnlyFolder) override() {
|
2020-03-24 11:51:17 +00:00
|
|
|
l.Infof("Overriding global state on folder %v", f.Description)
|
|
|
|
|
2018-05-21 06:56:24 +00:00
|
|
|
f.setState(FolderScanning)
|
2020-03-27 12:05:09 +00:00
|
|
|
defer f.setState(FolderIdle)
|
|
|
|
|
2018-05-21 06:56:24 +00:00
|
|
|
batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
|
|
|
|
batchSizeBytes := 0
|
2020-01-21 17:23:08 +00:00
|
|
|
snap := f.fset.Snapshot()
|
|
|
|
defer snap.Release()
|
2020-05-30 07:50:23 +00:00
|
|
|
snap.WithNeed(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
|
2018-05-21 06:56:24 +00:00
|
|
|
need := fi.(protocol.FileInfo)
|
|
|
|
if len(batch) == maxBatchSizeFiles || batchSizeBytes > maxBatchSizeBytes {
|
2019-04-07 11:29:17 +00:00
|
|
|
f.updateLocalsFromScanning(batch)
|
2018-05-21 06:56:24 +00:00
|
|
|
batch = batch[:0]
|
|
|
|
batchSizeBytes = 0
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
have, ok := snap.Get(protocol.LocalDeviceID, need.Name)
|
2018-05-21 06:56:24 +00: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 15:58:09 +00:00
|
|
|
need.SetDeleted(f.shortID)
|
2018-05-21 06:56:24 +00: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 11:29:17 +00:00
|
|
|
f.updateLocalsFromScanning(batch)
|
2018-05-21 06:56:24 +00:00
|
|
|
}
|
|
|
|
}
|