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"
|
2021-06-25 02:38:04 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
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
|
|
|
}
|
|
|
|
|
2021-06-25 02:38:04 -07:00
|
|
|
func newSendOnlyFolder(model *model, fset *db.FileSet, ignores *ignore.Matcher, cfg config.FolderConfiguration, _ versioner.Versioner, evLogger events.Logger, ioLimiter *util.Semaphore) 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)
|
2021-03-07 05:43:22 -07:00
|
|
|
func (f *sendOnlyFolder) pull() (bool, error) {
|
2021-04-29 13:01:46 -07:00
|
|
|
batch := db.NewFileInfoBatch(func(files []protocol.FileInfo) error {
|
2021-04-26 06:36:51 -07:00
|
|
|
f.updateLocalsFromPulling(files)
|
|
|
|
return nil
|
|
|
|
})
|
2018-02-25 01:39:00 -07:00
|
|
|
|
2021-03-07 05:43:22 -07:00
|
|
|
snap, err := f.dbSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
defer snap.Release()
|
2020-05-30 00:50:23 -07:00
|
|
|
snap.WithNeed(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
|
2021-04-29 13:01:46 -07:00
|
|
|
batch.FlushIfFull()
|
2021-04-26 06:36:51 -07:00
|
|
|
|
|
|
|
file := intf.(protocol.FileInfo)
|
2018-02-25 01:39:00 -07:00
|
|
|
|
2019-03-10 23:28:54 -07:00
|
|
|
if f.ignores.ShouldIgnore(intf.FileName()) {
|
2021-02-08 07:30:39 -07:00
|
|
|
file.SetIgnored()
|
2021-04-29 13:01:46 -07:00
|
|
|
batch.Append(file)
|
2018-02-25 01:39:00 -07:00
|
|
|
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 {
|
2021-04-26 06:36:51 -07:00
|
|
|
if intf.IsInvalid() {
|
|
|
|
// Global invalid file just exists for need accounting
|
2021-04-29 13:01:46 -07:00
|
|
|
batch.Append(file)
|
2021-04-26 06:36:51 -07:00
|
|
|
} else 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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:01:46 -07:00
|
|
|
batch.Append(file)
|
2018-02-25 01:39:00 -07:00
|
|
|
l.Debugln(f, "Merging versions of identical file", file)
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2021-04-29 13:01:46 -07:00
|
|
|
batch.Flush()
|
2018-05-11 01:45:13 -07:00
|
|
|
|
2021-03-07 05:43:22 -07:00
|
|
|
return true, nil
|
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() {
|
2021-03-07 05:43:22 -07:00
|
|
|
f.doInSync(f.override)
|
2020-03-27 05:05:09 -07:00
|
|
|
}
|
|
|
|
|
2021-03-07 05:43:22 -07:00
|
|
|
func (f *sendOnlyFolder) override() error {
|
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)
|
|
|
|
|
2021-04-29 13:01:46 -07:00
|
|
|
batch := db.NewFileInfoBatch(func(files []protocol.FileInfo) error {
|
|
|
|
f.updateLocalsFromScanning(files)
|
|
|
|
return nil
|
|
|
|
})
|
2021-03-07 05:43:22 -07:00
|
|
|
snap, err := f.dbSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
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)
|
2021-04-29 13:01:46 -07:00
|
|
|
_ = batch.FlushIfFull()
|
2018-05-20 23:56:24 -07:00
|
|
|
|
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
|
2021-04-29 13:01:46 -07:00
|
|
|
batch.Append(need)
|
2018-05-20 23:56:24 -07:00
|
|
|
return true
|
|
|
|
})
|
2021-04-29 13:01:46 -07:00
|
|
|
return batch.Flush()
|
2018-05-20 23:56:24 -07:00
|
|
|
}
|