-
Notifications
You must be signed in to change notification settings - Fork 9
/
lfs_js.js
416 lines (347 loc) · 11.5 KB
/
lfs_js.js
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// configurable constants
var LFS_READ_SIZE = 64
var LFS_PROG_SIZE = 64
var LFS_BLOCK_SIZE = 512
var LFS_LOOKAHEAD = 512
// internal constants
var LFS_TYPE_REG = 0x11
var LFS_TYPE_DIR = 0x22
var LFS_O_RDONLY = 1
var LFS_O_WRONLY = 2
var LFS_O_RDWR = 3
var LFS_O_CREAT = 0x0100
var LFS_O_EXCL = 0x0200
var LFS_O_TRUNC = 0x0400
var LFS_O_APPEND = 0x0800
var LFS_SEEK_SET = 0
var LFS_SEEK_CUR = 1
var LFS_SEEK_END = 2
// LFS class
function LFS(bd, read_size, prog_size, block_size, lookahead) {
this.bd = bd
this._mount = 0
// setup config
this.read_size = bd.read_size || 0
if (this.read_size < (read_size || LFS_READ_SIZE)) {
this.read_size = (read_size || LFS_READ_SIZE)
}
this.prog_size = bd.prog_size || 0
if (this.prog_size < (prog_size || LFS_PROG_SIZE)) {
this.prog_size = (prog_size || LFS_PROG_SIZE)
}
this.block_size = bd.erase_size || 0
if (this.block_size < (block_size || LFS_BLOCK_SIZE)) {
this.block_size = (block_size || LFS_BLOCK_SIZE)
}
this.block_count = (bd.size || 0) / this.block_size
this.lookahead = 32 * ~~((this.block_count + 31)/32)
if (this.lookahead < (lookahead || LFS_LOOKAHEAD)) {
this.lookahead = (lookahead || LFS_LOOKAHEAD)
}
// wrap bd functions in C runtime
// needs global thunks due to emscripten limitations
if (!LFS._readptr) {
LFS._readptr = Runtime.addFunction(function(cfg,
block, off, buffer, size) {
return LFS._readthunk(block, off, buffer, size)
})
}
if (!LFS._progptr) {
LFS._progptr = Runtime.addFunction(function(cfg,
block, off, buffer, size) {
return LFS._progthunk(block, off, buffer, size)
})
}
if (!LFS._eraseptr) {
LFS._eraseptr = Runtime.addFunction(function(cfg, block) {
return LFS._erasethunk(block)
})
}
if (!LFS._syncptr) {
LFS._syncptr = Runtime.addFunction(function(cfg) {
return LFS._syncthunk()
})
}
if (!LFS._traverseptr) {
LFS._traverseptr = Runtime.addFunction(function(cfg, block) {
return LFS._traversethunk(block)
})
}
// setup bd thunks
LFS._readthunk = bd.read.bind(bd)
LFS._progthunk = bd.prog.bind(bd)
LFS._erasethunk = (bd.erase || function(){return 0}).bind(bd)
LFS._syncthunk = (bd.sync || function(){return 0}).bind(bd)
// constants
this.types = {
'reg': LFS_TYPE_REG,
'dir': LFS_TYPE_DIR,
}
this.flags = {
'rdonly': LFS_O_RDONLY,
'wronly': LFS_O_WRONLY,
'rdwr': LFS_O_RDWR,
'creat': LFS_O_CREAT,
'excl': LFS_O_EXCL,
'trunc': LFS_O_TRUNC,
'append': LFS_O_APPEND,
}
this.whences = {
'set': LFS_SEEK_SET,
'cur': LFS_SEEK_CUR,
'end': LFS_SEEK_END,
}
// link in C functions
var n = 'number'
var s = 'string'
this._lfs_new = Module.cwrap('lfs_new', n, [])
this._lfs_new_config = Module.cwrap('lfs_new_config', n,
[n, n, n, n, n, n, n, n, n])
this._lfs_new_info = Module.cwrap('lfs_new_info', n, [])
this._lfs_new_file = Module.cwrap('lfs_new_file', n, [])
this._lfs_new_dir = Module.cwrap('lfs_new_dir', n, [])
this._lfs_format = Module.cwrap('lfs_format', n, [n, n])
this._lfs_mount = Module.cwrap('lfs_mount', n, [n, n])
this._lfs_unmount = Module.cwrap('lfs_unmount', n, [n])
this._lfs_remove = Module.cwrap('lfs_remove', n, [n, s])
this._lfs_rename = Module.cwrap('lfs_rename', n, [n, s, s])
this._lfs_stat = Module.cwrap('lfs_stat', n, [n, s, n])
this._lfs_file_open = Module.cwrap('lfs_file_open', n, [n, n, s, n])
this._lfs_file_close = Module.cwrap('lfs_file_close', n, [n, n])
this._lfs_file_sync = Module.cwrap('lfs_file_sync', n, [n, n])
this._lfs_file_read = Module.cwrap('lfs_file_read', n, [n, n, n, n])
this._lfs_file_write = Module.cwrap('lfs_file_write', n, [n, n, n, n])
this._lfs_file_seek = Module.cwrap('lfs_file_seek', n, [n, n, n, n])
this._lfs_file_truncate = Module.cwrap('lfs_file_seek', n, [n, n, n])
this._lfs_file_tell = Module.cwrap('lfs_file_tell', n, [n, n])
this._lfs_file_rewind = Module.cwrap('lfs_file_rewind', n, [n, n])
this._lfs_file_size = Module.cwrap('lfs_file_size', n, [n, n])
this._lfs_mkdir = Module.cwrap('lfs_mkdir', n, [n, s])
this._lfs_dir_open = Module.cwrap('lfs_dir_open', n, [n, n, s])
this._lfs_dir_close = Module.cwrap('lfs_dir_close', n, [n, n])
this._lfs_dir_read = Module.cwrap('lfs_dir_read', n, [n, n, n])
this._lfs_dir_seek = Module.cwrap('lfs_dir_seek', n, [n, n, n])
this._lfs_dir_tell = Module.cwrap('lfs_dir_tell', n, [n, n])
this._lfs_dir_rewind = Module.cwrap('lfs_dir_rewind', n, [n, n])
this._lfs_traverse = Module.cwrap('lfs_traverse', n, [n, n, n])
this._lfs_deorphan = Module.cwrap('lfs_deorphan', n, [n])
}
LFS.prototype.format = function() {
if (this._mount > 0) {
// temporarily unmount filesystems
Module._free(this._lfs_config)
Module._free(this._lfs)
}
// allocate memory
this._lfs_config = this._lfs_new_config(
LFS._readptr, LFS._progptr, LFS._eraseptr, LFS._syncptr,
this.read_size, this.prog_size,
this.block_size, this.block_count,
this.lookahead)
this._lfs = this._lfs_new()
// call format
var err = this._lfs_format(this._lfs, this._lfs_config)
// clean up
if (this._mount == 0) {
Module._free(this._lfs_config)
Module._free(this._lfs)
}
return err
}
LFS.prototype.mount = function() {
this._mount += 1
if (this._mount != 1) {
return 0
}
// allocate memory
this._lfs_config = this._lfs_new_config(
LFS._readptr, LFS._progptr, LFS._eraseptr, LFS._syncptr,
this.read_size, this.prog_size,
this.block_size, this.block_count,
this.lookahead)
this._lfs = this._lfs_new()
// call mount
var err = this._lfs_mount(this._lfs, this._lfs_config)
if (err) {
this._mount -= 1
}
return err
}
LFS.prototype.unmount = function() {
this._mount -= 1
if (this._mount != 0) {
return 0
}
// call unmount
var err = this._lfs_unmount(this._lfs)
// clean up
Module._free(this._lfs_config)
Module._free(this._lfs)
return err
}
LFS.prototype.remove = function(path) {
return this._lfs_remove(this._lfs, path)
}
LFS.prototype.rename = function(oldpath, newpath) {
return this._lfs_rename(this._lfs, oldpath, newpath)
}
LFS.prototype.stat = function(path) {
// fill out butter with stat
var info = this._lfs_new_info()
var err = this._lfs_stat(this._lfs, path, info)
if (err) {
// return err code instead of object
Module._free(info)
return err;
}
// extract results
var res = {
type: (
Module.HEAPU8[info+0] == this.LFS_TYPE_DIR ? 'dir' :
Module.HEAPU8[info+0] == this.LFS_TYPE_REG ? 'reg' :
Module.HEAPU8[info+0]),
size: Module.HEAPU32[(info+4)/4],
name: Pointer_stringify(info+8),
}
Module._free(info)
return res
}
LFS.File = function(lfs, name, flags) {
this.lfs = lfs
this.name = name
this.flags = flags
// setup flags
var mask = 0
for (var i = 0; i < (flags || []).length; i++) {
console.assert(this.lfs.flags[flags[i]])
mask |= this.lfs.flags[flags[i]]
}
// allocate memory and open file
this._file = this.lfs._lfs_new_file()
var err = this.lfs._lfs_file_open(this.lfs._lfs, this._file, name, mask)
if (err < 0) {
Module._free(this._file)
this.err = err
}
}
LFS.prototype.open = function(name, flags) {
var res = new LFS.File(this, name, flags)
if (res.err) {
return res.err;
}
return res
}
LFS.File.prototype.close = function() {
var err = this.lfs._lfs_file_close(this.lfs._lfs, this._file)
Module._free(this._file)
return err
}
LFS.File.prototype.sync = function() {
return this.lfs._lfs_file_sync(this.lfs._lfs, this._file)
}
LFS.File.prototype.read = function(size) {
if (!size) {
size = this.size()
}
var buffer = Module._malloc(size)
var res = this.lfs._lfs_file_read(this.lfs._lfs, this._file, buffer, size)
if (res < 0) {
Module._free(buffer)
return res
}
var string = Pointer_stringify(buffer, res)
Module._free(buffer)
return string
}
LFS.File.prototype.write = function(string) {
var buffer = Module._malloc(string.length)
writeStringToMemory(string, buffer, true)
var res = this.lfs._lfs_file_write(this.lfs._lfs, this._file,
buffer, string.length)
Module._free(buffer)
return res
}
LFS.File.prototype.seek = function(off, whence) {
console.assert(this.lfs.whences[whence || 'set'])
return this.lfs._lfs_file_seek(this.lfs._lfs, this._file,
off, this.lfs.whences[whence || 'set'])
}
LFS.File.prototype.truncate = function(size) {
return this.lfs._lfs_file_truncate(this.lfs._lfs, this._file, size)
}
LFS.File.prototype.tell = function() {
return this.lfs._lfs_file_tell(this.lfs._lfs, this._file)
}
LFS.File.prototype.rewind = function() {
return this.lfs._lfs_file_rewind(this.lfs._lfs, this._file)
}
LFS.File.prototype.size = function() {
return this.lfs._lfs_file_size(this.lfs._lfs, this._file)
}
LFS.prototype.mkdir = function(path) {
return this._lfs_mkdir(this._lfs, path)
}
LFS.Dir = function(lfs, name) {
this.lfs = lfs
this.name = name
// allocate memory and open dir
this._dir = this.lfs._lfs_new_dir()
var err = this.lfs._lfs_dir_open(this.lfs._lfs, this._dir, name)
if (err < 0) {
Module._free(this._dir)
this.err = err
}
}
LFS.prototype.opendir = function(name, flags) {
var res = new LFS.Dir(this, name, flags)
if (res.err) {
return res.err;
}
return res
}
LFS.Dir.prototype.close = function() {
var err = this.lfs._lfs_dir_close(this.lfs._lfs, this._dir)
Module._free(this._dir)
return err
}
LFS.Dir.prototype.read = function() {
// fill out butter with dir read
var info = this.lfs._lfs_new_info()
var err = this.lfs._lfs_dir_read(this.lfs._lfs, this._dir, info)
if (err == 0) {
// return null when complete
Module._free(info)
return null
} else if (err < 0) {
// return err code instead of object
Module._free(info)
return err;
}
// extract results
var res = {
type: (
Module.HEAPU8[info+0] == this.LFS_TYPE_DIR ? 'dir' :
Module.HEAPU8[info+0] == this.LFS_TYPE_REG ? 'reg' :
Module.HEAPU8[info+0]),
size: Module.HEAPU32[(info+4)/4],
name: Pointer_stringify(info+8),
}
Module._free(info)
return res
}
LFS.Dir.prototype.seek = function(off) {
return this.lfs._lfs_dir_seek(this.lfs._lfs, this._dir, off)
}
LFS.Dir.prototype.tell = function() {
return this.lfs._lfs_dir_tell(this.lfs._lfs, this._dir)
}
LFS.Dir.prototype.rewind = function() {
return this.lfs._lfs_dir_rewind(this.lfs._lfs, this._dir)
}
LFS.prototype.traverse = function(cb) {
LFS._traversethunk = cb
return this._lfs_traverse(this._lfs, LFS._traverseptr, 0)
}
LFS.prototype.deorphan = function() {
return this._lfs_deorphan(this._lfs)
}