2024-01-13 17:58:23 +00:00
|
|
|
// Copyright (C) 2024 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 https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
// Package result provides the result type for ignore matching. This is a
|
|
|
|
// separate package in order to break import cycles.
|
|
|
|
package ignoreresult
|
|
|
|
|
|
|
|
const (
|
|
|
|
NotIgnored R = 0
|
|
|
|
// `Ignored` is defined in platform specific files
|
|
|
|
IgnoredDeletable = Ignored | deletableBit
|
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
|
|
|
IgnoreAndSkip = Ignored | canSkipDirBit
|
2024-01-13 17:58:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Private definitions of the bits that make up the result value
|
|
|
|
ignoreBit R = 1 << iota
|
|
|
|
deletableBit
|
|
|
|
foldCaseBit
|
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
|
|
|
canSkipDirBit
|
2024-01-13 17:58:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type R uint8
|
|
|
|
|
|
|
|
// IsIgnored returns true if the result is ignored.
|
|
|
|
func (r R) IsIgnored() bool {
|
|
|
|
return r&ignoreBit != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDeletable returns true if the result is ignored and deletable.
|
|
|
|
func (r R) IsDeletable() bool {
|
|
|
|
return r.IsIgnored() && r&deletableBit != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCaseFolded returns true if the result was a case-insensitive match.
|
|
|
|
func (r R) IsCaseFolded() bool {
|
|
|
|
return r&foldCaseBit != 0
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// CanSkipDir returns true if the result is ignored and the directory can be
|
|
|
|
// skipped (no need to recurse deeper). Note that ignore matches are textual
|
|
|
|
// and based on the name only -- this being true does not mean that the
|
|
|
|
// matched item is a directory, merely that *if* it is a directory, it can
|
|
|
|
// be skipped.
|
|
|
|
func (r R) CanSkipDir() bool {
|
|
|
|
return r.IsIgnored() && r&canSkipDirBit != 0
|
|
|
|
}
|
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
// ToggleIgnored returns a copy of the result with the ignored bit toggled.
|
|
|
|
func (r R) ToggleIgnored() R {
|
|
|
|
return r ^ ignoreBit
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDeletable returns a copy of the result with the deletable bit set.
|
|
|
|
func (r R) WithDeletable() R {
|
|
|
|
return r | deletableBit
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFoldCase returns a copy of the result with the fold case bit set.
|
|
|
|
func (r R) WithFoldCase() R {
|
|
|
|
return r | foldCaseBit
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// WithSkipDir returns a copy of the result with the skip dir bit set.
|
|
|
|
func (r R) WithSkipDir() R {
|
|
|
|
return r | canSkipDirBit
|
|
|
|
}
|
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
// String returns a human readable representation of the result flags.
|
|
|
|
func (r R) String() string {
|
|
|
|
var s string
|
|
|
|
if r&ignoreBit != 0 {
|
|
|
|
s += "i"
|
|
|
|
} else {
|
|
|
|
s += "-"
|
|
|
|
}
|
|
|
|
if r&deletableBit != 0 {
|
|
|
|
s += "d"
|
|
|
|
} else {
|
|
|
|
s += "-"
|
|
|
|
}
|
|
|
|
if r&foldCaseBit != 0 {
|
|
|
|
s += "f"
|
|
|
|
} else {
|
|
|
|
s += "-"
|
|
|
|
}
|
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
|
|
|
if r&canSkipDirBit != 0 {
|
|
|
|
s += "s"
|
|
|
|
} else {
|
|
|
|
s += "-"
|
|
|
|
}
|
2024-01-13 17:58:23 +00:00
|
|
|
return s
|
|
|
|
}
|