2016-04-26 14:01:46 +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/.
|
2016-04-26 14:01:46 +00:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
2017-04-26 00:15:23 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2018-05-23 07:23:21 +00:00
|
|
|
"fmt"
|
2018-05-21 06:45:05 +00:00
|
|
|
"math/rand"
|
2018-11-07 10:04:41 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2018-10-05 08:26:25 +00:00
|
|
|
"sync/atomic"
|
2017-04-26 00:15:23 +00:00
|
|
|
"time"
|
2017-08-25 19:47:01 +00:00
|
|
|
|
2019-11-19 08:56:53 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2017-08-25 19:47:01 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2018-05-21 06:56:24 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2018-06-11 13:47:54 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2018-11-07 10:04:41 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2018-02-04 21:46:24 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore"
|
2019-02-12 12:25:11 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/locations"
|
2018-11-07 10:04:41 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2018-02-25 08:39:00 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2018-11-07 10:04:41 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/scanner"
|
2019-03-11 16:57:21 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/stats"
|
2018-02-04 21:46:24 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2017-10-20 14:52:55 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/watchaggregator"
|
2019-07-09 09:40:30 +00:00
|
|
|
|
|
|
|
"github.com/thejerf/suture"
|
2017-04-26 00:15:23 +00:00
|
|
|
)
|
2016-04-26 14:01:46 +00:00
|
|
|
|
|
|
|
type folder struct {
|
2019-07-09 09:40:30 +00:00
|
|
|
suture.Service
|
2016-04-26 14:01:46 +00:00
|
|
|
stateTracker
|
2017-08-25 19:47:01 +00:00
|
|
|
config.FolderConfiguration
|
2019-03-11 16:57:21 +00:00
|
|
|
*stats.FolderStatisticsReference
|
2020-02-01 07:02:18 +00:00
|
|
|
ioLimiter *byteSemaphore
|
2019-03-11 16:57:21 +00:00
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
localFlags uint32
|
2017-04-01 09:58:06 +00:00
|
|
|
|
2019-02-26 08:09:25 +00:00
|
|
|
model *model
|
2018-02-25 08:39:00 +00:00
|
|
|
shortID protocol.ShortID
|
2019-03-11 06:28:54 +00:00
|
|
|
fset *db.FileSet
|
|
|
|
ignores *ignore.Matcher
|
2018-02-25 08:39:00 +00:00
|
|
|
ctx context.Context
|
|
|
|
|
2018-05-21 06:45:05 +00:00
|
|
|
scanInterval time.Duration
|
|
|
|
scanTimer *time.Timer
|
|
|
|
scanNow chan rescanRequest
|
|
|
|
scanDelay chan time.Duration
|
2017-04-20 00:20:34 +00:00
|
|
|
initialScanFinished chan struct{}
|
2018-11-07 10:04:41 +00:00
|
|
|
scanErrors []FileError
|
|
|
|
scanErrorsMut sync.Mutex
|
2018-02-25 08:39:00 +00:00
|
|
|
|
|
|
|
pullScheduled chan struct{}
|
|
|
|
|
|
|
|
watchCancel context.CancelFunc
|
|
|
|
watchChan chan []string
|
|
|
|
restartWatchChan chan struct{}
|
|
|
|
watchErr error
|
2018-12-21 11:06:21 +00:00
|
|
|
watchMut sync.Mutex
|
2018-05-11 08:45:13 +00:00
|
|
|
|
|
|
|
puller puller
|
|
|
|
}
|
|
|
|
|
2018-05-21 06:45:05 +00:00
|
|
|
type rescanRequest struct {
|
|
|
|
subdirs []string
|
|
|
|
err chan error
|
|
|
|
}
|
|
|
|
|
2018-05-11 08:45:13 +00:00
|
|
|
type puller interface {
|
|
|
|
pull() bool // true when successfull and should not be retried
|
2016-04-26 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 07:02:18 +00:00
|
|
|
func newFolder(model *model, fset *db.FileSet, ignores *ignore.Matcher, cfg config.FolderConfiguration, evLogger events.Logger, ioLimiter *byteSemaphore) folder {
|
2017-08-25 19:47:01 +00:00
|
|
|
return folder{
|
2019-08-15 14:29:37 +00:00
|
|
|
stateTracker: newStateTracker(cfg.ID, evLogger),
|
2019-03-11 16:57:21 +00:00
|
|
|
FolderConfiguration: cfg,
|
|
|
|
FolderStatisticsReference: stats.NewFolderStatisticsReference(model.db, cfg.ID),
|
2020-02-01 07:02:18 +00:00
|
|
|
ioLimiter: ioLimiter,
|
2017-08-25 19:47:01 +00:00
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
model: model,
|
|
|
|
shortID: model.shortID,
|
2019-03-11 06:28:54 +00:00
|
|
|
fset: fset,
|
|
|
|
ignores: ignores,
|
2018-02-25 08:39:00 +00:00
|
|
|
|
2018-05-21 06:45:05 +00:00
|
|
|
scanInterval: time.Duration(cfg.RescanIntervalS) * time.Second,
|
|
|
|
scanTimer: time.NewTimer(time.Millisecond), // The first scan should be done immediately.
|
|
|
|
scanNow: make(chan rescanRequest),
|
|
|
|
scanDelay: make(chan time.Duration),
|
2017-08-25 19:47:01 +00:00
|
|
|
initialScanFinished: make(chan struct{}),
|
2018-11-07 10:04:41 +00:00
|
|
|
scanErrorsMut: sync.NewMutex(),
|
2018-02-25 08:39:00 +00:00
|
|
|
|
|
|
|
pullScheduled: make(chan struct{}, 1), // This needs to be 1-buffered so that we queue a pull if we're busy when it comes.
|
|
|
|
|
2018-06-11 13:47:54 +00:00
|
|
|
watchCancel: func() {},
|
|
|
|
restartWatchChan: make(chan struct{}, 1),
|
2018-12-21 11:06:21 +00:00
|
|
|
watchMut: sync.NewMutex(),
|
2017-08-25 19:47:01 +00:00
|
|
|
}
|
2016-04-26 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func (f *folder) serve(ctx context.Context) {
|
2018-10-05 08:26:25 +00:00
|
|
|
atomic.AddInt32(&f.model.foldersRunning, 1)
|
|
|
|
defer atomic.AddInt32(&f.model.foldersRunning, -1)
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
f.ctx = ctx
|
|
|
|
|
2018-05-11 08:45:13 +00:00
|
|
|
l.Debugln(f, "starting")
|
|
|
|
defer l.Debugln(f, "exiting")
|
|
|
|
|
|
|
|
defer func() {
|
2018-05-21 06:45:05 +00:00
|
|
|
f.scanTimer.Stop()
|
2018-05-11 08:45:13 +00:00
|
|
|
f.setState(FolderIdle)
|
|
|
|
}()
|
|
|
|
|
|
|
|
pause := f.basePause()
|
|
|
|
pullFailTimer := time.NewTimer(0)
|
|
|
|
<-pullFailTimer.C
|
|
|
|
|
|
|
|
if f.FSWatcherEnabled && f.CheckHealth() == nil {
|
|
|
|
f.startWatch()
|
|
|
|
}
|
|
|
|
|
|
|
|
initialCompleted := f.initialScanFinished
|
|
|
|
|
2019-04-10 14:22:23 +00:00
|
|
|
pull := func() {
|
|
|
|
startTime := time.Now()
|
2020-01-27 16:31:17 +00:00
|
|
|
if f.pull() {
|
2019-04-10 14:22:23 +00:00
|
|
|
// We're good. Don't schedule another pull and reset
|
|
|
|
// the pause interval.
|
|
|
|
pause = f.basePause()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Pulling failed, try again later.
|
|
|
|
delay := pause + time.Since(startTime)
|
|
|
|
l.Infof("Folder %v isn't making sync progress - retrying in %v.", f.Description(), delay)
|
|
|
|
pullFailTimer.Reset(delay)
|
|
|
|
if pause < 60*f.basePause() {
|
|
|
|
pause *= 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 08:45:13 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-f.ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-f.pullScheduled:
|
|
|
|
pullFailTimer.Stop()
|
|
|
|
select {
|
|
|
|
case <-pullFailTimer.C:
|
|
|
|
default:
|
|
|
|
}
|
2019-04-10 14:22:23 +00:00
|
|
|
pull()
|
2018-05-11 08:45:13 +00:00
|
|
|
|
|
|
|
case <-pullFailTimer.C:
|
2019-04-10 14:22:23 +00:00
|
|
|
pull()
|
2018-05-11 08:45:13 +00:00
|
|
|
|
|
|
|
case <-initialCompleted:
|
|
|
|
// Initial scan has completed, we should do a pull
|
|
|
|
initialCompleted = nil // never hit this case again
|
2020-01-27 16:31:17 +00:00
|
|
|
if !f.pull() {
|
2018-05-11 08:45:13 +00:00
|
|
|
// Pulling failed, try again later.
|
|
|
|
pullFailTimer.Reset(pause)
|
|
|
|
}
|
|
|
|
|
2018-05-21 06:45:05 +00:00
|
|
|
case <-f.scanTimer.C:
|
2019-08-30 12:27:26 +00:00
|
|
|
l.Debugln(f, "Scanning due to timer")
|
2018-05-11 08:45:13 +00:00
|
|
|
f.scanTimerFired()
|
|
|
|
|
2018-05-21 06:45:05 +00:00
|
|
|
case req := <-f.scanNow:
|
2019-08-30 12:27:26 +00:00
|
|
|
l.Debugln(f, "Scanning due to request")
|
2018-05-11 08:45:13 +00:00
|
|
|
req.err <- f.scanSubdirs(req.subdirs)
|
|
|
|
|
2018-05-21 06:45:05 +00:00
|
|
|
case next := <-f.scanDelay:
|
2019-08-30 12:27:26 +00:00
|
|
|
l.Debugln(f, "Delaying scan")
|
2018-05-21 06:45:05 +00:00
|
|
|
f.scanTimer.Reset(next)
|
2018-05-11 08:45:13 +00:00
|
|
|
|
|
|
|
case fsEvents := <-f.watchChan:
|
2019-08-30 12:27:26 +00:00
|
|
|
l.Debugln(f, "Scan due to watcher")
|
2018-05-11 08:45:13 +00:00
|
|
|
f.scanSubdirs(fsEvents)
|
|
|
|
|
|
|
|
case <-f.restartWatchChan:
|
2019-08-30 12:27:26 +00:00
|
|
|
l.Debugln(f, "Restart watcher")
|
2018-05-11 08:45:13 +00:00
|
|
|
f.restartWatch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
func (f *folder) BringToFront(string) {}
|
|
|
|
|
2019-04-07 11:29:17 +00:00
|
|
|
func (f *folder) Override() {}
|
2018-05-21 06:56:24 +00:00
|
|
|
|
2019-04-07 11:29:17 +00:00
|
|
|
func (f *folder) Revert() {}
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2016-04-26 14:01:46 +00:00
|
|
|
func (f *folder) DelayScan(next time.Duration) {
|
2018-05-21 06:45:05 +00:00
|
|
|
f.Delay(next)
|
2016-04-26 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 09:17:14 +00:00
|
|
|
func (f *folder) ignoresUpdated() {
|
2017-10-24 07:58:55 +00:00
|
|
|
if f.FSWatcherEnabled {
|
|
|
|
f.scheduleWatchRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
func (f *folder) SchedulePull() {
|
|
|
|
select {
|
|
|
|
case f.pullScheduled <- struct{}{}:
|
|
|
|
default:
|
|
|
|
// We might be busy doing a pull and thus not reading from this
|
|
|
|
// channel. The channel is 1-buffered, so one notification will be
|
|
|
|
// queued to ensure we recheck after the pull, but beyond that we must
|
|
|
|
// make sure to not block index receiving.
|
|
|
|
}
|
|
|
|
}
|
2017-11-07 06:59:35 +00:00
|
|
|
|
2019-06-27 18:25:38 +00:00
|
|
|
func (f *folder) Jobs(_, _ int) ([]string, []string, int) {
|
|
|
|
return nil, nil, 0
|
2017-10-24 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 14:01:46 +00:00
|
|
|
func (f *folder) Scan(subdirs []string) error {
|
2017-04-20 00:20:34 +00:00
|
|
|
<-f.initialScanFinished
|
2018-05-21 06:45:05 +00:00
|
|
|
req := rescanRequest{
|
|
|
|
subdirs: subdirs,
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
2018-07-04 07:07:33 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case f.scanNow <- req:
|
|
|
|
return <-req.err
|
|
|
|
case <-f.ctx.Done():
|
|
|
|
return f.ctx.Err()
|
|
|
|
}
|
2018-05-21 06:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) Reschedule() {
|
|
|
|
if f.scanInterval == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Sleep a random time between 3/4 and 5/4 of the configured interval.
|
|
|
|
sleepNanos := (f.scanInterval.Nanoseconds()*3 + rand.Int63n(2*f.scanInterval.Nanoseconds())) / 4
|
|
|
|
interval := time.Duration(sleepNanos) * time.Nanosecond
|
|
|
|
l.Debugln(f, "next rescan in", interval)
|
|
|
|
f.scanTimer.Reset(interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) Delay(next time.Duration) {
|
|
|
|
f.scanDelay <- next
|
2016-04-26 14:01:46 +00:00
|
|
|
}
|
2017-04-26 00:15:23 +00:00
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
// CheckHealth checks the folder for common errors, updates the folder state
|
|
|
|
// and returns the current folder error, or nil if the folder is healthy.
|
|
|
|
func (f *folder) CheckHealth() error {
|
|
|
|
err := f.getHealthError()
|
|
|
|
f.setError(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) getHealthError() error {
|
|
|
|
// Check for folder errors, with the most serious and specific first and
|
|
|
|
// generic ones like out of space on the home disk later.
|
|
|
|
|
|
|
|
if err := f.CheckPath(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-12 12:25:11 +00:00
|
|
|
dbPath := locations.Get(locations.Database)
|
|
|
|
if usage, err := fs.NewFilesystem(fs.FilesystemTypeBasic, dbPath).Usage("."); err == nil {
|
|
|
|
if err = config.CheckFreeSpace(f.model.cfg.Options().MinHomeDiskFree, usage); err != nil {
|
2019-11-19 08:56:53 +00:00
|
|
|
return errors.Wrapf(err, "insufficient space on disk for database (%v)", dbPath)
|
2019-02-12 12:25:11 +00:00
|
|
|
}
|
2017-10-24 07:58:55 +00:00
|
|
|
}
|
2016-04-26 14:01:46 +00:00
|
|
|
|
2017-10-12 06:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-27 16:31:17 +00:00
|
|
|
func (f *folder) pull() bool {
|
|
|
|
select {
|
|
|
|
case <-f.initialScanFinished:
|
|
|
|
default:
|
|
|
|
// Once the initial scan finished, a pull will be scheduled
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is nothing to do, don't even enter sync-waiting state.
|
|
|
|
abort := true
|
|
|
|
snap := f.fset.Snapshot()
|
|
|
|
snap.WithNeed(protocol.LocalDeviceID, func(intf db.FileIntf) bool {
|
|
|
|
abort = false
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
snap.Release()
|
|
|
|
if abort {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
f.setState(FolderSyncWaiting)
|
|
|
|
defer f.setState(FolderIdle)
|
|
|
|
|
2020-02-01 07:02:18 +00:00
|
|
|
f.ioLimiter.take(1)
|
|
|
|
defer f.ioLimiter.give(1)
|
2020-01-27 16:31:17 +00:00
|
|
|
|
|
|
|
return f.puller.pull()
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:20:34 +00:00
|
|
|
func (f *folder) scanSubdirs(subDirs []string) error {
|
2019-08-30 12:27:26 +00:00
|
|
|
if err := f.getHealthError(); err != nil {
|
|
|
|
// If there is a health error we set it as the folder error. We do not
|
|
|
|
// clear the folder error if there is no health error, as there might be
|
|
|
|
// an *other* folder error (failed to load ignores, for example). Hence
|
|
|
|
// we do not use the CheckHealth() convenience function here.
|
|
|
|
f.setError(err)
|
2018-11-07 10:04:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-30 12:27:26 +00:00
|
|
|
oldHash := f.ignores.Hash()
|
|
|
|
if err := f.ignores.Load(".stignore"); err != nil && !fs.IsNotExist(err) {
|
2019-11-19 08:56:53 +00:00
|
|
|
err = errors.Wrap(err, "loading ignores")
|
2019-08-30 12:27:26 +00:00
|
|
|
f.setError(err)
|
|
|
|
return err
|
|
|
|
}
|
2018-11-07 10:04:41 +00:00
|
|
|
|
2019-08-30 12:27:26 +00:00
|
|
|
// Check on the way out if the ignore patterns changed as part of scanning
|
|
|
|
// this folder. If they did we should schedule a pull of the folder so that
|
|
|
|
// we request things we might have suddenly become unignored and so on.
|
|
|
|
defer func() {
|
|
|
|
if f.ignores.Hash() != oldHash {
|
|
|
|
l.Debugln("Folder", f.Description(), "ignore patterns change detected while scanning; triggering puller")
|
|
|
|
f.ignoresUpdated()
|
|
|
|
f.SchedulePull()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// We've passed all health checks so now mark ourselves healthy and queued
|
|
|
|
// for scanning.
|
|
|
|
f.setError(nil)
|
2018-12-05 07:40:05 +00:00
|
|
|
f.setState(FolderScanWaiting)
|
2019-08-30 12:27:26 +00:00
|
|
|
|
2020-02-01 07:02:18 +00:00
|
|
|
f.ioLimiter.take(1)
|
|
|
|
defer f.ioLimiter.give(1)
|
2018-12-05 07:40:05 +00:00
|
|
|
|
2018-11-07 10:04:41 +00:00
|
|
|
for i := range subDirs {
|
|
|
|
sub := osutil.NativeFilename(subDirs[i])
|
|
|
|
|
|
|
|
if sub == "" {
|
|
|
|
// A blank subdirs means to scan the entire folder. We can trim
|
|
|
|
// the subDirs list and go on our way.
|
|
|
|
subDirs = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
subDirs[i] = sub
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
snap := f.fset.Snapshot()
|
|
|
|
// We release explicitly later in this function, however we might exit early
|
|
|
|
// and it's ok to release twice.
|
|
|
|
defer snap.Release()
|
|
|
|
|
2018-11-07 10:04:41 +00:00
|
|
|
// Clean the list of subitems to ensure that we start at a known
|
|
|
|
// directory, and don't scan subdirectories of things we've already
|
|
|
|
// scanned.
|
2019-03-11 06:28:54 +00:00
|
|
|
subDirs = unifySubs(subDirs, func(file string) bool {
|
2020-01-21 17:23:08 +00:00
|
|
|
_, ok := snap.Get(protocol.LocalDeviceID, file)
|
2018-11-07 10:04:41 +00:00
|
|
|
return ok
|
|
|
|
})
|
|
|
|
|
|
|
|
f.setState(FolderScanning)
|
|
|
|
|
2019-08-30 12:27:26 +00:00
|
|
|
mtimefs := f.fset.MtimeFS()
|
2018-11-07 10:04:41 +00:00
|
|
|
fchan := scanner.Walk(f.ctx, scanner.Config{
|
|
|
|
Folder: f.ID,
|
|
|
|
Subs: subDirs,
|
2019-03-11 06:28:54 +00:00
|
|
|
Matcher: f.ignores,
|
2018-11-07 10:04:41 +00:00
|
|
|
TempLifetime: time.Duration(f.model.cfg.Options().KeepTemporariesH) * time.Hour,
|
2020-01-21 17:23:08 +00:00
|
|
|
CurrentFiler: cFiler{snap},
|
2018-11-07 10:04:41 +00:00
|
|
|
Filesystem: mtimefs,
|
|
|
|
IgnorePerms: f.IgnorePerms,
|
|
|
|
AutoNormalize: f.AutoNormalize,
|
|
|
|
Hashers: f.model.numHashers(f.ID),
|
2019-03-11 06:28:54 +00:00
|
|
|
ShortID: f.shortID,
|
2018-11-07 10:04:41 +00:00
|
|
|
ProgressTickIntervalS: f.ScanProgressIntervalS,
|
|
|
|
LocalFlags: f.localFlags,
|
2019-07-23 19:48:53 +00:00
|
|
|
ModTimeWindow: f.ModTimeWindow(),
|
2019-08-21 06:05:43 +00:00
|
|
|
EventLogger: f.evLogger,
|
2018-11-07 10:04:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
batchFn := func(fs []protocol.FileInfo) error {
|
|
|
|
if err := f.CheckHealth(); err != nil {
|
|
|
|
l.Debugf("Stopping scan of folder %s due to: %s", f.Description(), err)
|
|
|
|
return err
|
|
|
|
}
|
2019-04-07 11:29:17 +00:00
|
|
|
f.updateLocalsFromScanning(fs)
|
2018-11-07 10:04:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Resolve items which are identical with the global state.
|
|
|
|
if f.localFlags&protocol.FlagLocalReceiveOnly != 0 {
|
|
|
|
oldBatchFn := batchFn // can't reference batchFn directly (recursion)
|
|
|
|
batchFn = func(fs []protocol.FileInfo) error {
|
|
|
|
for i := range fs {
|
2020-01-21 17:23:08 +00:00
|
|
|
switch gf, ok := snap.GetGlobal(fs[i].Name); {
|
2018-11-07 10:04:41 +00:00
|
|
|
case !ok:
|
|
|
|
continue
|
2019-07-23 19:48:53 +00:00
|
|
|
case gf.IsEquivalentOptional(fs[i], f.ModTimeWindow(), false, false, protocol.FlagLocalReceiveOnly):
|
2018-11-07 10:04:41 +00:00
|
|
|
// What we have locally is equivalent to the global file.
|
|
|
|
fs[i].Version = fs[i].Version.Merge(gf.Version)
|
|
|
|
fallthrough
|
|
|
|
case fs[i].IsDeleted() && gf.IsReceiveOnlyChanged():
|
|
|
|
// Our item is deleted and the global item is our own
|
|
|
|
// receive only file. We can't delete file infos, so
|
|
|
|
// we just pretend it is a normal deleted file (nobody
|
|
|
|
// cares about that).
|
|
|
|
fs[i].LocalFlags &^= protocol.FlagLocalReceiveOnly
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oldBatchFn(fs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
batch := newFileInfoBatch(batchFn)
|
|
|
|
|
|
|
|
// Schedule a pull after scanning, but only if we actually detected any
|
|
|
|
// changes.
|
|
|
|
changes := 0
|
|
|
|
defer func() {
|
|
|
|
if changes > 0 {
|
|
|
|
f.SchedulePull()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
f.clearScanErrors(subDirs)
|
|
|
|
for res := range fchan {
|
|
|
|
if res.Err != nil {
|
|
|
|
f.newScanError(res.Path, res.Err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := batch.flushIfFull(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.append(res.File)
|
|
|
|
changes++
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := batch.flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(subDirs) == 0 {
|
|
|
|
// If we have no specific subdirectories to traverse, set it to one
|
|
|
|
// empty prefix so we traverse the entire folder contents once.
|
|
|
|
subDirs = []string{""}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a scan of the database for each prefix, to check for deleted and
|
|
|
|
// ignored files.
|
|
|
|
var toIgnore []db.FileInfoTruncated
|
|
|
|
ignoredParent := ""
|
2020-01-21 17:23:08 +00:00
|
|
|
|
|
|
|
snap.Release()
|
|
|
|
snap = f.fset.Snapshot()
|
|
|
|
defer snap.Release()
|
|
|
|
|
2018-11-07 10:04:41 +00:00
|
|
|
for _, sub := range subDirs {
|
|
|
|
var iterError error
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
snap.WithPrefixedHaveTruncated(protocol.LocalDeviceID, sub, func(fi db.FileIntf) bool {
|
2019-07-23 08:49:22 +00:00
|
|
|
select {
|
|
|
|
case <-f.ctx.Done():
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-11-07 10:04:41 +00:00
|
|
|
file := fi.(db.FileInfoTruncated)
|
|
|
|
|
|
|
|
if err := batch.flushIfFull(); err != nil {
|
|
|
|
iterError = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-11-22 10:16:45 +00:00
|
|
|
if ignoredParent != "" && !fs.IsParent(file.Name, ignoredParent) {
|
2018-11-07 10:04:41 +00:00
|
|
|
for _, file := range toIgnore {
|
|
|
|
l.Debugln("marking file as ignored", file)
|
2019-03-11 06:28:54 +00:00
|
|
|
nf := file.ConvertToIgnoredFileInfo(f.shortID)
|
2018-11-07 10:04:41 +00:00
|
|
|
batch.append(nf)
|
|
|
|
changes++
|
|
|
|
if err := batch.flushIfFull(); err != nil {
|
|
|
|
iterError = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
toIgnore = toIgnore[:0]
|
|
|
|
ignoredParent = ""
|
|
|
|
}
|
|
|
|
|
2019-03-11 06:28:54 +00:00
|
|
|
switch ignored := f.ignores.Match(file.Name).IsIgnored(); {
|
2018-11-07 10:04:41 +00:00
|
|
|
case !file.IsIgnored() && ignored:
|
|
|
|
// File was not ignored at last pass but has been ignored.
|
|
|
|
if file.IsDirectory() {
|
|
|
|
// Delay ignoring as a child might be unignored.
|
|
|
|
toIgnore = append(toIgnore, file)
|
|
|
|
if ignoredParent == "" {
|
|
|
|
// If the parent wasn't ignored already, set
|
|
|
|
// this path as the "highest" ignored parent
|
|
|
|
ignoredParent = file.Name
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-12 06:47:39 +00:00
|
|
|
l.Debugln("marking file as ignored", file)
|
2019-03-11 06:28:54 +00:00
|
|
|
nf := file.ConvertToIgnoredFileInfo(f.shortID)
|
2018-11-07 10:04:41 +00:00
|
|
|
batch.append(nf)
|
|
|
|
changes++
|
|
|
|
|
|
|
|
case file.IsIgnored() && !ignored:
|
|
|
|
// Successfully scanned items are already un-ignored during
|
|
|
|
// the scan, so check whether it is deleted.
|
|
|
|
fallthrough
|
|
|
|
case !file.IsIgnored() && !file.IsDeleted() && !file.IsUnsupported():
|
|
|
|
// The file is not ignored, deleted or unsupported. Lets check if
|
|
|
|
// it's still here. Simply stat:ing it wont do as there are
|
|
|
|
// tons of corner cases (e.g. parent dir->symlink, missing
|
|
|
|
// permissions)
|
|
|
|
if !osutil.IsDeleted(mtimefs, file.Name) {
|
|
|
|
if ignoredParent != "" {
|
|
|
|
// Don't ignore parents of this not ignored item
|
|
|
|
toIgnore = toIgnore[:0]
|
|
|
|
ignoredParent = ""
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
nf := protocol.FileInfo{
|
|
|
|
Name: file.Name,
|
|
|
|
Type: file.Type,
|
|
|
|
Size: 0,
|
|
|
|
ModifiedS: file.ModifiedS,
|
|
|
|
ModifiedNs: file.ModifiedNs,
|
2019-03-11 06:28:54 +00:00
|
|
|
ModifiedBy: f.shortID,
|
2018-11-07 10:04:41 +00:00
|
|
|
Deleted: true,
|
2019-03-11 06:28:54 +00:00
|
|
|
Version: file.Version.Update(f.shortID),
|
2018-11-07 10:04:41 +00:00
|
|
|
LocalFlags: f.localFlags,
|
|
|
|
}
|
|
|
|
// We do not want to override the global version
|
2019-10-01 13:34:59 +00:00
|
|
|
// with the deleted file. Setting to an empty
|
|
|
|
// version makes sure the file gets in sync on
|
|
|
|
// the following pull.
|
2018-11-07 10:04:41 +00:00
|
|
|
if file.ShouldConflict() {
|
2019-10-01 13:34:59 +00:00
|
|
|
nf.Version = protocol.Vector{}
|
2018-11-07 10:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batch.append(nf)
|
|
|
|
changes++
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2019-07-23 08:49:22 +00:00
|
|
|
select {
|
|
|
|
case <-f.ctx.Done():
|
|
|
|
return f.ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-11-07 10:04:41 +00:00
|
|
|
if iterError == nil && len(toIgnore) > 0 {
|
|
|
|
for _, file := range toIgnore {
|
|
|
|
l.Debugln("marking file as ignored", f)
|
2019-03-11 06:28:54 +00:00
|
|
|
nf := file.ConvertToIgnoredFileInfo(f.shortID)
|
2018-11-07 10:04:41 +00:00
|
|
|
batch.append(nf)
|
|
|
|
changes++
|
|
|
|
if iterError = batch.flushIfFull(); iterError != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
toIgnore = toIgnore[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if iterError != nil {
|
|
|
|
return iterError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := batch.flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-11 16:57:21 +00:00
|
|
|
f.ScanCompleted()
|
2018-11-07 10:04:41 +00:00
|
|
|
f.setState(FolderIdle)
|
2016-04-26 14:01:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-09-07 06:17:47 +00:00
|
|
|
|
|
|
|
func (f *folder) scanTimerFired() {
|
|
|
|
err := f.scanSubdirs(nil)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-f.initialScanFinished:
|
|
|
|
default:
|
|
|
|
status := "Completed"
|
|
|
|
if err != nil {
|
|
|
|
status = "Failed"
|
|
|
|
}
|
|
|
|
l.Infoln(status, "initial scan of", f.Type.String(), "folder", f.Description())
|
|
|
|
close(f.initialScanFinished)
|
|
|
|
}
|
|
|
|
|
2018-05-21 06:45:05 +00:00
|
|
|
f.Reschedule()
|
2017-09-07 06:17:47 +00:00
|
|
|
}
|
2017-10-20 14:52:55 +00:00
|
|
|
|
2018-02-04 21:46:24 +00:00
|
|
|
func (f *folder) WatchError() error {
|
2018-12-21 11:06:21 +00:00
|
|
|
f.watchMut.Lock()
|
|
|
|
defer f.watchMut.Unlock()
|
2018-02-04 21:46:24 +00:00
|
|
|
return f.watchErr
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 21:46:24 +00:00
|
|
|
// stopWatch immediately aborts watching and may be called asynchronously
|
|
|
|
func (f *folder) stopWatch() {
|
2018-12-21 11:06:21 +00:00
|
|
|
f.watchMut.Lock()
|
2017-10-20 14:52:55 +00:00
|
|
|
f.watchCancel()
|
2018-12-21 11:06:21 +00:00
|
|
|
f.watchMut.Unlock()
|
2019-07-10 09:00:06 +00:00
|
|
|
f.setWatchError(nil)
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 21:46:24 +00:00
|
|
|
// scheduleWatchRestart makes sure watching is restarted from the main for loop
|
|
|
|
// in a folder's Serve and thus may be called asynchronously (e.g. when ignores change).
|
2017-10-24 07:58:55 +00:00
|
|
|
func (f *folder) scheduleWatchRestart() {
|
2017-10-20 14:52:55 +00:00
|
|
|
select {
|
2017-10-24 07:58:55 +00:00
|
|
|
case f.restartWatchChan <- struct{}{}:
|
2017-10-20 14:52:55 +00:00
|
|
|
default:
|
|
|
|
// We might be busy doing a pull and thus not reading from this
|
|
|
|
// channel. The channel is 1-buffered, so one notification will be
|
|
|
|
// queued to ensure we recheck after the pull.
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 07:58:55 +00:00
|
|
|
|
2018-02-04 21:46:24 +00:00
|
|
|
// restartWatch should only ever be called synchronously. If you want to use
|
|
|
|
// this asynchronously, you should probably use scheduleWatchRestart instead.
|
|
|
|
func (f *folder) restartWatch() {
|
|
|
|
f.stopWatch()
|
|
|
|
f.startWatch()
|
2018-06-11 13:47:54 +00:00
|
|
|
f.scanSubdirs(nil)
|
2018-02-04 21:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// startWatch should only ever be called synchronously. If you want to use
|
|
|
|
// this asynchronously, you should probably use scheduleWatchRestart instead.
|
|
|
|
func (f *folder) startWatch() {
|
|
|
|
ctx, cancel := context.WithCancel(f.ctx)
|
2018-12-21 11:06:21 +00:00
|
|
|
f.watchMut.Lock()
|
2018-02-04 21:46:24 +00:00
|
|
|
f.watchChan = make(chan []string)
|
|
|
|
f.watchCancel = cancel
|
2018-12-21 11:06:21 +00:00
|
|
|
f.watchMut.Unlock()
|
2019-05-25 19:08:26 +00:00
|
|
|
go f.monitorWatch(ctx)
|
2018-02-04 21:46:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 19:08:26 +00:00
|
|
|
// monitorWatch starts the filesystem watching and retries every minute on failure.
|
|
|
|
// It should not be used except in startWatch.
|
|
|
|
func (f *folder) monitorWatch(ctx context.Context) {
|
|
|
|
failTimer := time.NewTimer(0)
|
|
|
|
aggrCtx, aggrCancel := context.WithCancel(ctx)
|
|
|
|
var err error
|
|
|
|
var eventChan <-chan fs.Event
|
|
|
|
var errChan <-chan error
|
|
|
|
warnedOutside := false
|
2018-02-04 21:46:24 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-05-25 19:08:26 +00:00
|
|
|
case <-failTimer.C:
|
|
|
|
eventChan, errChan, err = f.Filesystem().Watch(".", f.ignores, ctx, f.IgnorePerms)
|
|
|
|
// We do this at most once per minute which is the
|
|
|
|
// default rescan time without watcher.
|
|
|
|
f.scanOnWatchErr()
|
|
|
|
f.setWatchError(err)
|
2018-02-04 21:46:24 +00:00
|
|
|
if err != nil {
|
2019-05-25 19:08:26 +00:00
|
|
|
failTimer.Reset(time.Minute)
|
2018-02-04 21:46:24 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-21 07:41:15 +00:00
|
|
|
watchaggregator.Aggregate(aggrCtx, eventChan, f.watchChan, f.FolderConfiguration, f.model.cfg, f.evLogger)
|
2018-02-04 21:46:24 +00:00
|
|
|
l.Debugln("Started filesystem watcher for folder", f.Description())
|
2019-05-25 19:08:26 +00:00
|
|
|
case err = <-errChan:
|
|
|
|
f.setWatchError(err)
|
|
|
|
// This error was previously a panic and should never occur, so generate
|
|
|
|
// a warning, but don't do it repetitively.
|
|
|
|
if !warnedOutside {
|
|
|
|
if _, ok := err.(*fs.ErrWatchEventOutsideRoot); ok {
|
|
|
|
l.Warnln(err)
|
|
|
|
warnedOutside = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aggrCancel()
|
|
|
|
errChan = nil
|
|
|
|
aggrCtx, aggrCancel = context.WithCancel(ctx)
|
|
|
|
failTimer.Reset(time.Minute)
|
2018-02-04 21:46:24 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 19:08:26 +00:00
|
|
|
// setWatchError sets the current error state of the watch and should be called
|
|
|
|
// regardless of whether err is nil or not.
|
|
|
|
func (f *folder) setWatchError(err error) {
|
|
|
|
f.watchMut.Lock()
|
|
|
|
prevErr := f.watchErr
|
|
|
|
f.watchErr = err
|
|
|
|
f.watchMut.Unlock()
|
|
|
|
if err != prevErr {
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"folder": f.ID,
|
|
|
|
}
|
|
|
|
if prevErr != nil {
|
|
|
|
data["from"] = prevErr.Error()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
data["to"] = err.Error()
|
|
|
|
}
|
2019-08-15 14:29:37 +00:00
|
|
|
f.evLogger.Log(events.FolderWatchStateChanged, data)
|
2019-05-25 19:08:26 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-07-10 09:00:06 +00:00
|
|
|
msg := fmt.Sprintf("Error while trying to start filesystem watcher for folder %s, trying again in 1min: %v", f.Description(), err)
|
|
|
|
if prevErr != err {
|
|
|
|
l.Infof(msg)
|
2019-05-25 19:08:26 +00:00
|
|
|
return
|
|
|
|
}
|
2019-07-10 09:00:06 +00:00
|
|
|
l.Debugf(msg)
|
2019-05-25 19:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// scanOnWatchErr schedules a full scan immediately if an error occurred while watching.
|
|
|
|
func (f *folder) scanOnWatchErr() {
|
|
|
|
f.watchMut.Lock()
|
2019-08-13 07:04:43 +00:00
|
|
|
err := f.watchErr
|
|
|
|
f.watchMut.Unlock()
|
|
|
|
if err != nil {
|
2019-05-25 19:08:26 +00:00
|
|
|
f.Delay(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
func (f *folder) setError(err error) {
|
2018-12-30 20:56:16 +00:00
|
|
|
select {
|
|
|
|
case <-f.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
_, _, oldErr := f.getState()
|
|
|
|
if (err != nil && oldErr != nil && oldErr.Error() == err.Error()) || (err == nil && oldErr == nil) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if oldErr == nil {
|
|
|
|
l.Warnf("Error on folder %s: %v", f.Description(), err)
|
|
|
|
} else {
|
|
|
|
l.Infof("Error on folder %s changed: %q -> %q", f.Description(), oldErr, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
l.Infoln("Cleared error on folder", f.Description())
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.FSWatcherEnabled {
|
|
|
|
if err != nil {
|
2018-02-04 21:46:24 +00:00
|
|
|
f.stopWatch()
|
2017-10-24 07:58:55 +00:00
|
|
|
} else {
|
|
|
|
f.scheduleWatchRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f.stateTracker.setError(err)
|
|
|
|
}
|
2018-05-11 08:45:13 +00:00
|
|
|
|
|
|
|
func (f *folder) basePause() time.Duration {
|
|
|
|
if f.PullerPauseS == 0 {
|
|
|
|
return defaultPullerPause
|
|
|
|
}
|
|
|
|
return time.Duration(f.PullerPauseS) * time.Second
|
|
|
|
}
|
2018-05-23 07:23:21 +00:00
|
|
|
|
|
|
|
func (f *folder) String() string {
|
|
|
|
return fmt.Sprintf("%s/%s@%p", f.Type, f.folderID, f)
|
|
|
|
}
|
2018-11-07 10:04:41 +00:00
|
|
|
|
|
|
|
func (f *folder) newScanError(path string, err error) {
|
|
|
|
f.scanErrorsMut.Lock()
|
|
|
|
f.scanErrors = append(f.scanErrors, FileError{
|
|
|
|
Err: err.Error(),
|
|
|
|
Path: path,
|
|
|
|
})
|
|
|
|
f.scanErrorsMut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) clearScanErrors(subDirs []string) {
|
|
|
|
f.scanErrorsMut.Lock()
|
|
|
|
defer f.scanErrorsMut.Unlock()
|
|
|
|
if len(subDirs) == 0 {
|
|
|
|
f.scanErrors = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
filtered := f.scanErrors[:0]
|
|
|
|
outer:
|
|
|
|
for _, fe := range f.scanErrors {
|
|
|
|
for _, sub := range subDirs {
|
2018-11-22 10:16:45 +00:00
|
|
|
if fe.Path == sub || fs.IsParent(fe.Path, sub) {
|
2018-11-07 10:04:41 +00:00
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filtered = append(filtered, fe)
|
|
|
|
}
|
|
|
|
f.scanErrors = filtered
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) Errors() []FileError {
|
|
|
|
f.scanErrorsMut.Lock()
|
|
|
|
defer f.scanErrorsMut.Unlock()
|
|
|
|
return append([]FileError{}, f.scanErrors...)
|
|
|
|
}
|
|
|
|
|
2019-04-07 11:29:17 +00:00
|
|
|
// ForceRescan marks the file such that it gets rehashed on next scan and then
|
|
|
|
// immediately executes that scan.
|
|
|
|
func (f *folder) ForceRescan(file protocol.FileInfo) error {
|
|
|
|
file.SetMustRescan(f.shortID)
|
|
|
|
f.fset.Update(protocol.LocalDeviceID, []protocol.FileInfo{file})
|
|
|
|
|
|
|
|
return f.Scan([]string{file.Name})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) updateLocalsFromScanning(fs []protocol.FileInfo) {
|
|
|
|
f.updateLocals(fs)
|
|
|
|
|
|
|
|
f.emitDiskChangeEvents(fs, events.LocalChangeDetected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) updateLocalsFromPulling(fs []protocol.FileInfo) {
|
|
|
|
f.updateLocals(fs)
|
|
|
|
|
|
|
|
f.emitDiskChangeEvents(fs, events.RemoteChangeDetected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) updateLocals(fs []protocol.FileInfo) {
|
|
|
|
f.fset.Update(protocol.LocalDeviceID, fs)
|
|
|
|
|
|
|
|
filenames := make([]string, len(fs))
|
|
|
|
for i, file := range fs {
|
|
|
|
filenames[i] = file.Name
|
|
|
|
}
|
|
|
|
|
2019-08-15 14:29:37 +00:00
|
|
|
f.evLogger.Log(events.LocalIndexUpdated, map[string]interface{}{
|
2019-04-07 11:29:17 +00:00
|
|
|
"folder": f.ID,
|
|
|
|
"items": len(fs),
|
|
|
|
"filenames": filenames,
|
|
|
|
"version": f.fset.Sequence(protocol.LocalDeviceID),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) emitDiskChangeEvents(fs []protocol.FileInfo, typeOfEvent events.EventType) {
|
|
|
|
for _, file := range fs {
|
|
|
|
if file.IsInvalid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
objType := "file"
|
|
|
|
action := "modified"
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case file.IsDeleted():
|
|
|
|
action = "deleted"
|
|
|
|
|
|
|
|
// If our local vector is version 1 AND it is the only version
|
|
|
|
// vector so far seen for this file then it is a new file. Else if
|
|
|
|
// it is > 1 it's not new, and if it is 1 but another shortId
|
|
|
|
// version vector exists then it is new for us but created elsewhere
|
|
|
|
// so the file is still not new but modified by us. Only if it is
|
|
|
|
// truly new do we change this to 'added', else we leave it as
|
|
|
|
// 'modified'.
|
|
|
|
case len(file.Version.Counters) == 1 && file.Version.Counters[0].Value == 1:
|
|
|
|
action = "added"
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.IsSymlink() {
|
|
|
|
objType = "symlink"
|
|
|
|
} else if file.IsDirectory() {
|
|
|
|
objType = "dir"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two different events can be fired here based on what EventType is passed into function
|
2019-08-15 14:29:37 +00:00
|
|
|
f.evLogger.Log(typeOfEvent, map[string]string{
|
2019-04-07 11:29:17 +00:00
|
|
|
"folder": f.ID,
|
|
|
|
"folderID": f.ID, // incorrect, deprecated, kept for historical compliance
|
|
|
|
"label": f.Label,
|
|
|
|
"action": action,
|
|
|
|
"type": objType,
|
|
|
|
"path": filepath.FromSlash(file.Name),
|
|
|
|
"modifiedBy": file.ModifiedBy.String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 10:04:41 +00:00
|
|
|
// The exists function is expected to return true for all known paths
|
|
|
|
// (excluding "" and ".")
|
|
|
|
func unifySubs(dirs []string, exists func(dir string) bool) []string {
|
|
|
|
if len(dirs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sort.Strings(dirs)
|
|
|
|
if dirs[0] == "" || dirs[0] == "." || dirs[0] == string(fs.PathSeparator) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
prev := "./" // Anything that can't be parent of a clean path
|
|
|
|
for i := 0; i < len(dirs); {
|
|
|
|
dir, err := fs.Canonicalize(dirs[i])
|
|
|
|
if err != nil {
|
|
|
|
l.Debugf("Skipping %v for scan: %s", dirs[i], err)
|
|
|
|
dirs = append(dirs[:i], dirs[i+1:]...)
|
|
|
|
continue
|
|
|
|
}
|
2018-11-22 10:16:45 +00:00
|
|
|
if dir == prev || fs.IsParent(dir, prev) {
|
2018-11-07 10:04:41 +00:00
|
|
|
dirs = append(dirs[:i], dirs[i+1:]...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parent := filepath.Dir(dir)
|
|
|
|
for parent != "." && parent != string(fs.PathSeparator) && !exists(parent) {
|
|
|
|
dir = parent
|
|
|
|
parent = filepath.Dir(dir)
|
|
|
|
}
|
|
|
|
dirs[i] = dir
|
|
|
|
prev = dir
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return dirs
|
|
|
|
}
|
2019-04-07 11:29:17 +00:00
|
|
|
|
|
|
|
type cFiler struct {
|
2020-01-21 17:23:08 +00:00
|
|
|
*db.Snapshot
|
2019-04-07 11:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements scanner.CurrentFiler
|
|
|
|
func (cf cFiler) CurrentFile(file string) (protocol.FileInfo, bool) {
|
|
|
|
return cf.Get(protocol.LocalDeviceID, file)
|
|
|
|
}
|