Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [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 | |
| 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 19 | "sort" |
Colin Cross | 741e14e | 2017-12-07 22:02:40 -0800 | [diff] [blame] | 20 | "strings" |
Colin Cross | e98d082 | 2018-09-21 15:30:13 -0700 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint/pathtools" |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 25 | func verifyGlob(key globKey, pattern string, excludes []string, g pathtools.GlobResult) { |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 26 | if pattern != g.Pattern { |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 27 | panic(fmt.Errorf("Mismatched patterns %q and %q for glob key %q", pattern, g.Pattern, key)) |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 28 | } |
Colin Cross | 54cb95a | 2018-02-23 11:09:18 -0800 | [diff] [blame] | 29 | if len(excludes) != len(g.Excludes) { |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 30 | panic(fmt.Errorf("Mismatched excludes %v and %v for glob key %q", excludes, g.Excludes, key)) |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 31 | } |
Colin Cross | 54cb95a | 2018-02-23 11:09:18 -0800 | [diff] [blame] | 32 | |
| 33 | for i := range excludes { |
| 34 | if g.Excludes[i] != excludes[i] { |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 35 | panic(fmt.Errorf("Mismatched excludes %v and %v for glob key %q", excludes, g.Excludes, key)) |
Colin Cross | 54cb95a | 2018-02-23 11:09:18 -0800 | [diff] [blame] | 36 | } |
| 37 | } |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | func (c *Context) glob(pattern string, excludes []string) ([]string, error) { |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 41 | // Sort excludes so that two globs with the same excludes in a different order reuse the same |
| 42 | // key. Make a copy first to avoid modifying the caller's version. |
| 43 | excludes = append([]string(nil), excludes...) |
| 44 | sort.Strings(excludes) |
| 45 | |
| 46 | key := globToKey(pattern, excludes) |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 47 | |
| 48 | // Try to get existing glob from the stored results |
| 49 | c.globLock.Lock() |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 50 | g, exists := c.globs[key] |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 51 | c.globLock.Unlock() |
| 52 | |
| 53 | if exists { |
| 54 | // Glob has already been done, double check it is identical |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 55 | verifyGlob(key, pattern, excludes, g) |
Colin Cross | a64ca94 | 2021-01-21 13:47:59 -0800 | [diff] [blame] | 56 | // Return a copy so that modifications don't affect the cached value. |
Colin Cross | 67c9925 | 2021-04-07 14:28:13 -0700 | [diff] [blame] | 57 | return append([]string(nil), g.Matches...), nil |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Get a globbed file list |
Colin Cross | 67c9925 | 2021-04-07 14:28:13 -0700 | [diff] [blame] | 61 | result, err := c.fs.Glob(pattern, excludes, pathtools.FollowSymlinks) |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | // Store the results |
| 67 | c.globLock.Lock() |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 68 | if g, exists = c.globs[key]; !exists { |
| 69 | c.globs[key] = result |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 70 | } |
| 71 | c.globLock.Unlock() |
| 72 | |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 73 | if exists { |
Colin Cross | 67c9925 | 2021-04-07 14:28:13 -0700 | [diff] [blame] | 74 | // Getting the list raced with another goroutine, throw away the results and use theirs |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 75 | verifyGlob(key, pattern, excludes, g) |
Colin Cross | a64ca94 | 2021-01-21 13:47:59 -0800 | [diff] [blame] | 76 | // Return a copy so that modifications don't affect the cached value. |
Colin Cross | 67c9925 | 2021-04-07 14:28:13 -0700 | [diff] [blame] | 77 | return append([]string(nil), g.Matches...), nil |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Colin Cross | a64ca94 | 2021-01-21 13:47:59 -0800 | [diff] [blame] | 80 | // Return a copy so that modifications don't affect the cached value. |
Colin Cross | 67c9925 | 2021-04-07 14:28:13 -0700 | [diff] [blame] | 81 | return append([]string(nil), result.Matches...), nil |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 84 | func (c *Context) Globs() pathtools.MultipleGlobResults { |
| 85 | keys := make([]globKey, 0, len(c.globs)) |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 86 | for k := range c.globs { |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 87 | keys = append(keys, k) |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 88 | } |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 89 | |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 90 | sort.Slice(keys, func(i, j int) bool { |
| 91 | if keys[i].pattern != keys[j].pattern { |
| 92 | return keys[i].pattern < keys[j].pattern |
| 93 | } |
| 94 | return keys[i].excludes < keys[j].excludes |
| 95 | }) |
| 96 | |
| 97 | globs := make(pathtools.MultipleGlobResults, len(keys)) |
| 98 | for i, key := range keys { |
| 99 | globs[i] = c.globs[key] |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return globs |
| 103 | } |
| 104 | |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 105 | // globKey combines a pattern and a list of excludes into a hashable struct to be used as a key in |
| 106 | // a map. |
| 107 | type globKey struct { |
| 108 | pattern string |
| 109 | excludes string |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Colin Cross | 2523698 | 2021-04-05 17:20:34 -0700 | [diff] [blame] | 112 | // globToKey converts a pattern and an excludes list into a globKey struct that is hashable and |
| 113 | // usable as a key in a map. |
| 114 | func globToKey(pattern string, excludes []string) globKey { |
| 115 | return globKey{pattern, strings.Join(excludes, "|")} |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 116 | } |