This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onefile.go
89 lines (78 loc) · 2.03 KB
/
onefile.go
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
package vfs
import (
"os"
pathpkg "path"
"github.com/pkg/errors"
)
// OneFile contains a link to a single OS file at the root of the VFS. The
// first argument is the full path to the local file, the second argument must
// be a single file name without any directories or leading slash.
func OneFile(path, newname string) FileSystem {
return oneFileFileSystem{
path: path,
name: newname,
}
}
func SafeOneFile(path, newname string) FileSystemFunc {
return func() (FileSystem, error) {
// todo , what is rules for newname?
// if strings.HasPrefix(new, "/") {
// return nil, errors.Errorf("mount paths may not contain a leading '/': %s", new)
// }
fi, err := os.Stat(path)
if err != nil {
return nil, errors.Wrapf(err, "%s is not a readable path", path)
}
if fi.IsDir() {
return nil, errors.Wrapf(err, "%s is a directory, not a file", path)
}
return oneFileFileSystem{
path: path,
name: newname,
}, nil
}
}
type oneFileFileSystem struct {
path string
name string
}
func (fs oneFileFileSystem) String() string {
return "onefile(" + fs.path + ":" + fs.name + ")"
}
func (fs oneFileFileSystem) Open(path string) (ReadSeekCloser, error) {
if path != pathpkg.Clean("/"+fs.name) {
return nil, os.ErrNotExist
}
return os.Open(fs.path)
}
func (fs oneFileFileSystem) Lstat(path string) (os.FileInfo, error) {
if path == "/" {
return dirInfo("/"), nil
}
if path != pathpkg.Clean("/"+fs.name) {
return nil, os.ErrNotExist
}
fi, err := os.Lstat(fs.path)
return osPathFI{fi, fs.path}, err
}
func (fs oneFileFileSystem) Stat(path string) (os.FileInfo, error) {
if path == "/" {
return dirInfo("/"), nil
}
if path != pathpkg.Clean("/"+fs.name) {
return nil, os.ErrNotExist
}
fi, err := os.Stat(fs.path)
return osPathFI{fi, fs.path}, err
}
func (fs oneFileFileSystem) ReadDir(path string) ([]os.FileInfo, error) {
if path == "/" {
fi, err := os.Stat(fs.path)
if err != nil {
return nil, err
}
rfi := renamedFileInfo(fi, fs.name)
return []os.FileInfo{rfi}, nil
}
return nil, os.ErrNotExist
}