-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfs.go
191 lines (155 loc) · 3.91 KB
/
fs.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
package main
import (
"io"
"os"
"time"
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/fuseutil"
"golang.org/x/net/context"
)
// Entry is an entry for a file or dir in the file system.
type Entry struct {
Attr fuseops.InodeAttributes
Dir *Dir
File *File
}
// FakeDataFS is a filesystem filled with fake data.
type FakeDataFS struct {
Seed int64
MaxSize int
FilesPerDir int
entries map[fuseops.InodeID]Entry
cache *Cache
fuseutil.NotImplementedFileSystem
}
// NewFakeDataFS creates a new filesystem.
func NewFakeDataFS(ctx context.Context, seed int64, maxSize int, filesPerDir int) (fs *FakeDataFS, err error) {
fs = &FakeDataFS{
Seed: seed,
MaxSize: maxSize,
FilesPerDir: filesPerDir,
cache: newCache(ctx),
entries: make(map[fuseops.InodeID]Entry),
}
V("create filesystem with seed %v, max size %v, %v files per dir\n", seed, maxSize, filesPerDir)
d, err := NewDir(fs, seed, "/", filesPerDir, maxSize), nil
if err != nil {
return nil, err
}
fs.entries[fuseops.RootInodeID] = Entry{
Dir: d,
Attr: fuseops.InodeAttributes{
Atime: time.Now(),
Ctime: time.Now(),
Mtime: time.Now(),
Crtime: time.Now(),
Uid: uint32(os.Getuid()),
Gid: uint32(os.Getgid()),
Mode: os.ModeDir | 0555,
},
}
return fs, nil
}
var rootAttributes = fuseops.InodeAttributes{
Atime: time.Now(),
Ctime: time.Now(),
Mtime: time.Now(),
Crtime: time.Now(),
Uid: uint32(os.Getuid()),
Gid: uint32(os.Getgid()),
Mode: os.ModeDir | 0555,
}
// GetInodeAttributes returns information about an inode.
func (f *FakeDataFS) GetInodeAttributes(ctx context.Context, op *fuseops.GetInodeAttributesOp) error {
entry, ok := f.entries[op.Inode]
if !ok {
return fuse.ENOENT
}
op.Attributes = entry.Attr
return nil
}
// OpenDir opens a directory.
func (f *FakeDataFS) OpenDir(ctx context.Context, op *fuseops.OpenDirOp) error {
return nil
}
// ReleaseDirHandle frees a handle returned by OpenDir.
func (f *FakeDataFS) ReleaseDirHandle(context.Context, *fuseops.ReleaseDirHandleOp) error {
return nil
}
// ForgetInode frees an inode.
func (f *FakeDataFS) ForgetInode(context.Context, *fuseops.ForgetInodeOp) error {
return nil
}
// ReadDir lists a directory.
func (f *FakeDataFS) ReadDir(ctx context.Context, op *fuseops.ReadDirOp) error {
entry, ok := f.entries[op.Inode]
if !ok {
return fuse.ENOENT
}
if entry.Dir == nil {
return fuse.EIO
}
if int(op.Offset) > len(entry.Dir.entries) {
return fuse.EIO
}
op.BytesRead = entry.Dir.ReadDir(op.Dst, int(op.Offset))
return nil
}
// StatFS apparently necessary and probably allows statfs to return
func (f *FakeDataFS) StatFS(
ctx context.Context,
op *fuseops.StatFSOp) (err error) {
return
}
// OpenFile determines if a file can be opened
func (f *FakeDataFS) OpenFile(
ctx context.Context,
op *fuseops.OpenFileOp) (err error) {
// Allow opening any file.
return
}
// LookUpInode returns information on an inode.
func (f *FakeDataFS) LookUpInode(ctx context.Context, op *fuseops.LookUpInodeOp) error {
entry, ok := f.entries[op.Parent]
if !ok {
return fuse.ENOENT
}
if entry.Dir == nil {
return fuse.EIO
}
d := entry.Dir
for _, entry := range d.entries {
if op.Name == entry.Name {
fsEntry, ok := f.entries[entry.Inode]
if !ok {
return fuse.EIO
}
op.Entry.Child = entry.Inode
op.Entry.Attributes = fsEntry.Attr
}
}
return nil
}
// ReadFile reads data from a file.
func (f *FakeDataFS) ReadFile(ctx context.Context, op *fuseops.ReadFileOp) error {
entry, ok := f.entries[op.Inode]
if !ok {
return fuse.ENOENT
}
if entry.File == nil {
return fuse.EIO
}
rd, err := f.cache.Get(op.Inode, op.Offset)
if err != nil {
rd = ContinuousFileReader(entry.File, op.Offset)
}
n, err := io.ReadFull(rd, op.Dst)
if err == io.ErrUnexpectedEOF {
err = nil
} else {
f.cache.Put(op.Inode, op.Offset+int64(n), rd)
}
op.BytesRead = len(op.Dst)
return err
}