forked from mafintosh/hyperfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1004 lines (838 loc) · 29.9 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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var path = require('path')
var level = require('level')
var crypto = require('crypto')
var mkdirp = require('mkdirp')
var pump = require('pump')
var fs = require('fs')
var fuse = require('fuse-bindings')
var lexint = require('lexicographic-integer')
var union = require('sorted-union-stream')
var events = require('events')
var mknod = require('mknod')
var through = require('through2')
var concurrent = require('through2-concurrent')
var subleveldown = require('subleveldown')
var enumerate = require('level-enumerate')
var from = require('from2')
var hyperlog = require('hyperlog')
var multiplex = require('multiplex')
var os = require('os')
var noop = function () {}
var ENOENT = new Error('ENOENT')
ENOENT.code = 'ENOENT'
module.exports = function (home) {
var hyperfs = {}
var db = level(path.join(home, 'db'))
var metadata = subleveldown(db, 'metadata', {valueEncoding: 'json'})
var inodes = subleveldown(db, 'inodes', {valueEncoding: 'json'})
var snapshots = subleveldown(db, 'snapshots')
var ancestors = subleveldown(db, 'ancestors')
var volumes = subleveldown(db, 'volumes', {valueEncoding: 'json'})
var log = hyperlog(subleveldown(db, 'log'))
var writeablePath = function () {
var name = crypto.randomBytes(32).toString('hex')
return path.join('writeable', name.slice(0, 2), name.slice(2, 4), name.slice(4))
}
var readablePath = function (hash) {
return path.join('readable', hash.slice(0, 2), hash.slice(2, 4), hash.slice(4))
}
var toIndexKey = function(name) {
var depth = name.split('/').length - 1
return lexint.pack(depth, 'hex') + '!' + name
}
hyperfs.show = function (key, cb) {
log.get(key, function (err, node) {
if (err) return cb(err)
cb(null, JSON.parse(node.value.toString()))
})
}
hyperfs.put = function (id, name, data, cb) {
var key = id + '!' + toIndexKey(name)
if (!data.ctime) data.ctime = Date.now()
if (!data.mtime) data.mtime = Date.now()
metadata.put(key, data, cb)
}
hyperfs.del = function (id, name, cb) {
var key = id + '!' + toIndexKey(name)
metadata.del(key, cb)
}
hyperfs.get = function (id, name, cb) {
var key = id + '!' + toIndexKey(name)
metadata.get(key, cb)
}
hyperfs.unmount = function (mnt, cb) {
fuse.unmount(mnt, cb)
}
var dirStream = function (layer, key) {
return metadata.createReadStream({
gt: layer + '!' + key,
lt: layer + '!' + key + '\xff'
})
}
var getInode = function (layer, ino, cb) {
inodes.get(layer + '!' + lexint.pack(ino, 'hex'), cb)
}
var putInode = function (layer, ino, data, cb) {
inodes.put(layer + '!' + lexint.pack(ino, 'hex'), data, cb)
}
var delInode = function (layer, ino, cb) {
inodes.del(layer + '!' + lexint.pack(ino, 'hex'), cb)
}
var countInodes = function (layer, cb) {
var rs = inodes.createKeyStream({
gt: layer + '!',
lt: layer + '!\xff',
limit: 1,
reverse: true
})
var cnt = 0
rs.on('data', function (data) {
cnt = lexint.unpack(data.split('!')[1], 'hex')
})
rs.on('error', function (err) {
cb(err)
})
rs.on('end', function () {
cb(null, cnt)
})
}
var toCompareKey = function (data) {
return data.key.slice(data.key.indexOf('!') + 1)
}
hyperfs.hasBlob = function (hash, cb) {
fs.stat(path.join(home, readablePath(hash)), function (err) {
cb(null, !err)
})
}
hyperfs.createBlobReadStream = function (key) {
return fs.createReadStream(path.join(home, readablePath(key)))
}
hyperfs.createBlobWriteStream = function (cb) {
var filename = path.join(os.tmpdir(), 'hyperfs-tmp-' + crypto.randomBytes(32).toString('hex'))
var hash = crypto.createHash('sha256')
var write = function (data, enc, cb) {
hash.update(data)
cb(null, data)
}
var ws = fs.createWriteStream(filename)
var hasher = through(write)
pump(hasher, ws, function (err) {
var key = hash.digest('hex')
var newFilename = path.join(home, readablePath(key))
mkdirp(path.join(newFilename, '..'), function (err) {
if (err) return cb(err)
fs.rename(filename, newFilename, function (err) {
if (err) return cb(err)
cb(null, key)
})
})
})
return hasher
}
hyperfs.readSnapshot = function (key, cb) {
snapshots.get(key, function (err, space) {
if (err) return cb(err)
var rs = snapshots.createValueStream({
gt: space + '!',
lt: space + '!\xff',
valueEncoding: 'json'
})
cb(null, rs)
})
}
hyperfs.snapshot = function (id, opts, cb) { // don't mutate the layer while running this for now
if (typeof opts === 'function') return hyperfs.snapshot(id, null, opts)
if (!opts) opts = {}
if (!cb) cb = noop
var message = opts.message
var onindex = function (v) {
var key = v.snapshot
if (!v.snapshot) return cb()
volumes.put(id, v, function (err) {
if (err) return cb(err)
var write = function (data, enc, cb) {
hyperfs.put(key, data.name, {special: data.special, deleted: data.deleted, mode: data.mode, uid: data.uid, gid: data.gid, ino: data.ino, rdev: data.rdev}, function (err) {
if (err) return cb(err)
hyperfs.del(id, data.name, function () {
getInode(id, data.ino || 0, function (err, inode) {
if (err && err.notFound) return cb() // we already processed this one
if (err) return cb(err)
if (opts.debug) console.error('Snapshotting', data.name)
if (!data.data || data.special) {
putInode(key, data.ino, inode, function (err) {
if (err) return cb(err)
delInode(id, data.ino, cb)
})
return
}
var filename = readablePath(data.data)
mkdirp(path.join(home, filename, '..'), function (err) {
if (err) return cb(err)
fs.rename(path.join(home, inode.data), path.join(home, filename), function () { // ignore errors for now to be resumeable
inode.data = filename
putInode(key, data.ino, inode, function (err) {
if (err) return cb(err)
delInode(id, data.ino, cb)
})
})
})
})
})
})
}
hyperfs.readSnapshot(key, function (err, rs) {
if (err) return cb(err)
pump(rs, through.obj(write), function () {
var node = {
snapshot: key,
message: message || ''
}
log.add(v.node ? [v.node] : [], JSON.stringify(node), function (err, node) {
if (err) return cb(err)
v.node = node.key
v.snapshot = null
volumes.put(id, v, function (err) {
if (err) return cb(err)
cb(null, node.key)
})
})
})
})
})
}
volumes.get(id, function (err, v) {
if (err) return cb(new Error('Volume does not exist'))
if (v.snapshot) return onindex(v)
var space = crypto.randomBytes(32).toString('hex')
var snapshotHash = crypto.createHash('sha256')
var i = 0
pump(
metadata.createReadStream({
gt: id + '!',
lt: id + '!\xff'
}),
through.obj(function (file, enc, cb) {
var name = file.key.slice(file.key.lastIndexOf('!') + 1)
if (opts.debug) console.error('Indexing', name)
getInode(id, file.value.ino || 0, function (err, data) {
if (err && !err.notFound) return cb(err)
var ondone = function () {
var val = JSON.stringify({
name: name,
deleted: file.value.deleted,
special: file.value.special,
data: file.hash,
mode: file.value.mode,
rdev: file.value.rdev,
uid: file.value.uid,
gid: file.value.gid,
ino: file.value.ino
})
snapshotHash.update(val)
snapshots.put(space + '!' + lexint.pack(i++, 'hex'), val, cb)
}
if (!data || !data.data || file.value.special) return ondone()
var hash = crypto.createHash('sha256')
var rs = fs.createReadStream(path.join(home, data.data))
rs.on('data', function (data) {
hash.update(data)
})
rs.on('error', cb)
rs.on('end', function () {
file.hash = hash.digest('hex')
ondone()
})
})
}),
function (err) {
if (err) return cb(err)
var key = snapshotHash.digest('hex')
snapshots.put(key, space, function (err) {
if (err) return cb(err)
v.snapshot = key
onindex(v)
})
}
)
})
}
hyperfs.nodes = function () {
var write = function (node, enc, cb) {
node.value = JSON.parse(node.value)
cb(null, node)
}
return pump(log.createReadStream(), through.obj(write))
}
hyperfs.ancestors = function (key, cb) {
var list = []
var loop = function (key) {
log.get(key, function (err, node) {
if (err) return cb(err)
list.unshift({node: node.key, snapshot: JSON.parse(node.value).snapshot})
if (!node.links.length) return cb(null, list)
loop(node.links[0])
})
}
loop(key)
}
hyperfs.list = function () {
return volumes.createKeyStream()
}
hyperfs.info = function (key, cb) {
return volumes.get(key, cb)
}
hyperfs.remove = function (key, cb) {
if (!cb) cb = noop
var write = function (data, enc, cb) {
var done = function (err) {
if (err) return cb(err)
metadata.del(data.key, cb)
}
if (!data.value.ino) return done()
delInode(key, data.value.ino, done)
}
pump(metadata.createReadStream({gt: key + '!', lt: key + '!\xff'}), through.obj(write), function (err) {
if (err) return cb(err)
volumes.del(key, cb)
})
}
hyperfs.create = function (key, opts, cb) {
if (typeof opts === 'function') return hyperfs.create(key, null, opts)
if (!cb) cb = noop
if (!opts) opts = {}
volumes.get(key, function (_, v) {
if (v) return cb(new Error('volume already exists'))
volumes.put(key, {id: key, node: opts.node}, cb)
})
}
hyperfs.replicate = function (opts) {
if (!opts) opts = {}
var drains = []
var blobs = 0
var onblobwrite = function (data, enc, cb) {
plex.emit('write', data.length)
cb(null, data)
}
var onblobread = function (data, enc, cb) {
plex.emit('read', data.length)
cb(null, data)
}
var plex = multiplex(function (stream, id) {
var parts = id.split('/')
if (parts[0] === 's') {
var encode = function (data, enc, cb) {
if (blobs) return drains.push(encode.bind(null, data, enc, cb))
cb(null, JSON.stringify(data))
}
plex.emit('send-snapshot', parts[1])
hyperfs.readSnapshot(parts[1], function (err, rs) {
if (err) return stream.destroy(err)
pump(rs, through.obj(encode), stream)
})
return
}
if (parts[0] === 'd') {
plex.emit('send-data', parts[1])
blobs++
graph.cork()
pump(hyperfs.createBlobReadStream(parts[1]), through(onblobwrite), stream, function () {
blobs--
if (!blobs) {
while (drains.length) drains.shift()()
}
graph.uncork()
})
return
}
})
var logOutgoing = plex.createStream('hyperlog')
var logIncoming = plex.receiveStream('hyperlog')
var onnode = function (node, enc, cb) {
var value = JSON.parse(node.value.toString())
var s = plex.createStream('s/' + value.snapshot)
var hash = crypto.createHash('sha256')
var space = crypto.randomBytes(32).toString('hex')
var ptr = 0
plex.emit('receive-snapshot', value.snapshot)
var write = function (data, enc, cb) {
var val = data.obj
var done = function () {
var meta = {special: val.special, deleted: val.deleted, mode: val.mode, uid: val.uid, gid: val.gid, ino: val.ino, rdev: val.rdev}
hyperfs.put(value.snapshot, val.name, meta, function (err) {
if (err) return cb(err)
if (!val.ino) return cb(null, data)
getInode(value.snapshot, val.ino, function (_, inode) {
inode = inode || {refs: [], data: val.data && readablePath(val.data)}
if (inode.refs.indexOf(val.name) === -1) inode.refs.push(val.name)
putInode(value.snapshot, val.ino, inode, function (err) {
if (err) return cb(err)
cb(null, data)
})
})
})
}
if (!val.data) return done()
hyperfs.hasBlob(val.data, function (err, exists) {
if (err) return cb(err)
if (exists) return done()
plex.emit('receive-data', val.data)
pump(plex.createStream('d/' + val.data, {chunked: true}), through(onblobread), hyperfs.createBlobWriteStream(function (err, key) {
if (err) return cb(err)
done()
}))
})
}
var onhash = function (data, enc, cb) {
snapshots.put(space + '!' + lexint.pack(data.i, 'hex'), data.raw, {valueEncoding: 'utf-8'}, cb)
}
var updateHash = function (val, enc, cb) {
var raw = val.toString()
hash.update(raw)
cb(null, {i: ptr++, raw: raw, obj: JSON.parse(raw)})
}
// hwm should be to set to a really high number as we handle that in the protocol
// TODO: make module that "buffers" in leveldb
pump(s, through.obj({highWaterMark: 1000000}), through.obj(updateHash), concurrent.obj({maxConcurrency: 64}, write), through.obj(onhash), function (err) {
if (err) return cb(err)
if (hash.digest('hex') !== value.snapshot) return cb(new Error('checksum mismatch'))
plex.emit('node', node)
snapshots.put(value.snapshot, space, function (err) {
if (err) return cb(err)
cb(null, node)
})
})
}
var graph = log.replicate({live: opts.live, process: through.obj({highWaterMark: 100}, onnode)})
graph.on('error', function (err) {
plex.destroy(err)
})
pump(logIncoming, graph, logOutgoing, function () {
plex.end()
})
return plex
}
hyperfs.mount = function (key, mnt, opts) {
if (!opts) opts = {}
var mount = new events.EventEmitter()
mount.id = null
mount.layers = null
mount.node = null
mount.mountpoint = mnt
mount.inodes = 0
mount.unmount = hyperfs.unmount.bind(hyperfs, mnt)
var wrap = function (cb) {
return function (err) {
if (err) return cb(fuse.errno(err.code))
cb(0)
}
}
var get = function (name, cb) {
var loop = function (i) {
if (i < 0) return cb(ENOENT)
hyperfs.get(mount.layers[i], name, function (err, file) {
if (err) return loop(i - 1)
if (file.deleted) return cb(ENOENT)
cb(null, file, mount.layers[i])
})
}
loop(mount.layers.length - 1)
}
var del = function (name, ino, cb) {
if (opts.debug) console.log('delete:', name)
var oninode = function (err) {
if (err) return cb(err)
getInode(mount.id, ino, function (err, data) {
if (err) return cb()
var i = data.refs.indexOf(name)
if (i < 0) throw new Error('BAD INODE: ' + name)
data.refs.splice(i, 1)
if (data.refs.length) return putInode(mount.id, ino, data, cb)
delInode(mount.id, ino, function (err) {
if (err) return cb(err)
if (!data.data) return cb()
fs.unlink(path.join(home, data.data), cb)
})
})
}
var loop = function (i) {
if (i === mount.layers.length - 1) return hyperfs.del(mount.id, name, oninode)
hyperfs.get(mount.layers[i], name, function (err, file) {
if (err) return loop(i + 1)
hyperfs.put(mount.id, name, {deleted: true}, oninode)
})
}
loop(0)
}
var cow = function (name, cb) { // TODO: batch for me for speed/consistency
get(name, function (err, file, layer) {
if (err && name === '/') return cb(null, {mode: root.mode})
if (err) return cb(err)
if (layer === mount.id) return cb(null, file)
if (opts.debug) console.log('copy-on-write:', name)
var store = function (data) {
if (data.refs.length === 1) {
hyperfs.put(mount.id, name, file, function (err) {
if (err) return cb(err)
cb(null, file)
})
return
}
var i = 0
var loop = function (err) {
if (err) return cb(err)
if (i === data.refs.length) return cb(null, file)
var r = data.refs[i++]
get(r, function (err, file) {
if (err) return cb(err)
hyperfs.put(mount.id, r, file, loop)
})
}
loop(0)
}
var copy = function (from, to, cb) {
mkdirp(path.join(home, to, '..'), function (err) {
if (err) return cb(err)
if (file.special) return mknod(path.join(home, to), file.mode, file.rdev, cb)
pump(fs.createReadStream(path.join(home, from)), fs.createWriteStream(path.join(home, to)), cb)
})
}
getInode(mount.id, file.ino, function (err) {
if (!err) return cb(null, file) // already copied
getInode(layer, file.ino, function (err, data) {
if (err) return cb(err)
if (!data.data && !file.special) {
putInode(mount.id, file.ino, data, function (err) {
if (err) return cb(err)
store(data)
})
return
}
var newPath = writeablePath()
copy(data.data, newPath, function (err) {
if (err) return cb(err)
putInode(mount.id, file.ino, {refs: data.refs, data: newPath}, function (err) {
if (err) return cb(err)
store(data)
})
})
})
})
})
}
var ready = function (root) {
var ops = {}
ops.force = true
ops.options = ['suid', 'dev']
ops.displayFolder = true
ops.statfs = function (pathname, cb) { // TODO: return actual corrent data here instead
cb(0, {
bsize: 1000000,
frsize: 1000000,
blocks: 1000000,
bfree: 1000000,
bavail: 1000000,
files: 1000000,
ffree: 1000000,
favail: 1000000,
fsid: 1000000,
flag: 1000000,
namemax: 1000000
})
}
ops.link = function (name, dest, cb) {
if (opts.debug) console.log('link:', name, dest)
cow(name, function (err, file) {
if (err) return cb(fuse.errno(err.code))
hyperfs.put(mount.id, dest, file, function (err) {
if (err) return cb(fuse.errno(err.code))
getInode(mount.id, file.ino, function (err, data) {
if (err) return cb(fuse.errno(err.code))
data.refs.push(dest)
putInode(mount.id, file.ino, data, wrap(cb))
})
})
})
}
ops.fgetattr = function (name, fd, cb) {
if (name === '/') return cb(0, root)
var onfile = function (err, file, layer) {
if (err) return cb(fuse.errno(err.code))
if (file.special && layer !== mount.id) {
cow(name, function (err, file) {
onfile(err, file, mount.id)
})
return
}
var nlink = 1
var onstat = function (err, stat) {
if (err) return cb(fuse.errno(err.code))
cb(0, {
mode: file.mode,
size: file.size || stat.size,
blksize: 4096,
blocks: stat.blocks,
dev: stat.dev,
rdev: file.rdev || stat.rdev,
nlink: nlink,
ino: file.ino || stat.ino,
uid: file.uid || process.getuid(),
gid: file.gid || process.getgid(),
mtime: new Date(file.mtime || 0),
ctime: new Date(file.ctime || 0),
atime: new Date(file.mtime || 0)
})
}
if (file.mode & 040000) return onstat(null, root)
getInode(layer, file.ino, function (err, inode) {
if (err && fd > -1) return fs.fstat(fd, onstat)
if (err) throw new Error('NO INODE FOR ' + name)
if (err) return cb(fuse.errno(err.code))
nlink = inode.refs.length
if (fd < 0) fs.lstat(path.join(home, inode.data), onstat)
else fs.fstat(fd, onstat)
})
}
get(name, onfile)
}
ops.getattr = function (name, cb) {
ops.fgetattr(name, -1, cb)
}
ops.readdir = function (name, cb) {
if (!/\/$/.test(name)) name += '/'
var key = toIndexKey(name)
var result = []
var stream = dirStream(mount.layers[mount.layers.length - 1], key)
for (var i = mount.layers.length - 2; i >= 0; i--) {
stream = union(stream, dirStream(mount.layers[i], key), toCompareKey)
}
stream.on('error', wrap(cb))
stream.on('data', function (data) {
if (data.value.deleted) return
result.push(data.key.slice(data.key.lastIndexOf('/') + 1)) // haxx
})
stream.on('end', function () {
cb(null, result)
})
}
ops.truncate = function (name, size, cb) {
if (opts.debug) console.log('truncate:', name, size)
cow(name, function (err, file) {
if (err) return cb(fuse.errno(err.code))
getInode(mount.id, file.ino, function (err, data) {
if (err) return cb(fuse.errno(err.code))
fs.truncate(path.join(home, data.data), size, wrap(cb))
})
})
}
ops.ftruncate = function (name, fd, size, cb) {
if (opts.debug) console.log('ftruncate:', name, fd, size)
fs.ftruncate(fd, size, wrap(cb))
}
ops.fsync = function (name, fd, datasync, cb) {
fs.fsync(fd, wrap(cb))
}
ops.rename = function (name, dest, cb) {
if (opts.debug) console.log('rename:', name, dest)
ops.link(name, dest, function (errno) {
if (errno) return cb(errno)
ops.unlink(name, cb)
})
}
ops.mknod = function (name, mode, dev, cb) {
if (opts.debug) console.log('mknod:', name, mode, dev)
var inode = ++mount.inodes
var filename = writeablePath()
putInode(mount.id, inode, {data: filename, refs: [name]}, function (err) {
if (err) return cb(fuse.errno(err.code))
mkdirp(path.join(home, filename, '..'), function (err) {
if (err) return cb(fuse.errno(err.code))
mknod(path.join(home, filename), mode, dev, function (err) {
if (err) return cb(fuse.errno(err.code))
hyperfs.put(mount.id, name, {special: true, rdev: dev, mode: mode, ino: inode}, wrap(cb))
})
})
})
}
ops.open = function (name, flags, cb) {
var open = function (layer, ino) {
getInode(layer, ino, function (err, data) {
if (err) return cb(fuse.errno(err.code))
fs.open(path.join(home, data.data), flags, function (err, fd) {
if (err) return cb(fuse.errno(err.code))
cb(0, fd)
})
})
}
var readonly = function () {
get(name, function (err, file, layer) {
if (err) return cb(fuse.errno(err.code))
if (file.special) return writeMaybe() // special file - always cow
open(layer, file.ino)
})
}
var writeMaybe = function () {
cow(name, function (err, file) {
if (err) return cb(fuse.errno(err))
open(mount.id, file.ino)
})
}
if (flags === 0) readonly() // readonly
else writeMaybe() // cow
}
ops.create = function (name, mode, cb) {
if (opts.debug) console.log('create:', name, mode)
var inode = ++mount.inodes
var filename = writeablePath()
putInode(mount.id, inode, {data: filename, refs: [name]}, function (err) {
if (err) return cb(fuse.errno(err.code))
mkdirp(path.join(home, filename, '..'), function (err) {
if (err) return cb(fuse.errno(err.code))
fs.open(path.join(home, filename), 'w+', mode, function (err, fd) {
if (err) return cb(fuse.errno(err.code))
hyperfs.put(mount.id, name, {mode: mode, ino: inode}, function (err) {
if (err) return cb(fuse.errno(err.code))
cb(0, fd)
})
})
})
})
}
ops.unlink = function (name, cb) {
if (opts.debug) console.log('unlink:', name)
cow(name, function (err, file) { // TODO: don't copy file if refs === 1 and deleting
if (err) return cb(fuse.errno(err.code))
del(name, file.ino, wrap(cb))
})
}
ops.mkdir = function (name, mode, cb) {
if (opts.debug) console.log('mkdir:', name, mode)
var inode = ++mount.inodes
putInode(mount.id, inode, {refs: [name]}, function (err) {
if (err) return cb(fuse.errno(err.code))
hyperfs.put(mount.id, name, {mode: mode | 040000, ino: inode}, wrap(cb))
})
}
ops.rmdir = function (name, cb) {
if (opts.debug) console.log('rmdir:', name)
cow(name, function (err, file) {
if (err) return cb(fuse.errno(err.code))
del(name, file.ino, wrap(cb))
})
}
ops.write = function (name, fd, buf, len, offset, cb) {
fs.write(fd, buf, 0, len, offset, function (err, bytes) {
if (err) return cb(fuse.errno(err.code))
cb(bytes)
})
}
ops.read = function (name, fd, buf, len, offset, cb) {
fs.read(fd, buf, 0, len, offset, function (err, bytes) {
if (err) return cb(fuse.errno(err.code))
cb(bytes)
})
}
ops.release = function (name, fd, cb) {
fs.close(fd, wrap(cb))
}
ops.symlink = function (name, dest, cb) {
if (opts.debug) console.log('symlink:', name, dest)
ops.create(dest, 41453, function (errno, fd) {
if (errno) return cb(errno)
var buf = new Buffer(name)
var pos = 0
var loop = function () {
fs.write(fd, buf, 0, buf.length, pos, function (err, bytes) {
if (err) return cb(fuse.errno(err.code))
if (bytes === buf.length) return fs.close(fd, wrap(cb))
pos += bytes
buf = buf.slice(bytes)
loop()
})
}
loop()
})
}
ops.readlink = function (name, cb) {
get(name, function (err, file, layer) {
if (err) return cb(fuse.errno(err.code))
getInode(layer, file.ino, function (err, data) {
if (err) return cb(fuse.errno(err.code))
fs.readFile(path.join(home, data.data), 'utf-8', function (err, res) {
if (err) return cb(fuse.errno(err.code))
cb(0, res)
})
})
})
}
ops.chmod = function (name, mode, cb) {
if (opts.debug) console.log('chmod:', name, mode)
cow(name, function (err, file) {
if (err) return cb(fuse.errno(err.code))
file.mode = mode
hyperfs.put(mount.id, name, file, wrap(cb))
})
}
ops.chown = function (name, uid, gid, cb) {
if (opts.debug) console.log('chown:', name, uid, gid)
cow(name, function (err, file) {
if (err) return cb(fuse.errno(err.code))
if (uid > -1) file.uid = uid
if (gid > -1) file.gid = gid
hyperfs.put(mount.id, name, file, wrap(cb))
})
}
ops.utimens = function (name, ctime, mtime, cb) {
if (opts.time === false) return cb(0)
cow(name, function (err, file) {
if (err) return cb(fuse.errno(err.code))
file.ctime = ctime.getTime()
file.mtime = mtime.getTime()
hyperfs.put(mount.id, name, file, wrap(cb))
})
}
fuse.mount(mnt, ops, function (err) {
if (err) return mount.emit('error', err)
mount.emit('ready')
})
}
var onlayers = function (err, layers) {
if (err) return mount.emit('error', err)
var toSnapshot = function (val) {
return val.snapshot
}
var toNode = function (val) {
return val.node
}
mount.layers = layers.map(toSnapshot).concat(mount.id) // push writable layer
mount.nodes = layers.map(toNode)
var done = function () {
mkdirp(mnt, function (err) {
if (err) return mount.emit('error', err)
fs.stat(mnt, function (err, st) {
if (err) return mount.emit('error', err)
ready(st)
})
})
}
mount.inodes = 1024
var loop = function (i) {
if (i < 0) return done()
countInodes(mount.layers[i], function (_, cnt) {
if (cnt) mount.inodes = Math.max(cnt, mount.inodes)
loop(i - 1)
})
}
loop(mount.layers.length - 1)
}
volumes.get(key, function (err, v) {
if (err) return mount.emit('error', new Error('Volume does not exist'))
mount.id = key
mount.mountpoint = mnt
mount.node = v.node
if (!v.node) return onlayers(null, [])
hyperfs.ancestors(v.node, onlayers)
})
return mount