blob: a07501a06c4900ccc35d30e88fd1269600613c26 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package glob
16
17import (
18 "io/ioutil"
19 "os"
20 "path/filepath"
21 "strings"
22
Colin Cross70b40592015-03-23 12:57:34 -070023 "github.com/google/blueprint/deptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080024)
25
26func IsGlob(glob string) bool {
27 return strings.IndexAny(glob, "*?[") >= 0
28}
29
30// GlobWithDepFile finds all files that match glob. It compares the list of files
31// against the contents of fileListFile, and rewrites fileListFile if it has changed. It also
32// writes all of the the directories it traversed as a depenencies on fileListFile to depFile.
33//
34// The format of glob is either path/*.ext for a single directory glob, or path/**/*.ext
35// for a recursive glob.
36//
37// Returns a list of file paths, and an error.
Colin Cross3e8ec072015-03-31 16:40:23 -070038func GlobWithDepFile(glob, fileListFile, depFile string, excludes []string) (files []string, err error) {
Colin Cross3f40fa42015-01-30 17:27:36 -080039 globPattern := filepath.Base(glob)
40 globDir := filepath.Dir(glob)
41 recursive := false
42
43 if filepath.Base(globDir) == "**" {
44 recursive = true
45 globDir = filepath.Dir(globDir)
46 }
47
48 var dirs []string
49
50 err = filepath.Walk(globDir,
51 func(path string, info os.FileInfo, err error) error {
52 if err != nil {
53 return err
54 }
55
56 if info.Mode().IsDir() {
57 dirs = append(dirs, path)
58 if !recursive && path != globDir {
59 return filepath.SkipDir
60 }
61 } else if info.Mode().IsRegular() {
62 match, err := filepath.Match(globPattern, info.Name())
63 if err != nil {
64 return err
65 }
66 if match {
Colin Cross3e8ec072015-03-31 16:40:23 -070067 for _, e := range excludes {
Colin Crossfce53272015-04-08 11:21:40 -070068 var excludeMatch bool
69 if filepath.Base(e) == e {
70 excludeMatch, err = filepath.Match(e, info.Name())
71 } else {
72 excludeMatch, err = filepath.Match(e, path)
73 }
Colin Cross3e8ec072015-03-31 16:40:23 -070074 if err != nil {
75 return err
76 }
77 if excludeMatch {
78 return nil
79 }
80 }
Colin Cross3f40fa42015-01-30 17:27:36 -080081 files = append(files, path)
82 }
83 }
84
85 return nil
86 })
87
Colin Cross3e8ec072015-03-31 16:40:23 -070088 fileList := strings.Join(files, "\n") + "\n"
Colin Cross3f40fa42015-01-30 17:27:36 -080089
90 writeFileIfChanged(fileListFile, []byte(fileList), 0666)
91 deptools.WriteDepFile(depFile, fileListFile, dirs)
92
93 return
94}
95
96func writeFileIfChanged(filename string, data []byte, perm os.FileMode) error {
97 var isChanged bool
98
99 dir := filepath.Dir(filename)
100 err := os.MkdirAll(dir, 0777)
101 if err != nil {
102 return err
103 }
104
105 info, err := os.Stat(filename)
106 if err != nil {
107 if os.IsNotExist(err) {
108 // The file does not exist yet.
109 isChanged = true
110 } else {
111 return err
112 }
113 } else {
114 if info.Size() != int64(len(data)) {
115 isChanged = true
116 } else {
117 oldData, err := ioutil.ReadFile(filename)
118 if err != nil {
119 return err
120 }
121
122 if len(oldData) != len(data) {
123 isChanged = true
124 } else {
125 for i := range data {
126 if oldData[i] != data[i] {
127 isChanged = true
128 break
129 }
130 }
131 }
132 }
133 }
134
135 if isChanged {
136 err = ioutil.WriteFile(filename, data, perm)
137 if err != nil {
138 return err
139 }
140 }
141
142 return nil
143}