1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
14
// FileItem is unit of work for archive.
23
// FileType is a file type.
28
RegularFileType FileType = iota
33
type walkerOptions struct {
36
fnmatchPatterns []string
37
types map[FileType]struct{}
40
// WalkerOption configures Walker.
41
type WalkerOption func(*walkerOptions)
43
// WithSkipRoot skips root path if it's a directory.
44
func WithSkipRoot() WalkerOption {
45
return func(o *walkerOptions) {
50
// WithMaxRecurseDepth controls maximum recursion depth while walking file tree.
52
// Value of -1 disables depth control.
53
func WithMaxRecurseDepth(maxDepth int) WalkerOption {
54
return func(o *walkerOptions) {
55
o.maxRecurseDepth = maxDepth
59
// WithFnmatchPatterns filters results to match the patterns.
61
// Default is not to do any filtering.
62
func WithFnmatchPatterns(patterns ...string) WalkerOption {
63
return func(o *walkerOptions) {
64
o.fnmatchPatterns = append(o.fnmatchPatterns, patterns...)
68
// WithFileTypes filters results by file types.
70
// Default is not to do any filtering.
71
func WithFileTypes(fileType ...FileType) WalkerOption {
72
return func(o *walkerOptions) {
73
o.types = make(map[FileType]struct{}, len(fileType))
74
for _, t := range fileType {
75
o.types[t] = struct{}{}
80
// Walker provides a channel of file info/paths for archival.
82
//nolint:gocyclo,cyclop
83
func Walker(ctx context.Context, rootPath string, options ...WalkerOption) (<-chan FileItem, error) {
84
var opts walkerOptions
85
opts.maxRecurseDepth = -1
87
for _, o := range options {
91
info, err := os.Lstat(rootPath)
96
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
97
rootPath, err = filepath.EvalSymlinks(rootPath)
103
ch := make(chan FileItem)
108
err := filepath.Walk(rootPath, func(path string, fileInfo os.FileInfo, walkErr error) error {
115
if path == rootPath && !fileInfo.IsDir() {
117
item.RelPath = filepath.Base(path)
118
} else if item.Error == nil {
119
item.RelPath, item.Error = filepath.Rel(rootPath, path)
122
// TODO: refactor all those `if item.Error == nil &&` conditions
124
if item.Error == nil && len(opts.types) > 0 {
127
for t := range opts.types {
129
case RegularFileType:
130
matches = fileInfo.Mode()&os.ModeType == 0
131
case DirectoryFileType:
132
matches = fileInfo.Mode()&os.ModeDir != 0
133
case SymlinkFileType:
134
matches = fileInfo.Mode()&os.ModeSymlink != 0
147
if item.Error == nil && path == rootPath && opts.skipRoot && fileInfo.IsDir() {
148
// skip containing directory
152
if item.Error == nil && fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
153
item.Link, item.Error = os.Readlink(path)
156
if item.Error == nil && len(opts.fnmatchPatterns) > 0 {
159
for _, pattern := range opts.fnmatchPatterns {
160
if matches, _ = filepath.Match(pattern, item.RelPath); matches { //nolint:errcheck
176
if item.Error == nil && fileInfo.IsDir() && atMaxDepth(opts.maxRecurseDepth, rootPath, path) {
177
return filepath.SkipDir
185
case ch <- FileItem{Error: err}:
193
// OSPathSeparator is the string version of the os.PathSeparator.
194
const OSPathSeparator = string(os.PathSeparator)
196
func atMaxDepth(max int, root, cur string) bool {
202
// always recurse the root directory
206
return (strings.Count(cur, OSPathSeparator) - strings.Count(root, OSPathSeparator)) >= max