You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fuse-friends_fuse-native/index.js

619 lines
15 KiB

const os = require('os')
const fs = require('fs')
const path = require('path')
const IS_OSX = os.platform() === 'darwin'
const OSX_FOLDER_ICON = '/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericFolderIcon.icns'
const HAS_FOLDER_ICON = IS_OSX && fs.existsSync(OSX_FOLDER_ICON)
5 years ago
const binding = require('node-gyp-build')(__dirname)
const OpcodesAndDefaults = new Map([
[ 'init', {
op: 0
} ],
[ 'error', {
op: 1
} ],
[ 'access', {
op: 2,
defaults: [ 0 ]
} ],
[ 'statfs', {
op: 3,
defaults: [ getStatfsArray() ]
} ],
[ 'fgetattr', {
op: 4,
defaults: [ getStatArray() ]
} ],
[ 'getattr', {
op: 5,
defaults: [ getStatArray() ]
} ],
[ 'flush', {
op: 6,
} ],
[ 'fsync', {
op: 7
} ],
[ 'fsyncdir', {
op: 8
} ],
[ 'readdir', {
op: 9
} ],
[ 'truncate', {
op: 10
} ],
[ 'ftruncate', {
op: 11
} ],
[ 'utimens', {
op: 12
} ],
[ 'readlink', {
op: 13
} ],
[ 'chown', {
op: 14
} ],
[ 'chmod', {
op: 15
} ],
[ 'mknod', {
op: 16
} ],
[ 'setxattr', {
op: 17
} ],
[ 'getxattr', {
op: 18
} ],
[ 'listxattr', {
op: 19
} ],
[ 'removexattr', {
op: 20
} ],
[ 'open', {
op: 21
} ],
[ 'opendir', {
op: 22
} ],
[ 'read', {
op: 23
} ],
[ 'write', {
op: 24
} ],
[ 'release', {
op: 25
} ],
[ 'releasedir', {
op: 26
} ],
[ 'create', {
op: 27
} ],
[ 'unlink', {
op: 28
} ],
[ 'rename', {
op: 29
} ],
[ 'link', {
op: 30
} ],
[ 'symlink', {
op: 31
} ],
[ 'mkdir', {
op: 32
} ],
[ 'rmdir', {
op: 33
} ],
[ 'destroy', {
op: 34
} ]
5 years ago
])
5 years ago
class Fuse {
5 years ago
constructor (mnt, ops, opts = {}) {
this.opts = opts
this.mnt = path.resolve(mnt)
5 years ago
this.ops = ops
5 years ago
this._thread = Buffer.alloc(binding.sizeof_fuse_thread_t)
this._handlers = this._makeHandlerArray()
// Keep the process alive while fuse is mounted.
this._timer = null
5 years ago
const implemented = [0, 1, 5]
5 years ago
if (ops) {
for (const [name, code] of Opcodes) {
if (ops[name]) implemented.push(code)
}
}
this._implemented = new Set(implemented)
// Used to determine if the user-defined callback needs to be nextTick'd.
this._sync = true
}
_fuseOptions () {
const options = []
if ((/\*|(^,)fuse-bindings(,$)/.test(process.env.DEBUG)) || this.opts.debug) options.push('debug')
if (this.opts.allowOther) options.push('allow_other')
if (this.opts.allowRoot) options.push('allow_root')
if (this.opts.autoUnmount) options.push('auto_unmount')
if (this.opts.defaultPermissions) options.push('default_permissions')
if (this.opts.blkdev) options.push('blkdev')
if (this.opts.blksize) options.push('blksize=' + this.opts.blksize)
if (this.opts.maxRead) options.push('max_read=' + this.opts.maxRead)
if (this.opts.fd) options.push('fd=' + this.opts.fd)
if (this.opts.userId) options.push('user_id=', this.opts.userId)
if (this.opts.fsname) options.push('fsname=' + this.opts.fsname)
if (this.opts.subtype) options.push('subtype=' + this.opts.subtype)
if (this.opts.kernelCache) options.push('kernel_cache')
if (this.opts.autoCache) options.push('auto_cache')
if (this.opts.umask) options.push('umask=' + this.opts.umask)
if (this.opts.uid) options.push('uid=' + this.opts.uid)
if (this.opts.gid) options.push('gid=' + this.opts.gid)
if (this.opts.entryTimeout) options.push('entry_timeout=' + this.opts.entryTimeout)
if (this.opts.attrTimeout) options.push('attr_timeout=' + this.opts.attrTimeout)
if (this.opts.acAttrTimeout) options.push('ac_attr_timeout=' + this.opts.acAttrTimeout)
if (this.opts.noforget) options.push('noforget')
if (this.opts.remember) options.push('remember=' + this.opts.remember)
if (this.opts.modules) options.push('modules=' + this.opts.modules)
if (this.opts.displayFolder && IS_OSX) { // only works on osx
options.push('volname=' + path.basename(this.mnt))
if (HAS_FOLDER_ICON) options.push('volicon=' + OSX_FOLDER_ICON)
}
return options.map(o => '-o' + o).join(' ')
}
5 years ago
_signal (signalFunc, args) {
/*
if (this._sync) process.nextTick(() => signalFunc.apply(null, args))
else signalFunc.apply(null, args)
*/
process.nextTick(() => signalFunc.apply(null, args))
}
// Handlers
_makeHandlerArray () {
const handlers = new Array(OpcodesAndDefaults.size)
for (const [name, { op, defaults }] of OpcodesAndDefaults) {
const nativeSignal = binding[`fuse_native_signal_${name}`]
if (!nativeSignal) continue
handlers[op] = makeHandler(name, op, defaults, nativeSignal)
}
return handlers
function makeHandler (name, op, defaults, nativeSignal) {
return () => {
const boundSignal = signal.bind(arguments[0])
const funcName = `_$name`
if (!this[funcName] || !this._implemented.has(op)) return boundSignal(-1, defaults)
this[funcName].apply(null, [boundSignal, [...arguments].slice(1) ])
}
function signal (nativeHandler, err, args) {
const args = [nativeHandler, err]
if (defaults) args.concat(defaults)
return nativeSignal(args)
}
}
}
_init (signal) {
if (!this.ops.init) return signal(0)
this.ops.init(err => {
return signal(err)
})
}
_error (signal) {
if (!this.ops.error) return signal(0)
this.ops.error(err => {
return signal(err)
})
}
_getattr (signal, path) {
if (!this.ops.getattr) {
if (path !== '/') return signal(Fuse.EPERM)
return signal(0, getStatArray({ mtime: new Date(0), atime: new Date(0), ctime: new Date(0), mode: 16877, size: 4096 }))
}
this.ops.getattr(path, (err, stat) => {
if (err) return signal(err)
return signal(0, getStatArray(stat))
})
5 years ago
}
_fgetattr (signal, path, fd) {
5 years ago
}
_setxattr (signal, path, name, value, size, position, flags) {
}
_getxattr (signal, path, name, value, size, position) {
}
_listxattr (signal, path, list, size) {
5 years ago
}
_open (signal, path) {
5 years ago
}
_create (signal, path, mode) {
}
_read (signal, path, fd, buf, len, offset) {
}
_write (signal, path, fd, buf, len, offset) {
}
_release (signal, path, fd) {
}
_releasedir (signal, path, fd) {
}
5 years ago
_readdir (signal, path) {
5 years ago
this.ops.readdir(path, (err, names, stats) => {
if (err) return signal(err)
5 years ago
if (stats) stats = stats.map(getStatArray)
return signal(null, [names, stats || []])
5 years ago
})
5 years ago
}
_statfs (signal) {
5 years ago
this.ops.statfs((err, statfs) => {
if (err) return signal(err)
5 years ago
const arr = getStatfsArray(statfs)
return signal(null, [arr])
5 years ago
})
}
5 years ago
_fd_op (handle, op, path, fd, buf, len, offset) {
const signalFunc = binding.fuse_native_signal_buffer.bind(binding)
if (!this._implemented.has(op)) return this._signal(signalFunc, [handle, -1])
const cb = (bytesProcessed) => {
return this._signal(signalFunc, [handle, bytesProcessed || 0])
}
switch (op) {
case (binding.op_read):
this.ops.read(path, fd, buf, len, offset, cb)
break
case (binding.op_write):
this.ops.write(path, fd, buf, len, offset, cb)
break
case(binding.op_release):
this.ops.release(path, fd, cb)
break
case(binding.op_releasedir):
this.ops.releasedir(path, fd, cb)
break
default:
return this._signal(signalFunc, [handle, 0])
}
}
on_stat_op (handle, op, path, fd) {
5 years ago
const signalFunc = binding.fuse_native_signal_stat.bind(binding)
5 years ago
if (!this._implemented.has(op)) return this._signal(signalFunc, [handle, -1, getStatArray()])
5 years ago
const cb = (err, stat) => {
const arr = getStatArray(stat)
return this._signal(signalFunc, [handle, err, arr])
}
5 years ago
switch (op) {
case (binding.op_getattr):
this.ops.getattr(path, cb)
5 years ago
break
case (binding.op_fgetattr):
this.ops.fgetattr(path, fd, cb)
5 years ago
default:
return this._signal(signalFunc, [handle, -1, getStatArray()])
5 years ago
}
}
on_path_op (handle, op, path, mode, flags, atim, mtim) {
5 years ago
const signalFunc = binding.fuse_native_signal_path.bind(binding)
if (!this._implemented.has(op)) return this._signal(signalFunc, [handle, -1, 0])
const cb = (err, fd) => {
return this._signal(signalFunc, [handle, err, fd || 0])
}
5 years ago
switch (op) {
case (binding.op_open):
this.ops.open(path, flags, cb)
break
case (binding.op_create):
this.ops.create(path, mode, cb)
break
case (binding.op_access):
this.ops.access(path, mode, cb)
break
case (binding.op_utimens):
this.ops.utimens(path, getDoubleInt(atim, 0), getDoubleInt(mtim, 0), cb)
break
default:
return this._signal(signalFunc, [handle, -1, 0])
}
}
on_xattr_op (handle, op, path, name, value, list, size, flags, position) {
const signalFunc = binding.fuse_native_signal_xattr.bind(binding)
if (!this._implemented.has(op)) return this._signal(signalFunc, [handle, -1, 0])
const cb = err => {
return this._signal(signalFunc, [handle, -1])
}
switch (op) {
case (binding.op_setxattr):
this.ops.setxattr(path, name, value, size, position || 0, flags, cb)
break
case (binding.op_getxattr):
this.ops.getxattr(path, name, value, size, position || 0, cb)
break
case (binding.op_listxattr):
this.ops.listxattr(path, list, size, cb)
break
case (binding.op_removexattr):
this.ops.removexattr(path, name, cb)
break
default:
return this._signal(signalFunc, [handle, -1])
}
5 years ago
}
on_symlink (path, target) {
}
// Public API
mount (cb) {
const opts = this._fuseOptions()
fs.stat(this.mnt, (err, stat) => {
if (err) return cb(new Error('Mountpoint does not exist'))
if (!stat.isDirectory()) return cb(new Error('Mountpoint is not a directory'))
fs.stat(path.join(this.mnt, '..'), (_, parent) => {
if (parent && parent.dev !== stat.dev) return cb(new Error('Mountpoint in use'))
try {
// TODO: asyncify
binding.fuse_native_mount(this.mnt, opts, this._thread, this, this._handlers)
} catch (err) {
return cb(err)
}
this._timer = setInterval(() => {}, 10000)
return cb(null)
})
})
}
unmount (cb) {
// TODO: asyncify
try {
binding.fuse_native_unmount(this.mnt, this._thread)
} catch (err) {
clearInterval(this._timer)
return process.nextTick(cb, err)
}
clearInterval(this._timer)
return process.nextTick(cb, null)
}
errno (code) {
return (code && Fuse[code.toUpperCase()]) || -1
}
5 years ago
}
Fuse.EPERM = -1
Fuse.ENOENT = -2
Fuse.ESRCH = -3
Fuse.EINTR = -4
Fuse.EIO = -5
Fuse.ENXIO = -6
Fuse.E2BIG = -7
Fuse.ENOEXEC = -8
Fuse.EBADF = -9
Fuse.ECHILD = -10
Fuse.EAGAIN = -11
Fuse.ENOMEM = -12
Fuse.EACCES = -13
Fuse.EFAULT = -14
Fuse.ENOTBLK = -15
Fuse.EBUSY = -16
Fuse.EEXIST = -17
Fuse.EXDEV = -18
Fuse.ENODEV = -19
Fuse.ENOTDIR = -20
Fuse.EISDIR = -21
Fuse.EINVAL = -22
Fuse.ENFILE = -23
Fuse.EMFILE = -24
Fuse.ENOTTY = -25
Fuse.ETXTBSY = -26
Fuse.EFBIG = -27
Fuse.ENOSPC = -28
Fuse.ESPIPE = -29
Fuse.EROFS = -30
Fuse.EMLINK = -31
Fuse.EPIPE = -32
Fuse.EDOM = -33
Fuse.ERANGE = -34
Fuse.EDEADLK = -35
Fuse.ENAMETOOLONG = -36
Fuse.ENOLCK = -37
Fuse.ENOSYS = -38
Fuse.ENOTEMPTY = -39
Fuse.ELOOP = -40
Fuse.EWOULDBLOCK = -11
Fuse.ENOMSG = -42
Fuse.EIDRM = -43
Fuse.ECHRNG = -44
Fuse.EL2NSYNC = -45
Fuse.EL3HLT = -46
Fuse.EL3RST = -47
Fuse.ELNRNG = -48
Fuse.EUNATCH = -49
Fuse.ENOCSI = -50
Fuse.EL2HLT = -51
Fuse.EBADE = -52
Fuse.EBADR = -53
Fuse.EXFULL = -54
Fuse.ENOANO = -55
Fuse.EBADRQC = -56
Fuse.EBADSLT = -57
Fuse.EDEADLOCK = -35
Fuse.EBFONT = -59
Fuse.ENOSTR = -60
Fuse.ENODATA = -61
Fuse.ETIME = -62
Fuse.ENOSR = -63
Fuse.ENONET = -64
Fuse.ENOPKG = -65
Fuse.EREMOTE = -66
Fuse.ENOLINK = -67
Fuse.EADV = -68
Fuse.ESRMNT = -69
Fuse.ECOMM = -70
Fuse.EPROTO = -71
Fuse.EMULTIHOP = -72
Fuse.EDOTDOT = -73
Fuse.EBADMSG = -74
Fuse.EOVERFLOW = -75
Fuse.ENOTUNIQ = -76
Fuse.EBADFD = -77
Fuse.EREMCHG = -78
Fuse.ELIBACC = -79
Fuse.ELIBBAD = -80
Fuse.ELIBSCN = -81
Fuse.ELIBMAX = -82
Fuse.ELIBEXEC = -83
Fuse.EILSEQ = -84
Fuse.ERESTART = -85
Fuse.ESTRPIPE = -86
Fuse.EUSERS = -87
Fuse.ENOTSOCK = -88
Fuse.EDESTADDRREQ = -89
Fuse.EMSGSIZE = -90
Fuse.EPROTOTYPE = -91
Fuse.ENOPROTOOPT = -92
Fuse.EPROTONOSUPPORT = -93
Fuse.ESOCKTNOSUPPORT = -94
Fuse.EOPNOTSUPP = -95
Fuse.EPFNOSUPPORT = -96
Fuse.EAFNOSUPPORT = -97
Fuse.EADDRINUSE = -98
Fuse.EADDRNOTAVAIL = -99
Fuse.ENETDOWN = -100
Fuse.ENETUNREACH = -101
Fuse.ENETRESET = -102
Fuse.ECONNABORTED = -103
Fuse.ECONNRESET = -104
Fuse.ENOBUFS = -105
Fuse.EISCONN = -106
Fuse.ENOTCONN = -107
Fuse.ESHUTDOWN = -108
Fuse.ETOOMANYREFS = -109
Fuse.ETIMEDOUT = -110
Fuse.ECONNREFUSED = -111
Fuse.EHOSTDOWN = -112
Fuse.EHOSTUNREACH = -113
Fuse.EALREADY = -114
Fuse.EINPROGRESS = -115
Fuse.ESTALE = -116
Fuse.EUCLEAN = -117
Fuse.ENOTNAM = -118
Fuse.ENAVAIL = -119
Fuse.EISNAM = -120
Fuse.EREMOTEIO = -121
Fuse.EDQUOT = -122
Fuse.ENOMEDIUM = -123
Fuse.EMEDIUMTYPE = -124
module.exports = Fuse
5 years ago
function getStatfsArray (statfs) {
5 years ago
const ints = new Uint32Array(11)
5 years ago
ints[0] = (statfs && statfs.bsize) || 0
ints[1] = (statfs && statfs.frsize) || 0
ints[2] = (statfs && statfs.blocks) || 0
ints[3] = (statfs && statfs.bfree) || 0
ints[4] = (statfs && statfs.bavail) || 0
ints[5] = (statfs && statfs.files) || 0
ints[6] = (statfs && statfs.ffree) || 0
ints[7] = (statfs && statfs.favail) || 0
ints[8] = (statfs && statfs.fsid) || 0
ints[9] = (statfs && statfs.flag) || 0
ints[10] = (statfs && statfs.namemax) || 0
return ints
5 years ago
}
5 years ago
5 years ago
function setDoubleInt (arr, idx, num) {
arr[idx] = num % 4294967296
arr[idx + 1] = (num - arr[idx]) / 4294967296
}
function getDoubleInt(arr, idx) {
arr = new Uint32Array(arr)
var num = arr[idx + 1] * 4294967296
num += arr[idx]
return num
}
5 years ago
function getStatArray (stat) {
5 years ago
const ints = new Uint32Array(16)
5 years ago
5 years ago
ints[0] = (stat && stat.mode) || 0
ints[1] = (stat && stat.uid) || 0
ints[2] = (stat && stat.gid) || 0
ints[3] = (stat && stat.size) || 0
ints[4] = (stat && stat.dev) || 0
ints[5] = (stat && stat.nlink) || 1
ints[6] = (stat && stat.ino) || 0
ints[7] = (stat && stat.rdev) || 0
ints[8] = (stat && stat.blksize) || 0
ints[9] = (stat && stat.blocks) || 0
5 years ago
setDoubleInt(ints, 10, (stat && stat.atim) || Date.now())
setDoubleInt(ints, 12, (stat && stat.atim) || Date.now())
setDoubleInt(ints, 14, (stat && stat.atim) || Date.now())
5 years ago
return ints
}
function noop () {}
function call (cb) { cb() }