2017-09-10 12:23:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-09-03 01:18:58 +00:00
|
|
|
"strconv"
|
2017-09-10 12:23:29 +00:00
|
|
|
"strings"
|
2017-10-04 18:30:43 +00:00
|
|
|
"sync"
|
2017-09-10 12:23:29 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/debug"
|
2017-09-10 12:34:28 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/filter"
|
2017-09-10 12:23:29 +00:00
|
|
|
"github.com/restic/restic/internal/fs"
|
2017-09-11 19:37:10 +00:00
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-09-10 12:23:29 +00:00
|
|
|
)
|
|
|
|
|
2017-10-04 18:30:43 +00:00
|
|
|
type rejectionCache struct {
|
|
|
|
m map[string]bool
|
|
|
|
mtx sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock locks the mutex in rc.
|
|
|
|
func (rc *rejectionCache) Lock() {
|
|
|
|
if rc != nil {
|
|
|
|
rc.mtx.Lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks the mutex in rc.
|
|
|
|
func (rc *rejectionCache) Unlock() {
|
|
|
|
if rc != nil {
|
|
|
|
rc.mtx.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the last stored value for dir and a second boolean that
|
|
|
|
// indicates whether that value was actually written to the cache. It is the
|
|
|
|
// callers responsibility to call rc.Lock and rc.Unlock before using this
|
|
|
|
// method, otherwise data races may occur.
|
|
|
|
func (rc *rejectionCache) Get(dir string) (bool, bool) {
|
|
|
|
if rc == nil || rc.m == nil {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
v, ok := rc.m[dir]
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store stores a new value for dir. It is the callers responsibility to call
|
|
|
|
// rc.Lock and rc.Unlock before using this method, otherwise data races may
|
|
|
|
// occur.
|
|
|
|
func (rc *rejectionCache) Store(dir string, rejected bool) {
|
|
|
|
if rc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if rc.m == nil {
|
|
|
|
rc.m = make(map[string]bool)
|
|
|
|
}
|
|
|
|
rc.m[dir] = rejected
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:25:25 +00:00
|
|
|
// RejectByNameFunc is a function that takes a filename of a
|
|
|
|
// file that would be included in the backup. The function returns true if it
|
|
|
|
// should be excluded (rejected) from the backup.
|
|
|
|
type RejectByNameFunc func(path string) bool
|
|
|
|
|
2017-09-10 12:23:29 +00:00
|
|
|
// RejectFunc is a function that takes a filename and os.FileInfo of a
|
|
|
|
// file that would be included in the backup. The function returns true if it
|
|
|
|
// should be excluded (rejected) from the backup.
|
2017-09-10 18:28:21 +00:00
|
|
|
type RejectFunc func(path string, fi os.FileInfo) bool
|
2017-09-10 12:23:29 +00:00
|
|
|
|
2018-07-31 15:25:25 +00:00
|
|
|
// rejectByPattern returns a RejectByNameFunc which rejects files that match
|
2017-09-10 12:34:28 +00:00
|
|
|
// one of the patterns.
|
2018-07-31 15:25:25 +00:00
|
|
|
func rejectByPattern(patterns []string) RejectByNameFunc {
|
2020-10-07 12:39:51 +00:00
|
|
|
parsedPatterns := filter.ParsePatterns(patterns)
|
2018-07-31 15:25:25 +00:00
|
|
|
return func(item string) bool {
|
2020-10-07 17:46:41 +00:00
|
|
|
matched, err := filter.List(parsedPatterns, item)
|
2017-09-10 12:34:28 +00:00
|
|
|
if err != nil {
|
|
|
|
Warnf("error for exclude pattern: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched {
|
2017-09-10 13:16:05 +00:00
|
|
|
debug.Log("path %q excluded by an exclude pattern", item)
|
2017-09-10 12:34:28 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 20:39:14 +00:00
|
|
|
// Same as `rejectByPattern` but case insensitive.
|
|
|
|
func rejectByInsensitivePattern(patterns []string) RejectByNameFunc {
|
|
|
|
for index, path := range patterns {
|
|
|
|
patterns[index] = strings.ToLower(path)
|
|
|
|
}
|
2019-01-19 11:08:13 +00:00
|
|
|
|
|
|
|
rejFunc := rejectByPattern(patterns)
|
2018-10-16 20:39:14 +00:00
|
|
|
return func(item string) bool {
|
2019-01-19 11:08:13 +00:00
|
|
|
return rejFunc(strings.ToLower(item))
|
2018-10-16 20:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:25:25 +00:00
|
|
|
// rejectIfPresent returns a RejectByNameFunc which itself returns whether a path
|
|
|
|
// should be excluded. The RejectByNameFunc considers a file to be excluded when
|
2017-09-10 12:23:29 +00:00
|
|
|
// it resides in a directory with an exclusion file, that is specified by
|
|
|
|
// excludeFileSpec in the form "filename[:content]". The returned error is
|
2017-10-04 18:30:43 +00:00
|
|
|
// non-nil if the filename component of excludeFileSpec is empty. If rc is
|
2018-07-31 15:25:25 +00:00
|
|
|
// non-nil, it is going to be used in the RejectByNameFunc to expedite the evaluation
|
2017-10-04 18:30:43 +00:00
|
|
|
// of a directory based on previous visits.
|
2018-07-31 15:25:25 +00:00
|
|
|
func rejectIfPresent(excludeFileSpec string) (RejectByNameFunc, error) {
|
2017-09-10 12:23:29 +00:00
|
|
|
if excludeFileSpec == "" {
|
2017-09-10 12:34:28 +00:00
|
|
|
return nil, errors.New("name for exclusion tagfile is empty")
|
2017-09-10 12:23:29 +00:00
|
|
|
}
|
|
|
|
colon := strings.Index(excludeFileSpec, ":")
|
|
|
|
if colon == 0 {
|
|
|
|
return nil, fmt.Errorf("no name for exclusion tagfile provided")
|
|
|
|
}
|
|
|
|
tf, tc := "", ""
|
|
|
|
if colon > 0 {
|
|
|
|
tf = excludeFileSpec[:colon]
|
|
|
|
tc = excludeFileSpec[colon+1:]
|
|
|
|
} else {
|
|
|
|
tf = excludeFileSpec
|
|
|
|
}
|
|
|
|
debug.Log("using %q as exclusion tagfile", tf)
|
2017-11-27 16:30:53 +00:00
|
|
|
rc := &rejectionCache{}
|
2018-07-31 15:25:25 +00:00
|
|
|
fn := func(filename string) bool {
|
2017-10-04 18:30:43 +00:00
|
|
|
return isExcludedByFile(filename, tf, tc, rc)
|
2017-09-10 12:23:29 +00:00
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isExcludedByFile interprets filename as a path and returns true if that file
|
2020-05-16 06:05:26 +00:00
|
|
|
// is in an excluded directory. A directory is identified as excluded if it contains a
|
2017-10-04 18:30:43 +00:00
|
|
|
// tagfile which bears the name specified in tagFilename and starts with
|
|
|
|
// header. If rc is non-nil, it is used to expedite the evaluation of a
|
|
|
|
// directory based on previous visits.
|
|
|
|
func isExcludedByFile(filename, tagFilename, header string, rc *rejectionCache) bool {
|
2017-09-10 12:23:29 +00:00
|
|
|
if tagFilename == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
dir, base := filepath.Split(filename)
|
|
|
|
if base == tagFilename {
|
|
|
|
return false // do not exclude the tagfile itself
|
|
|
|
}
|
2017-10-04 18:30:43 +00:00
|
|
|
rc.Lock()
|
|
|
|
defer rc.Unlock()
|
|
|
|
|
|
|
|
rejected, visited := rc.Get(dir)
|
|
|
|
if visited {
|
|
|
|
return rejected
|
|
|
|
}
|
|
|
|
rejected = isDirExcludedByFile(dir, tagFilename, header)
|
|
|
|
rc.Store(dir, rejected)
|
|
|
|
return rejected
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDirExcludedByFile(dir, tagFilename, header string) bool {
|
2017-09-10 12:23:29 +00:00
|
|
|
tf := filepath.Join(dir, tagFilename)
|
|
|
|
_, err := fs.Lstat(tf)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
Warnf("could not access exclusion tagfile: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// when no signature is given, the mere presence of tf is enough reason
|
|
|
|
// to exclude filename
|
|
|
|
if len(header) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// From this stage, errors mean tagFilename exists but it is malformed.
|
|
|
|
// Warnings will be generated so that the user is informed that the
|
|
|
|
// indented ignore-action is not performed.
|
|
|
|
f, err := os.Open(tf)
|
|
|
|
if err != nil {
|
|
|
|
Warnf("could not open exclusion tagfile: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
buf := make([]byte, len(header))
|
|
|
|
_, err = io.ReadFull(f, buf)
|
|
|
|
// EOF is handled with a dedicated message, otherwise the warning were too cryptic
|
|
|
|
if err == io.EOF {
|
|
|
|
Warnf("invalid (too short) signature in exclusion tagfile %q\n", tf)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
Warnf("could not read signature from exclusion tagfile %q: %v\n", tf, err)
|
|
|
|
return false
|
|
|
|
}
|
2020-03-06 22:33:26 +00:00
|
|
|
if !bytes.Equal(buf, []byte(header)) {
|
2017-09-10 12:23:29 +00:00
|
|
|
Warnf("invalid signature in exclusion tagfile %q\n", tf)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2017-09-10 13:13:40 +00:00
|
|
|
|
|
|
|
// gatherDevices returns the set of unique device ids of the files and/or
|
|
|
|
// directory paths listed in "items".
|
|
|
|
func gatherDevices(items []string) (deviceMap map[string]uint64, err error) {
|
|
|
|
deviceMap = make(map[string]uint64)
|
|
|
|
for _, item := range items {
|
2018-05-11 19:31:13 +00:00
|
|
|
item, err = filepath.Abs(filepath.Clean(item))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-10 13:13:40 +00:00
|
|
|
fi, err := fs.Lstat(item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id, err := fs.DeviceID(fi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
deviceMap[item] = id
|
|
|
|
}
|
|
|
|
if len(deviceMap) == 0 {
|
|
|
|
return nil, errors.New("zero allowed devices")
|
|
|
|
}
|
|
|
|
return deviceMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// rejectByDevice returns a RejectFunc that rejects files which are on a
|
|
|
|
// different file systems than the files/dirs in samples.
|
|
|
|
func rejectByDevice(samples []string) (RejectFunc, error) {
|
|
|
|
allowed, err := gatherDevices(samples)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
debug.Log("allowed devices: %v\n", allowed)
|
|
|
|
|
|
|
|
return func(item string, fi os.FileInfo) bool {
|
2018-05-11 19:26:18 +00:00
|
|
|
item = filepath.Clean(item)
|
|
|
|
|
2017-09-10 13:13:40 +00:00
|
|
|
id, err := fs.DeviceID(fi)
|
|
|
|
if err != nil {
|
|
|
|
// This should never happen because gatherDevices() would have
|
|
|
|
// errored out earlier. If it still does that's a reason to panic.
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-05-16 11:44:14 +00:00
|
|
|
for dir := item; ; dir = filepath.Dir(dir) {
|
2017-09-10 13:13:40 +00:00
|
|
|
debug.Log("item %v, test dir %v", item, dir)
|
|
|
|
|
|
|
|
allowedID, ok := allowed[dir]
|
|
|
|
if !ok {
|
2018-05-16 11:44:14 +00:00
|
|
|
if dir == filepath.Dir(dir) {
|
|
|
|
break
|
|
|
|
}
|
2017-09-10 13:13:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if allowedID != id {
|
|
|
|
debug.Log("path %q on disallowed device %d", item, id)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Sprintf("item %v, device id %v not found, allowedDevs: %v", item, id, allowed))
|
|
|
|
}, nil
|
|
|
|
}
|
2017-09-11 19:37:10 +00:00
|
|
|
|
2018-07-31 15:25:25 +00:00
|
|
|
// rejectResticCache returns a RejectByNameFunc that rejects the restic cache
|
2017-09-11 19:37:10 +00:00
|
|
|
// directory (if set).
|
2018-07-31 15:25:25 +00:00
|
|
|
func rejectResticCache(repo *repository.Repository) (RejectByNameFunc, error) {
|
2017-09-11 19:37:10 +00:00
|
|
|
if repo.Cache == nil {
|
2018-07-31 15:25:25 +00:00
|
|
|
return func(string) bool {
|
2017-09-11 19:37:10 +00:00
|
|
|
return false
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
cacheBase := repo.Cache.BaseDir()
|
|
|
|
|
|
|
|
if cacheBase == "" {
|
|
|
|
return nil, errors.New("cacheBase is empty string")
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:25:25 +00:00
|
|
|
return func(item string) bool {
|
2017-09-11 19:37:10 +00:00
|
|
|
if fs.HasPathPrefix(cacheBase, item) {
|
|
|
|
debug.Log("rejecting restic cache directory %v", item)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}, nil
|
|
|
|
}
|
2020-09-03 01:18:58 +00:00
|
|
|
|
|
|
|
func rejectBySize(maxSizeStr string) (RejectFunc, error) {
|
|
|
|
maxSize, err := parseSizeStr(maxSizeStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(item string, fi os.FileInfo) bool {
|
|
|
|
// directory will be ignored
|
|
|
|
if fi.IsDir() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
filesize := fi.Size()
|
|
|
|
if filesize > maxSize {
|
|
|
|
debug.Log("file %s is oversize: %d", item, filesize)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSizeStr(sizeStr string) (int64, error) {
|
|
|
|
numStr := sizeStr[:len(sizeStr)-1]
|
|
|
|
var unit int64 = 1
|
|
|
|
|
|
|
|
switch sizeStr[len(sizeStr)-1] {
|
|
|
|
case 'b', 'B':
|
|
|
|
// use initialized values, do nothing here
|
|
|
|
case 'k', 'K':
|
|
|
|
unit = 1024
|
|
|
|
case 'm', 'M':
|
|
|
|
unit = 1024 * 1024
|
|
|
|
case 'g', 'G':
|
|
|
|
unit = 1024 * 1024 * 1024
|
|
|
|
case 't', 'T':
|
|
|
|
unit = 1024 * 1024 * 1024 * 1024
|
|
|
|
default:
|
|
|
|
numStr = sizeStr
|
|
|
|
}
|
|
|
|
value, err := strconv.ParseInt(numStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return value * unit, nil
|
|
|
|
}
|