2014-09-30 02:21:41 -04:00
|
|
|
package fileutils
|
|
|
|
|
|
|
|
import (
|
2015-04-09 15:07:06 -04:00
|
|
|
"errors"
|
2015-03-29 17:17:23 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2014-09-30 02:21:41 -04:00
|
|
|
"path/filepath"
|
2015-10-14 17:42:21 -04:00
|
|
|
"regexp"
|
2015-04-09 15:07:06 -04:00
|
|
|
"strings"
|
2015-10-14 17:42:21 -04:00
|
|
|
"text/scanner"
|
2015-03-29 15:48:52 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2014-09-30 02:21:41 -04:00
|
|
|
)
|
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
// PatternMatcher allows checking paths agaist a list of patterns
|
|
|
|
type PatternMatcher struct {
|
|
|
|
patterns []*Pattern
|
|
|
|
exclusions bool
|
2015-04-09 15:07:06 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
// NewPatternMatcher creates a new matcher object for specific patterns that can
|
|
|
|
// be used later to match against patterns against paths
|
|
|
|
func NewPatternMatcher(patterns []string) (*PatternMatcher, error) {
|
|
|
|
pm := &PatternMatcher{
|
|
|
|
patterns: make([]*Pattern, 0, len(patterns)),
|
|
|
|
}
|
|
|
|
for _, p := range patterns {
|
2015-04-09 15:07:06 -04:00
|
|
|
// Eliminate leading and trailing whitespace.
|
2017-03-08 17:23:25 -05:00
|
|
|
p = strings.TrimSpace(p)
|
|
|
|
if p == "" {
|
2015-04-09 15:07:06 -04:00
|
|
|
continue
|
|
|
|
}
|
2017-03-08 17:23:25 -05:00
|
|
|
p = filepath.Clean(p)
|
|
|
|
newp := &Pattern{}
|
|
|
|
if p[0] == '!' {
|
|
|
|
if len(p) == 1 {
|
|
|
|
return nil, errors.New("illegal exclusion pattern: \"!\"")
|
2015-04-09 15:07:06 -04:00
|
|
|
}
|
2017-03-08 17:23:25 -05:00
|
|
|
newp.exclusion = true
|
|
|
|
p = p[1:]
|
|
|
|
pm.exclusions = true
|
2015-04-09 15:07:06 -04:00
|
|
|
}
|
2017-03-08 17:23:25 -05:00
|
|
|
// Do some syntax checking on the pattern.
|
|
|
|
// filepath's Match() has some really weird rules that are inconsistent
|
|
|
|
// so instead of trying to dup their logic, just call Match() for its
|
|
|
|
// error state and if there is an error in the pattern return it.
|
|
|
|
// If this becomes an issue we can remove this since its really only
|
|
|
|
// needed in the error (syntax) case - which isn't really critical.
|
|
|
|
if _, err := filepath.Match(p, "."); err != nil {
|
|
|
|
return nil, err
|
2015-04-09 15:07:06 -04:00
|
|
|
}
|
2017-03-08 17:23:25 -05:00
|
|
|
newp.cleanedPattern = p
|
|
|
|
newp.dirs = strings.Split(p, string(os.PathSeparator))
|
|
|
|
pm.patterns = append(pm.patterns, newp)
|
2015-04-09 15:07:06 -04:00
|
|
|
}
|
2017-03-08 17:23:25 -05:00
|
|
|
return pm, nil
|
2015-04-09 15:07:06 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
// Matches matches path against all the patterns. Matches is not safe to be
|
|
|
|
// called concurrently
|
|
|
|
func (pm *PatternMatcher) Matches(file string) (bool, error) {
|
2015-04-09 15:07:06 -04:00
|
|
|
matched := false
|
2016-02-11 22:12:01 -05:00
|
|
|
file = filepath.FromSlash(file)
|
2015-04-09 15:07:06 -04:00
|
|
|
parentPath := filepath.Dir(file)
|
2016-02-11 22:12:01 -05:00
|
|
|
parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
|
2015-04-09 15:07:06 -04:00
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
for _, pattern := range pm.patterns {
|
2015-04-09 15:07:06 -04:00
|
|
|
negative := false
|
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
if pattern.exclusion {
|
2015-04-09 15:07:06 -04:00
|
|
|
negative = true
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
match, err := pattern.match(file)
|
2014-09-30 02:21:41 -04:00
|
|
|
if err != nil {
|
2017-03-08 17:23:25 -05:00
|
|
|
return false, err
|
2014-09-30 02:21:41 -04:00
|
|
|
}
|
2015-04-09 15:07:06 -04:00
|
|
|
|
|
|
|
if !match && parentPath != "." {
|
|
|
|
// Check to see if the pattern matches one of our parent dirs.
|
2017-03-08 17:23:25 -05:00
|
|
|
if len(pattern.dirs) <= len(parentPathDirs) {
|
|
|
|
match, _ = pattern.match(strings.Join(parentPathDirs[:len(pattern.dirs)], string(os.PathSeparator)))
|
2014-09-30 02:21:41 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-09 15:07:06 -04:00
|
|
|
|
|
|
|
if match {
|
|
|
|
matched = !negative
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched {
|
|
|
|
logrus.Debugf("Skipping excluded path: %s", file)
|
2014-09-30 02:21:41 -04:00
|
|
|
}
|
2015-05-29 22:38:56 -04:00
|
|
|
|
2015-04-09 15:07:06 -04:00
|
|
|
return matched, nil
|
2014-09-30 02:21:41 -04:00
|
|
|
}
|
2015-03-29 17:17:23 -04:00
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
// Exclusions returns true if any of the patterns define exclusions
|
|
|
|
func (pm *PatternMatcher) Exclusions() bool {
|
|
|
|
return pm.exclusions
|
|
|
|
}
|
2015-10-14 17:42:21 -04:00
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
// Patterns returns array of active patterns
|
|
|
|
func (pm *PatternMatcher) Patterns() []*Pattern {
|
|
|
|
return pm.patterns
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pattern defines a single regexp used used to filter file paths.
|
|
|
|
type Pattern struct {
|
|
|
|
cleanedPattern string
|
|
|
|
dirs []string
|
|
|
|
regexp *regexp.Regexp
|
|
|
|
exclusion bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pattern) String() string {
|
|
|
|
return p.cleanedPattern
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exclusion returns true if this pattern defines exclusion
|
|
|
|
func (p *Pattern) Exclusion() bool {
|
|
|
|
return p.exclusion
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pattern) match(path string) (bool, error) {
|
|
|
|
|
|
|
|
if p.regexp == nil {
|
|
|
|
if err := p.compile(); err != nil {
|
|
|
|
return false, filepath.ErrBadPattern
|
|
|
|
}
|
2015-10-14 17:42:21 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
b := p.regexp.MatchString(path)
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pattern) compile() error {
|
|
|
|
regStr := "^"
|
|
|
|
pattern := p.cleanedPattern
|
2015-10-14 17:42:21 -04:00
|
|
|
// Go through the pattern and convert it to a regexp.
|
|
|
|
// We use a scanner so we can support utf-8 chars.
|
|
|
|
var scan scanner.Scanner
|
|
|
|
scan.Init(strings.NewReader(pattern))
|
|
|
|
|
|
|
|
sl := string(os.PathSeparator)
|
|
|
|
escSL := sl
|
|
|
|
if sl == `\` {
|
|
|
|
escSL += `\`
|
|
|
|
}
|
|
|
|
|
|
|
|
for scan.Peek() != scanner.EOF {
|
|
|
|
ch := scan.Next()
|
|
|
|
|
|
|
|
if ch == '*' {
|
|
|
|
if scan.Peek() == '*' {
|
|
|
|
// is some flavor of "**"
|
|
|
|
scan.Next()
|
|
|
|
|
2016-12-01 18:03:57 -05:00
|
|
|
// Treat **/ as ** so eat the "/"
|
|
|
|
if string(scan.Peek()) == sl {
|
|
|
|
scan.Next()
|
|
|
|
}
|
|
|
|
|
2015-10-14 17:42:21 -04:00
|
|
|
if scan.Peek() == scanner.EOF {
|
|
|
|
// is "**EOF" - to align with .gitignore just accept all
|
|
|
|
regStr += ".*"
|
|
|
|
} else {
|
|
|
|
// is "**"
|
2016-12-01 18:03:57 -05:00
|
|
|
// Note that this allows for any # of /'s (even 0) because
|
|
|
|
// the .* will eat everything, even /'s
|
|
|
|
regStr += "(.*" + escSL + ")?"
|
2015-10-14 17:42:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// is "*" so map it to anything but "/"
|
|
|
|
regStr += "[^" + escSL + "]*"
|
|
|
|
}
|
|
|
|
} else if ch == '?' {
|
|
|
|
// "?" is any char except "/"
|
|
|
|
regStr += "[^" + escSL + "]"
|
2016-10-13 17:18:43 -04:00
|
|
|
} else if ch == '.' || ch == '$' {
|
2015-10-14 17:42:21 -04:00
|
|
|
// Escape some regexp special chars that have no meaning
|
|
|
|
// in golang's filepath.Match
|
|
|
|
regStr += `\` + string(ch)
|
|
|
|
} else if ch == '\\' {
|
|
|
|
// escape next char. Note that a trailing \ in the pattern
|
|
|
|
// will be left alone (but need to escape it)
|
|
|
|
if sl == `\` {
|
|
|
|
// On windows map "\" to "\\", meaning an escaped backslash,
|
|
|
|
// and then just continue because filepath.Match on
|
|
|
|
// Windows doesn't allow escaping at all
|
|
|
|
regStr += escSL
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if scan.Peek() != scanner.EOF {
|
|
|
|
regStr += `\` + string(scan.Next())
|
|
|
|
} else {
|
|
|
|
regStr += `\`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
regStr += string(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
regStr += "$"
|
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
re, err := regexp.Compile(regStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-14 17:42:21 -04:00
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
p.regexp = re
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches returns true if file matches any of the patterns
|
|
|
|
// and isn't excluded by any of the subsequent patterns.
|
|
|
|
func Matches(file string, patterns []string) (bool, error) {
|
|
|
|
pm, err := NewPatternMatcher(patterns)
|
2015-10-14 17:42:21 -04:00
|
|
|
if err != nil {
|
2017-03-08 17:23:25 -05:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
file = filepath.Clean(file)
|
|
|
|
|
|
|
|
if file == "." {
|
|
|
|
// Don't let them exclude everything, kind of silly.
|
|
|
|
return false, nil
|
2015-10-14 17:42:21 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:23:25 -05:00
|
|
|
return pm.Matches(file)
|
2015-10-14 17:42:21 -04:00
|
|
|
}
|
|
|
|
|
2015-07-12 16:43:42 -04:00
|
|
|
// CopyFile copies from src to dst until either EOF is reached
|
2016-05-03 11:12:38 -04:00
|
|
|
// on src or an error occurs. It verifies src exists and removes
|
2015-07-12 16:43:42 -04:00
|
|
|
// the dst if it exists.
|
2015-03-29 17:17:23 -04:00
|
|
|
func CopyFile(src, dst string) (int64, error) {
|
2015-04-29 10:27:12 -04:00
|
|
|
cleanSrc := filepath.Clean(src)
|
|
|
|
cleanDst := filepath.Clean(dst)
|
|
|
|
if cleanSrc == cleanDst {
|
2015-03-29 17:17:23 -04:00
|
|
|
return 0, nil
|
|
|
|
}
|
2015-04-29 10:27:12 -04:00
|
|
|
sf, err := os.Open(cleanSrc)
|
2015-03-29 17:17:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer sf.Close()
|
2015-04-29 10:27:12 -04:00
|
|
|
if err := os.Remove(cleanDst); err != nil && !os.IsNotExist(err) {
|
2015-03-29 17:17:23 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2015-04-29 10:27:12 -04:00
|
|
|
df, err := os.Create(cleanDst)
|
2015-03-29 17:17:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer df.Close()
|
|
|
|
return io.Copy(df, sf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadSymlinkedDirectory returns the target directory of a symlink.
|
|
|
|
// The target of the symbolic link may not be a file.
|
|
|
|
func ReadSymlinkedDirectory(path string) (string, error) {
|
|
|
|
var realPath string
|
|
|
|
var err error
|
|
|
|
if realPath, err = filepath.Abs(path); err != nil {
|
|
|
|
return "", fmt.Errorf("unable to get absolute path for %s: %s", path, err)
|
|
|
|
}
|
|
|
|
if realPath, err = filepath.EvalSymlinks(realPath); err != nil {
|
|
|
|
return "", fmt.Errorf("failed to canonicalise path for %s: %s", path, err)
|
|
|
|
}
|
|
|
|
realPathInfo, err := os.Stat(realPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to stat target '%s' of '%s': %s", realPath, path, err)
|
|
|
|
}
|
|
|
|
if !realPathInfo.Mode().IsDir() {
|
|
|
|
return "", fmt.Errorf("canonical path points to a file '%s'", realPath)
|
|
|
|
}
|
|
|
|
return realPath, nil
|
|
|
|
}
|
2015-07-07 23:15:09 -04:00
|
|
|
|
|
|
|
// CreateIfNotExists creates a file or a directory only if it does not already exist.
|
|
|
|
func CreateIfNotExists(path string, isDir bool) error {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if isDir {
|
|
|
|
return os.MkdirAll(path, 0755)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|