-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
155 lines (132 loc) · 3.83 KB
/
index.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
const fuse = require('fuse-bindings')
const Nanoresource = require('nanoresource')
const path = require('path')
const mkdirp = require('mkdirp')
const EMPTY = Buffer.alloc(512)
module.exports = (mnt, name, opts) => new BlockDevice(mnt, name, opts)
class BlockDevice extends Nanoresource {
constructor (mnt, opts) {
super()
if (!opts || !opts.read) throw new Error('opts.read(index, cb) is required')
if (!opts || !opts.write) throw new Error('opts.write(index, block, cb) is required')
this.mnt = path.dirname(mnt)
this.name = path.basename(mnt)
this.path = mnt
this.size = opts.size || 4 * 1024 * 1024 * 1024
this.ctime = new Date()
this.mtime = this.ctime
this.uid = opts.uid || (process.getuid ? process.getuid() : 0)
this.gid = opts.gid || (process.getgid ? process.getgid() : 0)
this.blockSize = 512
this.EMPTY = EMPTY
this.options = opts.options || []
this.files = [ this.name ]
this.onread = opts.read
this.onwrite = opts.write
this.onerror = opts.error || noop
this.onmount = opts.mount || noop
this.onunmount = opts.unmount || noop
this.open()
}
_open (cb) {
const self = this
const ops = {
force: true,
options: self.options,
readdir (path, cb) {
if (path === '/') return cb(0, self.files)
cb(0)
},
chown (path, uid, gid, cb) {
cb(0)
},
utimens (path, atime, mtime, cb) {
cb(0)
},
truncate (path, size, cb) {
cb(0)
},
getattr (path, cb) {
if (path === '/') return cb(0, self._stat(100, 0o40755))
if (path === '/' + self.name) return cb(0, self._stat(self.size, 0o100644))
if (hasLock(path)) return cb(0, self._stat(0, 0o100644))
cb(fuse.ENOENT)
},
create (path, mode, cb) {
ops.open(path, mode, cb)
},
open (path, flags, cb) {
if (path === '/' + self.name) return cb(0, 42)
if (hasLock(path)) return cb(0, 43)
if (/\.lck$/.test(path)) {
self.files.push(path.split('/').pop())
return cb(0, 44)
}
cb(0, fuse.ENOENT)
},
read (path, fd, buf, len, pos, cb) {
if (hasLock(path)) return cb(0)
if ((pos & 511) || (len & 511)) return cb(fuse.EINVAL)
if (!len || pos >= self.size) return cb(0)
const blk = pos / 512
const cnt = len / 512
if (!self.active()) return cb(fuse.ESPIPE)
self.onread(blk, cnt, buf, function (err) {
self.inactive()
if (err) return cb(fuse.ESPIPE) // open for better error
cb(len)
})
},
write (path, fd, buf, len, pos, cb) {
if (hasLock(path)) return cb(0)
if ((pos & 511) || (len & 511)) return cb(fuse.EINVAL)
if (!len) return cb(0)
const blk = pos / 512
const cnt = len / 512
if (!self.active()) return cb(fuse.ESPIPE)
self.onwrite(blk, cnt, buf, function (err) {
self.inactive()
if (err) return cb(fuse.ESPIPE)
cb(len)
})
},
release (path, fd, cb) {
cb(0)
}
}
function hasLock (path) {
return /\.lck$/.test(path) && self.files.indexOf(path.slice(1)) > -1
}
mkdirp(this.mnt, function () {
fuse.mount(self.mnt, ops, function (err) {
if (err) return onerror(err)
self.onmount()
cb(null)
})
function onerror (err) {
self.onerror(err)
cb(err)
}
})
}
_close (cb) {
const self = this
fuse.unmount(this.mnt, function (err) {
if (!err) self.onunmount()
cb(err)
})
}
_stat (size, mode) {
return {
ctime: this.ctime,
mtime: this.mtime,
atime: this.mtime,
nlink: 1,
size,
mode,
uid: this.uid,
gid: this.gid
}
}
}
function noop () {}