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"
|
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"
|
2021-06-25 09:38:04 +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
|
|
|
}
|
|
|
|
|
2021-06-25 09:38:04 +00: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 08:45:13 +00:00
|
|
|
f := &sendOnlyFolder{
|
2020-07-14 08:48:50 +00:00
|
|
|
folder: newFolder(model, fset, ignores, cfg, evLogger, ioLimiter, nil),
|
2014-09-30 15:56:02 +00:00
|
|
|
}
|
2018-05-11 08:45:13 +00:00
|
|
|
f.folder.puller = f
|
|
|
|
return f
|
2014-09-30 15:56:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*sendOnlyFolder) PullErrors() []FileError {
|
2018-01-14 17:01:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-25 08:39:00 +00:00
|
|
|
|
|
|
|
// pull checks need for files that only differ by metadata (no changes on disk)
|
2021-03-07 12:43:22 +00:00
|
|
|
func (f *sendOnlyFolder) pull() (bool, error) {
|
2021-04-29 20:01:46 +00:00
|
|
|
batch := db.NewFileInfoBatch(func(files []protocol.FileInfo) error {
|
2021-04-26 13:36:51 +00:00
|
|
|
f.updateLocalsFromPulling(files)
|
|
|
|
return nil
|
|
|
|
})
|
2018-02-25 08:39:00 +00:00
|
|
|
|
2021-03-07 12:43:22 +00:00
|
|
|
snap, err := f.dbSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
defer snap.Release()
|
2020-05-30 07:50:23 +00:00
|
|
|
snap.WithNeed(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
|
2021-04-29 20:01:46 +00:00
|
|
|
batch.FlushIfFull()
|
2021-04-26 13:36:51 +00:00
|
|
|
|
|
|
|
file := intf.(protocol.FileInfo)
|
2018-02-25 08:39:00 +00:00
|
|
|
|
2019-03-11 06:28:54 +00:00
|
|
|
if f.ignores.ShouldIgnore(intf.FileName()) {
|
2021-02-08 14:30:39 +00:00
|
|
|
file.SetIgnored()
|
2021-04-29 20:01:46 +00:00
|
|
|
batch.Append(file)
|
2018-02-25 08:39:00 +00:00
|
|
|
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 {
|
2021-04-26 13:36:51 +00:00
|
|
|
if intf.IsInvalid() {
|
|
|
|
// Global invalid file just exists for need accounting
|
2021-04-29 20:01:46 +00:00
|
|
|
batch.Append(file)
|
2021-04-26 13:36:51 +00:00
|
|
|
} else 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")
|
2020-10-07 08:05:13 +00:00
|
|
|
f.evLogger.Log(events.Failure, "got deleted file that doesn't exist locally as needed when pulling on send-only")
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-26 06:24:58 +00:00
|
|
|
if !file.IsEquivalentOptional(curFile, protocol.FileInfoComparison{
|
|
|
|
ModTimeWindow: f.modTimeWindow,
|
|
|
|
IgnorePerms: f.IgnorePerms,
|
|
|
|
IgnoreOwnership: !f.SyncOwnership,
|
2022-09-14 07:50:55 +00:00
|
|
|
IgnoreXattrs: !f.SyncXattrs,
|
2022-07-26 06:24:58 +00:00
|
|
|
}) {
|
2018-02-25 08:39:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-04-29 20:01:46 +00:00
|
|
|
batch.Append(file)
|
2018-02-25 08:39:00 +00:00
|
|
|
l.Debugln(f, "Merging versions of identical file", file)
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2021-04-29 20:01:46 +00:00
|
|
|
batch.Flush()
|
2018-05-11 08:45:13 +00:00
|
|
|
|
2021-03-07 12:43:22 +00:00
|
|
|
return true, nil
|
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() {
|
2021-03-07 12:43:22 +00:00
|
|
|
f.doInSync(f.override)
|
2020-03-27 12:05:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 12:43:22 +00:00
|
|
|
func (f *sendOnlyFolder) override() error {
|
2020-12-12 11:32:24 +00:00
|
|
|
l.Infoln("Overriding global state on folder", f.Description())
|
2020-03-24 11:51:17 +00:00
|
|
|
|
2018-05-21 06:56:24 +00:00
|
|
|
f.setState(FolderScanning)
|
2020-03-27 12:05:09 +00:00
|
|
|
defer f.setState(FolderIdle)
|
|
|
|
|
2021-04-29 20:01:46 +00:00
|
|
|
batch := db.NewFileInfoBatch(func(files []protocol.FileInfo) error {
|
|
|
|
f.updateLocalsFromScanning(files)
|
|
|
|
return nil
|
|
|
|
})
|
2021-03-07 12:43:22 +00:00
|
|
|
snap, err := f.dbSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-21 17:23:08 +00:00
|
|
|
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)
|
2021-04-29 20:01:46 +00:00
|
|
|
_ = batch.FlushIfFull()
|
2018-05-21 06:56:24 +00:00
|
|
|
|
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
|
2021-04-29 20:01:46 +00:00
|
|
|
batch.Append(need)
|
2018-05-21 06:56:24 +00:00
|
|
|
return true
|
|
|
|
})
|
2021-04-29 20:01:46 +00:00
|
|
|
return batch.Flush()
|
2018-05-21 06:56:24 +00:00
|
|
|
}
|