2018-01-12 16:30:54 +00:00
|
|
|
// Package filter controls the filtering of files
|
|
|
|
package filter
|
2015-09-27 15:13:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2015-09-27 15:13:20 +00:00
|
|
|
"fmt"
|
2018-01-12 16:30:54 +00:00
|
|
|
"log"
|
2015-09-27 15:13:20 +00:00
|
|
|
"os"
|
2016-04-21 19:06:21 +00:00
|
|
|
"path"
|
2015-09-27 15:13:20 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-12-17 11:52:38 +00:00
|
|
|
"time"
|
2015-09-27 15:13:20 +00:00
|
|
|
|
2016-06-12 14:06:02 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2018-12-13 10:47:09 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2015-09-27 15:13:20 +00:00
|
|
|
)
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Active is the globally active filter
|
|
|
|
var Active = mustNewFilter(nil)
|
2015-09-27 15:13:20 +00:00
|
|
|
|
|
|
|
// rule is one filter rule
|
|
|
|
type rule struct {
|
2019-01-20 17:56:59 +00:00
|
|
|
Include bool
|
|
|
|
Regexp *regexp.Regexp
|
|
|
|
boundedRecursion bool
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if rule matches path
|
|
|
|
func (r *rule) Match(path string) bool {
|
|
|
|
return r.Regexp.MatchString(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String the rule
|
|
|
|
func (r *rule) String() string {
|
|
|
|
c := "-"
|
|
|
|
if r.Include {
|
|
|
|
c = "+"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s %s", c, r.Regexp.String())
|
|
|
|
}
|
|
|
|
|
2016-05-16 16:14:04 +00:00
|
|
|
// rules is a slice of rules
|
|
|
|
type rules struct {
|
|
|
|
rules []rule
|
|
|
|
existing map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add adds a rule if it doesn't exist already
|
2019-01-20 17:56:59 +00:00
|
|
|
func (rs *rules) add(Include bool, re *regexp.Regexp, boundedRecursion bool) {
|
2016-05-16 16:14:04 +00:00
|
|
|
if rs.existing == nil {
|
|
|
|
rs.existing = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
newRule := rule{
|
2019-01-20 17:56:59 +00:00
|
|
|
Include: Include,
|
|
|
|
Regexp: re,
|
|
|
|
boundedRecursion: boundedRecursion,
|
2016-05-16 16:14:04 +00:00
|
|
|
}
|
|
|
|
newRuleString := newRule.String()
|
|
|
|
if _, ok := rs.existing[newRuleString]; ok {
|
|
|
|
return // rule already exists
|
|
|
|
}
|
|
|
|
rs.rules = append(rs.rules, newRule)
|
|
|
|
rs.existing[newRuleString] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear clears all the rules
|
|
|
|
func (rs *rules) clear() {
|
|
|
|
rs.rules = nil
|
|
|
|
rs.existing = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// len returns the number of rules
|
|
|
|
func (rs *rules) len() int {
|
|
|
|
return len(rs.rules)
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:56:59 +00:00
|
|
|
// boundedRecursion returns true if the set of filters would only
|
|
|
|
// need bounded recursion to evaluate
|
|
|
|
func (rs *rules) boundedRecursion() bool {
|
|
|
|
var (
|
|
|
|
excludeAll = false
|
|
|
|
boundedRecursion = true
|
|
|
|
)
|
|
|
|
for _, rule := range rs.rules {
|
|
|
|
if rule.Include {
|
|
|
|
boundedRecursion = boundedRecursion && rule.boundedRecursion
|
|
|
|
} else if rule.Regexp.String() == `^.*$` {
|
|
|
|
excludeAll = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return excludeAll && boundedRecursion
|
|
|
|
}
|
|
|
|
|
2016-10-07 10:39:39 +00:00
|
|
|
// FilesMap describes the map of files to transfer
|
|
|
|
type FilesMap map[string]struct{}
|
2015-09-27 15:13:20 +00:00
|
|
|
|
2019-04-30 12:06:24 +00:00
|
|
|
// Opt configures the filter
|
2018-01-12 16:30:54 +00:00
|
|
|
type Opt struct {
|
2015-09-27 15:13:20 +00:00
|
|
|
DeleteExcluded bool
|
2018-01-12 16:30:54 +00:00
|
|
|
FilterRule []string
|
|
|
|
FilterFrom []string
|
|
|
|
ExcludeRule []string
|
|
|
|
ExcludeFrom []string
|
2017-11-16 14:37:00 +00:00
|
|
|
ExcludeFile string
|
2018-01-12 16:30:54 +00:00
|
|
|
IncludeRule []string
|
|
|
|
IncludeFrom []string
|
|
|
|
FilesFrom []string
|
|
|
|
MinAge fs.Duration
|
|
|
|
MaxAge fs.Duration
|
|
|
|
MinSize fs.SizeSuffix
|
|
|
|
MaxSize fs.SizeSuffix
|
2018-11-12 14:29:37 +00:00
|
|
|
IgnoreCase bool
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// DefaultOpt is the default config for the filter
|
|
|
|
var DefaultOpt = Opt{
|
2018-03-12 20:52:42 +00:00
|
|
|
MinAge: fs.DurationOff,
|
|
|
|
MaxAge: fs.DurationOff,
|
2018-01-12 16:30:54 +00:00
|
|
|
MinSize: fs.SizeSuffix(-1),
|
|
|
|
MaxSize: fs.SizeSuffix(-1),
|
2015-12-17 11:52:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Filter describes any filtering in operation
|
|
|
|
type Filter struct {
|
|
|
|
Opt Opt
|
|
|
|
ModTimeFrom time.Time
|
|
|
|
ModTimeTo time.Time
|
|
|
|
fileRules rules
|
|
|
|
dirRules rules
|
|
|
|
files FilesMap // files if filesFrom
|
|
|
|
dirs FilesMap // dirs from filesFrom
|
|
|
|
}
|
2015-12-17 11:52:38 +00:00
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// NewFilter parses the command line options and creates a Filter
|
|
|
|
// object. If opt is nil, then DefaultOpt will be used
|
|
|
|
func NewFilter(opt *Opt) (f *Filter, err error) {
|
|
|
|
f = &Filter{}
|
2015-12-17 11:52:38 +00:00
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Make a copy of the options
|
|
|
|
if opt != nil {
|
|
|
|
f.Opt = *opt
|
|
|
|
} else {
|
|
|
|
f.Opt = DefaultOpt
|
|
|
|
}
|
2015-12-17 11:52:38 +00:00
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Filter flags
|
2018-03-12 20:52:42 +00:00
|
|
|
if f.Opt.MinAge.IsSet() {
|
2018-01-12 16:30:54 +00:00
|
|
|
f.ModTimeTo = time.Now().Add(-time.Duration(f.Opt.MinAge))
|
|
|
|
fs.Debugf(nil, "--min-age %v to %v", f.Opt.MinAge, f.ModTimeTo)
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2018-03-12 20:52:42 +00:00
|
|
|
if f.Opt.MaxAge.IsSet() {
|
2018-01-12 16:30:54 +00:00
|
|
|
f.ModTimeFrom = time.Now().Add(-time.Duration(f.Opt.MaxAge))
|
2018-04-26 08:17:22 +00:00
|
|
|
if !f.ModTimeTo.IsZero() && f.ModTimeTo.Before(f.ModTimeFrom) {
|
2018-01-12 16:30:54 +00:00
|
|
|
log.Fatal("filter: --min-age can't be larger than --max-age")
|
|
|
|
}
|
|
|
|
fs.Debugf(nil, "--max-age %v to %v", f.Opt.MaxAge, f.ModTimeFrom)
|
|
|
|
}
|
|
|
|
|
2016-01-10 11:42:53 +00:00
|
|
|
addImplicitExclude := false
|
2017-11-20 22:33:54 +00:00
|
|
|
foundExcludeRule := false
|
2015-12-17 11:52:38 +00:00
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
for _, rule := range f.Opt.IncludeRule {
|
|
|
|
err = f.Add(true, rule)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
addImplicitExclude = true
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
for _, rule := range f.Opt.IncludeFrom {
|
|
|
|
err := forEachLine(rule, func(line string) error {
|
|
|
|
return f.Add(true, line)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
addImplicitExclude = true
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
for _, rule := range f.Opt.ExcludeRule {
|
|
|
|
err = f.Add(false, rule)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
foundExcludeRule = true
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
for _, rule := range f.Opt.ExcludeFrom {
|
|
|
|
err := forEachLine(rule, func(line string) error {
|
|
|
|
return f.Add(false, line)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
foundExcludeRule = true
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2017-11-20 22:33:54 +00:00
|
|
|
|
|
|
|
if addImplicitExclude && foundExcludeRule {
|
2018-06-22 21:18:55 +00:00
|
|
|
fs.Errorf(nil, "Using --filter is recommended instead of both --include and --exclude as the order they are parsed in is indeterminate")
|
2017-11-20 22:33:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
for _, rule := range f.Opt.FilterRule {
|
|
|
|
err = f.AddRule(rule)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
for _, rule := range f.Opt.FilterFrom {
|
|
|
|
err := forEachLine(rule, f.AddRule)
|
2016-01-10 11:42:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
for _, rule := range f.Opt.FilesFrom {
|
|
|
|
f.initAddFile() // init to show --files-from set even if no files within
|
|
|
|
err := forEachLine(rule, func(line string) error {
|
|
|
|
return f.AddFile(line)
|
|
|
|
})
|
2015-12-17 11:52:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
if addImplicitExclude {
|
|
|
|
err = f.Add(false, "/**")
|
2015-12-17 11:52:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
if fs.Config.Dump&fs.DumpFilters != 0 {
|
2015-09-27 15:13:20 +00:00
|
|
|
fmt.Println("--- start filters ---")
|
|
|
|
fmt.Println(f.DumpFilters())
|
|
|
|
fmt.Println("--- end filters ---")
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
func mustNewFilter(opt *Opt) *Filter {
|
|
|
|
f, err := NewFilter(opt)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2016-05-16 16:14:04 +00:00
|
|
|
// addDirGlobs adds directory globs from the file glob passed in
|
|
|
|
func (f *Filter) addDirGlobs(Include bool, glob string) error {
|
|
|
|
for _, dirGlob := range globToDirGlobs(glob) {
|
|
|
|
// Don't add "/" as we always include the root
|
|
|
|
if dirGlob == "/" {
|
|
|
|
continue
|
|
|
|
}
|
2018-11-12 14:29:37 +00:00
|
|
|
dirRe, err := globToRegexp(dirGlob, f.Opt.IgnoreCase)
|
2016-05-16 16:14:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-20 17:56:59 +00:00
|
|
|
boundedRecursion := globBoundedRecursion(dirGlob)
|
|
|
|
f.dirRules.add(Include, dirRe, boundedRecursion)
|
2016-05-16 16:14:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-27 15:13:20 +00:00
|
|
|
// Add adds a filter rule with include or exclude status indicated
|
|
|
|
func (f *Filter) Add(Include bool, glob string) error {
|
2016-05-16 16:14:04 +00:00
|
|
|
isDirRule := strings.HasSuffix(glob, "/")
|
|
|
|
isFileRule := !isDirRule
|
2016-12-07 11:17:10 +00:00
|
|
|
if strings.Contains(glob, "**") {
|
2016-05-16 16:14:04 +00:00
|
|
|
isDirRule, isFileRule = true, true
|
|
|
|
}
|
2018-11-12 14:29:37 +00:00
|
|
|
re, err := globToRegexp(glob, f.Opt.IgnoreCase)
|
2015-09-27 15:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-20 17:56:59 +00:00
|
|
|
boundedRecursion := globBoundedRecursion(glob)
|
2016-05-16 16:14:04 +00:00
|
|
|
if isFileRule {
|
2019-01-20 17:56:59 +00:00
|
|
|
f.fileRules.add(Include, re, boundedRecursion)
|
2016-05-16 16:14:04 +00:00
|
|
|
// If include rule work out what directories are needed to scan
|
|
|
|
// if exclude rule, we can't rule anything out
|
|
|
|
// Unless it is `*` which matches everything
|
|
|
|
// NB ** and /** are DirRules
|
|
|
|
if Include || glob == "*" {
|
|
|
|
err = f.addDirGlobs(Include, glob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isDirRule {
|
2019-01-20 17:56:59 +00:00
|
|
|
f.dirRules.add(Include, re, boundedRecursion)
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRule adds a filter rule with include/exclude indicated by the prefix
|
|
|
|
//
|
|
|
|
// These are
|
|
|
|
//
|
|
|
|
// + glob
|
|
|
|
// - glob
|
|
|
|
// !
|
|
|
|
//
|
|
|
|
// '+' includes the glob, '-' excludes it and '!' resets the filter list
|
|
|
|
//
|
|
|
|
// Line comments may be introduced with '#' or ';'
|
|
|
|
func (f *Filter) AddRule(rule string) error {
|
|
|
|
switch {
|
|
|
|
case rule == "!":
|
|
|
|
f.Clear()
|
|
|
|
return nil
|
|
|
|
case strings.HasPrefix(rule, "- "):
|
|
|
|
return f.Add(false, rule[2:])
|
|
|
|
case strings.HasPrefix(rule, "+ "):
|
|
|
|
return f.Add(true, rule[2:])
|
|
|
|
}
|
2016-06-12 14:06:02 +00:00
|
|
|
return errors.Errorf("malformed rule %q", rule)
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 10:11:15 +00:00
|
|
|
// initAddFile creates f.files and f.dirs
|
|
|
|
func (f *Filter) initAddFile() {
|
2015-09-27 15:13:20 +00:00
|
|
|
if f.files == nil {
|
2016-10-07 10:39:39 +00:00
|
|
|
f.files = make(FilesMap)
|
|
|
|
f.dirs = make(FilesMap)
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
2017-03-04 10:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddFile adds a single file to the files from list
|
|
|
|
func (f *Filter) AddFile(file string) error {
|
|
|
|
f.initAddFile()
|
2015-09-27 15:13:20 +00:00
|
|
|
file = strings.Trim(file, "/")
|
|
|
|
f.files[file] = struct{}{}
|
2016-04-21 19:06:21 +00:00
|
|
|
// Put all the parent directories into f.dirs
|
|
|
|
for {
|
|
|
|
file = path.Dir(file)
|
|
|
|
if file == "." {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _, found := f.dirs[file]; found {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
f.dirs[file] = struct{}{}
|
|
|
|
}
|
2015-09-27 15:13:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-07 10:39:39 +00:00
|
|
|
// Files returns all the files from the `--files-from` list
|
|
|
|
//
|
|
|
|
// It may be nil if the list is empty
|
|
|
|
func (f *Filter) Files() FilesMap {
|
|
|
|
return f.files
|
|
|
|
}
|
|
|
|
|
2015-09-27 15:13:20 +00:00
|
|
|
// Clear clears all the filter rules
|
|
|
|
func (f *Filter) Clear() {
|
2016-05-16 16:14:04 +00:00
|
|
|
f.fileRules.clear()
|
|
|
|
f.dirRules.clear()
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 19:58:00 +00:00
|
|
|
// InActive returns false if any filters are active
|
|
|
|
func (f *Filter) InActive() bool {
|
|
|
|
return (f.files == nil &&
|
|
|
|
f.ModTimeFrom.IsZero() &&
|
|
|
|
f.ModTimeTo.IsZero() &&
|
2018-01-12 16:30:54 +00:00
|
|
|
f.Opt.MinSize < 0 &&
|
|
|
|
f.Opt.MaxSize < 0 &&
|
2016-05-16 16:14:04 +00:00
|
|
|
f.fileRules.len() == 0 &&
|
2017-11-16 14:37:00 +00:00
|
|
|
f.dirRules.len() == 0 &&
|
2018-01-12 16:30:54 +00:00
|
|
|
len(f.Opt.ExcludeFile) == 0)
|
2016-02-25 19:58:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 17:56:59 +00:00
|
|
|
// BoundedRecursion returns true if the filter can be evaluated with
|
|
|
|
// bounded recursion only.
|
|
|
|
func (f *Filter) BoundedRecursion() bool {
|
|
|
|
return f.fileRules.boundedRecursion()
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:06:21 +00:00
|
|
|
// includeRemote returns whether this remote passes the filter rules.
|
|
|
|
func (f *Filter) includeRemote(remote string) bool {
|
2016-05-16 16:14:04 +00:00
|
|
|
for _, rule := range f.fileRules.rules {
|
2016-04-21 19:06:21 +00:00
|
|
|
if rule.Match(remote) {
|
|
|
|
return rule.Include
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-16 14:38:00 +00:00
|
|
|
// ListContainsExcludeFile checks if exclude file is present in the list.
|
2018-01-12 16:30:54 +00:00
|
|
|
func (f *Filter) ListContainsExcludeFile(entries fs.DirEntries) bool {
|
|
|
|
if len(f.Opt.ExcludeFile) == 0 {
|
2017-11-16 14:38:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
2018-01-12 16:30:54 +00:00
|
|
|
obj, ok := entry.(fs.Object)
|
2017-11-16 14:38:00 +00:00
|
|
|
if ok {
|
|
|
|
basename := path.Base(obj.Remote())
|
2018-01-12 16:30:54 +00:00
|
|
|
if basename == f.Opt.ExcludeFile {
|
2017-11-16 14:38:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-09 09:28:36 +00:00
|
|
|
// IncludeDirectory returns a function which checks whether this
|
|
|
|
// directory should be included in the sync or not.
|
2019-06-17 08:34:30 +00:00
|
|
|
func (f *Filter) IncludeDirectory(ctx context.Context, fs fs.Fs) func(string) (bool, error) {
|
2017-11-09 09:28:36 +00:00
|
|
|
return func(remote string) (bool, error) {
|
|
|
|
remote = strings.Trim(remote, "/")
|
|
|
|
// first check if we need to remove directory based on
|
|
|
|
// the exclude file
|
2019-06-17 08:34:30 +00:00
|
|
|
excl, err := f.DirContainsExcludeFile(ctx, fs, remote)
|
2017-11-09 09:28:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if excl {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filesFrom takes precedence
|
|
|
|
if f.files != nil {
|
|
|
|
_, include := f.dirs[remote]
|
|
|
|
return include, nil
|
|
|
|
}
|
|
|
|
remote += "/"
|
|
|
|
for _, rule := range f.dirRules.rules {
|
|
|
|
if rule.Match(remote) {
|
|
|
|
return rule.Include, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
2016-04-21 19:06:21 +00:00
|
|
|
}
|
2017-11-09 09:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DirContainsExcludeFile checks if exclude file is present in a
|
|
|
|
// directroy. If fs is nil, it works properly if ExcludeFile is an
|
|
|
|
// empty string (for testing).
|
2019-06-17 08:34:30 +00:00
|
|
|
func (f *Filter) DirContainsExcludeFile(ctx context.Context, fremote fs.Fs, remote string) (bool, error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
if len(f.Opt.ExcludeFile) > 0 {
|
2019-06-17 08:34:30 +00:00
|
|
|
exists, err := fs.FileExists(ctx, fremote, path.Join(remote, f.Opt.ExcludeFile))
|
2017-11-09 09:28:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return true, nil
|
2016-05-16 16:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-09 09:28:36 +00:00
|
|
|
return false, nil
|
2016-04-21 19:06:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-27 15:13:20 +00:00
|
|
|
// Include returns whether this object should be included into the
|
|
|
|
// sync or not
|
2015-12-17 11:52:38 +00:00
|
|
|
func (f *Filter) Include(remote string, size int64, modTime time.Time) bool {
|
2015-09-27 15:13:20 +00:00
|
|
|
// filesFrom takes precedence
|
|
|
|
if f.files != nil {
|
|
|
|
_, include := f.files[remote]
|
|
|
|
return include
|
|
|
|
}
|
2015-12-17 11:52:38 +00:00
|
|
|
if !f.ModTimeFrom.IsZero() && modTime.Before(f.ModTimeFrom) {
|
|
|
|
return false
|
|
|
|
}
|
2015-12-17 14:22:43 +00:00
|
|
|
if !f.ModTimeTo.IsZero() && modTime.After(f.ModTimeTo) {
|
2015-12-17 11:52:38 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
if f.Opt.MinSize >= 0 && size < int64(f.Opt.MinSize) {
|
2015-09-27 15:13:20 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
if f.Opt.MaxSize >= 0 && size > int64(f.Opt.MaxSize) {
|
2015-09-27 15:13:20 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-04-21 19:06:21 +00:00
|
|
|
return f.includeRemote(remote)
|
2015-09-27 15:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-17 11:52:38 +00:00
|
|
|
// IncludeObject returns whether this object should be included into
|
|
|
|
// the sync or not. This is a convenience function to avoid calling
|
|
|
|
// o.ModTime(), which is an expensive operation.
|
2019-06-17 08:34:30 +00:00
|
|
|
func (f *Filter) IncludeObject(ctx context.Context, o fs.Object) bool {
|
2015-12-17 11:52:38 +00:00
|
|
|
var modTime time.Time
|
|
|
|
|
2015-12-17 14:22:43 +00:00
|
|
|
if !f.ModTimeFrom.IsZero() || !f.ModTimeTo.IsZero() {
|
2019-06-17 08:34:30 +00:00
|
|
|
modTime = o.ModTime(ctx)
|
2015-12-17 11:52:38 +00:00
|
|
|
} else {
|
|
|
|
modTime = time.Unix(0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Include(o.Remote(), o.Size(), modTime)
|
|
|
|
}
|
|
|
|
|
2015-09-27 15:13:20 +00:00
|
|
|
// forEachLine calls fn on every line in the file pointed to by path
|
|
|
|
//
|
|
|
|
// It ignores empty lines and lines starting with '#' or ';'
|
|
|
|
func forEachLine(path string, fn func(string) error) (err error) {
|
|
|
|
in, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
defer fs.CheckClose(in, &err)
|
2015-09-27 15:13:20 +00:00
|
|
|
scanner := bufio.NewScanner(in)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if len(line) == 0 || line[0] == '#' || line[0] == ';' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := fn(line)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scanner.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DumpFilters dumps the filters in textual form, 1 per line
|
|
|
|
func (f *Filter) DumpFilters() string {
|
|
|
|
rules := []string{}
|
2015-12-17 11:52:38 +00:00
|
|
|
if !f.ModTimeFrom.IsZero() {
|
|
|
|
rules = append(rules, fmt.Sprintf("Last-modified date must be equal or greater than: %s", f.ModTimeFrom.String()))
|
|
|
|
}
|
|
|
|
if !f.ModTimeTo.IsZero() {
|
|
|
|
rules = append(rules, fmt.Sprintf("Last-modified date must be equal or less than: %s", f.ModTimeTo.String()))
|
|
|
|
}
|
2016-05-16 16:14:04 +00:00
|
|
|
rules = append(rules, "--- File filter rules ---")
|
|
|
|
for _, rule := range f.fileRules.rules {
|
2015-09-27 15:13:20 +00:00
|
|
|
rules = append(rules, rule.String())
|
|
|
|
}
|
2016-05-16 16:14:04 +00:00
|
|
|
rules = append(rules, "--- Directory filter rules ---")
|
|
|
|
for _, dirRule := range f.dirRules.rules {
|
|
|
|
rules = append(rules, dirRule.String())
|
|
|
|
}
|
2015-09-27 15:13:20 +00:00
|
|
|
return strings.Join(rules, "\n")
|
|
|
|
}
|
2018-10-19 16:41:14 +00:00
|
|
|
|
|
|
|
// HaveFilesFrom returns true if --files-from has been supplied
|
|
|
|
func (f *Filter) HaveFilesFrom() bool {
|
|
|
|
return f.files != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errFilesFromNotSet = errors.New("--files-from not set so can't use Filter.ListR")
|
|
|
|
|
|
|
|
// MakeListR makes function to return all the files set using --files-from
|
2019-06-17 08:34:30 +00:00
|
|
|
func (f *Filter) MakeListR(ctx context.Context, NewObject func(ctx context.Context, remote string) (fs.Object, error)) fs.ListRFn {
|
|
|
|
return func(ctx context.Context, dir string, callback fs.ListRCallback) error {
|
2018-10-19 16:41:14 +00:00
|
|
|
if !f.HaveFilesFrom() {
|
|
|
|
return errFilesFromNotSet
|
|
|
|
}
|
2018-12-13 10:47:09 +00:00
|
|
|
var (
|
|
|
|
remotes = make(chan string, fs.Config.Checkers)
|
|
|
|
g errgroup.Group
|
|
|
|
)
|
|
|
|
for i := 0; i < fs.Config.Checkers; i++ {
|
|
|
|
g.Go(func() (err error) {
|
|
|
|
var entries = make(fs.DirEntries, 1)
|
|
|
|
for remote := range remotes {
|
2019-06-17 08:34:30 +00:00
|
|
|
entries[0], err = NewObject(ctx, remote)
|
2018-12-13 10:47:09 +00:00
|
|
|
if err == fs.ErrorObjectNotFound {
|
|
|
|
// Skip files that are not found
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
err = callback(entries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2018-10-19 16:41:14 +00:00
|
|
|
for remote := range f.files {
|
2018-12-13 10:47:09 +00:00
|
|
|
remotes <- remote
|
2018-10-19 16:41:14 +00:00
|
|
|
}
|
2018-12-13 10:47:09 +00:00
|
|
|
close(remotes)
|
|
|
|
return g.Wait()
|
2018-10-19 16:41:14 +00:00
|
|
|
}
|
|
|
|
}
|