Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 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. |
| 4 | |
| 5 | package vfs |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
Agniva De Sarker | 16d1af8 | 2018-01-28 19:53:55 +0530 | [diff] [blame] | 9 | "go/build" |
Peter Weinberger | 365db56 | 2023-09-13 12:37:49 -0400 | [diff] [blame] | 10 | "io/ioutil" |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 11 | "os" |
| 12 | pathpkg "path" |
| 13 | "path/filepath" |
Agniva De Sarker | 16d1af8 | 2018-01-28 19:53:55 +0530 | [diff] [blame] | 14 | "runtime" |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 15 | ) |
| 16 | |
Agniva De Sarker | 6c1c5e9 | 2018-08-24 22:48:03 +0530 | [diff] [blame] | 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. |
| 19 | |
Agniva De Sarker | b776bcb | 2018-08-25 10:22:28 +0530 | [diff] [blame] | 20 | // GOROOT is the GOROOT path under which the godoc binary is running. |
Agniva De Sarker | ba93f94 | 2018-08-23 10:54:26 +0530 | [diff] [blame] | 21 | // It is needed to check whether a filesystem root is under GOROOT or not. |
Agniva De Sarker | b776bcb | 2018-08-25 10:22:28 +0530 | [diff] [blame] | 22 | // This is set from cmd/godoc/main.go. |
Agniva De Sarker | ba93f94 | 2018-08-23 10:54:26 +0530 | [diff] [blame] | 23 | var GOROOT = runtime.GOROOT() |
| 24 | |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 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 { |
Agniva De Sarker | 16d1af8 | 2018-01-28 19:53:55 +0530 | [diff] [blame] | 31 | var t RootType |
| 32 | switch { |
Agniva De Sarker | ba93f94 | 2018-08-23 10:54:26 +0530 | [diff] [blame] | 33 | case root == GOROOT: |
Agniva De Sarker | 16d1af8 | 2018-01-28 19:53:55 +0530 | [diff] [blame] | 34 | t = RootTypeGoRoot |
| 35 | case isGoPath(root): |
| 36 | t = RootTypeGoPath |
Agniva De Sarker | 16d1af8 | 2018-01-28 19:53:55 +0530 | [diff] [blame] | 37 | } |
| 38 | return osFS{rootPath: root, rootType: t} |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 39 | } |
| 40 | |
Agniva De Sarker | 16d1af8 | 2018-01-28 19:53:55 +0530 | [diff] [blame] | 41 | type osFS struct { |
| 42 | rootPath string |
| 43 | rootType RootType |
| 44 | } |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 45 | |
Agniva De Sarker | 16d1af8 | 2018-01-28 19:53:55 +0530 | [diff] [blame] | 46 | func isGoPath(path string) bool { |
| 47 | for _, bp := range filepath.SplitList(build.Default.GOPATH) { |
| 48 | for _, gp := range filepath.SplitList(path) { |
| 49 | if bp == gp { |
| 50 | return true |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | return false |
| 55 | } |
| 56 | |
| 57 | func (root osFS) String() string { return "os(" + root.rootPath + ")" } |
| 58 | |
| 59 | // RootType returns the root type for the filesystem. |
| 60 | // |
| 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 { |
| 65 | return root.rootType |
| 66 | } |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 67 | |
| 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) |
| 74 | |
Agniva De Sarker | 16d1af8 | 2018-01-28 19:53:55 +0530 | [diff] [blame] | 75 | return filepath.Join(root.rootPath, path) |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func (root osFS) Open(path string) (ReadSeekCloser, error) { |
| 79 | f, err := os.Open(root.resolve(path)) |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | fi, err := f.Stat() |
| 84 | if err != nil { |
Gustav Paul | 68353d2 | 2015-05-31 20:34:06 +0200 | [diff] [blame] | 85 | f.Close() |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 86 | return nil, err |
| 87 | } |
| 88 | if fi.IsDir() { |
Gustav Paul | 68353d2 | 2015-05-31 20:34:06 +0200 | [diff] [blame] | 89 | f.Close() |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 90 | return nil, fmt.Errorf("Open: %s is a directory", path) |
| 91 | } |
| 92 | return f, nil |
| 93 | } |
| 94 | |
| 95 | func (root osFS) Lstat(path string) (os.FileInfo, error) { |
| 96 | return os.Lstat(root.resolve(path)) |
| 97 | } |
| 98 | |
| 99 | func (root osFS) Stat(path string) (os.FileInfo, error) { |
Ian Lance Taylor | f03b335 | 2017-08-07 16:04:23 -0700 | [diff] [blame] | 100 | return os.Stat(root.resolve(path)) |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | func (root osFS) ReadDir(path string) ([]os.FileInfo, error) { |
Peter Weinberger | 365db56 | 2023-09-13 12:37:49 -0400 | [diff] [blame] | 104 | return ioutil.ReadDir(root.resolve(path)) // is sorted |
Andrew Gerrand | 5ff0687 | 2013-07-17 15:02:27 +1000 | [diff] [blame] | 105 | } |