blob: 45ba2855b73ed5851dddc73ad9330aac409ed142 [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
23 "blueprint/deptools"
24)
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.
38func GlobWithDepFile(glob, fileListFile, depFile string) (files []string, err error) {
39 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 {
67 files = append(files, path)
68 }
69 }
70
71 return nil
72 })
73
74 fileList := strings.Join(files, "\n")
75
76 writeFileIfChanged(fileListFile, []byte(fileList), 0666)
77 deptools.WriteDepFile(depFile, fileListFile, dirs)
78
79 return
80}
81
82func writeFileIfChanged(filename string, data []byte, perm os.FileMode) error {
83 var isChanged bool
84
85 dir := filepath.Dir(filename)
86 err := os.MkdirAll(dir, 0777)
87 if err != nil {
88 return err
89 }
90
91 info, err := os.Stat(filename)
92 if err != nil {
93 if os.IsNotExist(err) {
94 // The file does not exist yet.
95 isChanged = true
96 } else {
97 return err
98 }
99 } else {
100 if info.Size() != int64(len(data)) {
101 isChanged = true
102 } else {
103 oldData, err := ioutil.ReadFile(filename)
104 if err != nil {
105 return err
106 }
107
108 if len(oldData) != len(data) {
109 isChanged = true
110 } else {
111 for i := range data {
112 if oldData[i] != data[i] {
113 isChanged = true
114 break
115 }
116 }
117 }
118 }
119 }
120
121 if isChanged {
122 err = ioutil.WriteFile(filename, data, perm)
123 if err != nil {
124 return err
125 }
126 }
127
128 return nil
129}