File: emptyvfs.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 (89 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download | duplicates (2)
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
// Copyright 2016 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"
	"os"
	"time"
)

// NewNameSpace returns a NameSpace pre-initialized with an empty
// emulated directory mounted on the root mount point "/". This
// allows directory traversal routines to work properly even if
// a folder is not explicitly mounted at root by the user.
func NewNameSpace() NameSpace {
	ns := NameSpace{}
	ns.Bind("/", &emptyVFS{}, "/", BindReplace)
	return ns
}

// type emptyVFS emulates a FileSystem consisting of an empty directory
type emptyVFS struct{}

// Open implements Opener. Since emptyVFS is an empty directory, all
// attempts to open a file should returns errors.
func (e *emptyVFS) Open(path string) (ReadSeekCloser, error) {
	if path == "/" {
		return nil, fmt.Errorf("open: / is a directory")
	}
	return nil, os.ErrNotExist
}

// Stat returns os.FileInfo for an empty directory if the path is
// is root "/" or error. os.FileInfo is implemented by emptyVFS
func (e *emptyVFS) Stat(path string) (os.FileInfo, error) {
	if path == "/" {
		return e, nil
	}
	return nil, os.ErrNotExist
}

func (e *emptyVFS) Lstat(path string) (os.FileInfo, error) {
	return e.Stat(path)
}

// ReadDir returns an empty os.FileInfo slice for "/", else error.
func (e *emptyVFS) ReadDir(path string) ([]os.FileInfo, error) {
	if path == "/" {
		return []os.FileInfo{}, nil
	}
	return nil, os.ErrNotExist
}

func (e *emptyVFS) String() string {
	return "emptyVFS(/)"
}

func (e *emptyVFS) RootType(path string) RootType {
	return ""
}

// These functions below implement os.FileInfo for the single
// empty emulated directory.

func (e *emptyVFS) Name() string {
	return "/"
}

func (e *emptyVFS) Size() int64 {
	return 0
}

func (e *emptyVFS) Mode() os.FileMode {
	return os.ModeDir | os.ModePerm
}

func (e *emptyVFS) ModTime() time.Time {
	return time.Time{}
}

func (e *emptyVFS) IsDir() bool {
	return true
}

func (e *emptyVFS) Sys() interface{} {
	return nil
}