2017-10-20 14:52:55 +00:00
|
|
|
// Copyright (C) 2016 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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2022-12-21 21:01:00 +00:00
|
|
|
//go:build !(solaris && !cgo) && !(darwin && !cgo) && !(android && amd64)
|
|
|
|
// +build !solaris cgo
|
|
|
|
// +build !darwin cgo
|
|
|
|
// +build !android !amd64
|
2017-10-20 14:52:55 +00:00
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2018-03-14 13:48:22 +00:00
|
|
|
"github.com/syncthing/notify"
|
2017-10-20 14:52:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Notify does not block on sending to channel, so the channel must be buffered.
|
|
|
|
// The actual number is magic.
|
|
|
|
// Not meant to be changed, but must be changeable for tests
|
|
|
|
var backendBuffer = 500
|
|
|
|
|
2019-05-25 19:08:26 +00:00
|
|
|
func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, <-chan error, error) {
|
2019-09-22 07:03:22 +00:00
|
|
|
watchPath, roots, err := f.watchPaths(name)
|
2017-10-20 14:52:55 +00:00
|
|
|
if err != nil {
|
2019-05-25 19:08:26 +00:00
|
|
|
return nil, nil, err
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
outChan := make(chan Event)
|
|
|
|
backendChan := make(chan notify.EventInfo, backendBuffer)
|
|
|
|
|
|
|
|
eventMask := subEventMask
|
|
|
|
if !ignorePerms {
|
|
|
|
eventMask |= permEventMask
|
|
|
|
}
|
|
|
|
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
absShouldIgnore := func(absPath string) bool {
|
|
|
|
rel, err := f.unrootedChecked(absPath, roots)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
2018-05-14 07:47:23 +00:00
|
|
|
}
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
return ignore.Match(rel).CanSkipDir()
|
2018-05-14 07:47:23 +00:00
|
|
|
}
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
err = notify.WatchWithFilter(watchPath, backendChan, absShouldIgnore, eventMask)
|
2018-05-14 07:47:23 +00:00
|
|
|
if err != nil {
|
2017-10-20 14:52:55 +00:00
|
|
|
notify.Stop(backendChan)
|
|
|
|
if reachedMaxUserWatches(err) {
|
2018-03-23 11:56:38 +00:00
|
|
|
err = errors.New("failed to setup inotify handler. Please increase inotify limits, see https://docs.syncthing.net/users/faq.html#inotify-limits")
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
2019-05-25 19:08:26 +00:00
|
|
|
return nil, nil, err
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 19:08:26 +00:00
|
|
|
errChan := make(chan error)
|
2019-11-21 07:41:15 +00:00
|
|
|
go f.watchLoop(ctx, name, roots, backendChan, outChan, errChan, ignore)
|
2017-10-20 14:52:55 +00:00
|
|
|
|
2019-05-25 19:08:26 +00:00
|
|
|
return outChan, errChan, nil
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func (f *BasicFilesystem) watchLoop(ctx context.Context, name string, roots []string, backendChan chan notify.EventInfo, outChan chan<- Event, errChan chan<- error, ignore Matcher) {
|
2017-10-20 14:52:55 +00:00
|
|
|
for {
|
|
|
|
// Detect channel overflow
|
|
|
|
if len(backendChan) == backendBuffer {
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-backendChan:
|
|
|
|
default:
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// When next scheduling a scan, do it on the entire folder as events have been lost.
|
|
|
|
outChan <- Event{Name: name, Type: NonRemove}
|
|
|
|
l.Debugln(f.Type(), f.URI(), "Watch: Event overflow, send \".\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ev := <-backendChan:
|
2019-09-22 07:03:22 +00:00
|
|
|
relPath, err := f.unrootedChecked(ev.Path(), roots)
|
2019-05-25 19:08:26 +00:00
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case errChan <- err:
|
|
|
|
l.Debugln(f.Type(), f.URI(), "Watch: Sending error", err)
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
notify.Stop(backendChan)
|
|
|
|
l.Debugln(f.Type(), f.URI(), "Watch: Stopped due to", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
if ignore.Match(relPath).IsIgnored() {
|
2017-10-20 14:52:55 +00:00
|
|
|
l.Debugln(f.Type(), f.URI(), "Watch: Ignoring", relPath)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
evType := f.eventType(ev.Event())
|
|
|
|
select {
|
|
|
|
case outChan <- Event{Name: relPath, Type: evType}:
|
|
|
|
l.Debugln(f.Type(), f.URI(), "Watch: Sending", relPath, evType)
|
|
|
|
case <-ctx.Done():
|
|
|
|
notify.Stop(backendChan)
|
|
|
|
l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
notify.Stop(backendChan)
|
|
|
|
l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*BasicFilesystem) eventType(notifyType notify.Event) EventType {
|
2017-10-20 14:52:55 +00:00
|
|
|
if notifyType&rmEventMask != 0 {
|
|
|
|
return Remove
|
|
|
|
}
|
|
|
|
return NonRemove
|
|
|
|
}
|