blob: a8c348d92e38f09f5988a0bb39a9fceeb4bc28e7 [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 {
68 excludeMatch, err := filepath.Match(e, info.Name())
69 if err != nil {
70 return err
71 }
72 if excludeMatch {
73 return nil
74 }
75 }
Colin Cross3f40fa42015-01-30 17:27:36 -080076 files = append(files, path)
77 }
78 }
79
80 return nil
81 })
82
Colin Cross3e8ec072015-03-31 16:40:23 -070083 fileList := strings.Join(files, "\n") + "\n"
Colin Cross3f40fa42015-01-30 17:27:36 -080084
85 writeFileIfChanged(fileListFile, []byte(fileList), 0666)
86 deptools.WriteDepFile(depFile, fileListFile, dirs)
87
88 return
89}
90
91func writeFileIfChanged(filename string, data []byte, perm os.FileMode) error {
92 var isChanged bool
93
94 dir := filepath.Dir(filename)
95 err := os.MkdirAll(dir, 0777)
96 if err != nil {
97 return err
98 }
99
100 info, err := os.Stat(filename)
101 if err != nil {
102 if os.IsNotExist(err) {
103 // The file does not exist yet.
104 isChanged = true
105 } else {
106 return err
107 }
108 } else {
109 if info.Size() != int64(len(data)) {
110 isChanged = true
111 } else {
112 oldData, err := ioutil.ReadFile(filename)
113 if err != nil {
114 return err
115 }
116
117 if len(oldData) != len(data) {
118 isChanged = true
119 } else {
120 for i := range data {
121 if oldData[i] != data[i] {
122 isChanged = true
123 break
124 }
125 }
126 }
127 }
128 }
129
130 if isChanged {
131 err = ioutil.WriteFile(filename, data, perm)
132 if err != nil {
133 return err
134 }
135 }
136
137 return nil
138}