aboutsummaryrefslogtreecommitdiffstats
path: root/glob/glob.go
blob: a07501a06c4900ccc35d30e88fd1269600613c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2015 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.

package glob

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/google/blueprint/deptools"
)

func IsGlob(glob string) bool {
	return strings.IndexAny(glob, "*?[") >= 0
}

// GlobWithDepFile finds all files that match glob.  It compares the list of files
// against the contents of fileListFile, and rewrites fileListFile if it has changed.  It also
// writes all of the the directories it traversed as a depenencies on fileListFile to depFile.
//
// The format of glob is either path/*.ext for a single directory glob, or path/**/*.ext
// for a recursive glob.
//
// Returns a list of file paths, and an error.
func GlobWithDepFile(glob, fileListFile, depFile string, excludes []string) (files []string, err error) {
	globPattern := filepath.Base(glob)
	globDir := filepath.Dir(glob)
	recursive := false

	if filepath.Base(globDir) == "**" {
		recursive = true
		globDir = filepath.Dir(globDir)
	}

	var dirs []string

	err = filepath.Walk(globDir,
		func(path string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}

			if info.Mode().IsDir() {
				dirs = append(dirs, path)
				if !recursive && path != globDir {
					return filepath.SkipDir
				}
			} else if info.Mode().IsRegular() {
				match, err := filepath.Match(globPattern, info.Name())
				if err != nil {
					return err
				}
				if match {
					for _, e := range excludes {
						var excludeMatch bool
						if filepath.Base(e) == e {
							excludeMatch, err = filepath.Match(e, info.Name())
						} else {
							excludeMatch, err = filepath.Match(e, path)
						}
						if err != nil {
							return err
						}
						if excludeMatch {
							return nil
						}
					}
					files = append(files, path)
				}
			}

			return nil
		})

	fileList := strings.Join(files, "\n") + "\n"

	writeFileIfChanged(fileListFile, []byte(fileList), 0666)
	deptools.WriteDepFile(depFile, fileListFile, dirs)

	return
}

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
}