GoPLS Viewer

Home|gopls/godoc/vfs/os.go
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
5package vfs
6
7import (
8    "fmt"
9    "go/build"
10    "io/ioutil"
11    "os"
12    pathpkg "path"
13    "path/filepath"
14    "runtime"
15)
16
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
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.
23var GOROOT = runtime.GOROOT()
24
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.
30func OS(root stringFileSystem {
31    var t RootType
32    switch {
33    case root == GOROOT:
34        t = RootTypeGoRoot
35    case isGoPath(root):
36        t = RootTypeGoPath
37    }
38    return osFS{rootPathrootrootTypet}
39}
40
41type osFS struct {
42    rootPath string
43    rootType RootType
44}
45
46func isGoPath(path stringbool {
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
57func (root osFSString() 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.
64func (root osFSRootType(path stringRootType {
65    return root.rootType
66}
67
68func (root osFSresolve(path stringstring {
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
75    return filepath.Join(root.rootPathpath)
76}
77
78func (root osFSOpen(path string) (ReadSeekClosererror) {
79    ferr := os.Open(root.resolve(path))
80    if err != nil {
81        return nilerr
82    }
83    fierr := f.Stat()
84    if err != nil {
85        f.Close()
86        return nilerr
87    }
88    if fi.IsDir() {
89        f.Close()
90        return nilfmt.Errorf("Open: %s is a directory"path)
91    }
92    return fnil
93}
94
95func (root osFSLstat(path string) (os.FileInfoerror) {
96    return os.Lstat(root.resolve(path))
97}
98
99func (root osFSStat(path string) (os.FileInfoerror) {
100    return os.Stat(root.resolve(path))
101}
102
103func (root osFSReadDir(path string) ([]os.FileInfoerror) {
104    return ioutil.ReadDir(root.resolve(path)) // is sorted
105}
106
MembersX
isGoPath.RangeStmt_1251.bp
osFS.resolve.path
osFS.Open.f
osFS.Stat
osFS.Stat.path
isGoPath.path
OS.root
OS.t
osFS.RootType.root
osFS.Open.path
osFS.ReadDir.root
osFS.ReadDir
build
OS
isGoPath
runtime
osFS.rootPath
osFS.String
osFS.RootType
osFS.Lstat.root
osFS
osFS.Open.fi
osFS.Lstat.path
osFS.ReadDir.path
osFS.resolve
osFS.String.root
osFS.Open
osFS.Lstat
osFS.rootType
osFS.Stat.root
osFS.Open.err
filepath
isGoPath.RangeStmt_1251.BlockStmt.RangeStmt_1315.gp
osFS.RootType.path
osFS.resolve.root
osFS.Open.root
ioutil
Members
X