File: os.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.5.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 16,592 kB
  • sloc: javascript: 2,011; asm: 1,635; sh: 192; yacc: 155; makefile: 52; ansic: 8
file content (120 lines) | stat: -rw-r--r-- 3,119 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package vfs

import (
	"fmt"
	"go/build"
	"io/ioutil"
	"os"
	pathpkg "path"
	"path/filepath"
	"runtime"
)

// We expose a new variable because otherwise we need to copy the findGOROOT logic again
// from cmd/godoc which is already copied twice from the standard library.

// GOROOT is the GOROOT path under which the godoc binary is running.
// It is needed to check whether a filesystem root is under GOROOT or not.
// This is set from cmd/godoc/main.go.
var GOROOT = runtime.GOROOT()

// OS returns an implementation of FileSystem reading from the
// tree rooted at root.  Recording a root is convenient everywhere
// but necessary on Windows, because the slash-separated path
// passed to Open has no way to specify a drive letter.  Using a root
// lets code refer to OS(`c:\`), OS(`d:\`) and so on.
func OS(root string) FileSystem {
	var t RootType
	switch {
	case root == GOROOT:
		t = RootTypeGoRoot
	case isGoPath(root):
		t = RootTypeGoPath
	}
	return osFS{rootPath: root, rootType: t}
}

type osFS struct {
	rootPath string
	rootType RootType
}

func isGoPath(path string) bool {
	for _, bp := range filepath.SplitList(build.Default.GOPATH) {
		for _, gp := range filepath.SplitList(path) {
			if bp == gp {
				return true
			}
		}
	}
	return false
}

func (root osFS) String() string { return "os(" + root.rootPath + ")" }

// RootType returns the root type for the filesystem.
//
// Note that we ignore the path argument because roottype is a property of
// this filesystem. But for other filesystems, the roottype might need to be
// dynamically deduced at call time.
func (root osFS) RootType(path string) RootType {
	return root.rootType
}

func (root osFS) resolve(path string) string {
	// Clean the path so that it cannot possibly begin with ../.
	// If it did, the result of filepath.Join would be outside the
	// tree rooted at root.  We probably won't ever see a path
	// with .. in it, but be safe anyway.
	path = pathpkg.Clean("/" + path)

	return filepath.Join(root.rootPath, path)
}

func (root osFS) Open(path string) (ReadSeekCloser, error) {
	f, err := os.Open(root.resolve(path))
	if err != nil {
		return nil, err
	}
	fi, err := f.Stat()
	if err != nil {
		f.Close()
		return nil, err
	}
	if fi.IsDir() {
		f.Close()
		return nil, fmt.Errorf("Open: %s is a directory", path)
	}
	return f, nil
}

func (root osFS) Lstat(path string) (os.FileInfo, error) {
	return os.Lstat(root.resolve(path))
}

func (root osFS) Stat(path string) (os.FileInfo, error) {
	return os.Stat(root.resolve(path))
}

func (root osFS) ReadDir(path string) ([]os.FileInfo, error) {
	dirName := root.resolve(path)
	fis, err := ioutil.ReadDir(dirName) // is sorted
	if err != nil {
		return nil, err
	}
	// Replace symlinks with what they are pointing to
	for i, fi := range fis {
		if fi.Mode()&os.ModeSymlink != 0 {
			fi, err = os.Stat(filepath.Join(dirName, fi.Name()))
			if err != nil {
				return nil, err
			}
		}
		fis[i] = fi
	}
	return fis, nil
}