summaryrefslogtreecommitdiff
path: root/internal/fusefrontend_reverse_v1api/excluder.go
blob: c44816c5e0be7018bb534e243706b5e640d0d2c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package fusefrontend_reverse

import (
	"io/ioutil"
	"os"
	"strings"

	"github.com/rfjakob/gocryptfs/internal/exitcodes"
	"github.com/rfjakob/gocryptfs/internal/fusefrontend"
	"github.com/rfjakob/gocryptfs/internal/tlog"

	"github.com/sabhiram/go-gitignore"
)

// prepareExcluder creates an object to check if paths are excluded
// based on the patterns specified in the command line.
func (rfs *ReverseFS) prepareExcluder(args fusefrontend.Args) {
	if len(args.Exclude) > 0 || len(args.ExcludeWildcard) > 0 || len(args.ExcludeFrom) > 0 {
		excluder, err := ignore.CompileIgnoreLines(getExclusionPatterns(args)...)
		if err != nil {
			tlog.Fatal.Printf("Error compiling exclusion rules: %q", err)
			os.Exit(exitcodes.ExcludeError)
		}
		rfs.excluder = excluder
	}
}

// getExclusionPatters prepares a list of patterns to be excluded.
// Patterns passed in the -exclude command line option are prefixed
// with a leading '/' to preserve backwards compatibility (before
// wildcard matching was implemented, exclusions always were matched
// against the full path).
func getExclusionPatterns(args fusefrontend.Args) []string {
	patterns := make([]string, len(args.Exclude)+len(args.ExcludeWildcard))
	// add -exclude
	for i, p := range args.Exclude {
		patterns[i] = "/" + p
	}
	// add -exclude-wildcard
	copy(patterns[len(args.Exclude):], args.ExcludeWildcard)
	// add -exclude-from
	for _, file := range args.ExcludeFrom {
		lines, err := getLines(file)
		if err != nil {
			tlog.Fatal.Printf("Error reading exclusion patterns: %q", err)
			os.Exit(exitcodes.ExcludeError)
		}
		patterns = append(patterns, lines...)
	}
	return patterns
}

// getLines reads a file and splits it into lines
func getLines(file string) ([]string, error) {
	buffer, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, err
	}
	return strings.Split(string(buffer), "\n"), nil
}

// isExcludedPlain finds out if the plaintext path "pPath" is
// excluded (used when -exclude is passed by the user).
func (rfs *ReverseFS) isExcludedPlain(pPath string) bool {
	return rfs.excluder != nil && rfs.excluder.MatchesPath(pPath)
}