forked from willscott/memphis
/
billy.go
340 lines (297 loc) · 7.69 KB
/
billy.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package memphis
import (
"fmt"
"math/rand"
"os"
"path"
"strings"
"time"
"github.com/go-git/go-billy/v5"
)
// Billy wraps a filesystem subtree in the billy filesystem interface
type Billy struct {
euid uint32 // euid is the effective user ID that the billy view will assume
egid uint32 // egid is the effective group ID that the billy view will assume
root *Tree
}
// AsBillyFS provides a billy-comptatible view of the current memphis directory tree
func (t *Tree) AsBillyFS(euid, egid uint32) *Billy {
return &Billy{
euid,
egid,
t,
}
}
// Create makes a new empty file
func (b *Billy) Create(filename string) (billy.File, error) {
dir, name := path.Split(filename)
treeRef := b.root.WalkDir(strings.Split(dir, Separator))
if treeRef == nil {
return nil, ErrNotDir
}
if _, ok := treeRef.files[name]; ok {
return nil, ErrExists
}
f := treeRef.Create(name, b.euid, b.egid, 0666)
return &BillyFile{f, 0}, nil
}
// Open is a shortcut to openfile
func (b *Billy) Open(filename string) (billy.File, error) {
return b.OpenFile(filename, os.O_RDONLY, 0666)
}
// OpenFile opens a file for access
func (b *Billy) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
dir, name := path.Split(filename)
parent := b.root.WalkDir(strings.Split(dir, Separator))
if parent == nil {
return nil, os.ErrNotExist
}
if _, dok := parent.directories[name]; dok {
return nil, os.ErrExist
}
// If create mode - need parent but not file.
if (flag & os.O_CREATE) != 0 {
if _, fok := parent.files[name]; fok {
return nil, os.ErrExist
}
return b.Create(filename)
}
f, d, err := b.root.Get(strings.Split(filename, Separator), true)
if err != nil {
return nil, err
}
if d != nil {
return nil, os.ErrExist
}
return &BillyFile{f, 0}, nil
}
// Stat returns file metadata
func (b *Billy) Stat(filename string) (os.FileInfo, error) {
return b.getFileInfo(filename, false)
}
// Lstat provides symlink info
func (b *Billy) Lstat(filename string) (os.FileInfo, error) {
return b.getFileInfo(filename, false)
}
func (b *Billy) getFileInfo(filename string, followLinks bool) (os.FileInfo, error) {
dir, name := path.Split(filename)
parent := b.root.WalkDir(strings.Split(dir, Separator))
if parent == nil {
return nil, os.ErrNotExist
}
if f, ok := parent.files[name]; ok {
if ((f.Mode() & os.ModeSymlink) != 0) && followLinks {
// todo: maybe resolve circular links?
return b.getFileInfo(string(f.Bytes()), true)
}
return f, nil
}
if d := parent.WalkDir([]string{name}); d != nil {
return &DirMeta{name, d}, nil
}
return nil, os.ErrNotExist
}
// Rename a file
func (b *Billy) Rename(oldpath, newpath string) error {
oldDir, oldName := path.Split(oldpath)
oldParent := b.root.WalkDir(strings.Split(oldDir, Separator))
if oldParent == nil {
return os.ErrNotExist
}
newDir, newName := path.Split(newpath)
newParent := b.root.WalkDir(strings.Split(newDir, Separator))
if newParent == nil {
return os.ErrNotExist
}
if _, ok := newParent.files[newName]; ok {
return os.ErrExist
}
if _, ok := newParent.directories[newName]; ok {
return os.ErrExist
}
// TODO: permissions check.
if f, ok := oldParent.files[oldName]; ok {
// move file.
newParent.files[newName] = f
newParent.files[newName].name = newName
delete(oldParent.files, oldName)
return nil
} else if d, ok := oldParent.directories[oldName]; ok {
// move dir.
newParent.directories[newName] = d
delete(oldParent.directories, newName)
return nil
}
return os.ErrNotExist
}
// Remove deletes a file
func (b *Billy) Remove(filename string) error {
dir, name := path.Split(filename)
parent := b.root.WalkDir(strings.Split(dir, Separator))
if _, ok := parent.files[name]; ok {
// TODO: permissions.
delete(parent.files, name)
return nil
}
if d := parent.WalkDir([]string{name}); d != nil {
// TODO: permissions.
// Directory must be empty
if len(d.files) > 0 || len(d.directories) > 0 {
return os.ErrExist
}
delete(parent.directories, name)
return nil
}
return os.ErrNotExist
}
// Join constructs a path
func (b *Billy) Join(elem ...string) string {
return path.Join(elem...)
}
// TempFile create an empty tempfile
func (b *Billy) TempFile(dir, prefix string) (billy.File, error) {
d := b.root.WalkDir(strings.Split(dir, Separator))
if d == nil {
return nil, os.ErrNotExist
}
r := rand.Int()
n := fmt.Sprintf("%s%d", prefix, r)
return b.Create(path.Join(dir, n))
}
// ReadDir lists directory contents
func (b *Billy) ReadDir(path string) ([]os.FileInfo, error) {
d := b.root.WalkDir(strings.Split(path, Separator))
if d == nil {
return nil, os.ErrNotExist
}
items := make([]os.FileInfo, 0, len(d.directories)+len(d.files))
for name := range d.directories {
dir := d.WalkDir([]string{name})
items = append(items, &DirMeta{name, dir})
}
for _, file := range d.files {
items = append(items, file)
}
return items, nil
}
// MkdirAll creates a new directory
func (b *Billy) MkdirAll(filename string, perm os.FileMode) error {
parts := strings.Split(filename, Separator)
cur := b.root
for _, p := range parts {
// todo: permissions
if next := cur.WalkDir([]string{p}); next != nil {
cur = next
} else {
cur = cur.CreateDir(p, b.euid, b.egid, perm.Perm()|os.ModeDir)
}
}
return nil
}
// Symlink creates a symbolic link
func (b *Billy) Symlink(target, link string) error {
f, err := b.Create(link)
if err != nil {
return err
}
bf := f.(*BillyFile)
bf.Write([]byte(target))
bf.File.mode |= os.ModeSymlink
return nil
}
// Readlink returns symlink contents
func (b *Billy) Readlink(link string) (string, error) {
f, err := b.getFileInfo(link, false)
if err != nil {
return "", err
}
//todo: validate read permission.
ffile, ok := f.(*File)
if !ok {
return "", os.ErrExist
}
return string(ffile.Bytes()), nil
}
// Chmod changes file permissions
func (b *Billy) Chmod(name string, mode os.FileMode) error {
f, err := b.getFileInfo(name, false)
if err != nil {
return err
}
ffile, ok := f.(*File)
if !ok {
fdir, ok := f.(*DirMeta)
if !ok {
return os.ErrInvalid
}
fdir.Tree.mode = mode
return nil
}
ffile.mode = mode
return nil
}
// Lchown changes symlink ownership
func (b *Billy) Lchown(name string, uid, gid int) error {
return b.changeOwnership(name, uid, gid, false)
}
// Chown changes file ownership
func (b *Billy) Chown(name string, uid, gid int) error {
return b.changeOwnership(name, uid, gid, true)
}
func (b *Billy) changeOwnership(name string, uid, gid int, followLinks bool) error {
if b.euid != 0 && b.egid != 0 {
return os.ErrPermission
}
f, err := b.getFileInfo(name, followLinks)
if err != nil {
return err
}
ffile, ok := f.(*File)
if !ok {
fdir, ok := f.(*DirMeta)
if !ok {
return os.ErrInvalid
}
fdir.Tree.uid = uint32(uid)
fdir.Tree.gid = uint32(gid)
return nil
}
ffile.uid = uint32(uid)
ffile.gid = uint32(gid)
return nil
}
// Chtimes changes file access time
func (b *Billy) Chtimes(name string, atime time.Time, mtime time.Time) error {
f, err := b.getFileInfo(name, false)
if err != nil {
return err
}
ffile, ok := f.(*File)
if !ok {
fdir, ok := f.(*DirMeta)
if !ok {
return os.ErrInvalid
}
fdir.Tree.modTime = mtime
return nil
}
ffile.modTime = mtime
return nil
}
// Chroot returns a subtree of the filesystem
func (b *Billy) Chroot(path string) (billy.Filesystem, error) {
dir := b.root.WalkDir(strings.Split(path, Separator))
if dir == nil {
return nil, os.ErrNotExist
}
return &Billy{b.euid, b.egid, dir}, nil
}
// Root prints the path of the current fs root
func (b *Billy) Root() string {
return Separator
}
// Capabilities tells billy what this FS can do
func (b *Billy) Capabilities() billy.Capability {
return billy.AllCapabilities
}
var _ billy.Filesystem = (*Billy)(nil)