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"
|
|
|
|
"time"
|
2017-08-25 19:47:01 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2017-10-20 14:52:55 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/watchaggregator"
|
2017-04-26 00:15:23 +00:00
|
|
|
)
|
2016-04-26 14:01:46 +00:00
|
|
|
|
|
|
|
type folder struct {
|
|
|
|
stateTracker
|
2017-08-25 19:47:01 +00:00
|
|
|
config.FolderConfiguration
|
2017-04-01 09:58:06 +00:00
|
|
|
|
2017-04-20 00:20:34 +00:00
|
|
|
scan folderScanner
|
|
|
|
model *Model
|
2017-04-26 00:15:23 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2017-04-20 00:20:34 +00:00
|
|
|
initialScanFinished chan struct{}
|
2017-10-20 14:52:55 +00:00
|
|
|
watchCancel context.CancelFunc
|
|
|
|
watchChan chan []string
|
2017-10-24 07:58:55 +00:00
|
|
|
restartWatchChan chan struct{}
|
2016-04-26 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 19:47:01 +00:00
|
|
|
func newFolder(model *Model, cfg config.FolderConfiguration) folder {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
return folder{
|
|
|
|
stateTracker: newStateTracker(cfg.ID),
|
|
|
|
FolderConfiguration: cfg,
|
|
|
|
|
|
|
|
scan: newFolderScanner(cfg),
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
model: model,
|
|
|
|
initialScanFinished: make(chan struct{}),
|
|
|
|
}
|
2016-04-26 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
func (f *folder) BringToFront(string) {}
|
|
|
|
|
2016-04-26 14:01:46 +00:00
|
|
|
func (f *folder) DelayScan(next time.Duration) {
|
|
|
|
f.scan.Delay(next)
|
|
|
|
}
|
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
func (f *folder) IndexUpdated() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) IgnoresUpdated() {
|
|
|
|
if f.FSWatcherEnabled {
|
|
|
|
f.scheduleWatchRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *folder) Jobs() ([]string, []string) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
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
|
2016-04-26 14:01:46 +00:00
|
|
|
return f.scan.Scan(subdirs)
|
|
|
|
}
|
2017-04-26 00:15:23 +00:00
|
|
|
|
2016-04-26 14:01:46 +00:00
|
|
|
func (f *folder) Stop() {
|
2017-04-26 00:15:23 +00:00
|
|
|
f.cancel()
|
2016-04-26 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
func (f *folder) BlockStats() map[string]int {
|
|
|
|
return nil
|
2016-04-26 14:01:46 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.CheckFreeSpace(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.model.cfg.CheckHomeFreeSpace(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-26 14:01:46 +00:00
|
|
|
|
2017-10-12 06:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:20:34 +00:00
|
|
|
func (f *folder) scanSubdirs(subDirs []string) error {
|
2017-04-26 00:15:23 +00:00
|
|
|
if err := f.model.internalScanFolderSubdirs(f.ctx, f.folderID, subDirs); err != nil {
|
2016-04-26 14:01:46 +00:00
|
|
|
// Potentially sets the error twice, once in the scanner just
|
|
|
|
// by doing a check, and once here, if the error returned is
|
2017-10-24 07:58:55 +00:00
|
|
|
// the same one as returned by CheckHealth, though
|
2016-04-26 14:01:46 +00:00
|
|
|
// duplicate set is handled by setError.
|
|
|
|
f.setError(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.scan.Reschedule()
|
|
|
|
}
|
2017-10-20 14:52:55 +00:00
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
func (f *folder) startWatch() {
|
2017-10-20 14:52:55 +00:00
|
|
|
ctx, cancel := context.WithCancel(f.ctx)
|
|
|
|
f.model.fmut.RLock()
|
|
|
|
ignores := f.model.folderIgnores[f.folderID]
|
|
|
|
f.model.fmut.RUnlock()
|
|
|
|
eventChan, err := f.Filesystem().Watch(".", ignores, ctx, f.IgnorePerms)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnf("Failed to start filesystem watcher for folder %s: %v", f.Description(), err)
|
|
|
|
} else {
|
|
|
|
f.watchChan = make(chan []string)
|
|
|
|
f.watchCancel = cancel
|
|
|
|
watchaggregator.Aggregate(eventChan, f.watchChan, f.FolderConfiguration, f.model.cfg, ctx)
|
|
|
|
l.Infoln("Started filesystem watcher for folder", f.Description())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 07:58:55 +00:00
|
|
|
func (f *folder) restartWatch() {
|
2017-10-20 14:52:55 +00:00
|
|
|
f.watchCancel()
|
2017-10-24 07:58:55 +00:00
|
|
|
f.startWatch()
|
2017-10-20 14:52:55 +00:00
|
|
|
f.Scan(nil)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
func (f *folder) setError(err error) {
|
|
|
|
_, _, 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 {
|
|
|
|
f.watchCancel()
|
|
|
|
} else {
|
|
|
|
f.scheduleWatchRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f.stateTracker.setError(err)
|
|
|
|
}
|