From fdc594e811976d3f5eaf54b7d15ad61f32fb3b1d Mon Sep 17 00:00:00 2001 From: Rafe Colton Date: Mon, 29 Sep 2014 23:21:41 -0700 Subject: [PATCH] Move Matches() file path matching function into pkg/fileutils This is the second of two steps to break the archive package's dependence on utils so that archive may be moved into pkg. `Matches()` is also a good candidate pkg in that it is small, concise, and not specific to docker internals Signed-off-by: Rafe Colton --- fileutils/fileutils.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 fileutils/fileutils.go diff --git a/fileutils/fileutils.go b/fileutils/fileutils.go new file mode 100644 index 0000000..acc27f5 --- /dev/null +++ b/fileutils/fileutils.go @@ -0,0 +1,26 @@ +package fileutils + +import ( + "github.com/docker/docker/pkg/log" + "path/filepath" +) + +// Matches returns true if relFilePath matches any of the patterns +func Matches(relFilePath string, patterns []string) (bool, error) { + for _, exclude := range patterns { + matched, err := filepath.Match(exclude, relFilePath) + if err != nil { + log.Errorf("Error matching: %s (pattern: %s)", relFilePath, exclude) + return false, err + } + if matched { + if filepath.Clean(relFilePath) == "." { + log.Errorf("Can't exclude whole path, excluding pattern: %s", exclude) + continue + } + log.Debugf("Skipping excluded path: %s", relFilePath) + return true, nil + } + } + return false, nil +}