-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
jazz.go
369 lines (333 loc) · 8.02 KB
/
jazz.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package jazz
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"sync"
"syscall/js"
"time"
"tractor.dev/toolkit-go/engine/fs/watchfs"
"tractor.dev/wanix/internal/cache"
)
func fsUtil(name string, args ...any) js.Value {
v := js.Global().Get("jazz").Get("fsutil").Get(name).Invoke(args...)
if js.Global().Get("jazz").Get("fsutil").Get("isPromise").Invoke(v).Bool() {
done := make(chan js.Value, 1)
v.Call("then", js.FuncOf(func(this js.Value, args []js.Value) any {
done <- args[0]
return nil
}))
return <-done
} else {
return v
}
}
type jazzfs struct {
cache *cache.C // speed up slow, frequent stat
}
func NewJazzFs() fs.FS {
return &jazzfs{
cache: cache.New(2 * time.Second),
}
}
func (fs *jazzfs) Create(name string) (fs.File, error) {
return fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
}
func (fs *jazzfs) Open(name string) (fs.File, error) {
return fs.OpenFile(name, os.O_RDONLY, 0)
}
func (fs *jazzfs) OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error) {
fmt.Println("open:", name)
v := fsUtil("walk", name)
if v.IsNull() {
if flag&os.O_CREATE == 0 {
return nil, os.ErrNotExist
}
// if strings.Contains(name, "/") {
// fs.MkdirAll(filepath.Dir(name), 0755)
// }
fsUtil("writeFile", name, "")
v = fsUtil("walk", name)
if v.IsNull() {
fmt.Println("open: unable to find created file")
return nil, os.ErrNotExist
}
}
f := &jazzfile{node: v, path: name, fs: fs}
if flag&os.O_APPEND > 0 {
f.Seek(0, 2)
}
if flag&os.O_TRUNC > 0 {
f.Seek(0, 0)
}
return f, nil
}
func (fs *jazzfs) Mkdir(name string, perm fs.FileMode) error {
ret := fsUtil("mkdir", name)
if ret.IsNull() {
return os.ErrNotExist
}
return nil
}
func (fs *jazzfs) MkdirAll(path string, perm fs.FileMode) error {
fsUtil("mkdirAll", path)
return nil
}
// should fail if file does not exist
func (fs *jazzfs) Remove(name string) error {
ret := fsUtil("remove", name)
if ret.IsNull() {
return os.ErrNotExist
}
return nil
}
// does not fail if the path does not exist (return nil).
func (fs *jazzfs) RemoveAll(path string) error {
// TODO: this does not remove children
fsUtil("remove", path)
return nil
}
func (fs *jazzfs) Rename(oldname string, newname string) error {
ret := fsUtil("rename", oldname, newname)
if ret.IsNull() {
return os.ErrNotExist
}
return nil
}
func (fs *jazzfs) Stat(name string) (fs.FileInfo, error) {
v, found := fs.cache.Get(name)
if found {
fmt.Println("stat cached:", name)
return v.(*jazzinfo), nil
}
fmt.Println("stat:", name)
ret := fsUtil("stat", name)
if ret.IsNull() {
return nil, os.ErrNotExist
}
vv := &jazzinfo{ret}
fs.cache.Set(name, vv, cache.DefaultExpiration)
return vv, nil
}
func (fs *jazzfs) ReadDir(name string) (entries []fs.DirEntry, err error) {
fmt.Println("readdir:", name)
ret := fsUtil("readdir", name)
if ret.IsNull() {
return nil, os.ErrNotExist
}
for i := 0; i < ret.Length(); i++ {
fi, err := fs.Stat(filepath.Join(name, ret.Index(i).String()))
if err != nil {
fmt.Println("stat err:", err, ret.Index(i).String())
return nil, err
}
entries = append(entries, fi.(*jazzinfo))
}
return
}
func (fs *jazzfs) Chmod(name string, mode os.FileMode) error {
fmt.Println("chmod: not implemented") // TODO: Implement
return nil
}
func (fs *jazzfs) Chown(name string, uid int, gid int) error {
fmt.Println("chown: not implemented") // TODO: Implement
return nil
}
func (fs *jazzfs) Chtimes(name string, atime time.Time, mtime time.Time) error {
fmt.Println("chtimes: not implemented") // TODO: Implement
return nil
}
func (fs *jazzfs) Watch(name string, cfg *watchfs.Config) (*watchfs.Watch, error) {
fmt.Println("watch:", name)
if cfg == nil {
cfg = &watchfs.Config{}
}
watch, inbox, closer := watchfs.NewWatch(name, *cfg)
if cfg.Handler != nil {
go func() {
for e := range watch.Iter() {
cfg.Handler(e)
}
}()
}
inflateEvent := func(v js.Value) watchfs.Event {
return watchfs.Event{
Path: v.Get("path").String(),
OldPath: v.Get("path").String(),
Type: watchfs.EventWrite,
Err: nil,
FileInfo: &jazzinfo{v},
}
}
go func() {
defer close(inbox)
token := fsUtil("watch", name, js.FuncOf(func(this js.Value, args []js.Value) any {
select {
case inbox <- inflateEvent(args[0].Get("detail")):
default:
}
return nil
}))
<-closer
fsUtil("unwatch", token)
}()
return watch, nil
}
type jazzfile struct {
fs *jazzfs
node js.Value
path string
offset int64
cache []byte
lastFid string
dirty bool
wmu sync.Mutex
}
func (f *jazzfile) Name() string {
return filepath.Base(f.path)
}
func (f *jazzfile) Stat() (fs.FileInfo, error) {
return f.fs.Stat(f.path)
}
func (f *jazzfile) ReadDir(n int) ([]fs.DirEntry, error) {
return f.fs.ReadDir(f.path)
}
func (f *jazzfile) data() ([]byte, error) {
fi, err := f.fs.Stat(f.path)
if err != nil {
return nil, err
}
stat := fi.(*jazzinfo)
fid := stat.val.Get("fileID")
if fid.IsUndefined() {
return []byte{}, os.ErrNotExist
}
if fid.String() == f.lastFid {
return f.cache, nil
}
jsbuf := fsUtil("fetchFile", fid)
buf := make([]byte, jsbuf.Length())
js.CopyBytesToGo(buf, jsbuf)
// ch <- buf
// select {
// case <-time.After(2 * time.Second):
// log("slow file load, returning cached", f.path)
// return f.rcache, nil
// case b := <-ch:
f.cache = buf
f.lastFid = fid.String()
return buf, nil
// }
}
func (f *jazzfile) setData(data []byte) {
f.wmu.Lock()
defer f.wmu.Unlock()
buf := js.Global().Get("Uint8Array").New(len(data))
js.CopyBytesToJS(buf, data)
ret := fsUtil("writeFile", f.path, buf)
if ret.IsNull() {
fmt.Println("writeFile failed:", f.path)
return
}
}
func (f *jazzfile) Read(p []byte) (n int, err error) {
d, err := f.data()
if err != nil {
return 0, err
}
if f.offset >= int64(len(d)) {
return 0, io.EOF
}
rest := d[f.offset:]
if len(rest) < len(p) {
n = len(rest)
} else {
n = len(p)
}
copy(p, rest[:n])
f.offset += int64(n)
return n, nil
}
func (f *jazzfile) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0: // file start
f.offset = offset
case 1: // current offset
f.offset += offset
case 2: // file end
d, err := f.data()
if err != nil {
return 0, err
}
f.offset = int64(len(d)) + offset
}
return f.offset, nil
}
func (f *jazzfile) Write(p []byte) (n int, err error) {
cur, err := f.data()
if err != nil {
return 0, err
}
new := make([]byte, f.offset+int64(len(p)))
copy(new, cur[:f.offset])
n = copy(new[f.offset:], p)
f.cache = new
f.offset += int64(n)
f.dirty = true
return n, nil
}
func (f *jazzfile) Close() error {
f.offset = 0
if f.dirty {
return f.Sync()
}
return nil
}
func (f *jazzfile) Sync() error {
if !f.dirty {
return nil
}
f.dirty = false
ch := make(chan bool, 1)
go func() {
f.setData(f.cache)
ch <- true
}()
select {
case <-time.After(4 * time.Second):
fmt.Println("long write, continuing async!")
return nil
case <-ch:
return nil
}
}
// func (f *jazzfile) WriteAt(p []byte, off int64) (n int, err error) {
// fmt.Println("writeAt: not implemented") // TODO: Implement
// return 0, nil
// }
// func (f *jazzfile) ReadAt(p []byte, off int64) (n int, err error) {
// fmt.Println("readAt: not implemented") // TODO: Implement
// return 0, nil
// }
// func (f *jazzfile) Truncate(size int64) error {
// fmt.Println("truncate: not implemented") // TODO: Implement
// return nil
// }
type jazzinfo struct {
val js.Value
}
func (i *jazzinfo) Name() string { return i.val.Get("name").String() }
func (i *jazzinfo) Size() int64 { return int64(i.val.Get("size").Int()) }
func (i *jazzinfo) ModTime() time.Time { return time.Unix(int64(i.val.Get("mtime").Int()), 0) }
func (i *jazzinfo) IsDir() bool { return i.val.Get("isDir").Bool() }
func (i *jazzinfo) Sys() any { return nil }
func (i *jazzinfo) Info() (fs.FileInfo, error) { return i, nil }
func (i *jazzinfo) Type() fs.FileMode { return i.Mode() }
func (i *jazzinfo) Mode() fs.FileMode {
if i.IsDir() {
return 0755 | fs.ModeDir
}
return 0644
}