1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 // We expose a new variable because otherwise we need to copy the findGOROOT logic again
18 // from cmd/godoc which is already copied twice from the standard library.
20 // GOROOT is the GOROOT path under which the godoc binary is running.
21 // It is needed to check whether a filesystem root is under GOROOT or not.
22 // This is set from cmd/godoc/main.go.
23 var GOROOT = runtime.GOROOT()
25 // OS returns an implementation of FileSystem reading from the
26 // tree rooted at root. Recording a root is convenient everywhere
27 // but necessary on Windows, because the slash-separated path
28 // passed to Open has no way to specify a drive letter. Using a root
29 // lets code refer to OS(`c:\`), OS(`d:\`) and so on.
30 func OS(root string) FileSystem {
38 return osFS{rootPath: root, rootType: t}
46 func isGoPath(path string) bool {
47 for _, bp := range filepath.SplitList(build.Default.GOPATH) {
48 for _, gp := range filepath.SplitList(path) {
57 func (root osFS) String() string { return "os(" + root.rootPath + ")" }
59 // RootType returns the root type for the filesystem.
61 // Note that we ignore the path argument because roottype is a property of
62 // this filesystem. But for other filesystems, the roottype might need to be
63 // dynamically deduced at call time.
64 func (root osFS) RootType(path string) RootType {
68 func (root osFS) resolve(path string) string {
69 // Clean the path so that it cannot possibly begin with ../.
70 // If it did, the result of filepath.Join would be outside the
71 // tree rooted at root. We probably won't ever see a path
72 // with .. in it, but be safe anyway.
73 path = pathpkg.Clean("/" + path)
75 return filepath.Join(root.rootPath, path)
78 func (root osFS) Open(path string) (ReadSeekCloser, error) {
79 f, err := os.Open(root.resolve(path))
90 return nil, fmt.Errorf("Open: %s is a directory", path)
95 func (root osFS) Lstat(path string) (os.FileInfo, error) {
96 return os.Lstat(root.resolve(path))
99 func (root osFS) Stat(path string) (os.FileInfo, error) {
100 return os.Stat(root.resolve(path))
103 func (root osFS) ReadDir(path string) ([]os.FileInfo, error) {
104 return ioutil.ReadDir(root.resolve(path)) // is sorted