/
sillyfiles.go
341 lines (309 loc) · 8.38 KB
/
sillyfiles.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
341
package main
import (
"fmt"
"os"
"syscall"
"bitbucket.org/taruti/go-extra/fuse"
)
type FileSystem struct {
maxino uint64
nodes map[uint64]*Inode
}
func NewFS() *FileSystem {
x := FileSystem{0, make(map[uint64]*Inode)}
x.MkDir(1, ".") // Make root directory...
return &x
}
func (fs *FileSystem) MkDir(parent uint64, name string) *Inode {
if parent != 1 {
// Let's check that the parent actually exists...
if _,ok := fs.nodes[parent]; !ok {
panic("yikes, the parent doesn't exist!")
}
}
var a = new(Inode);
a.Ino = fs.maxino + 1
fs.maxino = a.Ino
a.Mode = fuse.S_IFDIR + 0755
a.Uid = 1137
a.Gid = 1137
a.Nlink = 1
sec, nsec, _ := os.Time()
a.Mtime = uint64(sec) - 3600
a.Mtimensec = uint32(nsec)
a.Ctime = uint64(sec) - 3600
a.Ctimensec = uint32(nsec)
a.DirContents = make(map[string]uint64)
// Create . and .. entries here
//a.DirContents["."] = a.Ino
//a.DirContents[".."] = parent
fs.nodes[a.Ino] = a;
// Add entry to parent directoory
if name != "." {
fs.nodes[parent].DirContents[name] = a.Ino
}
return a
}
func (fs *FileSystem) Create(parent uint64, name string) *Inode {
if _,ok := fs.nodes[parent]; !ok {
panic("yikes, the parent doesn't exist!")
}
var a = new(Inode);
a.Ino = fs.maxino + 1
fs.maxino = a.Ino
a.Mode = fuse.S_IFREG + 0644
a.Uid = 1137
a.Gid = 1137
a.Nlink = 1
sec, nsec, _ := os.Time()
a.Mtime = uint64(sec) - 3600
a.Mtimensec = uint32(nsec)
a.Ctime = uint64(sec) - 3600
a.Ctimensec = uint32(nsec)
a.FileContents = make([]byte, 0, 256)
fs.nodes[a.Ino] = a;
fs.nodes[parent].DirContents[name] = a.Ino
fs.Log("Parent", parent, "contains", fs.nodes[parent].DirContents)
return a
}
func (fs *FileSystem) Delete(parent uint64, name string) fuse.Error {
if _,ok := fs.nodes[parent]; !ok {
panic("yikes, the parent doesn't exist!")
}
if ino, ok := fs.nodes[parent].DirContents[name]; ok {
if len(fs.nodes[ino].DirContents) > 0 {
return syscall.ENOTEMPTY // is this right?
}
fs.nodes[parent].DirContents[name] = ino, false // delete entry
fs.nodes[ino].Nlink -= 1 // uncount this entry
if fs.nodes[ino].Nlink <= 0 {
// Trash entry when last link is removed
fs.nodes[ino] = fs.nodes[ino], false
}
return fuse.OK
}
return fuse.ENOENT
}
func (_ *FileSystem) Log(v ...interface{}) {
fmt.Println(v)
}
func (fs *FileSystem) Dump(s string) {
fmt.Println(s, "with maxino", fs.maxino)
for ino, v := range fs.nodes {
fmt.Println(ino, *v)
}
}
func (fs *FileSystem) FuseLookup(h *fuse.Header, p []byte) (*fuse.Attr, fuse.Error) {
fs.Log("FuseLookup", h.Nodeid, p)
if par,ok := fs.nodes[h.Nodeid]; ok {
if inum,ok := par.DirContents[string(p)]; ok {
if i,ok := fs.nodes[inum]; ok {
return i.ToAttr(), fuse.OK
}
}
}
return nil, fuse.ENOENT
}
func (fs *FileSystem) FuseDelete(h *fuse.Header, name []byte) fuse.Error {
fs.Log("FuseDelete", h.Nodeid, name)
return fs.Delete(h.Nodeid, string(name))
}
func (fs *FileSystem) FuseForget(f *fuse.Forget) {
fs.Log("FuseForget", f)
}
func (fs *FileSystem) FuseGetAttr(p *fuse.GetAttr) (*fuse.Attr, fuse.Error) {
fs.Log("FuseGetAttr", p, "with Nodeid", p.Nodeid)
if i,ok := fs.nodes[p.Nodeid]; ok {
return i.ToAttr(), fuse.OK
}
return nil, fuse.ENOENT
}
func (fs *FileSystem) FuseMkDir(m *fuse.MkDir, p []byte) (*fuse.Attr, fuse.Error) {
fs.Log("FuseMkDir", m, p)
inew := fs.MkDir(m.Nodeid, string(p))
inew.Uid, inew.Gid = uint32(m.Uid), uint32(m.Gid);
fs.Dump("FuseMkDir")
return inew.ToAttr(), fuse.OK
}
func (fs *FileSystem) FuseCreate(c *fuse.Create, p []byte) (flags fuse.Flags, fh fuse.FuseFile, a *fuse.Attr, e fuse.Error) {
fs.Log("FuseCreate", c)
if c.Flags & os.O_WRONLY != 0 {
fs.Log("O_WRONLY");
} else if c.Flags & os.O_RDONLY != 0 {
fs.Log("O_RDONLY");
} else if c.Flags & os.O_RDWR != 0 {
fs.Log("O_RDWR");
}
if c.Flags & os.O_NONBLOCK != 0 {
fs.Log("O_NONBLOCK");
}
if c.Flags & os.O_EXCL != 0 {
fs.Log("O_EXCL");
}
// O_CREAT is always present in FuseCreate...
inew := fs.Create(c.Nodeid, string(p))
inew.Mode = fuse.S_IFREG | c.Mode
inew.HandleFlags(c.Flags) // Handle O_TRUNC
inew.Uid, inew.Gid = uint32(c.Uid), uint32(c.Gid);
return c.Flags, &OpenInode{inew,0}, inew.ToAttr(), fuse.OK
}
func (fs *FileSystem) FuseOpen(o *fuse.Open) (flags fuse.Flags, fh fuse.FuseFile, e fuse.Error) {
fs.Log("FuseOpen", o)
flags = o.Flags
fs.Log("open flags are", o.Flags)
if o.Flags & os.O_WRONLY != 0 {
fs.Log("O_WRONLY");
} else if o.Flags & os.O_RDONLY != 0 {
fs.Log("O_RDONLY");
} else if o.Flags & os.O_RDWR != 0 {
fs.Log("O_RDWR");
}
if o.Flags & os.O_NONBLOCK != 0 {
fs.Log("O_NONBLOCK");
}
if o.Flags & os.O_EXCL != 0 {
fs.Log("O_EXCL");
}
// O_CREAT isn't a possibility in FuseOpen...
if i,ok := fs.nodes[o.Nodeid]; ok {
i.HandleFlags(o.Flags) // handle O_TRUNC
if o.Flags & os.O_APPEND != 0 {
fs.Log("O_APPEND");
i.HandleFlags(o.Flags) // handle O_TRUNC
fh := &OpenInode { i, -1 }
return flags, fh, fuse.OK
} else {
return flags, &OpenInode { i, 0 }, fuse.OK
}
}
return 0, nil, fuse.ENOENT
}
func (fs *FileSystem) FuseGetXAttrSize(h *fuse.Header, p []byte) (int, fuse.Error) {
fs.Log("FuseGetXAttrSize", h, p)
return 0, fuse.OK
}
func (fs *FileSystem) FuseGetXAttr(h *fuse.Header, p []byte) ([]byte, fuse.Error) {
fs.Log("FuseGetXAttr", h, p)
return nil, fuse.OK
}
func (fs *FileSystem) FuseListXAttr(h *fuse.Header, l *fuse.XAttrList) fuse.Error {
fs.Log("FuseListXAttr", h, l)
return fuse.OK
}
func (fs *FileSystem) FuseInit(i *fuse.Init) fuse.Error {
fs.Log("FuseInit", i)
return fuse.OK
}
func (fs *FileSystem) FuseAccess(a *fuse.Access) fuse.Error {
fs.Log("FuseAccess", a)
return fuse.OK
}
func (fs *FileSystem) NilFile() fuse.FuseFile {
fs.Log("NilFile")
var i Inode
return &OpenInode{&i,0}
}
type Inode struct {
Ino uint64
Atime, Mtime, Ctime uint64
Atimensec, Mtimensec, Ctimensec uint32
Mode uint32
Nlink uint32
Uid, Gid uint32
Nreading, Nwriting uint32
FileContents []byte
DirContents map[string]uint64
}
type OpenInode struct {
Ino *Inode
Location int
}
func (i *Inode) Log(v ...interface{}) {
fmt.Println("inode", i.Ino, ":", v)
}
func (i *OpenInode) Log(v ...interface{}) {
fmt.Println("inode", i.Ino.Ino, ":", v)
}
func (i *Inode) IsDir() bool {
return (i.Mode & (fuse.S_IFDIR | fuse.S_IFREG)) == fuse.S_IFDIR
}
func (i *Inode) HandleFlags(f fuse.Flags) {
// Here we do things like O_TRUNC
if f & os.O_TRUNC != 0 {
i.Log("O_TRUNC");
i.FileContents = i.FileContents[0:0] // truncate file!
}
}
func (i *OpenInode) FuseRead(io *fuse.Io) ([]byte, fuse.Error) {
i.Log("FuseRead", io) // the following is wrong...
i.Log("offset is", io.Offset)
i.Log("size is", io.Size)
if i.Ino.IsDir() {
de := new(fuse.DEntryList)
var num uint64 = 0
for path,ino := range i.Ino.DirContents {
i.Log("::: ",path,ino)
if num >= io.Offset {
de.AddString(path, ino, fuse.S_IFDIR)
num += 1
}
}
return de.Bytes(), fuse.OK
}
return i.Ino.FileContents[i.Location:], fuse.OK
}
func (oi *OpenInode) FuseWrite(io *fuse.Io, d []byte) (uint64, fuse.Error) {
oi.Log("FuseWrite", io, string(d))
oi.Log("io.Offset as int is ", int(io.Offset))
oi.Log("io.Offset is ", io.Offset)
oi.Log("io itself is ", io)
start := oi.Location + int(io.Offset)
if oi.Location < 0 {
start = len(oi.Ino.FileContents)
}
stop := start + len(d)
if cap(oi.Ino.FileContents) < stop {
oi.Log("Expanding FileContents...")
fc := make([]byte, stop, 2*stop)
for i,v := range oi.Ino.FileContents {
fc[i] = v
}
oi.Ino.FileContents = fc
} else {
if len(oi.Ino.FileContents) < stop {
oi.Ino.FileContents = oi.Ino.FileContents[0:stop]
}
}
for i,v := range d {
oi.Ino.FileContents[start + i] = v
}
oi.Log("FileContents is now: ", string(oi.Ino.FileContents))
if oi.Location >= 0 {
oi.Location += len(d)
}
return uint64(len(d)), fuse.OK
}
func (i *OpenInode) FuseRelease(r *fuse.Release) fuse.Error {
i.Log("FuseRelease", r)
if i.Ino.Nreading > 0 { i.Ino.Nreading -= 1 }
if i.Ino.Nwriting > 0 { i.Ino.Nwriting -= 1 }
if !i.Ino.IsDir() {
i.Log("File contents:", string(i.Ino.FileContents))
}
return fuse.OK
}
func (i *OpenInode) FuseFlush(f *fuse.Flush) fuse.Error {
i.Log("FuseFlush", f)
return fuse.OK
}
func (i *Inode) ToAttr() *fuse.Attr {
var a fuse.Attr
a.Ino = i.Ino
a.Size = uint64(len(i.FileContents))
a.Atime, a.Mtime, a.Ctime = i.Atime, i.Mtime, i.Ctime
a.Atimensec, a.Mtimensec, a.Ctimensec = i.Atimensec, i.Mtimensec, i.Ctimensec
a.Mode = i.Mode
a.Nlink = i.Nlink
a.Uid, a.Gid = i.Uid, i.Gid
return &a
}