Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. | |
| 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 common | |
| 16 | |
| 17 import ( | |
| 18 "fmt" | |
| 19 "path/filepath" | |
| 20 ) | |
| 21 | |
| 22 // FilesystemView provides a very simple restricted "view" into a filesystem. | |
|
mithro
2017/07/19 04:48:41
Maybe just 'FilesystemView provides a filtered "vi
mcgreevy
2017/07/19 05:11:04
Done.
| |
| 23 // It translates absolute paths to relative paths based on its configured root p ath. | |
| 24 // It also hides any paths which match a blacklist entry. | |
| 25 type FilesystemView struct { | |
| 26 root string | |
| 27 blacklist []string | |
| 28 } | |
| 29 | |
| 30 // NewFilesystemView returns a FilesystemView based on the supplied root and bla cklist, or | |
| 31 // an error if blacklist contains a bad pattern. | |
| 32 // root is the the base path used by RelativePath to calulate relative paths. | |
| 33 // blacklist is a list of globs of files to ignore. See RelativePath for more i nformation. | |
| 34 func NewFilesystemView(root string, blacklist []string) (FilesystemView, error) { | |
| 35 for _, b := range blacklist { | |
| 36 if _, err := filepath.Match(b, b); err != nil { | |
| 37 return FilesystemView{}, fmt.Errorf("bad blacklist patte rn \"%s\"", b) | |
| 38 } | |
| 39 } | |
| 40 return FilesystemView{root: root, blacklist: blacklist}, nil | |
| 41 } | |
| 42 | |
| 43 // RelativePath returns a version of path which is relative to the FilesystemVie w root, | |
| 44 // or an empty string if path matches a blacklist entry. | |
|
mithro
2017/07/19 04:48:41
Why an empty string rather than nil?
mcgreevy
2017/07/19 05:11:04
That would require returning an *string, rather th
mithro
2017/07/19 05:46:18
I can't think of a way that an empty string would
mcgreevy
2017/07/21 04:15:50
I considered all these options (plus returning an
| |
| 45 // | |
| 46 // Blacklist entries are matched against each of: | |
| 47 // * the path relative to the FilesystemView root. | |
| 48 // * the basename return by filepath.Base(path). | |
|
mithro
2017/07/19 04:48:41
Is this a full string match or partial?
IE would
mcgreevy
2017/07/19 05:11:04
It's full match, but they're not regexes -- they'r
mithro
2017/07/19 05:46:18
Ack - Looks okay.
I might change "Blacklist entri
mcgreevy
2017/07/21 04:15:50
I did...
| |
| 49 func (ff FilesystemView) RelativePath(path string) (string, error) { | |
| 50 relPath, err := filepath.Rel(ff.root, path) | |
| 51 if err != nil { | |
| 52 return "", fmt.Errorf("calculating relative path(%q): %v", path, err) | |
| 53 } | |
| 54 if ff.skipRelPath(relPath) { | |
| 55 relPath = "" | |
| 56 } | |
| 57 return relPath, nil | |
| 58 } | |
| 59 | |
| 60 func (ff FilesystemView) skipRelPath(relPath string) bool { | |
| 61 // filepath.Rel is documented to call filepath.Clean on its result befor e returning it, | |
| 62 // which results in "." for an empty relative path. | |
| 63 if relPath == "." { // Root directory. | |
| 64 return false | |
| 65 } | |
| 66 | |
| 67 for _, glob := range ff.blacklist { | |
| 68 if match(glob, relPath) || match(glob, filepath.Base(relPath)) { | |
| 69 return true | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 return false | |
| 74 } | |
| 75 | |
| 76 // match is equivalent to filepath.Match, but assumes that pattern is valid. | |
| 77 func match(pattern, name string) bool { | |
| 78 matched, _ := filepath.Match(pattern, name) | |
| 79 return matched | |
| 80 } | |
| OLD | NEW |