forked from ryyppy/node-mount
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mount.js
172 lines (141 loc) · 4.22 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
"use strict";
var _binding = require('./build/Release/mount')
, Util = require('util')
;
module.exports = {
_binding: _binding,
mount: _mount,
umount: _umount,
mountSync: _mountSync,
umountSync: _umountSync,
unmountSync: Util.deprecate(_umount, 'mount.unmountSync: Use mount.umountSync() instead'),
unmount: Util.deprecate(_umount, 'mount.unmount: Use mount.umount() instead'),
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(array) {
var flags = 0
for(var i=0; i<array.length; i++) {
var option = array[i].toLowerCase()
if(option == "bind") {
flags |= module.exports.MS_BIND;
} else if(option == "readonly") {
flags |= module.exports.MS_RDONLY;
} else if(option == "remount") {
flags |= module.exports.MS_REMOUNT;
} else if(option == "noexec"){
flags |= module.exports.MS_NOEXEC;
} else {
throw new Error("Invalid option: "+option);
}
}
return flags
}
function _mount() {
var argc = arguments.length
;
//At least [devFile, target, fsType, cb]
if(argc < 4 || typeof arguments[argc-1] !== 'function') {
throw new Error('Invalid arguments')
}
var cb = arguments[argc-1];
//ensure that options is an array or number
if(argc > 4 && (typeof arguments[3] !== 'number' && (typeof arguments[3] === 'object' && arguments[3].constructor !== Array))) {
throw new Error('Argument options must be an array or number')
}
if(argc > 5 && typeof arguments[4] !== 'string') {
throw new Error('Argument dataStr must be a string')
}
if(argc > 6) {
throw new Error('Too many arguments')
}
//Last param is always callback
var devFile = String(arguments[0])
, target = String(arguments[1])
, fsType = String(arguments[2])
, options = 0
, dataStr = ''
;
if(argc === 5) {
options = arguments[3]
} else if(argc === 6) {
options = arguments[3]
dataStr = arguments[4]
}
if(options.constructor !== Number) {
options = __makeMountFlags(options)
}
_binding.mount(devFile, target, fsType, options, dataStr, cb)
}
function _mountSync() {
var argc = arguments.length;
if(argc < 3) {
throw new Error('Invalid arguments')
}
//ensure that options is an array or number
if(argc > 4 && (typeof arguments[3] !== 'number' && (typeof arguments[3] === 'object' && arguments[3].constructor !== Array))) {
throw new Error('Argument options must be an array or number')
}
if(argc > 4 && typeof arguments[4] !== 'string') {
throw new Error('Argument dataStr must be a string')
}
if(argc > 5) {
throw new Error('Too many arguments')
}
var devFile = String(arguments[0])
, target = String(arguments[1])
, fsType = String(arguments[2])
, options = []
, dataStr = ''
;
if(argc === 4) {
options = arguments[3]
} else if(argc === 5) {
options = arguments[3]
dataStr = arguments[4]
}
if(options.constructor !== Number) {
options = __makeMountFlags(options)
}
return _binding.mountSync(devFile, target, fsType, options, dataStr);
}
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)
}