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
|
|
|
)
|
|
|
|
|
2016-05-03 11:12:38 -04:00
|
|
|
// exclusion returns true if the specified pattern is an exclusion
|
2015-07-12 16:43:42 -04:00
|
|
|
func exclusion(pattern string) bool {
|
2015-04-09 15:07:06 -04:00
|
|
|
return pattern[0] == '!'
|
|
|
|
}
|
|
|
|
|
2016-05-03 11:12:38 -04:00
|
|
|
// empty returns true if the specified pattern is empty
|
2015-07-12 16:43:42 -04:00
|
|
|
func empty(pattern string) bool {
|
2015-04-09 15:07:06 -04:00
|
|
|
return pattern == ""
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:51:27 -04:00
|
|
|
// CleanPatterns takes a slice of patterns returns a new
|
2015-04-09 15:07:06 -04:00
|
|
|
// slice of patterns cleaned with filepath.Clean, stripped
|
|
|
|
// of any empty patterns and lets the caller know whether the
|
|
|
|
// slice contains any exception patterns (prefixed with !).
|
|
|
|
func CleanPatterns(patterns []string) ([]string, [][]string, bool, error) {
|
|
|
|
// Loop over exclusion patterns and:
|
|
|
|
// 1. Clean them up.
|
|
|
|
// 2. Indicate whether we are dealing with any exception rules.
|
2016-07-21 06:03:37 -04:00
|
|
|
// 3. Error if we see a single exclusion marker on its own (!).
|
2015-04-09 15:07:06 -04:00
|
|
|
cleanedPatterns := []string{}
|
|
|
|
patternDirs := [][]string{}
|
|
|
|
exceptions := false
|
|
|
|
for _, pattern := range patterns {
|
|
|
|
// Eliminate leading and trailing whitespace.
|
|
|
|
pattern = strings.TrimSpace(pattern)
|
2015-07-12 16:43:42 -04:00
|
|
|
if empty(pattern) {
|
2015-04-09 15:07:06 -04:00
|
|
|
continue
|
|
|
|
}
|
2015-07-12 16:43:42 -04:00
|
|
|
if exclusion(pattern) {
|
2015-04-09 15:07:06 -04:00
|
|
|
if len(pattern) == 1 {
|
|
|
|
return nil, nil, false, errors.New("Illegal exclusion pattern: !")
|
|
|
|
}
|
|
|
|
exceptions = true
|
|
|
|
}
|
|
|
|
pattern = filepath.Clean(pattern)
|
|
|
|
cleanedPatterns = append(cleanedPatterns, pattern)
|
2015-07-12 16:43:42 -04:00
|
|
|
if exclusion(pattern) {
|
2015-04-09 15:07:06 -04:00
|
|
|
pattern = pattern[1:]
|
|
|
|
}
|
2016-02-11 22:12:01 -05:00
|
|
|
patternDirs = append(patternDirs, strings.Split(pattern, string(os.PathSeparator)))
|
2015-04-09 15:07:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return cleanedPatterns, patternDirs, exceptions, 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) {
|
|
|
|
file = filepath.Clean(file)
|
|
|
|
|
|
|
|
if file == "." {
|
|
|
|
// Don't let them exclude everything, kind of silly.
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
patterns, patDirs, _, err := CleanPatterns(patterns)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return OptimizedMatches(file, patterns, patDirs)
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:51:27 -04:00
|
|
|
// OptimizedMatches is basically the same as fileutils.Matches() but optimized for archive.go.
|
2015-04-09 15:07:06 -04:00
|
|
|
// It will assume that the inputs have been preprocessed and therefore the function
|
2015-12-13 11:00:39 -05:00
|
|
|
// doesn't need to do as much error checking and clean-up. This was done to avoid
|
2015-04-09 15:07:06 -04:00
|
|
|
// repeating these steps on each file being checked during the archive process.
|
|
|
|
// The more generic fileutils.Matches() can't make these assumptions.
|
|
|
|
func OptimizedMatches(file string, patterns []string, patDirs [][]string) (bool, error) {
|
|
|
|
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
|
|
|
|
|
|
|
for i, pattern := range patterns {
|
|
|
|
negative := false
|
|
|
|
|
2015-07-12 16:43:42 -04:00
|
|
|
if exclusion(pattern) {
|
2015-04-09 15:07:06 -04:00
|
|
|
negative = true
|
|
|
|
pattern = pattern[1:]
|
|
|
|
}
|
|
|
|
|
2015-10-14 17:42:21 -04:00
|
|
|
match, err := regexpMatch(pattern, file)
|
2014-09-30 02:21:41 -04:00
|
|
|
if err != nil {
|
2015-10-14 17:42:21 -04:00
|
|
|
return false, fmt.Errorf("Error in pattern (%s): %s", pattern, 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.
|
|
|
|
if len(patDirs[i]) <= len(parentPathDirs) {
|
2016-02-11 22:12:01 -05:00
|
|
|
match, _ = regexpMatch(strings.Join(patDirs[i], string(os.PathSeparator)),
|
|
|
|
strings.Join(parentPathDirs[:len(patDirs[i])], 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
|
|
|
|
2015-10-14 17:42:21 -04:00
|
|
|
// regexpMatch tries to match the logic of filepath.Match but
|
|
|
|
// does so using regexp logic. We do this so that we can expand the
|
|
|
|
// wildcard set to include other things, like "**" to mean any number
|
|
|
|
// of directories. This means that we should be backwards compatible
|
|
|
|
// with filepath.Match(). We'll end up supporting more stuff, due to
|
|
|
|
// the fact that we're using regexp, but that's ok - it does no harm.
|
2016-02-11 22:12:01 -05:00
|
|
|
//
|
|
|
|
// As per the comment in golangs filepath.Match, on Windows, escaping
|
|
|
|
// is disabled. Instead, '\\' is treated as path separator.
|
2015-10-14 17:42:21 -04:00
|
|
|
func regexpMatch(pattern, path string) (bool, error) {
|
|
|
|
regStr := "^"
|
|
|
|
|
|
|
|
// 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(pattern, path); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
if scan.Peek() == scanner.EOF {
|
|
|
|
// is "**EOF" - to align with .gitignore just accept all
|
|
|
|
regStr += ".*"
|
|
|
|
} else {
|
|
|
|
// is "**"
|
|
|
|
regStr += "((.*" + escSL + ")|([^" + escSL + "]*))"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Treat **/ as ** so eat the "/"
|
|
|
|
if string(scan.Peek()) == sl {
|
|
|
|
scan.Next()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// is "*" so map it to anything but "/"
|
|
|
|
regStr += "[^" + escSL + "]*"
|
|
|
|
}
|
|
|
|
} else if ch == '?' {
|
|
|
|
// "?" is any char except "/"
|
|
|
|
regStr += "[^" + escSL + "]"
|
|
|
|
} else if strings.Index(".$", string(ch)) != -1 {
|
|
|
|
// 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 += "$"
|
|
|
|
|
|
|
|
res, err := regexp.MatchString(regStr, path)
|
|
|
|
|
|
|
|
// Map regexp's error to filepath's so no one knows we're not using filepath
|
|
|
|
if err != nil {
|
|
|
|
err = filepath.ErrBadPattern
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|