2017-10-27 15:05:01 +00:00
|
|
|
var fuse = require('node-gyp-build')(__dirname)
|
2015-03-17 12:32:09 +00:00
|
|
|
var fs = require('fs')
|
2015-03-21 15:05:35 +00:00
|
|
|
var os = require('os')
|
2015-03-22 11:46:33 +00:00
|
|
|
var xtend = require('xtend')
|
2015-03-12 02:11:29 +00:00
|
|
|
var path = require('path')
|
|
|
|
|
|
|
|
var noop = function () {}
|
2015-03-17 12:32:09 +00:00
|
|
|
var call = function (cb) { cb() }
|
2015-03-12 02:11:29 +00:00
|
|
|
|
2015-03-26 18:34:26 +00:00
|
|
|
var IS_OSX = os.platform() === 'darwin'
|
|
|
|
var OSX_FOLDER_ICON = '/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericFolderIcon.icns'
|
|
|
|
var HAS_FOLDER_ICON = IS_OSX && fs.existsSync(OSX_FOLDER_ICON)
|
2015-03-21 15:05:35 +00:00
|
|
|
|
2015-03-12 02:11:29 +00:00
|
|
|
var FuseBuffer = function () {
|
|
|
|
this.length = 0
|
2015-03-16 21:28:33 +00:00
|
|
|
this.parent = undefined
|
2015-03-12 02:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FuseBuffer.prototype = Buffer.prototype
|
|
|
|
|
2015-03-16 21:28:33 +00:00
|
|
|
fuse.setBuffer(FuseBuffer)
|
2015-03-21 15:05:35 +00:00
|
|
|
fuse.setCallback(function (index, callback) {
|
|
|
|
return callback.bind(null, index)
|
|
|
|
})
|
2015-03-16 21:28:33 +00:00
|
|
|
|
2015-05-05 00:22:05 +00:00
|
|
|
exports.context = function () {
|
|
|
|
var ctx = {}
|
|
|
|
fuse.populateContext(ctx)
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:05:18 +00:00
|
|
|
exports.mount = function (mnt, ops, opts, cb) {
|
|
|
|
if (typeof opts === 'function') return exports.mount(mnt, ops, null, opts)
|
2015-03-17 12:32:09 +00:00
|
|
|
if (!cb) cb = noop
|
|
|
|
|
2015-09-23 16:05:18 +00:00
|
|
|
ops = xtend(ops, opts) // clone
|
2015-03-12 02:11:29 +00:00
|
|
|
if (/\*|(^,)fuse-bindings(,$)/.test(process.env.DEBUG)) ops.options = ['debug'].concat(ops.options || [])
|
2015-03-17 12:32:09 +00:00
|
|
|
mnt = path.resolve(mnt)
|
|
|
|
|
2015-03-26 18:34:26 +00:00
|
|
|
if (ops.displayFolder && IS_OSX) { // only works on osx
|
2015-03-21 15:05:35 +00:00
|
|
|
if (!ops.options) ops.options = []
|
|
|
|
ops.options.push('volname=' + path.basename(mnt))
|
2015-03-26 18:34:26 +00:00
|
|
|
if (HAS_FOLDER_ICON) ops.options.push('volicon=' + OSX_FOLDER_ICON)
|
2015-03-21 15:05:35 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 03:11:42 +00:00
|
|
|
var callback = function (err) {
|
|
|
|
callback = noop
|
2015-03-21 15:05:35 +00:00
|
|
|
setImmediate(cb.bind(null, err))
|
2015-03-20 03:11:42 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 12:32:09 +00:00
|
|
|
var init = ops.init || call
|
|
|
|
ops.init = function (next) {
|
2015-03-20 03:11:42 +00:00
|
|
|
callback()
|
2015-09-03 04:14:48 +00:00
|
|
|
if (init.length > 1) init(mnt, next) // backwards compat for now
|
|
|
|
else init(next)
|
2015-03-17 12:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var error = ops.error || call
|
|
|
|
ops.error = function (next) {
|
2015-03-20 03:11:42 +00:00
|
|
|
callback(new Error('Mount failed'))
|
2015-03-17 12:32:09 +00:00
|
|
|
error(next)
|
|
|
|
}
|
|
|
|
|
2015-03-22 11:46:33 +00:00
|
|
|
if (!ops.getattr) { // we need this for unmount to work on osx
|
|
|
|
ops.getattr = function (path, cb) {
|
|
|
|
if (path !== '/') return cb(fuse.EPERM)
|
|
|
|
cb(null, {mtime: new Date(0), atime: new Date(0), ctime: new Date(0), mode: 16877, size: 4096})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 03:11:42 +00:00
|
|
|
var mount = function () {
|
2015-08-06 19:33:27 +00:00
|
|
|
// TODO: I got a feeling this can be done better
|
2015-08-08 20:56:37 +00:00
|
|
|
if (os.platform() !== 'win32') {
|
|
|
|
fs.stat(mnt, function (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(mnt, '..'), function (_, parent) {
|
|
|
|
if (parent && parent.dev !== stat.dev) return cb(new Error('Mountpoint in use'))
|
|
|
|
fuse.mount(mnt, ops)
|
2015-08-06 19:33:27 +00:00
|
|
|
})
|
2015-08-08 20:56:37 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
fuse.mount(mnt, ops)
|
2015-08-06 19:33:27 +00:00
|
|
|
}
|
2015-03-20 03:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ops.force) return mount()
|
|
|
|
exports.unmount(mnt, mount)
|
2015-03-12 02:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.unmount = function (mnt, cb) {
|
2016-10-17 11:50:38 +00:00
|
|
|
fuse.unmount(path.resolve(mnt), cb)
|
2015-03-12 02:11:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 18:20:09 +00:00
|
|
|
exports.errno = function (code) {
|
2015-05-19 18:24:47 +00:00
|
|
|
return (code && exports[code.toUpperCase()]) || -1
|
2015-03-24 18:20:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 02:11:29 +00:00
|
|
|
exports.EPERM = -1
|
|
|
|
exports.ENOENT = -2
|
|
|
|
exports.ESRCH = -3
|
|
|
|
exports.EINTR = -4
|
|
|
|
exports.EIO = -5
|
|
|
|
exports.ENXIO = -6
|
|
|
|
exports.E2BIG = -7
|
|
|
|
exports.ENOEXEC = -8
|
|
|
|
exports.EBADF = -9
|
|
|
|
exports.ECHILD = -10
|
|
|
|
exports.EAGAIN = -11
|
|
|
|
exports.ENOMEM = -12
|
|
|
|
exports.EACCES = -13
|
|
|
|
exports.EFAULT = -14
|
|
|
|
exports.ENOTBLK = -15
|
|
|
|
exports.EBUSY = -16
|
|
|
|
exports.EEXIST = -17
|
|
|
|
exports.EXDEV = -18
|
|
|
|
exports.ENODEV = -19
|
|
|
|
exports.ENOTDIR = -20
|
|
|
|
exports.EISDIR = -21
|
|
|
|
exports.EINVAL = -22
|
|
|
|
exports.ENFILE = -23
|
|
|
|
exports.EMFILE = -24
|
|
|
|
exports.ENOTTY = -25
|
|
|
|
exports.ETXTBSY = -26
|
|
|
|
exports.EFBIG = -27
|
|
|
|
exports.ENOSPC = -28
|
|
|
|
exports.ESPIPE = -29
|
|
|
|
exports.EROFS = -30
|
|
|
|
exports.EMLINK = -31
|
|
|
|
exports.EPIPE = -32
|
|
|
|
exports.EDOM = -33
|
|
|
|
exports.ERANGE = -34
|
|
|
|
exports.EDEADLK = -35
|
|
|
|
exports.ENAMETOOLONG = -36
|
|
|
|
exports.ENOLCK = -37
|
|
|
|
exports.ENOSYS = -38
|
|
|
|
exports.ENOTEMPTY = -39
|
|
|
|
exports.ELOOP = -40
|
|
|
|
exports.EWOULDBLOCK = -11
|
|
|
|
exports.ENOMSG = -42
|
|
|
|
exports.EIDRM = -43
|
|
|
|
exports.ECHRNG = -44
|
|
|
|
exports.EL2NSYNC = -45
|
|
|
|
exports.EL3HLT = -46
|
|
|
|
exports.EL3RST = -47
|
|
|
|
exports.ELNRNG = -48
|
|
|
|
exports.EUNATCH = -49
|
|
|
|
exports.ENOCSI = -50
|
|
|
|
exports.EL2HLT = -51
|
|
|
|
exports.EBADE = -52
|
|
|
|
exports.EBADR = -53
|
|
|
|
exports.EXFULL = -54
|
|
|
|
exports.ENOANO = -55
|
|
|
|
exports.EBADRQC = -56
|
|
|
|
exports.EBADSLT = -57
|
|
|
|
exports.EDEADLOCK = -35
|
|
|
|
exports.EBFONT = -59
|
|
|
|
exports.ENOSTR = -60
|
|
|
|
exports.ENODATA = -61
|
|
|
|
exports.ETIME = -62
|
|
|
|
exports.ENOSR = -63
|
|
|
|
exports.ENONET = -64
|
|
|
|
exports.ENOPKG = -65
|
|
|
|
exports.EREMOTE = -66
|
|
|
|
exports.ENOLINK = -67
|
|
|
|
exports.EADV = -68
|
|
|
|
exports.ESRMNT = -69
|
|
|
|
exports.ECOMM = -70
|
|
|
|
exports.EPROTO = -71
|
|
|
|
exports.EMULTIHOP = -72
|
|
|
|
exports.EDOTDOT = -73
|
|
|
|
exports.EBADMSG = -74
|
|
|
|
exports.EOVERFLOW = -75
|
|
|
|
exports.ENOTUNIQ = -76
|
|
|
|
exports.EBADFD = -77
|
|
|
|
exports.EREMCHG = -78
|
|
|
|
exports.ELIBACC = -79
|
|
|
|
exports.ELIBBAD = -80
|
|
|
|
exports.ELIBSCN = -81
|
|
|
|
exports.ELIBMAX = -82
|
|
|
|
exports.ELIBEXEC = -83
|
|
|
|
exports.EILSEQ = -84
|
|
|
|
exports.ERESTART = -85
|
|
|
|
exports.ESTRPIPE = -86
|
|
|
|
exports.EUSERS = -87
|
|
|
|
exports.ENOTSOCK = -88
|
|
|
|
exports.EDESTADDRREQ = -89
|
|
|
|
exports.EMSGSIZE = -90
|
|
|
|
exports.EPROTOTYPE = -91
|
|
|
|
exports.ENOPROTOOPT = -92
|
|
|
|
exports.EPROTONOSUPPORT = -93
|
|
|
|
exports.ESOCKTNOSUPPORT = -94
|
|
|
|
exports.EOPNOTSUPP = -95
|
|
|
|
exports.EPFNOSUPPORT = -96
|
|
|
|
exports.EAFNOSUPPORT = -97
|
|
|
|
exports.EADDRINUSE = -98
|
|
|
|
exports.EADDRNOTAVAIL = -99
|
|
|
|
exports.ENETDOWN = -100
|
|
|
|
exports.ENETUNREACH = -101
|
|
|
|
exports.ENETRESET = -102
|
|
|
|
exports.ECONNABORTED = -103
|
|
|
|
exports.ECONNRESET = -104
|
|
|
|
exports.ENOBUFS = -105
|
|
|
|
exports.EISCONN = -106
|
|
|
|
exports.ENOTCONN = -107
|
|
|
|
exports.ESHUTDOWN = -108
|
|
|
|
exports.ETOOMANYREFS = -109
|
|
|
|
exports.ETIMEDOUT = -110
|
|
|
|
exports.ECONNREFUSED = -111
|
|
|
|
exports.EHOSTDOWN = -112
|
|
|
|
exports.EHOSTUNREACH = -113
|
|
|
|
exports.EALREADY = -114
|
|
|
|
exports.EINPROGRESS = -115
|
|
|
|
exports.ESTALE = -116
|
|
|
|
exports.EUCLEAN = -117
|
|
|
|
exports.ENOTNAM = -118
|
|
|
|
exports.ENAVAIL = -119
|
|
|
|
exports.EISNAM = -120
|
|
|
|
exports.EREMOTEIO = -121
|
|
|
|
exports.EDQUOT = -122
|
|
|
|
exports.ENOMEDIUM = -123
|
|
|
|
exports.EMEDIUMTYPE = -124
|