2019-08-01 12:24:01 +00:00
|
|
|
const tape = require('tape')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const concat = require('concat-stream')
|
|
|
|
|
|
|
|
const Fuse = require('../')
|
|
|
|
const mnt = require('./fixtures/mnt')
|
|
|
|
const stat = require('./fixtures/stat')
|
2019-11-19 13:04:46 +00:00
|
|
|
const { unmount } = require('./helpers')
|
2019-08-01 12:24:01 +00:00
|
|
|
|
|
|
|
tape('read', function (t) {
|
|
|
|
var ops = {
|
|
|
|
force: true,
|
|
|
|
readdir: function (path, cb) {
|
2019-08-06 08:55:08 +00:00
|
|
|
if (path === '/') return process.nextTick(cb, null, ['test'])
|
|
|
|
return process.nextTick(cb, Fuse.ENOENT)
|
2019-08-01 12:24:01 +00:00
|
|
|
},
|
|
|
|
getattr: function (path, cb) {
|
2019-08-06 08:55:08 +00:00
|
|
|
if (path === '/') return process.nextTick(cb, null, stat({ mode: 'dir', size: 4096 }))
|
|
|
|
if (path === '/test') return process.nextTick(cb, null, stat({ mode: 'file', size: 11 }))
|
|
|
|
return process.nextTick(cb, Fuse.ENOENT)
|
2019-08-01 12:24:01 +00:00
|
|
|
},
|
|
|
|
open: function (path, flags, cb) {
|
2019-08-06 08:55:08 +00:00
|
|
|
return process.nextTick(cb, 0, 42)
|
2019-08-01 12:24:01 +00:00
|
|
|
},
|
|
|
|
release: function (path, fd, cb) {
|
|
|
|
t.same(fd, 42, 'fd was passed to release')
|
2019-08-06 08:55:08 +00:00
|
|
|
return process.nextTick(cb, 0)
|
2019-08-01 12:24:01 +00:00
|
|
|
},
|
|
|
|
read: function (path, fd, buf, len, pos, cb) {
|
|
|
|
var str = 'hello world'.slice(pos, pos + len)
|
2019-08-06 08:55:08 +00:00
|
|
|
if (!str) return process.nextTick(cb, 0)
|
2019-08-01 12:24:01 +00:00
|
|
|
buf.write(str)
|
2019-08-06 08:55:08 +00:00
|
|
|
return process.nextTick(cb, str.length)
|
2019-08-01 12:24:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fuse = new Fuse(mnt, ops, { debug: true })
|
|
|
|
fuse.mount(function (err) {
|
|
|
|
t.error(err, 'no error')
|
|
|
|
|
|
|
|
fs.readFile(path.join(mnt, 'test'), function (err, buf) {
|
|
|
|
t.error(err, 'no error')
|
2019-08-06 08:55:08 +00:00
|
|
|
t.same(buf, Buffer.from('hello world'), 'read file')
|
2019-08-01 12:24:01 +00:00
|
|
|
|
|
|
|
fs.readFile(path.join(mnt, 'test'), function (err, buf) {
|
|
|
|
t.error(err, 'no error')
|
2019-08-06 08:55:08 +00:00
|
|
|
t.same(buf, Buffer.from('hello world'), 'read file again')
|
2019-08-01 12:24:01 +00:00
|
|
|
|
|
|
|
fs.createReadStream(path.join(mnt, 'test'), { start: 0, end: 4 }).pipe(concat(function (buf) {
|
2019-08-06 08:55:08 +00:00
|
|
|
t.same(buf, Buffer.from('hello'), 'partial read file')
|
2019-08-01 12:24:01 +00:00
|
|
|
|
|
|
|
fs.createReadStream(path.join(mnt, 'test'), { start: 6, end: 10 }).pipe(concat(function (buf) {
|
2019-08-06 08:55:08 +00:00
|
|
|
t.same(buf, Buffer.from('world'), 'partial read file + start offset')
|
2019-08-01 12:24:01 +00:00
|
|
|
|
2019-11-19 13:04:46 +00:00
|
|
|
unmount(fuse, function () {
|
2019-08-01 12:24:01 +00:00
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|