This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
204 lines (180 loc) · 5.74 KB
/
index.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
const fs = require('fs')
const { spawn, spawnSync } = require('child_process')
const EventEmitter = require('events')
const binding = require('./build/Release/fs_admin.node')
const fsAdmin = module.exports
fsAdmin.testMode = false
switch (process.platform) {
case 'darwin':
Object.assign(fsAdmin, {
clearAuthorizationCache () {
binding.clearAuthorizationCache()
},
createWriteStream (filePath) {
let authopen
// Prompt for credentials synchronously to avoid creating multiple simultaneous prompts.
if (!binding.spawnAsAdmin('/bin/echo', [], fsAdmin.testMode, () => {})) {
const result = new EventEmitter()
result.write = result.end = function () {}
process.nextTick(() => result.emit('error', new Error('Failed to obtain credentials')))
return result
}
if (fsAdmin.testMode) {
authopen = spawn('/bin/dd', ['of=' + filePath])
} else {
authopen = spawn('/usr/libexec/authopen', ['-extauth', '-w', '-c', filePath])
authopen.stdin.write(binding.getAuthorizationForm())
}
const result = new EventEmitter()
result.write = (chunk, encoding, callback) => {
authopen.stdin.write(chunk, encoding, callback)
}
result.end = (callback) => {
if (callback) result.on('finish', callback)
authopen.stdin.end()
}
authopen.on('exit', (exitCode) => {
if (exitCode !== 0) {
result.emit('error', new Error('authopen exited with code ' + exitCode))
}
result.emit('finish')
})
return result
},
symlink (target, filePath, callback) {
binding.spawnAsAdmin(
'/bin/ln',
['-s', target, filePath],
fsAdmin.testMode,
wrapCallback('ln', callback)
)
},
unlink (filePath, callback) {
binding.spawnAsAdmin(
'/bin/rm',
['-rf', filePath],
fsAdmin.testMode,
wrapCallback('rm', callback)
)
},
makeTree (directoryPath, callback) {
binding.spawnAsAdmin(
'/bin/mkdir',
['-p', directoryPath],
fsAdmin.testMode,
wrapCallback('mkdir', callback)
)
},
recursiveCopy (sourcePath, destinationPath, callback) {
binding.spawnAsAdmin(
'/bin/rm',
['-r', '-f', destinationPath],
fsAdmin.testMode,
wrapCallback('rm', (error) => {
if (error) return callback(error)
binding.spawnAsAdmin(
'/bin/cp',
['-r', sourcePath, destinationPath],
fsAdmin.testMode,
wrapCallback('cp', callback)
)
})
)
}
})
break
case 'win32':
Object.assign(fsAdmin, {
symlink (target, filePath, callback) {
binding.spawnAsAdmin(
'cmd',
['/c', 'mklink', '/j', filePath, target],
fsAdmin.testMode,
wrapCallback('mklink', callback)
)
},
unlink (filePath, callback) {
fs.stat(filePath, (error, status) => {
if (error) return callback(error)
if (status.isDirectory()) {
binding.spawnAsAdmin(
'cmd',
['/c', 'rmdir', '/s', '/q', filePath],
fsAdmin.testMode,
wrapCallback('rmdir', callback)
)
} else {
binding.spawnAsAdmin(
'cmd',
['/c', 'del', '/f', '/q', filePath],
fsAdmin.testMode,
wrapCallback('del', callback)
)
}
})
},
makeTree (directoryPath, callback) {
binding.spawnAsAdmin(
'cmd',
['/c', 'mkdir', directoryPath],
fsAdmin.testMode,
wrapCallback('mkdir', callback)
)
},
recursiveCopy (sourcePath, destinationPath, callback) {
binding.spawnAsAdmin(
'cmd',
['/c', require.resolve('./src/copy-folder.cmd'), sourcePath, destinationPath],
fsAdmin.testMode,
wrapCallback('robocopy', callback)
)
}
})
break
case 'linux':
Object.assign(fsAdmin, {
clearAuthorizationCache () {
spawnSync('/bin/pkcheck', ['--revoke-temp'])
},
createWriteStream (filePath) {
if (!fsAdmin.testMode) {
// Prompt for credentials synchronously to avoid creating multiple simultaneous prompts.
const noopCommand = spawnSync('/usr/bin/pkexec', ['/bin/dd'])
if (noopCommand.error || noopCommand.status !== 0) {
const result = new EventEmitter()
result.write = result.end = function () {}
process.nextTick(() => {
result.emit('error', new Error('Failed to obtain credentials'))
})
return result
}
}
const dd = fsAdmin.testMode
? spawn('/bin/dd', ['of=' + filePath])
: spawn('/usr/bin/pkexec', ['/bin/dd', 'of=' + filePath])
const stream = new EventEmitter()
stream.write = (chunk, encoding, callback) => {
dd.stdin.write(chunk, encoding, callback)
}
stream.end = (callback) => {
if (callback) stream.on('finish', callback)
dd.stdin.end()
}
dd.on('exit', (exitCode) => {
if (exitCode !== 0) {
stream.emit('error', new Error('dd exited with code ' + exitCode))
}
stream.emit('finish')
})
return stream
}
})
break
}
function wrapCallback (commandName, callback) {
return (exitCode) => callback(
exitCode === 0
? null
: new Error(commandName + ' failed with exit status ' + exitCode)
)
}