forked from mafintosh/fuse-block-device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
39 lines (35 loc) · 862 Bytes
/
example.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
const blockDevice = require('./')
const blocks = new Map()
const blk = blockDevice('./mnt/4gb', {
uid: 1000,
gid: 1000,
options: process.getuid() ? [] : [ 'allow_other' ],
read (index, cnt, buf, cb) {
for (let i = 0; i < cnt; i++) {
const b = blocks.get(index + i) || blk.EMPTY
b.copy(buf, i * 512)
}
cb(null)
},
write (index, cnt, buf, cb) {
for (let i = 0; i < cnt; i++) {
const b = blocks.get(index + i) || Buffer.alloc(512)
buf.copy(b, 0, i * 512)
blocks.set(index + i, b)
}
cb(null)
},
mount () {
console.log('device mounted')
},
error (err) {
console.log('error', err)
}
})
let max = 0
setInterval(function () {
if (max === blocks.size) return
max = blocks.size
console.log('Total bytes: ' + blocks.size * 512)
}, 1000)
process.once('SIGINT', () => blk.close())