fileutils.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package fileutils
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. "text/scanner"
  11. "github.com/Sirupsen/logrus"
  12. )
  13. // exclusion return true if the specified pattern is an exclusion
  14. func exclusion(pattern string) bool {
  15. return pattern[0] == '!'
  16. }
  17. // empty return true if the specified pattern is empty
  18. func empty(pattern string) bool {
  19. return pattern == ""
  20. }
  21. // CleanPatterns takes a slice of patterns returns a new
  22. // slice of patterns cleaned with filepath.Clean, stripped
  23. // of any empty patterns and lets the caller know whether the
  24. // slice contains any exception patterns (prefixed with !).
  25. func CleanPatterns(patterns []string) ([]string, [][]string, bool, error) {
  26. // Loop over exclusion patterns and:
  27. // 1. Clean them up.
  28. // 2. Indicate whether we are dealing with any exception rules.
  29. // 3. Error if we see a single exclusion marker on it's own (!).
  30. cleanedPatterns := []string{}
  31. patternDirs := [][]string{}
  32. exceptions := false
  33. for _, pattern := range patterns {
  34. // Eliminate leading and trailing whitespace.
  35. pattern = strings.TrimSpace(pattern)
  36. if empty(pattern) {
  37. continue
  38. }
  39. if exclusion(pattern) {
  40. if len(pattern) == 1 {
  41. return nil, nil, false, errors.New("Illegal exclusion pattern: !")
  42. }
  43. exceptions = true
  44. }
  45. pattern = filepath.Clean(pattern)
  46. cleanedPatterns = append(cleanedPatterns, pattern)
  47. if exclusion(pattern) {
  48. pattern = pattern[1:]
  49. }
  50. patternDirs = append(patternDirs, strings.Split(pattern, "/"))
  51. }
  52. return cleanedPatterns, patternDirs, exceptions, nil
  53. }
  54. // Matches returns true if file matches any of the patterns
  55. // and isn't excluded by any of the subsequent patterns.
  56. func Matches(file string, patterns []string) (bool, error) {
  57. file = filepath.Clean(file)
  58. if file == "." {
  59. // Don't let them exclude everything, kind of silly.
  60. return false, nil
  61. }
  62. patterns, patDirs, _, err := CleanPatterns(patterns)
  63. if err != nil {
  64. return false, err
  65. }
  66. return OptimizedMatches(file, patterns, patDirs)
  67. }
  68. // OptimizedMatches is basically the same as fileutils.Matches() but optimized for archive.go.
  69. // It will assume that the inputs have been preprocessed and therefore the function
  70. // doen't need to do as much error checking and clean-up. This was done to avoid
  71. // repeating these steps on each file being checked during the archive process.
  72. // The more generic fileutils.Matches() can't make these assumptions.
  73. func OptimizedMatches(file string, patterns []string, patDirs [][]string) (bool, error) {
  74. matched := false
  75. parentPath := filepath.Dir(file)
  76. parentPathDirs := strings.Split(parentPath, "/")
  77. for i, pattern := range patterns {
  78. negative := false
  79. if exclusion(pattern) {
  80. negative = true
  81. pattern = pattern[1:]
  82. }
  83. match, err := regexpMatch(pattern, file)
  84. if err != nil {
  85. return false, fmt.Errorf("Error in pattern (%s): %s", pattern, err)
  86. }
  87. if !match && parentPath != "." {
  88. // Check to see if the pattern matches one of our parent dirs.
  89. if len(patDirs[i]) <= len(parentPathDirs) {
  90. match, _ = regexpMatch(strings.Join(patDirs[i], "/"),
  91. strings.Join(parentPathDirs[:len(patDirs[i])], "/"))
  92. }
  93. }
  94. if match {
  95. matched = !negative
  96. }
  97. }
  98. if matched {
  99. logrus.Debugf("Skipping excluded path: %s", file)
  100. }
  101. return matched, nil
  102. }
  103. // regexpMatch tries to match the logic of filepath.Match but
  104. // does so using regexp logic. We do this so that we can expand the
  105. // wildcard set to include other things, like "**" to mean any number
  106. // of directories. This means that we should be backwards compatible
  107. // with filepath.Match(). We'll end up supporting more stuff, due to
  108. // the fact that we're using regexp, but that's ok - it does no harm.
  109. func regexpMatch(pattern, path string) (bool, error) {
  110. regStr := "^"
  111. // Do some syntax checking on the pattern.
  112. // filepath's Match() has some really weird rules that are inconsistent
  113. // so instead of trying to dup their logic, just call Match() for its
  114. // error state and if there is an error in the pattern return it.
  115. // If this becomes an issue we can remove this since its really only
  116. // needed in the error (syntax) case - which isn't really critical.
  117. if _, err := filepath.Match(pattern, path); err != nil {
  118. return false, err
  119. }
  120. // Go through the pattern and convert it to a regexp.
  121. // We use a scanner so we can support utf-8 chars.
  122. var scan scanner.Scanner
  123. scan.Init(strings.NewReader(pattern))
  124. sl := string(os.PathSeparator)
  125. escSL := sl
  126. if sl == `\` {
  127. escSL += `\`
  128. }
  129. for scan.Peek() != scanner.EOF {
  130. ch := scan.Next()
  131. if ch == '*' {
  132. if scan.Peek() == '*' {
  133. // is some flavor of "**"
  134. scan.Next()
  135. if scan.Peek() == scanner.EOF {
  136. // is "**EOF" - to align with .gitignore just accept all
  137. regStr += ".*"
  138. } else {
  139. // is "**"
  140. regStr += "((.*" + escSL + ")|([^" + escSL + "]*))"
  141. }
  142. // Treat **/ as ** so eat the "/"
  143. if string(scan.Peek()) == sl {
  144. scan.Next()
  145. }
  146. } else {
  147. // is "*" so map it to anything but "/"
  148. regStr += "[^" + escSL + "]*"
  149. }
  150. } else if ch == '?' {
  151. // "?" is any char except "/"
  152. regStr += "[^" + escSL + "]"
  153. } else if strings.Index(".$", string(ch)) != -1 {
  154. // Escape some regexp special chars that have no meaning
  155. // in golang's filepath.Match
  156. regStr += `\` + string(ch)
  157. } else if ch == '\\' {
  158. // escape next char. Note that a trailing \ in the pattern
  159. // will be left alone (but need to escape it)
  160. if sl == `\` {
  161. // On windows map "\" to "\\", meaning an escaped backslash,
  162. // and then just continue because filepath.Match on
  163. // Windows doesn't allow escaping at all
  164. regStr += escSL
  165. continue
  166. }
  167. if scan.Peek() != scanner.EOF {
  168. regStr += `\` + string(scan.Next())
  169. } else {
  170. regStr += `\`
  171. }
  172. } else {
  173. regStr += string(ch)
  174. }
  175. }
  176. regStr += "$"
  177. res, err := regexp.MatchString(regStr, path)
  178. // Map regexp's error to filepath's so no one knows we're not using filepath
  179. if err != nil {
  180. err = filepath.ErrBadPattern
  181. }
  182. return res, err
  183. }
  184. // CopyFile copies from src to dst until either EOF is reached
  185. // on src or an error occurs. It verifies src exists and remove
  186. // the dst if it exists.
  187. func CopyFile(src, dst string) (int64, error) {
  188. cleanSrc := filepath.Clean(src)
  189. cleanDst := filepath.Clean(dst)
  190. if cleanSrc == cleanDst {
  191. return 0, nil
  192. }
  193. sf, err := os.Open(cleanSrc)
  194. if err != nil {
  195. return 0, err
  196. }
  197. defer sf.Close()
  198. if err := os.Remove(cleanDst); err != nil && !os.IsNotExist(err) {
  199. return 0, err
  200. }
  201. df, err := os.Create(cleanDst)
  202. if err != nil {
  203. return 0, err
  204. }
  205. defer df.Close()
  206. return io.Copy(df, sf)
  207. }
  208. // ReadSymlinkedDirectory returns the target directory of a symlink.
  209. // The target of the symbolic link may not be a file.
  210. func ReadSymlinkedDirectory(path string) (string, error) {
  211. var realPath string
  212. var err error
  213. if realPath, err = filepath.Abs(path); err != nil {
  214. return "", fmt.Errorf("unable to get absolute path for %s: %s", path, err)
  215. }
  216. if realPath, err = filepath.EvalSymlinks(realPath); err != nil {
  217. return "", fmt.Errorf("failed to canonicalise path for %s: %s", path, err)
  218. }
  219. realPathInfo, err := os.Stat(realPath)
  220. if err != nil {
  221. return "", fmt.Errorf("failed to stat target '%s' of '%s': %s", realPath, path, err)
  222. }
  223. if !realPathInfo.Mode().IsDir() {
  224. return "", fmt.Errorf("canonical path points to a file '%s'", realPath)
  225. }
  226. return realPath, nil
  227. }
  228. // CreateIfNotExists creates a file or a directory only if it does not already exist.
  229. func CreateIfNotExists(path string, isDir bool) error {
  230. if _, err := os.Stat(path); err != nil {
  231. if os.IsNotExist(err) {
  232. if isDir {
  233. return os.MkdirAll(path, 0755)
  234. }
  235. if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
  236. return err
  237. }
  238. f, err := os.OpenFile(path, os.O_CREATE, 0755)
  239. if err != nil {
  240. return err
  241. }
  242. f.Close()
  243. }
  244. }
  245. return nil
  246. }