forked from hertzg/node-mount
-
Notifications
You must be signed in to change notification settings - Fork 11
/
mount.js
263 lines (199 loc) · 5.36 KB
/
mount.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
"use strict";
var fs = require('fs')
var detectSeries = require('async').detectSeries
var _binding = require('./build/Release/mount')
const IS_LINUX = require('os').platform() === 'linux'
module.exports =
{
_binding: _binding,
mount: _mount,
umount: _umount,
mountSync: _mountSync,
umountSync: _umountSync,
overrideOrder: ['ext4', 'ext3', 'ext2'],
MS_RDONLY: 1,
MS_NOSUID: 2,
MS_NODEV: 4,
MS_NOEXEC: 8,
MS_SYNCHRONOUS: 16,
MS_REMOUNT: 32,
MS_MANDLOCK: 64,
MS_DIRSYNC: 128,
MS_NOATIME: 1024,
MS_NODIRATIME: 2048,
MS_BIND: 4096,
MS_MOVE: 8192,
MS_REC: 16384,
MS_VERBOSE: 32768,
MS_SILENT: 32768,
MS_POSIXACL: (1<<16),
MS_UNBINDABLE: (1<<17),
MS_PRIVATE: (1<<18),
MS_SLAVE: (1<<19),
MS_SHARED: (1<<20),
MS_RELATIME: (1<<21),
MS_KERNMOUNT: (1<<22),
MS_I_VERSION: (1<<23),
MS_STRICTATIME: (1<<24),
MS_NOSEC: (1<<28),
MS_BORN: (1<<29),
MS_ACTIVE: (1<<30),
MS_NOUSER: (1<<31),
}
function makeMountFlags_reduce(flags, option)
{
option = option.toLowerCase()
switch(option)
{
case "bind":
flags |= module.exports.MS_BIND;
break
case "readonly":
flags |= module.exports.MS_RDONLY;
break
case "remount":
flags |= module.exports.MS_REMOUNT;
break
case "noexec":
flags |= module.exports.MS_NOEXEC;
break
default:
throw new Error("Invalid option: "+option);
}
return flags
}
function makeMountFlags(array)
{
return array.reduce(makeMountFlags_reduce, 0)
}
/**
* Create a comma-separated `key=value` list from an object
*/
function makeMountDataStr(object)
{
var result = []
for(var key in object)
result.push(key+'='+object[key])
return result.join(',')
}
function checkArguments(target, fsType, options, dataStr, callback)
{
if(target === undefined) throw new Error('target is mandatory')
if(typeof fsType === 'number' || fsType instanceof Array)
{
callback = dataStr
dataStr = options
options = fsType
fsType = undefined
}
if(options != null && options.constructor.name === 'Object'
|| options instanceof Function)
{
callback = dataStr
dataStr = options
options = undefined
}
if(dataStr instanceof Function)
{
callback = dataStr
dataStr = undefined
}
// default values
fsType = fsType || 'auto'
options = options || 0
dataStr = dataStr || ''
// ensure that `options` is an array or number
if(typeof options !== 'number' && options.constructor !== Array)
throw new Error('options must be an array or a number, not '+typeof options)
// ensure that `dataStr` is a string or a literal object
if(typeof dataStr !== 'string' && dataStr.constructor !== Object)
throw new Error('dataStr must be a string or an object, not '+typeof dataStr)
if(options instanceof Array)
options = makeMountFlags(options)
var devFile = fsType
if(dataStr.constructor === Object)
{
if(IS_LINUX)
{
devFile = dataStr.devFile || fsType // TODO: find correct BSD flag name
delete dataStr.devFile
}
dataStr = makeMountDataStr(dataStr)
}
return [devFile, target, fsType, options, dataStr, callback]
}
function removeNoDev(value)
{
return value && value.indexOf('nodev') < 0
}
function trim(value)
{
return value.trim()
}
function removeDuplicates(item, pos, self)
{
return self.indexOf(item) === pos;
}
function fsList(data)
{
var supported = data.split('\n').filter(removeNoDev).map(trim)
return module.exports.overrideOrder.concat(supported).filter(removeDuplicates)
}
function _mount(target, fsType, options, dataStr, cb)
{
var argv = checkArguments(target, fsType, options, dataStr, cb)
cb = argv[5]
//Last param is always callback
if(typeof cb !== 'function')
throw new Error('Last argument must be a callback function')
if(argv[2] === 'auto' && !(argv[3] & module.exports.MS_MOVE))
fs.readFile('/proc/filesystems', 'utf8', function(error, filesystems)
{
if(error) return cb(error)
filesystems = fsList(filesystems)
detectSeries(filesystems, function(item, callback)
{
argv[2] = item
argv[5] = function(error)
{
callback(!error)
}
_binding.mount.apply(_binding, argv)
},
function(result)
{
cb(result ? null : new Error('Unknown filesystem for ' + target))
})
})
else
_binding.mount.apply(_binding, argv)
}
function _mountSync(target, fsType, options, dataStr)
{
var argv = checkArguments(target, fsType, options, dataStr)
argv.length = 5
if(argv[2] === 'auto' && !(argv[3] & module.exports.MS_MOVE))
{
var filesystems = fs.readFileSync('/proc/filesystems', 'utf8')
filesystems = fsList(filesystems)
for(argv[2] in filesystems)
if(_binding.mountSync.apply(_binding, argv))
return true
throw new Error('Unknown filesystem for ' + target)
}
return _binding.mountSync.apply(_binding, argv)
}
function _umount(target, cb)
{
//Require exactly 2 parameters
if(arguments.length !== 2 || typeof cb !== 'function')
throw new Error('Invalid arguments')
_binding.umount(target, cb)
}
function _umountSync(target)
{
//Require exactly 1 parameter
if(typeof target !== 'string')
throw new Error('Invalid arguments')
return _binding.umountSync(target)
}