Shinichiro Hamaji | b69bf8a | 2015-06-10 14:52:06 +0900 | [diff] [blame] | 1 | // 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 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | package kati |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 16 | |
| 17 | import ( |
Fumitoshi Ukai | c0ded23 | 2015-07-07 11:40:04 +0900 | [diff] [blame] | 18 | "bytes" |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 19 | "errors" |
| 20 | "fmt" |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 21 | "os" |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 22 | "os/exec" |
| 23 | "path/filepath" |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 24 | "runtime" |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 25 | "sort" |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 26 | "strings" |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 27 | "sync" |
| 28 | "time" |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 29 | |
| 30 | "github.com/golang/glog" |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 31 | ) |
| 32 | |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 33 | type wildcardCacheT struct { |
| 34 | mu sync.Mutex |
| 35 | m map[string][]string |
| 36 | } |
| 37 | |
| 38 | var wildcardCache = &wildcardCacheT{ |
| 39 | m: make(map[string][]string), |
| 40 | } |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 41 | |
Fumitoshi Ukai | c0ded23 | 2015-07-07 11:40:04 +0900 | [diff] [blame] | 42 | func wildcardUnescape(pat string) string { |
| 43 | var buf bytes.Buffer |
| 44 | for i := 0; i < len(pat); i++ { |
| 45 | if pat[i] == '\\' && i+1 < len(pat) { |
| 46 | switch pat[i+1] { |
| 47 | case '*', '?', '[', '\\': |
| 48 | writeByte(&buf, pat[i]) |
| 49 | default: |
| 50 | i++ |
| 51 | } |
| 52 | } |
| 53 | writeByte(&buf, pat[i]) |
| 54 | } |
| 55 | return buf.String() |
| 56 | } |
| 57 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 58 | func wildcardGlob(pat string) ([]string, error) { |
Fumitoshi Ukai | 9d959c3 | 2015-06-19 18:06:01 +0900 | [diff] [blame] | 59 | // TODO(ukai): use find cache for glob if exists. |
Fumitoshi Ukai | c0ded23 | 2015-07-07 11:40:04 +0900 | [diff] [blame] | 60 | pat = wildcardUnescape(pat) |
Fumitoshi Ukai | 9d959c3 | 2015-06-19 18:06:01 +0900 | [diff] [blame] | 61 | pattern := filepath.Clean(pat) |
| 62 | if pattern != pat { |
| 63 | // For some reason, go's Glob normalizes |
| 64 | // foo/../bar to bar. |
| 65 | i := strings.IndexAny(pattern, "*?[") |
| 66 | if i < 0 { |
| 67 | // no wildcard. if any files matched with pattern, |
| 68 | // return pat. |
| 69 | _, err := os.Stat(pat) |
| 70 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 71 | return nil, nil |
Fumitoshi Ukai | 9d959c3 | 2015-06-19 18:06:01 +0900 | [diff] [blame] | 72 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 73 | return []string{pat}, nil |
Fumitoshi Ukai | 9d959c3 | 2015-06-19 18:06:01 +0900 | [diff] [blame] | 74 | } |
| 75 | if strings.Contains(pattern[i+1:], "..") { |
| 76 | // We ask shell to expand a glob to avoid this. |
| 77 | cmdline := []string{"/bin/sh", "-c", "/bin/ls -d " + pat} |
| 78 | cmd := exec.Cmd{ |
| 79 | Path: cmdline[0], |
| 80 | Args: cmdline, |
| 81 | } |
| 82 | // Ignore errors. |
| 83 | out, _ := cmd.Output() |
| 84 | ws := newWordScanner(out) |
| 85 | var files []string |
| 86 | for ws.Scan() { |
| 87 | files = append(files, string(ws.Bytes())) |
| 88 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 89 | return files, nil |
Fumitoshi Ukai | 9d959c3 | 2015-06-19 18:06:01 +0900 | [diff] [blame] | 90 | } |
| 91 | // prefix + meta + suffix, and suffix doesn't have '..' |
| 92 | prefix := pattern[:i] |
| 93 | i = strings.IndexAny(pat, "*?[") |
| 94 | if i < 0 { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 95 | return nil, fmt.Errorf("wildcard metachar mismatch? pattern=%q pat=%q", pattern, pat) |
Fumitoshi Ukai | 9d959c3 | 2015-06-19 18:06:01 +0900 | [diff] [blame] | 96 | } |
| 97 | oprefix := pat[:i] |
| 98 | matched, err := filepath.Glob(pattern) |
| 99 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 100 | return nil, err |
Fumitoshi Ukai | 9d959c3 | 2015-06-19 18:06:01 +0900 | [diff] [blame] | 101 | } |
| 102 | var files []string |
| 103 | for _, m := range matched { |
| 104 | file := oprefix + strings.TrimPrefix(m, prefix) |
| 105 | _, err := os.Stat(file) |
| 106 | if err != nil { |
| 107 | continue |
| 108 | } |
| 109 | files = append(files, file) |
| 110 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 111 | return files, nil |
Fumitoshi Ukai | 9d959c3 | 2015-06-19 18:06:01 +0900 | [diff] [blame] | 112 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 113 | return filepath.Glob(pat) |
Fumitoshi Ukai | 9d959c3 | 2015-06-19 18:06:01 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 116 | func wildcard(w evalWriter, pat string) error { |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 117 | if UseWildcardCache { |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 118 | // TODO(ukai): make sure it didn't chdir? |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 119 | wildcardCache.mu.Lock() |
| 120 | files, ok := wildcardCache.m[pat] |
| 121 | wildcardCache.mu.Unlock() |
| 122 | if ok { |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 123 | for _, file := range files { |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 124 | w.writeWordString(file) |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 125 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 126 | return nil |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 127 | } |
| 128 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 129 | files, err := wildcardGlob(pat) |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 133 | for _, file := range files { |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 134 | w.writeWordString(file) |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 135 | } |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 136 | if UseWildcardCache { |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 137 | wildcardCache.mu.Lock() |
| 138 | wildcardCache.m[pat] = files |
| 139 | wildcardCache.mu.Unlock() |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 140 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 141 | return nil |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 142 | } |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 143 | |
| 144 | type fileInfo struct { |
| 145 | path string |
| 146 | mode os.FileMode |
| 147 | } |
| 148 | |
| 149 | type androidFindCacheT struct { |
| 150 | once sync.Once |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 151 | filesch chan []fileInfo |
| 152 | leavesch chan []fileInfo |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 153 | files []fileInfo |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 154 | leaves []fileInfo |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 155 | scanTime time.Duration |
| 156 | } |
| 157 | |
| 158 | var ( |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 159 | androidFindCache androidFindCacheT |
| 160 | androidDefaultLeafNames = []string{"CleanSpec.mk", "Android.mk"} |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 161 | ) |
| 162 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 163 | // AndroidFindCacheInit initializes find cache for android build. |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 164 | func AndroidFindCacheInit(prunes, leafNames []string) { |
Fumitoshi Ukai | 1394d10 | 2015-07-08 16:45:49 +0900 | [diff] [blame] | 165 | if !UseFindCache { |
| 166 | return |
| 167 | } |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 168 | if leafNames != nil { |
| 169 | androidDefaultLeafNames = leafNames |
| 170 | } |
| 171 | androidFindCache.init(prunes) |
| 172 | } |
| 173 | |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 174 | func (c *androidFindCacheT) ready() bool { |
Fumitoshi Ukai | 1394d10 | 2015-07-08 16:45:49 +0900 | [diff] [blame] | 175 | if !UseFindCache { |
| 176 | return false |
| 177 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 178 | if c.files != nil { |
| 179 | return true |
| 180 | } |
| 181 | select { |
| 182 | case c.files = <-c.filesch: |
| 183 | } |
| 184 | return c.files != nil |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 185 | } |
| 186 | |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 187 | func (c *androidFindCacheT) leavesReady() bool { |
Fumitoshi Ukai | 1394d10 | 2015-07-08 16:45:49 +0900 | [diff] [blame] | 188 | if !UseFindCache { |
| 189 | return false |
| 190 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 191 | if c.leaves != nil { |
| 192 | return true |
| 193 | } |
| 194 | select { |
| 195 | case c.leaves = <-c.leavesch: |
| 196 | } |
| 197 | return c.leaves != nil |
| 198 | } |
| 199 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 200 | func (c *androidFindCacheT) init(prunes []string) { |
Fumitoshi Ukai | 1394d10 | 2015-07-08 16:45:49 +0900 | [diff] [blame] | 201 | if !UseFindCache { |
| 202 | return |
| 203 | } |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 204 | c.once.Do(func() { |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 205 | c.filesch = make(chan []fileInfo, 1) |
| 206 | c.leavesch = make(chan []fileInfo, 1) |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 207 | go c.start(prunes, androidDefaultLeafNames) |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 208 | }) |
| 209 | } |
| 210 | |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 211 | func (c *androidFindCacheT) start(prunes, leafNames []string) { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 212 | glog.Infof("find cache init: prunes=%q leafNames=%q", prunes, leafNames) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 213 | te := traceEvent.begin("findcache", literal("init"), traceEventFindCache) |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 214 | defer func() { |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 215 | traceEvent.end(te) |
| 216 | c.scanTime = time.Since(te.t) |
Fumitoshi Ukai | 49599e5 | 2015-06-26 10:10:24 +0900 | [diff] [blame] | 217 | logStats("android find cache scan: %v", c.scanTime) |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 218 | }() |
| 219 | |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 220 | dirs := make(chan string, 32) |
| 221 | filech := make(chan fileInfo, 1000) |
| 222 | leafch := make(chan fileInfo, 1000) |
| 223 | var wg sync.WaitGroup |
| 224 | numWorker := runtime.NumCPU() - 1 |
| 225 | wg.Add(numWorker) |
| 226 | for i := 0; i < numWorker; i++ { |
| 227 | go func() { |
| 228 | defer wg.Done() |
| 229 | for dir := range dirs { |
| 230 | err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 231 | if info.IsDir() { |
| 232 | for _, prune := range prunes { |
| 233 | if info.Name() == prune { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 234 | glog.V(1).Infof("find cache prune: %s", path) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 235 | return filepath.SkipDir |
| 236 | } |
| 237 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 238 | } |
| 239 | filech <- fileInfo{ |
| 240 | path: path, |
| 241 | mode: info.Mode(), |
| 242 | } |
| 243 | for _, leaf := range leafNames { |
| 244 | if info.Name() == leaf { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 245 | glog.V(1).Infof("find cache leaf: %s", path) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 246 | leafch <- fileInfo{ |
| 247 | path: path, |
| 248 | mode: info.Mode(), |
| 249 | } |
| 250 | break |
| 251 | } |
| 252 | } |
| 253 | return nil |
| 254 | }) |
| 255 | if err != nil && err != filepath.SkipDir { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 256 | glog.Warningf("error in adnroid find cache: %v", err) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 257 | close(c.filesch) |
| 258 | close(c.leavesch) |
| 259 | return |
Fumitoshi Ukai | 0daac1f | 2015-06-11 11:56:50 +0900 | [diff] [blame] | 260 | } |
| 261 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 262 | }() |
| 263 | } |
| 264 | |
| 265 | go func() { |
Fumitoshi Ukai | 040271f | 2015-06-18 11:04:46 +0900 | [diff] [blame] | 266 | dirs := make(map[string]bool) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 267 | leavesTe := traceEvent.begin("findcache", literal("leaves"), traceEventFindCacheLeaves) |
| 268 | var leaves []fileInfo |
Fumitoshi Ukai | 040271f | 2015-06-18 11:04:46 +0900 | [diff] [blame] | 269 | nfiles := 0 |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 270 | for leaf := range leafch { |
| 271 | leaves = append(leaves, leaf) |
Fumitoshi Ukai | 040271f | 2015-06-18 11:04:46 +0900 | [diff] [blame] | 272 | nfiles++ |
| 273 | for dir := filepath.Dir(leaf.path); dir != "."; dir = filepath.Dir(dir) { |
| 274 | if dirs[dir] { |
| 275 | break |
| 276 | } |
| 277 | leaves = append(leaves, fileInfo{ |
| 278 | path: dir, |
| 279 | mode: leaf.mode | os.ModeDir, |
| 280 | }) |
| 281 | dirs[dir] = true |
| 282 | } |
Fumitoshi Ukai | 0daac1f | 2015-06-11 11:56:50 +0900 | [diff] [blame] | 283 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 284 | sort.Sort(fileInfoByLeaf(leaves)) |
| 285 | c.leavesch <- leaves |
| 286 | traceEvent.end(leavesTe) |
Fumitoshi Ukai | 49599e5 | 2015-06-26 10:10:24 +0900 | [diff] [blame] | 287 | logStats("%d leaves %d dirs in find cache", nfiles, len(dirs)) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 288 | if !glog.V(1) { |
| 289 | return |
| 290 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 291 | for i, leaf := range leaves { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 292 | glog.Infof("android findleaves cache: %d: %s %v", i, leaf.path, leaf.mode) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 293 | } |
| 294 | }() |
| 295 | |
| 296 | go func() { |
| 297 | filesTe := traceEvent.begin("findcache", literal("files"), traceEventFindCacheFiles) |
| 298 | var files []fileInfo |
| 299 | for file := range filech { |
| 300 | files = append(files, file) |
| 301 | } |
| 302 | sort.Sort(fileInfoByName(files)) |
| 303 | c.filesch <- files |
| 304 | traceEvent.end(filesTe) |
Fumitoshi Ukai | 49599e5 | 2015-06-26 10:10:24 +0900 | [diff] [blame] | 305 | logStats("%d files in find cache", len(files)) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 306 | if !glog.V(1) { |
| 307 | return |
| 308 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 309 | for i, fi := range files { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 310 | glog.Infof("android find cache: %d: %s %v", i, fi.path, fi.mode) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 311 | } |
| 312 | }() |
| 313 | |
| 314 | curdir, err := os.Open(".") |
| 315 | if err != nil { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 316 | glog.Warningf("open . failed: %v", err) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 317 | close(c.filesch) |
| 318 | close(c.leavesch) |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 319 | return |
| 320 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 321 | names, err := curdir.Readdirnames(-1) |
| 322 | if err != nil { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 323 | glog.Warningf("readdir . failed: %v", err) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 324 | close(c.filesch) |
| 325 | close(c.leavesch) |
| 326 | return |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 327 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 328 | curdir.Close() |
| 329 | |
| 330 | for _, name := range names { |
| 331 | dirs <- name |
| 332 | } |
| 333 | close(dirs) |
| 334 | wg.Wait() |
| 335 | close(filech) |
| 336 | close(leafch) |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | type fileInfoByName []fileInfo |
| 340 | |
| 341 | func (f fileInfoByName) Len() int { return len(f) } |
| 342 | func (f fileInfoByName) Swap(i, j int) { f[i], f[j] = f[j], f[i] } |
| 343 | func (f fileInfoByName) Less(i, j int) bool { |
| 344 | return f[i].path < f[j].path |
| 345 | } |
| 346 | |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 347 | type fileInfoByLeaf []fileInfo |
| 348 | |
| 349 | func (f fileInfoByLeaf) Len() int { return len(f) } |
| 350 | func (f fileInfoByLeaf) Swap(i, j int) { f[i], f[j] = f[j], f[i] } |
| 351 | func (f fileInfoByLeaf) Less(i, j int) bool { |
| 352 | di := strings.Count(f[i].path, "/") |
| 353 | dj := strings.Count(f[j].path, "/") |
| 354 | if di != dj { |
| 355 | return di < dj |
| 356 | } |
Fumitoshi Ukai | c39b188 | 2015-06-16 16:29:08 +0900 | [diff] [blame] | 357 | diri := filepath.Dir(f[i].path) + "/" |
| 358 | dirj := filepath.Dir(f[j].path) + "/" |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 359 | if diri != dirj { |
| 360 | return diri < dirj |
| 361 | } |
| 362 | mdi := f[i].mode & os.ModeDir |
| 363 | mdj := f[j].mode & os.ModeDir |
| 364 | if mdi != mdj { |
| 365 | return mdi < mdj |
| 366 | } |
| 367 | return f[i].path < f[j].path |
| 368 | } |
| 369 | |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 370 | var errSkipDir = errors.New("skip dir") |
| 371 | |
| 372 | func (c *androidFindCacheT) walk(dir string, walkFn func(int, fileInfo) error) error { |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 373 | i := sort.Search(len(c.files), func(i int) bool { |
| 374 | return c.files[i].path >= dir |
| 375 | }) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 376 | glog.V(1).Infof("android find in dir cache: %s i=%d/%d", dir, i, len(c.files)) |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 377 | start := i |
| 378 | var skipdirs []string |
| 379 | Loop: |
| 380 | for i := start; i < len(c.files); i++ { |
| 381 | if c.files[i].path == dir { |
| 382 | err := walkFn(i, c.files[i]) |
| 383 | if err != nil { |
| 384 | return err |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 385 | } |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 386 | continue |
| 387 | } |
| 388 | if !strings.HasPrefix(c.files[i].path, dir) { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 389 | glog.V(1).Infof("android find in dir cache: %s end=%d/%d", dir, i, len(c.files)) |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 390 | return nil |
| 391 | } |
| 392 | if !strings.HasPrefix(c.files[i].path, dir+"/") { |
| 393 | continue |
| 394 | } |
| 395 | for _, skip := range skipdirs { |
| 396 | if strings.HasPrefix(c.files[i].path, skip+"/") { |
| 397 | continue Loop |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 398 | } |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 399 | } |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 400 | |
| 401 | err := walkFn(i, c.files[i]) |
| 402 | if err == errSkipDir { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 403 | glog.V(1).Infof("android find in skip dir: %s", c.files[i].path) |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 404 | skipdirs = append(skipdirs, c.files[i].path) |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 405 | continue |
| 406 | } |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 407 | if err != nil { |
| 408 | return err |
| 409 | } |
| 410 | } |
| 411 | return nil |
| 412 | } |
| 413 | |
| 414 | // pattern in repo/android/build/core/definitions.mk |
| 415 | // find-subdir-assets |
| 416 | // if [ -d $1 ] ; then cd $1 ; find ./ -not -name '.*' -and -type f -and -not -type l ; fi |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 417 | func (c *androidFindCacheT) findInDir(w evalWriter, dir string) { |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 418 | dir = filepath.Clean(dir) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 419 | glog.V(1).Infof("android find in dir cache: %s", dir) |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 420 | c.walk(dir, func(_ int, fi fileInfo) error { |
| 421 | // -not -name '.*' |
| 422 | if strings.HasPrefix(filepath.Base(fi.path), ".") { |
| 423 | return nil |
| 424 | } |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 425 | // -type f and -not -type l |
| 426 | // regular type and not symlink |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 427 | if !fi.mode.IsRegular() { |
| 428 | return nil |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 429 | } |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 430 | name := strings.TrimPrefix(fi.path, dir+"/") |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 431 | name = "./" + name |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 432 | w.writeWordString(name) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 433 | glog.V(1).Infof("android find in dir cache: %s=> %s", dir, name) |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 434 | return nil |
| 435 | }) |
Fumitoshi Ukai | 106fb79 | 2015-06-09 10:37:35 +0900 | [diff] [blame] | 436 | } |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 437 | |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 438 | // pattern in repo/android/build/core/definitions.mk |
Fumitoshi Ukai | b234e27 | 2015-06-18 13:14:16 +0900 | [diff] [blame] | 439 | // all-java-files-under etc |
| 440 | // cd ${LOCAL_PATH} ; find -L $1 -name "*<ext>" -and -not -name ".*" |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 441 | // returns false if symlink is found. |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 442 | func (c *androidFindCacheT) findExtFilesUnder(w evalWriter, chdir, root, ext string) bool { |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 443 | chdir = filepath.Clean(chdir) |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 444 | dir := filepath.Join(chdir, root) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 445 | glog.V(1).Infof("android find %s in dir cache: %s %s", ext, chdir, root) |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 446 | // check symlinks |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 447 | var matches []int |
| 448 | err := c.walk(dir, func(i int, fi fileInfo) error { |
| 449 | if fi.mode&os.ModeSymlink == os.ModeSymlink { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 450 | glog.Warningf("android find %s in dir cache: detect symlink %s %v", ext, c.files[i].path, c.files[i].mode) |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 451 | return fmt.Errorf("symlink %s", fi.path) |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 452 | } |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 453 | matches = append(matches, i) |
| 454 | return nil |
| 455 | }) |
| 456 | if err != nil { |
| 457 | return false |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 458 | } |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 459 | // no symlinks |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 460 | for _, i := range matches { |
| 461 | fi := c.files[i] |
| 462 | base := filepath.Base(fi.path) |
Fumitoshi Ukai | b234e27 | 2015-06-18 13:14:16 +0900 | [diff] [blame] | 463 | // -name "*<ext>" |
| 464 | if filepath.Ext(base) != ext { |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 465 | continue |
| 466 | } |
| 467 | // -not -name ".*" |
| 468 | if strings.HasPrefix(base, ".") { |
| 469 | continue |
| 470 | } |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 471 | name := strings.TrimPrefix(fi.path, chdir+"/") |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 472 | w.writeWordString(name) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 473 | glog.V(1).Infof("android find %s in dir cache: %s=> %s", ext, dir, name) |
Fumitoshi Ukai | 1a77c8c | 2015-06-09 12:56:22 +0900 | [diff] [blame] | 474 | } |
| 475 | return true |
| 476 | } |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 477 | |
| 478 | // pattern: in repo/android/build/core/base_rules.mk |
| 479 | // java_resource_file_groups+= ... |
| 480 | // cd ${TOP_DIR}${LOCAL_PATH}/${dir} && find . -type d -a -name ".svn" -prune \ |
| 481 | // -o -type f -a \! -name "*.java" -a \! -name "package.html" -a \! \ |
| 482 | // -name "overview.html" -a \! -name ".*.swp" -a \! -name ".DS_Store" \ |
| 483 | // -a \! -name "*~" -print ) |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 484 | func (c *androidFindCacheT) findJavaResourceFileGroup(w evalWriter, dir string) { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 485 | glog.V(1).Infof("android find java resource in dir cache: %s", dir) |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 486 | c.walk(filepath.Clean(dir), func(_ int, fi fileInfo) error { |
| 487 | // -type d -a -name ".svn" -prune |
| 488 | if fi.mode.IsDir() && filepath.Base(fi.path) == ".svn" { |
| 489 | return errSkipDir |
| 490 | } |
| 491 | // -type f |
| 492 | if !fi.mode.IsRegular() { |
| 493 | return nil |
| 494 | } |
| 495 | // ! -name "*.java" -a ! -name "package.html" -a |
| 496 | // ! -name "overview.html" -a ! -name ".*.swp" -a |
| 497 | // ! -name ".DS_Store" -a ! -name "*~" |
| 498 | base := filepath.Base(fi.path) |
| 499 | if filepath.Ext(base) == ".java" || |
| 500 | base == "package.html" || |
| 501 | base == "overview.html" || |
| 502 | (strings.HasPrefix(base, ".") && strings.HasSuffix(base, ".swp")) || |
| 503 | base == ".DS_Store" || |
| 504 | strings.HasSuffix(base, "~") { |
| 505 | return nil |
| 506 | } |
| 507 | name := strings.TrimPrefix(fi.path, dir+"/") |
| 508 | name = "./" + name |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 509 | w.writeWordString(name) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 510 | glog.V(1).Infof("android find java resource in dir cache: %s=> %s", dir, name) |
Fumitoshi Ukai | 7adc0f5 | 2015-06-09 15:34:26 +0900 | [diff] [blame] | 511 | return nil |
| 512 | }) |
| 513 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 514 | |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 515 | func (c *androidFindCacheT) findleaves(w evalWriter, dir, name string, prunes []string, mindepth int) bool { |
Fumitoshi Ukai | 40e35f5 | 2015-06-16 16:33:04 +0900 | [diff] [blame] | 516 | var found []string |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 517 | var dirs []string |
Fumitoshi Ukai | 2a2deb3 | 2015-07-08 11:46:14 +0900 | [diff] [blame] | 518 | dir = filepath.Clean(dir) |
Fumitoshi Ukai | 3b6c1e8 | 2015-06-29 17:27:37 +0900 | [diff] [blame] | 519 | topdepth := strings.Count(dir, "/") |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 520 | dirs = append(dirs, dir) |
| 521 | for len(dirs) > 0 { |
| 522 | dir = filepath.Clean(dirs[0]) + "/" |
| 523 | dirs = dirs[1:] |
| 524 | if dir == "./" { |
| 525 | dir = "" |
| 526 | } |
| 527 | depth := strings.Count(dir, "/") |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 528 | // glog.V(1).Infof("android findleaves dir=%q depth=%d dirs=%q", dir, depth, dirs) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 529 | i := sort.Search(len(c.leaves), func(i int) bool { |
| 530 | di := strings.Count(c.leaves[i].path, "/") |
| 531 | if di != depth { |
| 532 | return di >= depth |
| 533 | } |
Fumitoshi Ukai | c39b188 | 2015-06-16 16:29:08 +0900 | [diff] [blame] | 534 | diri := filepath.Dir(c.leaves[i].path) + "/" |
| 535 | if diri != dir { |
| 536 | return diri >= dir |
| 537 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 538 | return c.leaves[i].path >= dir |
| 539 | }) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 540 | glog.V(1).Infof("android findleaves dir=%q i=%d/%d", dir, i, len(c.leaves)) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 541 | |
| 542 | Scandir: |
| 543 | for ; i < len(c.leaves); i++ { |
| 544 | if dir == "" && strings.Contains(c.leaves[i].path, "/") { |
| 545 | break |
| 546 | } |
| 547 | if !strings.HasPrefix(c.leaves[i].path, dir) { |
| 548 | break |
| 549 | } |
| 550 | if mindepth < 0 || depth >= topdepth+mindepth { |
| 551 | if !c.leaves[i].mode.IsDir() && filepath.Base(c.leaves[i].path) == name { |
| 552 | n := "./" + c.leaves[i].path |
Fumitoshi Ukai | 40e35f5 | 2015-06-16 16:33:04 +0900 | [diff] [blame] | 553 | found = append(found, n) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 554 | glog.V(1).Infof("android findleaves name=%s=> %s (depth=%d topdepth=%d mindepth=%d)", name, n, depth, topdepth, mindepth) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 555 | break Scandir |
| 556 | } |
| 557 | } |
| 558 | if c.leaves[i].mode.IsDir() { |
| 559 | dirs = append(dirs, c.leaves[i].path) |
| 560 | } |
| 561 | } |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 562 | // glog.V(1).Infof("android findleaves next dirs=%q", dirs) |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 563 | } |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 564 | glog.V(1).Infof("android findleave done") |
Fumitoshi Ukai | 40e35f5 | 2015-06-16 16:33:04 +0900 | [diff] [blame] | 565 | sort.Strings(found) |
| 566 | for _, f := range found { |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 567 | w.writeWordString(f) |
Fumitoshi Ukai | 40e35f5 | 2015-06-16 16:33:04 +0900 | [diff] [blame] | 568 | } |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 569 | return true |
| 570 | } |