2015-01-23 23:15:10 +01:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-10-24 19:42:57 +02:00
|
|
|
package pathtools
|
|
|
|
|
|
|
|
import (
|
2015-04-24 00:45:25 +02:00
|
|
|
"errors"
|
2016-11-05 16:37:33 +01:00
|
|
|
"fmt"
|
2016-11-01 19:10:51 +01:00
|
|
|
"io/ioutil"
|
2015-04-23 22:29:28 +02:00
|
|
|
"os"
|
2014-10-24 19:42:57 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-11-01 19:10:51 +01:00
|
|
|
|
|
|
|
"github.com/google/blueprint/deptools"
|
2014-10-24 19:42:57 +02:00
|
|
|
)
|
|
|
|
|
2019-03-26 00:14:37 +01:00
|
|
|
var GlobMultipleRecursiveErr = errors.New("pattern contains multiple '**'")
|
|
|
|
var GlobLastRecursiveErr = errors.New("pattern has '**' as last path element")
|
|
|
|
var GlobInvalidRecursiveErr = errors.New("pattern contains other characters between '**' and path separator")
|
2015-04-24 00:45:25 +02:00
|
|
|
|
2018-02-23 23:49:45 +01:00
|
|
|
// Glob returns the list of files and directories that match the given pattern
|
|
|
|
// but do not match the given exclude patterns, along with the list of
|
|
|
|
// directories and other dependencies that were searched to construct the file
|
|
|
|
// list. The supported glob and exclude patterns are equivalent to
|
|
|
|
// filepath.Glob, with an extension that recursive glob (** matching zero or
|
|
|
|
// more complete path entries) is supported. Any directories in the matches
|
|
|
|
// list will have a '/' suffix.
|
2016-11-01 19:10:51 +01:00
|
|
|
//
|
|
|
|
// In general ModuleContext.GlobWithDeps or SingletonContext.GlobWithDeps
|
|
|
|
// should be used instead, as they will automatically set up dependencies
|
|
|
|
// to rerun the primary builder when the list of matching files changes.
|
2018-09-22 00:30:13 +02:00
|
|
|
func Glob(pattern string, excludes []string, follow ShouldFollowSymlinks) (matches, deps []string, err error) {
|
|
|
|
return startGlob(OsFs, pattern, excludes, follow)
|
2017-02-01 22:21:35 +01:00
|
|
|
}
|
|
|
|
|
2018-09-22 00:30:13 +02:00
|
|
|
func startGlob(fs FileSystem, pattern string, excludes []string,
|
|
|
|
follow ShouldFollowSymlinks) (matches, deps []string, err error) {
|
|
|
|
|
2015-06-18 19:48:15 +02:00
|
|
|
if filepath.Base(pattern) == "**" {
|
2015-04-24 00:45:25 +02:00
|
|
|
return nil, nil, GlobLastRecursiveErr
|
|
|
|
} else {
|
2018-09-22 00:30:13 +02:00
|
|
|
matches, deps, err = glob(fs, pattern, false, follow)
|
2015-04-24 00:45:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 02:56:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
matches, err = filterExcludes(matches, excludes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-23 02:01:24 +02:00
|
|
|
// If the pattern has wildcards, we added dependencies on the
|
|
|
|
// containing directories to know about changes.
|
|
|
|
//
|
|
|
|
// If the pattern didn't have wildcards, and didn't find matches, the
|
|
|
|
// most specific found directories were added.
|
|
|
|
//
|
|
|
|
// But if it didn't have wildcards, and did find a match, no
|
|
|
|
// dependencies were added, so add the match itself to detect when it
|
|
|
|
// is removed.
|
|
|
|
if !isWild(pattern) {
|
|
|
|
deps = append(deps, matches...)
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:49:45 +01:00
|
|
|
for i, match := range matches {
|
2018-09-22 00:30:13 +02:00
|
|
|
isSymlink, err := fs.IsSymlink(match)
|
2018-09-20 23:36:10 +02:00
|
|
|
if err != nil {
|
2018-09-22 00:30:13 +02:00
|
|
|
return nil, nil, err
|
2018-09-20 23:36:10 +02:00
|
|
|
}
|
2018-09-22 00:30:13 +02:00
|
|
|
if !(isSymlink && follow == DontFollowSymlinks) {
|
|
|
|
isDir, err := fs.IsDir(match)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if isSymlink {
|
|
|
|
return nil, nil, fmt.Errorf("%s: dangling symlink", match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("%s: %s", match, err.Error())
|
|
|
|
}
|
2018-09-20 23:36:10 +02:00
|
|
|
|
2018-09-22 00:30:13 +02:00
|
|
|
if isDir {
|
|
|
|
matches[i] = match + "/"
|
|
|
|
}
|
2018-02-23 23:49:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 02:01:24 +02:00
|
|
|
return matches, deps, nil
|
2015-04-24 00:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// glob is a recursive helper function to handle globbing each level of the pattern individually,
|
|
|
|
// allowing searched directories to be tracked. Also handles the recursive glob pattern, **.
|
2018-09-22 00:30:13 +02:00
|
|
|
func glob(fs FileSystem, pattern string, hasRecursive bool,
|
|
|
|
follow ShouldFollowSymlinks) (matches, dirs []string, err error) {
|
|
|
|
|
2015-04-23 22:29:28 +02:00
|
|
|
if !isWild(pattern) {
|
2015-06-18 19:48:15 +02:00
|
|
|
// If there are no wilds in the pattern, check whether the file exists or not.
|
|
|
|
// Uses filepath.Glob instead of manually statting to get consistent results.
|
|
|
|
pattern = filepath.Clean(pattern)
|
2017-02-01 22:21:35 +01:00
|
|
|
matches, err = fs.glob(pattern)
|
2015-06-18 19:48:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return matches, dirs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
// Some part of the non-wild pattern didn't exist. Add the last existing directory
|
|
|
|
// as a dependency.
|
|
|
|
var matchDirs []string
|
|
|
|
for len(matchDirs) == 0 {
|
2019-03-26 00:14:37 +01:00
|
|
|
pattern = filepath.Dir(pattern)
|
2017-02-01 22:21:35 +01:00
|
|
|
matchDirs, err = fs.glob(pattern)
|
2015-06-18 19:48:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return matches, dirs, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dirs = append(dirs, matchDirs...)
|
|
|
|
}
|
2015-04-23 22:29:28 +02:00
|
|
|
return matches, dirs, err
|
2014-10-24 19:42:57 +02:00
|
|
|
}
|
|
|
|
|
2015-04-23 22:29:28 +02:00
|
|
|
dir, file := saneSplit(pattern)
|
2015-04-24 00:45:25 +02:00
|
|
|
|
|
|
|
if file == "**" {
|
|
|
|
if hasRecursive {
|
|
|
|
return matches, dirs, GlobMultipleRecursiveErr
|
|
|
|
}
|
|
|
|
hasRecursive = true
|
2019-03-26 00:14:37 +01:00
|
|
|
} else if strings.Contains(file, "**") {
|
|
|
|
return matches, dirs, GlobInvalidRecursiveErr
|
2015-04-24 00:45:25 +02:00
|
|
|
}
|
|
|
|
|
2018-09-22 00:30:13 +02:00
|
|
|
dirMatches, dirs, err := glob(fs, dir, hasRecursive, follow)
|
2015-04-24 00:45:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-23 22:29:28 +02:00
|
|
|
for _, m := range dirMatches {
|
2018-09-20 23:36:10 +02:00
|
|
|
isDir, err := fs.IsDir(m)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if isSymlink, _ := fs.IsSymlink(m); isSymlink {
|
|
|
|
return nil, nil, fmt.Errorf("dangling symlink: %s", m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-11-05 16:37:33 +01:00
|
|
|
return nil, nil, fmt.Errorf("unexpected error after glob: %s", err)
|
2018-09-20 23:36:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if isDir {
|
2015-04-24 00:45:25 +02:00
|
|
|
if file == "**" {
|
2018-09-22 00:30:13 +02:00
|
|
|
recurseDirs, err := fs.ListDirsRecursive(m, follow)
|
2015-04-24 00:45:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
matches = append(matches, recurseDirs...)
|
|
|
|
} else {
|
|
|
|
dirs = append(dirs, m)
|
2018-09-19 22:36:42 +02:00
|
|
|
newMatches, err := fs.glob(filepath.Join(MatchEscape(m), file))
|
2015-04-24 00:45:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2016-07-26 03:40:02 +02:00
|
|
|
if file[0] != '.' {
|
|
|
|
newMatches = filterDotFiles(newMatches)
|
|
|
|
}
|
2015-04-24 00:45:25 +02:00
|
|
|
matches = append(matches, newMatches...)
|
2014-10-24 19:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 22:29:28 +02:00
|
|
|
return matches, dirs, nil
|
2014-10-24 19:42:57 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 00:45:25 +02:00
|
|
|
// Faster version of dir, file := filepath.Dir(path), filepath.File(path) with no allocations
|
2015-04-23 22:29:28 +02:00
|
|
|
// Similar to filepath.Split, but returns "." if dir is empty and trims trailing slash if dir is
|
2015-04-24 00:45:25 +02:00
|
|
|
// not "/". Returns ".", "" if path is "."
|
2015-04-23 22:29:28 +02:00
|
|
|
func saneSplit(path string) (dir, file string) {
|
2015-04-24 00:45:25 +02:00
|
|
|
if path == "." {
|
|
|
|
return ".", ""
|
|
|
|
}
|
2015-04-23 22:29:28 +02:00
|
|
|
dir, file = filepath.Split(path)
|
|
|
|
switch dir {
|
|
|
|
case "":
|
|
|
|
dir = "."
|
|
|
|
case "/":
|
|
|
|
// Nothing
|
|
|
|
default:
|
|
|
|
dir = dir[:len(dir)-1]
|
2014-10-24 19:42:57 +02:00
|
|
|
}
|
2015-04-23 22:29:28 +02:00
|
|
|
return dir, file
|
2014-10-24 19:42:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func isWild(pattern string) bool {
|
|
|
|
return strings.ContainsAny(pattern, "*?[")
|
|
|
|
}
|
2015-02-06 05:01:49 +01:00
|
|
|
|
2015-04-24 02:56:11 +02:00
|
|
|
// Filters the strings in matches based on the glob patterns in excludes. Hierarchical (a/*) and
|
|
|
|
// recursive (**) glob patterns are supported.
|
|
|
|
func filterExcludes(matches []string, excludes []string) ([]string, error) {
|
|
|
|
if len(excludes) == 0 {
|
|
|
|
return matches, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret []string
|
|
|
|
matchLoop:
|
|
|
|
for _, m := range matches {
|
|
|
|
for _, e := range excludes {
|
2017-11-22 21:50:21 +01:00
|
|
|
exclude, err := Match(e, m)
|
2015-04-24 02:56:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if exclude {
|
|
|
|
continue matchLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = append(ret, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2016-07-26 03:40:02 +02:00
|
|
|
// filterDotFiles filters out files that start with '.'
|
|
|
|
func filterDotFiles(matches []string) []string {
|
|
|
|
ret := make([]string, 0, len(matches))
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
_, name := filepath.Split(match)
|
|
|
|
if name[0] == '.' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, match)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:50:21 +01:00
|
|
|
// Match returns true if name matches pattern using the same rules as filepath.Match, but supporting
|
2019-03-26 00:14:37 +01:00
|
|
|
// recursive globs (**).
|
2017-11-22 21:50:21 +01:00
|
|
|
func Match(pattern, name string) (bool, error) {
|
2015-04-24 02:56:11 +02:00
|
|
|
if filepath.Base(pattern) == "**" {
|
|
|
|
return false, GlobLastRecursiveErr
|
|
|
|
}
|
|
|
|
|
2018-07-14 06:17:48 +02:00
|
|
|
patternDir := pattern[len(pattern)-1] == '/'
|
|
|
|
nameDir := name[len(name)-1] == '/'
|
|
|
|
|
|
|
|
if patternDir != nameDir {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if nameDir {
|
|
|
|
name = name[:len(name)-1]
|
|
|
|
pattern = pattern[:len(pattern)-1]
|
|
|
|
}
|
|
|
|
|
2015-04-24 02:56:11 +02:00
|
|
|
for {
|
|
|
|
var patternFile, nameFile string
|
2019-03-26 00:14:37 +01:00
|
|
|
pattern, patternFile = filepath.Dir(pattern), filepath.Base(pattern)
|
2015-04-24 02:56:11 +02:00
|
|
|
|
|
|
|
if patternFile == "**" {
|
2019-03-26 00:14:37 +01:00
|
|
|
if strings.Contains(pattern, "**") {
|
|
|
|
return false, GlobMultipleRecursiveErr
|
|
|
|
}
|
|
|
|
// Test if the any prefix of name matches the part of the pattern before **
|
|
|
|
for {
|
|
|
|
if name == "." || name == "/" {
|
|
|
|
return name == pattern, nil
|
|
|
|
}
|
|
|
|
if match, err := filepath.Match(pattern, name); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if match {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
name = filepath.Dir(name)
|
|
|
|
}
|
|
|
|
} else if strings.Contains(patternFile, "**") {
|
|
|
|
return false, GlobInvalidRecursiveErr
|
2015-04-24 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-03-26 00:14:37 +01:00
|
|
|
name, nameFile = filepath.Dir(name), filepath.Base(name)
|
2015-04-24 02:56:11 +02:00
|
|
|
|
2019-03-26 00:14:37 +01:00
|
|
|
if nameFile == "." && patternFile == "." {
|
2015-04-24 02:56:11 +02:00
|
|
|
return true, nil
|
2019-03-26 00:14:37 +01:00
|
|
|
} else if nameFile == "/" && patternFile == "/" {
|
|
|
|
return true, nil
|
|
|
|
} else if nameFile == "." || patternFile == "." || nameFile == "/" || patternFile == "/" {
|
2015-04-24 02:56:11 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-03-26 00:14:37 +01:00
|
|
|
match, err := filepath.Match(patternFile, nameFile)
|
2015-04-24 02:56:11 +02:00
|
|
|
if err != nil || !match {
|
|
|
|
return match, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 05:01:49 +01:00
|
|
|
func GlobPatternList(patterns []string, prefix string) (globedList []string, depDirs []string, err error) {
|
|
|
|
var (
|
|
|
|
matches []string
|
2015-04-21 01:41:55 +02:00
|
|
|
deps []string
|
2015-02-06 05:01:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
globedList = make([]string, 0)
|
|
|
|
depDirs = make([]string, 0)
|
|
|
|
|
|
|
|
for _, pattern := range patterns {
|
|
|
|
if isWild(pattern) {
|
2018-09-22 00:30:13 +02:00
|
|
|
matches, deps, err = Glob(filepath.Join(prefix, pattern), nil, FollowSymlinks)
|
2015-02-06 05:01:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
globedList = append(globedList, matches...)
|
|
|
|
depDirs = append(depDirs, deps...)
|
|
|
|
} else {
|
|
|
|
globedList = append(globedList, filepath.Join(prefix, pattern))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return globedList, depDirs, nil
|
|
|
|
}
|
2016-11-01 19:10:51 +01:00
|
|
|
|
|
|
|
// IsGlob returns true if the pattern contains any glob characters (*, ?, or [).
|
|
|
|
func IsGlob(pattern string) bool {
|
|
|
|
return strings.IndexAny(pattern, "*?[") >= 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasGlob returns true if any string in the list contains any glob characters (*, ?, or [).
|
|
|
|
func HasGlob(in []string) bool {
|
|
|
|
for _, s := range in {
|
|
|
|
if IsGlob(s) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:49:45 +01:00
|
|
|
// GlobWithDepFile finds all files and directories that match glob. Directories
|
|
|
|
// will have a trailing '/'. It compares the list of matches against the
|
|
|
|
// contents of fileListFile, and rewrites fileListFile if it has changed. It
|
|
|
|
// also writes all of the the directories it traversed as dependencies on
|
|
|
|
// fileListFile to depFile.
|
2016-11-01 19:10:51 +01:00
|
|
|
//
|
2018-02-23 23:49:45 +01:00
|
|
|
// The format of glob is either path/*.ext for a single directory glob, or
|
|
|
|
// path/**/*.ext for a recursive glob.
|
2016-11-01 19:10:51 +01:00
|
|
|
//
|
|
|
|
// Returns a list of file paths, and an error.
|
|
|
|
//
|
|
|
|
// In general ModuleContext.GlobWithDeps or SingletonContext.GlobWithDeps
|
|
|
|
// should be used instead, as they will automatically set up dependencies
|
|
|
|
// to rerun the primary builder when the list of matching files changes.
|
|
|
|
func GlobWithDepFile(glob, fileListFile, depFile string, excludes []string) (files []string, err error) {
|
2018-09-22 00:30:13 +02:00
|
|
|
files, deps, err := Glob(glob, excludes, FollowSymlinks)
|
2016-11-01 19:10:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileList := strings.Join(files, "\n") + "\n"
|
|
|
|
|
|
|
|
WriteFileIfChanged(fileListFile, []byte(fileList), 0666)
|
2017-06-23 02:01:24 +02:00
|
|
|
deptools.WriteDepFile(depFile, fileListFile, deps)
|
2016-11-01 19:10:51 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFileIfChanged wraps ioutil.WriteFile, but only writes the file if
|
|
|
|
// the files does not already exist with identical contents. This can be used
|
|
|
|
// along with ninja restat rules to skip rebuilding downstream rules if no
|
|
|
|
// changes were made by a rule.
|
|
|
|
func WriteFileIfChanged(filename string, data []byte, perm os.FileMode) error {
|
|
|
|
var isChanged bool
|
|
|
|
|
|
|
|
dir := filepath.Dir(filename)
|
|
|
|
err := os.MkdirAll(dir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// The file does not exist yet.
|
|
|
|
isChanged = true
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if info.Size() != int64(len(data)) {
|
|
|
|
isChanged = true
|
|
|
|
} else {
|
|
|
|
oldData, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(oldData) != len(data) {
|
|
|
|
isChanged = true
|
|
|
|
} else {
|
|
|
|
for i := range data {
|
|
|
|
if oldData[i] != data[i] {
|
|
|
|
isChanged = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isChanged {
|
|
|
|
err = ioutil.WriteFile(filename, data, perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-19 22:36:42 +02:00
|
|
|
|
|
|
|
var matchEscaper = strings.NewReplacer(
|
|
|
|
`*`, `\*`,
|
|
|
|
`?`, `\?`,
|
|
|
|
`[`, `\[`,
|
|
|
|
`]`, `\]`,
|
|
|
|
)
|
|
|
|
|
|
|
|
// MatchEscape returns its inputs with characters that would be interpreted by
|
|
|
|
func MatchEscape(s string) string {
|
|
|
|
return matchEscaper.Replace(s)
|
|
|
|
}
|