openSourceFile opens a source file from a name encoded in a profile. File names in a profile after can be relative paths, so search them in each of the paths in searchPath and their parents. In case the profile contains absolute paths, additional paths may be configured to trim from the source paths
(path, searchPath, trim string)
| 1004 | // paths in the profile. This effectively turns the path into a relative path |
| 1005 | // searching it using searchPath as usual). |
| 1006 | func openSourceFile(path, searchPath, trim string) (*os.File, error) { |
| 1007 | path = trimPath(path, trim, searchPath) |
| 1008 | // If file is still absolute, require file to exist. |
| 1009 | if filepath.IsAbs(path) { |
| 1010 | f, err := os.Open(path) |
| 1011 | return f, err |
| 1012 | } |
| 1013 | // Scan each component of the path. |
| 1014 | for _, dir := range filepath.SplitList(searchPath) { |
| 1015 | // Search up for every parent of each possible path. |
| 1016 | for { |
| 1017 | filename := filepath.Join(dir, path) |
| 1018 | if f, err := os.Open(filename); err == nil { |
| 1019 | return f, nil |
| 1020 | } |
| 1021 | parent := filepath.Dir(dir) |
| 1022 | if parent == dir { |
| 1023 | break |
| 1024 | } |
| 1025 | dir = parent |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | return nil, fmt.Errorf("could not find file %s on path %s", path, searchPath) |
| 1030 | } |
| 1031 | |
| 1032 | // trimPath cleans up a path by removing prefixes that are commonly |
| 1033 | // found on profiles plus configured prefixes. |
searching dependent graphs…