-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory.go
122 lines (95 loc) · 2.06 KB
/
directory.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
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
121
122
package local
import (
"io/fs"
"os"
"go.sancus.dev/cms/os/types"
"go.sancus.dev/cms/os/utils"
)
const (
NewDirectoryMode = os.FileMode(0755)
)
type Directory struct {
fs.FileInfo
path string // relative to root
actual string // relative to server
}
func (d Directory) Name() string {
if d.path == "/" {
return "/"
} else {
return d.FileInfo.Name()
}
}
func (d Directory) Path() string {
return d.path
}
func (d Directory) Size() int64 {
return 0
}
func (d Directory) Type() fs.FileMode {
return d.Mode()
}
func (d *Directory) Sys() interface{} {
return d
}
func (d *Directory) Info() (fs.FileInfo, error) {
return d, nil
}
func chdir(path string, actual string, mkdir bool) (types.Directory, error) {
if mkdir {
var mode = NewDirectoryMode
if err := os.MkdirAll(actual, mode); err != nil {
return nil, err
}
}
if fi, err := os.Stat(actual); err != nil {
return nil, err
} else if !fi.IsDir() {
return nil, os.ErrNotExist
} else {
next := &Directory{
FileInfo: fi,
path: path,
actual: actual,
}
return next, nil
}
}
func (d Directory) chdir(path string, mkdir bool) (types.Directory, error) {
// Allow Relative paths, within the same root
s0, err := utils.Join(d.path, path)
if err != nil {
return nil, err
}
s1, err := utils.Join(d.actual, path)
if err != nil {
return nil, err
}
return chdir(s0, s1, mkdir)
}
func (fs *Filesystem) chdir(path string, mkdir bool) (types.Directory, error) {
// validate path
s0, err := utils.Join(path)
if err != nil {
return nil, err
}
fs.mu.Lock()
defer fs.mu.Unlock()
s1, err := utils.Join(fs.root, s0)
if err != nil {
return nil, err
}
return chdir(s0, s1, mkdir)
}
func (d Directory) Chdir(path string) (types.Directory, error) {
return d.chdir(path, false)
}
func (d Directory) MkdirAll(path string) (types.Directory, error) {
return d.chdir(path, true)
}
func (fs *Filesystem) Chdir(path string) (types.Directory, error) {
return fs.chdir(path, false)
}
func (fs *Filesystem) MkdirAll(path string) (types.Directory, error) {
return fs.chdir(path, true)
}