| 66 | } |
| 67 | |
| 68 | func newBasicFilesystem(root string, opts ...Option) *BasicFilesystem { |
| 69 | if root == "" { |
| 70 | root = "." // Otherwise "" becomes "/" below |
| 71 | } |
| 72 | |
| 73 | // The reason it's done like this: |
| 74 | // C: -> C:\ -> C:\ (issue that this is trying to fix) |
| 75 | // C:\somedir -> C:\somedir\ -> C:\somedir |
| 76 | // C:\somedir\ -> C:\somedir\ -> C:\somedir |
| 77 | // This way in the tests, we get away without OS specific separators |
| 78 | // in the test configs. |
| 79 | sep := string(filepath.Separator) |
| 80 | root = filepath.Clean(filepath.Dir(root + sep)) |
| 81 | |
| 82 | // Attempt tilde expansion; leave unchanged in case of error |
| 83 | if path, err := ExpandTilde(root); err == nil { |
| 84 | root = path |
| 85 | } |
| 86 | |
| 87 | // Attempt absolutification; leave unchanged in case of error |
| 88 | if !filepath.IsAbs(root) { |
| 89 | // Abs() looks like a fairly expensive syscall on Windows, while |
| 90 | // IsAbs() is a whole bunch of string mangling. I think IsAbs() may be |
| 91 | // somewhat faster in the general case, hence the outer if... |
| 92 | if path, err := filepath.Abs(root); err == nil { |
| 93 | root = path |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Attempt to enable long filename support on Windows. We may still not |
| 98 | // have an absolute path here if the previous steps failed. |
| 99 | if build.IsWindows { |
| 100 | root = longFilenameSupport(root) |
| 101 | } |
| 102 | |
| 103 | fs := &BasicFilesystem{ |
| 104 | root: root, |
| 105 | options: opts, |
| 106 | userCache: newValueCache(time.Hour, user.LookupId), |
| 107 | groupCache: newValueCache(time.Hour, user.LookupGroupId), |
| 108 | } |
| 109 | for _, opt := range opts { |
| 110 | opt.apply(fs) |
| 111 | } |
| 112 | return fs |
| 113 | } |
| 114 | |
| 115 | // rooted expands the relative path to the full path that is then used with os |
| 116 | // package. If the relative path somehow causes the final path to escape the root |