2015-10-27 10:37:03 +00:00
|
|
|
// Copyright (C) 2014 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-10-27 10:37:03 +00:00
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2024-05-24 06:51:02 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
2017-10-24 07:58:55 +00:00
|
|
|
"errors"
|
2016-11-17 15:12:41 +00:00
|
|
|
"fmt"
|
2022-09-14 07:50:55 +00:00
|
|
|
"path"
|
2023-05-03 08:25:36 +00:00
|
|
|
"path/filepath"
|
2020-11-20 13:21:54 +00:00
|
|
|
"sort"
|
2019-07-23 19:48:53 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-12-10 15:43:15 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/disk"
|
2015-10-27 10:37:03 +00:00
|
|
|
|
2022-07-28 17:36:39 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2022-04-10 18:55:05 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-10-27 10:37:03 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
var (
|
2018-03-18 00:42:31 +00:00
|
|
|
ErrPathNotDirectory = errors.New("folder path not a directory")
|
|
|
|
ErrPathMissing = errors.New("folder path missing")
|
2020-01-16 14:30:29 +00:00
|
|
|
ErrMarkerMissing = errors.New("folder marker missing (this indicates potential data loss, search docs/forum to get information about how to proceed)")
|
2017-10-24 07:58:55 +00:00
|
|
|
)
|
|
|
|
|
2020-11-03 18:09:32 +00:00
|
|
|
const (
|
|
|
|
DefaultMarkerName = ".stfolder"
|
2021-02-12 07:38:43 +00:00
|
|
|
EncryptionTokenName = "syncthing-encryption_password_token"
|
2020-11-03 18:09:32 +00:00
|
|
|
maxConcurrentWritesDefault = 2
|
|
|
|
maxConcurrentWritesLimit = 64
|
|
|
|
)
|
2017-11-05 12:18:05 +00:00
|
|
|
|
2015-10-27 10:37:03 +00:00
|
|
|
func (f FolderConfiguration) Copy() FolderConfiguration {
|
|
|
|
c := f
|
|
|
|
c.Devices = make([]FolderDeviceConfiguration, len(f.Devices))
|
|
|
|
copy(c.Devices, f.Devices)
|
2015-10-27 10:53:42 +00:00
|
|
|
c.Versioning = f.Versioning.Copy()
|
2015-10-27 10:37:03 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-04-10 18:55:05 +00:00
|
|
|
// Filesystem creates a filesystem for the path and options of this folder.
|
|
|
|
// The fset parameter may be nil, in which case no mtime handling on top of
|
2022-08-23 13:44:11 +00:00
|
|
|
// the filesystem is provided.
|
2022-04-10 18:55:05 +00:00
|
|
|
func (f FolderConfiguration) Filesystem(fset *db.FileSet) fs.Filesystem {
|
2015-10-27 10:37:03 +00:00
|
|
|
// This is intentionally not a pointer method, because things like
|
2022-04-10 18:55:05 +00:00
|
|
|
// cfg.Folders["default"].Filesystem(nil) should be valid.
|
|
|
|
opts := make([]fs.Option, 0, 3)
|
2020-08-19 17:58:51 +00:00
|
|
|
if f.FilesystemType == fs.FilesystemTypeBasic && f.JunctionsAsDirs {
|
2021-03-11 14:23:56 +00:00
|
|
|
opts = append(opts, new(fs.OptionJunctionsAsDirs))
|
2020-08-19 17:58:51 +00:00
|
|
|
}
|
2020-07-28 09:13:15 +00:00
|
|
|
if !f.CaseSensitiveFS {
|
2022-04-10 18:55:05 +00:00
|
|
|
opts = append(opts, new(fs.OptionDetectCaseConflicts))
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
2022-04-10 18:55:05 +00:00
|
|
|
if fset != nil {
|
|
|
|
opts = append(opts, fset.MtimeOption())
|
|
|
|
}
|
|
|
|
return fs.NewFilesystem(f.FilesystemType, f.Path, opts...)
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 19:48:53 +00:00
|
|
|
func (f FolderConfiguration) ModTimeWindow() time.Duration {
|
2020-08-25 06:11:14 +00:00
|
|
|
dur := time.Duration(f.RawModTimeWindowS) * time.Second
|
2022-07-28 17:36:39 +00:00
|
|
|
if f.RawModTimeWindowS < 1 && build.IsAndroid {
|
2022-04-10 18:55:05 +00:00
|
|
|
if usage, err := disk.Usage(f.Filesystem(nil).URI()); err != nil {
|
2020-08-25 06:11:14 +00:00
|
|
|
dur = 2 * time.Second
|
|
|
|
l.Debugf(`Detecting FS at "%v" on android: Setting mtime window to 2s: err == "%v"`, f.Path, err)
|
2021-09-27 19:29:51 +00:00
|
|
|
} else if strings.HasPrefix(strings.ToLower(usage.Fstype), "ext2") || strings.HasPrefix(strings.ToLower(usage.Fstype), "ext3") || strings.HasPrefix(strings.ToLower(usage.Fstype), "ext4") {
|
|
|
|
l.Debugf(`Detecting FS at %v on android: Leaving mtime window at 0: usage.Fstype == "%v"`, f.Path, usage.Fstype)
|
|
|
|
} else {
|
2020-08-25 06:11:14 +00:00
|
|
|
dur = 2 * time.Second
|
|
|
|
l.Debugf(`Detecting FS at "%v" on android: Setting mtime window to 2s: usage.Fstype == "%v"`, f.Path, usage.Fstype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dur
|
2019-07-23 19:48:53 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 10:37:03 +00:00
|
|
|
func (f *FolderConfiguration) CreateMarker() error {
|
2018-03-18 00:42:31 +00:00
|
|
|
if err := f.CheckPath(); err != ErrMarkerMissing {
|
2017-10-24 07:58:55 +00:00
|
|
|
return err
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
2017-11-05 12:18:05 +00:00
|
|
|
if f.MarkerName != DefaultMarkerName {
|
|
|
|
// Folder uses a non-default marker so we shouldn't mess with it.
|
|
|
|
// Pretend we created it and let the subsequent health checks sort
|
|
|
|
// out the actual situation.
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-27 10:37:03 +00:00
|
|
|
|
2024-05-24 06:51:02 +00:00
|
|
|
ffs := f.Filesystem(nil)
|
|
|
|
|
|
|
|
// Create the marker as a directory
|
|
|
|
err := ffs.Mkdir(DefaultMarkerName, 0o755)
|
2017-10-24 07:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-24 06:51:02 +00:00
|
|
|
|
|
|
|
// Create a file inside it, reducing the risk of the marker directory
|
|
|
|
// being removed by automated cleanup tools.
|
|
|
|
markerFile := filepath.Join(DefaultMarkerName, f.markerFilename())
|
|
|
|
if err := fs.WriteFile(ffs, markerFile, f.markerContents(), 0o644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync & hide the containing directory
|
|
|
|
if dir, err := ffs.Open("."); err != nil {
|
2017-10-24 07:58:55 +00:00
|
|
|
l.Debugln("folder marker: open . failed:", err)
|
|
|
|
} else if err := dir.Sync(); err != nil {
|
|
|
|
l.Debugln("folder marker: fsync . failed:", err)
|
|
|
|
}
|
2024-05-24 06:51:02 +00:00
|
|
|
ffs.Hide(DefaultMarkerName)
|
2017-10-24 07:58:55 +00:00
|
|
|
|
2015-10-27 10:37:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-24 06:51:02 +00:00
|
|
|
func (f *FolderConfiguration) RemoveMarker() error {
|
|
|
|
ffs := f.Filesystem(nil)
|
|
|
|
_ = ffs.Remove(filepath.Join(DefaultMarkerName, f.markerFilename()))
|
|
|
|
return ffs.Remove(DefaultMarkerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FolderConfiguration) markerFilename() string {
|
|
|
|
h := sha256.Sum256([]byte(f.ID))
|
|
|
|
return fmt.Sprintf("syncthing-folder-%x.txt", h[:3])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FolderConfiguration) markerContents() []byte {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("# This directory is a Syncthing folder marker.\n# Do not delete.\n\n")
|
|
|
|
fmt.Fprintf(&buf, "folderID: %s\n", f.ID)
|
|
|
|
fmt.Fprintf(&buf, "created: %s\n", time.Now().Format(time.RFC3339))
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
// CheckPath returns nil if the folder root exists and contains the marker file
|
|
|
|
func (f *FolderConfiguration) CheckPath() error {
|
2023-05-03 08:25:36 +00:00
|
|
|
return f.checkFilesystemPath(f.Filesystem(nil), ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FolderConfiguration) checkFilesystemPath(ffs fs.Filesystem, path string) error {
|
|
|
|
fi, err := ffs.Stat(path)
|
2017-11-26 07:51:22 +00:00
|
|
|
if err != nil {
|
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-18 00:42:31 +00:00
|
|
|
return ErrPathMissing
|
2017-10-24 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 07:51:22 +00:00
|
|
|
// Users might have the root directory as a symlink or reparse point.
|
|
|
|
// Furthermore, OneDrive bullcrap uses a magic reparse point to the cloudz...
|
|
|
|
// Yet it's impossible for this to happen, as filesystem adds a trailing
|
|
|
|
// path separator to the root, so even if you point the filesystem at a file
|
|
|
|
// Stat ends up calling stat on C:\dir\file\ which, fails with "is not a directory"
|
|
|
|
// in the error check above, and we don't even get to here.
|
|
|
|
if !fi.IsDir() && !fi.IsSymlink() {
|
2018-03-18 00:42:31 +00:00
|
|
|
return ErrPathNotDirectory
|
2017-11-26 07:51:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
_, err = ffs.Stat(filepath.Join(path, f.MarkerName))
|
2017-10-24 07:58:55 +00:00
|
|
|
if err != nil {
|
2017-11-26 07:51:22 +00:00
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-18 00:42:31 +00:00
|
|
|
return ErrMarkerMissing
|
2017-10-24 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-10-27 10:37:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 23:50:56 +00:00
|
|
|
func (f *FolderConfiguration) CreateRoot() (err error) {
|
|
|
|
// Directory permission bits. Will be filtered down to something
|
|
|
|
// sane by umask on Unixes.
|
2023-05-03 08:25:36 +00:00
|
|
|
permBits := fs.FileMode(0o777)
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsWindows {
|
2017-04-23 23:50:56 +00:00
|
|
|
// Windows has no umask so we must chose a safer set of bits to
|
|
|
|
// begin with.
|
2023-05-03 08:25:36 +00:00
|
|
|
permBits = 0o700
|
2017-04-23 23:50:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-10 18:55:05 +00:00
|
|
|
filesystem := f.Filesystem(nil)
|
2017-08-19 14:36:56 +00:00
|
|
|
|
|
|
|
if _, err = filesystem.Stat("."); fs.IsNotExist(err) {
|
2018-06-10 13:41:20 +00:00
|
|
|
err = filesystem.MkdirAll(".", permBits)
|
2017-04-23 23:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-17 15:12:41 +00:00
|
|
|
func (f FolderConfiguration) Description() string {
|
2016-12-19 09:12:06 +00:00
|
|
|
if f.Label == "" {
|
|
|
|
return f.ID
|
|
|
|
}
|
2016-11-17 15:12:41 +00:00
|
|
|
return fmt.Sprintf("%q (%s)", f.Label, f.ID)
|
|
|
|
}
|
|
|
|
|
2015-10-27 10:37:03 +00:00
|
|
|
func (f *FolderConfiguration) DeviceIDs() []protocol.DeviceID {
|
|
|
|
deviceIDs := make([]protocol.DeviceID, len(f.Devices))
|
|
|
|
for i, n := range f.Devices {
|
|
|
|
deviceIDs[i] = n.DeviceID
|
|
|
|
}
|
|
|
|
return deviceIDs
|
|
|
|
}
|
|
|
|
|
2023-06-14 07:24:31 +00:00
|
|
|
func (f *FolderConfiguration) prepare(myID protocol.DeviceID, existingDevices map[protocol.DeviceID]*DeviceConfiguration) {
|
2020-11-20 13:21:54 +00:00
|
|
|
// Ensure that
|
|
|
|
// - any loose devices are not present in the wrong places
|
|
|
|
// - there are no duplicate devices
|
|
|
|
// - we are part of the devices
|
2023-06-14 07:24:31 +00:00
|
|
|
// - folder is not shared in trusted mode with an untrusted device
|
2020-11-20 13:21:54 +00:00
|
|
|
f.Devices = ensureExistingDevices(f.Devices, existingDevices)
|
|
|
|
f.Devices = ensureNoDuplicateFolderDevices(f.Devices)
|
|
|
|
f.Devices = ensureDevicePresent(f.Devices, myID)
|
2023-06-14 07:24:31 +00:00
|
|
|
f.Devices = ensureNoUntrustedTrustingSharing(f, f.Devices, existingDevices)
|
2020-11-20 13:21:54 +00:00
|
|
|
|
|
|
|
sort.Slice(f.Devices, func(a, b int) bool {
|
|
|
|
return f.Devices[a].DeviceID.Compare(f.Devices[b].DeviceID) == -1
|
|
|
|
})
|
|
|
|
|
2015-11-05 08:01:47 +00:00
|
|
|
if f.RescanIntervalS > MaxRescanIntervalS {
|
|
|
|
f.RescanIntervalS = MaxRescanIntervalS
|
|
|
|
} else if f.RescanIntervalS < 0 {
|
|
|
|
f.RescanIntervalS = 0
|
|
|
|
}
|
2016-05-09 11:30:19 +00:00
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
if f.FSWatcherDelayS <= 0 {
|
|
|
|
f.FSWatcherEnabled = false
|
|
|
|
f.FSWatcherDelayS = 10
|
2023-03-18 07:50:38 +00:00
|
|
|
} else if f.FSWatcherDelayS < 0.01 {
|
|
|
|
f.FSWatcherDelayS = 0.01
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 08:48:50 +00:00
|
|
|
if f.Versioning.CleanupIntervalS > MaxRescanIntervalS {
|
|
|
|
f.Versioning.CleanupIntervalS = MaxRescanIntervalS
|
|
|
|
} else if f.Versioning.CleanupIntervalS < 0 {
|
|
|
|
f.Versioning.CleanupIntervalS = 0
|
|
|
|
}
|
2017-01-04 21:04:13 +00:00
|
|
|
|
|
|
|
if f.WeakHashThresholdPct == 0 {
|
|
|
|
f.WeakHashThresholdPct = 25
|
|
|
|
}
|
2017-11-05 12:18:05 +00:00
|
|
|
|
|
|
|
if f.MarkerName == "" {
|
|
|
|
f.MarkerName = DefaultMarkerName
|
|
|
|
}
|
2020-11-03 18:09:32 +00:00
|
|
|
|
|
|
|
if f.MaxConcurrentWrites <= 0 {
|
|
|
|
f.MaxConcurrentWrites = maxConcurrentWritesDefault
|
|
|
|
} else if f.MaxConcurrentWrites > maxConcurrentWritesLimit {
|
|
|
|
f.MaxConcurrentWrites = maxConcurrentWritesLimit
|
|
|
|
}
|
2021-02-12 21:51:29 +00:00
|
|
|
|
|
|
|
if f.Type == FolderTypeReceiveEncrypted {
|
2021-05-20 20:33:23 +00:00
|
|
|
f.DisableTempIndexes = true
|
2021-02-12 21:51:29 +00:00
|
|
|
f.IgnorePerms = true
|
|
|
|
}
|
2015-11-05 08:01:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 08:33:32 +00:00
|
|
|
// RequiresRestartOnly returns a copy with only the attributes that require
|
|
|
|
// restart on change.
|
|
|
|
func (f FolderConfiguration) RequiresRestartOnly() FolderConfiguration {
|
|
|
|
copy := f
|
|
|
|
|
|
|
|
// Manual handling for things that are not taken care of by the tag
|
|
|
|
// copier, yet should not cause a restart.
|
|
|
|
|
|
|
|
blank := FolderConfiguration{}
|
2023-08-21 17:44:33 +00:00
|
|
|
copyMatchingTag(&blank, ©, "restart", func(v string) bool {
|
2017-12-07 08:33:32 +00:00
|
|
|
if len(v) > 0 && v != "false" {
|
|
|
|
panic(fmt.Sprintf(`unexpected tag value: %s. expected untagged or "false"`, v))
|
|
|
|
}
|
|
|
|
return v == "false"
|
|
|
|
})
|
|
|
|
return copy
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
func (f *FolderConfiguration) Device(device protocol.DeviceID) (FolderDeviceConfiguration, bool) {
|
2018-06-06 21:34:11 +00:00
|
|
|
for _, dev := range f.Devices {
|
|
|
|
if dev.DeviceID == device {
|
2020-11-09 14:33:32 +00:00
|
|
|
return dev, true
|
2018-06-06 21:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-09 14:33:32 +00:00
|
|
|
return FolderDeviceConfiguration{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FolderConfiguration) SharedWith(device protocol.DeviceID) bool {
|
|
|
|
_, ok := f.Device(device)
|
|
|
|
return ok
|
2018-06-06 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:22:28 +00:00
|
|
|
func (f *FolderConfiguration) CheckAvailableSpace(req uint64) error {
|
2018-10-12 11:34:56 +00:00
|
|
|
val := f.MinDiskFree.BaseValue()
|
|
|
|
if val <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-10 18:55:05 +00:00
|
|
|
fs := f.Filesystem(nil)
|
2018-08-25 08:16:38 +00:00
|
|
|
usage, err := fs.Usage(".")
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-24 16:07:51 +00:00
|
|
|
if err := checkAvailableSpace(req, f.MinDiskFree, usage); err != nil {
|
|
|
|
return fmt.Errorf("insufficient space in folder %v (%v): %w", f.Description(), fs.URI(), err)
|
2018-08-25 08:16:38 +00:00
|
|
|
}
|
2020-10-02 13:22:28 +00:00
|
|
|
return nil
|
2017-10-24 07:58:55 +00:00
|
|
|
}
|
2022-09-14 07:50:55 +00:00
|
|
|
|
|
|
|
func (f XattrFilter) Permit(s string) bool {
|
|
|
|
if len(f.Entries) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range f.Entries {
|
|
|
|
if ok, _ := path.Match(entry.Match, s); ok {
|
|
|
|
return entry.Permit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f XattrFilter) GetMaxSingleEntrySize() int {
|
|
|
|
return f.MaxSingleEntrySize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f XattrFilter) GetMaxTotalSize() int {
|
|
|
|
return f.MaxTotalSize
|
|
|
|
}
|