-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathFileUtilities.ts
More file actions
361 lines (342 loc) · 13.8 KB
/
FileUtilities.ts
File metadata and controls
361 lines (342 loc) · 13.8 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* --------------------------------------------------------------------------------------------
* Licensed to the .NET Foundation under one or more agreements.
* The .NET Foundation licenses this file to you under the MIT license.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import { exec } from 'child_process';
import * as crypto from 'crypto';
import * as eol from 'eol';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { promisify } from 'util';
import { SYSTEM_INFORMATION_CACHE_DURATION_MS } from '../Acquisition/CacheTimeConstants';
import { IAcquisitionWorkerContext } from '../Acquisition/IAcquisitionWorkerContext';
import { IEventStream } from '../EventStream/EventStream';
import
{
DotnetCommandFallbackArchitectureEvent,
DotnetCommandFallbackOSEvent,
DotnetFileWriteRequestEvent,
EmptyDirectoryToWipe,
FileIsBusy,
FileIsNotBusy,
FileToWipe,
SuppressedAcquisitionError
} from '../EventStream/EventStreamEvents';
import { CommandExecutor } from './CommandExecutor';
import { IFileUtilities } from './IFileUtilities';
import { IUtilityContext } from './IUtilityContext';
import { getDotnetExecutable } from './TypescriptUtilities';
export class FileUtilities extends IFileUtilities
{
public async writeFileOntoDisk(scriptContent: string, filePath: string, eventStream?: IEventStream)
{
eventStream?.post(new DotnetFileWriteRequestEvent(`Request to write ${filePath}`, new Date().toISOString(), filePath));
if (!(await this.exists(path.dirname(filePath))))
{
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
}
if (!(fs.existsSync(filePath)))
{
fs.writeFileSync(filePath, '');
}
await this.innerWriteFile(scriptContent, filePath, eventStream);
}
private async innerWriteFile(scriptContent: string, filePath: string, eventStream?: IEventStream)
{
scriptContent = eol.auto(scriptContent);
const existingScriptContent = await this.read(filePath);
// fs.writeFile will replace the file if it exists.
// https://nodejs.org/api/fs.html#fswritefilefile-data-options-callback
if (scriptContent !== existingScriptContent)
{
await fs.promises.writeFile(filePath, scriptContent);
eventStream?.post(new DotnetFileWriteRequestEvent(`File content needed to be updated.`, new Date().toISOString(), filePath));
}
else
{
eventStream?.post(new DotnetFileWriteRequestEvent(`File content is an exact match, not writing file.`, new Date().toISOString(), filePath));
}
await fs.promises.chmod(filePath, 0o744);
}
/**
* @param directoryToWipe the directory to delete all of the files in if privilege to do so exists.
* @param fileExtensionsToDelete - if undefined, delete all files. if not, delete only files with extensions in this array in lower case.
*/
public async wipeDirectory(directoryToWipe: string, eventStream?: IEventStream, fileExtensionsToDelete?: string[], verifyDotnetNotInUse = false)
{
if (!(await this.exists(directoryToWipe)))
{
eventStream?.post(new EmptyDirectoryToWipe(`The directory ${directoryToWipe} did not exist, so it was not wiped.`))
return;
}
else if (verifyDotnetNotInUse && await FileUtilities.fileIsOpen(path.join(directoryToWipe, getDotnetExecutable()), eventStream))
{
return;
}
const directoryEntries: string[] = await fs.promises.readdir(directoryToWipe);
for (const entry of directoryEntries)
{
try
{
eventStream?.post(new FileToWipe(`Path ${entry} may be deleted.`))
if (!fileExtensionsToDelete || fileExtensionsToDelete?.includes(path.extname(entry).toLocaleLowerCase()))
{
eventStream?.post(new FileToWipe(`Path ${entry} is being deleted -- if no error is reported, it should be wiped.`))
await fs.promises.rm(path.join(directoryToWipe, entry), { recursive: true, force: true });
}
}
catch (error: any)
{
eventStream?.post(new SuppressedAcquisitionError(error, `Failed to delete ${entry} when marked for deletion.`));
}
};
}
public async read(filePath: string): Promise<string>
{
try
{
const output = await fs.promises.readFile(filePath, 'utf8');
return output;
}
catch (error: any)
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
throw new Error(`Failed to read file ${filePath}: ${error?.message}`);
}
}
public async exists(filePath: string): Promise<boolean>
{
try
{
await fs.promises.stat(filePath);
return true;
}
catch
{
return false;
}
}
public async realpath(filePath: string): Promise<string | null>
{
try
{
const resolvedRealPath = await promisify(fs.realpath)(filePath);
return resolvedRealPath;
}
catch
{
return null;
}
}
/**
*
* @param nodeArchitecture the architecture in node style string of what to install
* @returns the architecture in the style that .net / the .net install scripts expect
*
* Node - amd64 is documented as an option for install scripts but its no longer used.
* s390x is also no longer used.
* ppc64le is supported but this version of node has no distinction of the endianness of the process.
* It has no mapping to mips or other node architectures.
*
* @remarks Falls back to string 'auto' if a mapping does not exist which is not a valid architecture.
*/
public nodeArchToDotnetArch(nodeArchitecture: string, eventStream: IEventStream)
{
switch (nodeArchitecture)
{
case 'x64': {
return nodeArchitecture;
}
case 'ia32': {
return 'x86';
}
case 'x86': {
// In case the function is called twice
return 'x86';
}
case 'arm': {
return nodeArchitecture;
}
case 'arm64': {
return nodeArchitecture;
}
case 's390x': {
return 's390x';
}
default: {
eventStream.post(new DotnetCommandFallbackArchitectureEvent(`The architecture ${os.arch()} of the platform is unexpected, falling back to auto-arch.`));
return 'auto';
}
}
}
/**
*
* @param nodeArchitecture the architecture output of dotnet --info from the runtime
* @returns the architecture in the style that node expects
*
* @remarks Falls back to string 'auto' if a mapping does not exist which is not a valid architecture.
* So far, the outputs are actually all identical so this is not really 'needed' but good to have in the future :)
*/
public static dotnetInfoArchToNodeArch(dotnetInfoArch: string, eventStream: IEventStream)
{
switch (dotnetInfoArch)
{
case 'x64': {
return dotnetInfoArch;
}
case 'x86': {
// In case the function is called twice
return dotnetInfoArch;
}
case 'arm': { // This shouldn't be an output yet, but its possible in the future
return dotnetInfoArch;
}
case 'arm64': {
return dotnetInfoArch;
}
default: {
eventStream.post(new DotnetCommandFallbackArchitectureEvent(`The architecture ${dotnetInfoArch} of the platform is unexpected, falling back to auto-arch.`));
return 'auto';
}
}
}
/**
*
* @param nodeOS the OS in node style string of what to install
* @returns the OS in the style that .net / the .net install scripts expect
*
*/
public nodeOSToDotnetOS(nodeOS: string, eventStream: IEventStream)
{
switch (nodeOS)
{
case 'win32': {
return 'win';
}
case 'darwin': {
return 'osx';
}
case 'linux': {
return nodeOS;
}
default: {
eventStream.post(new DotnetCommandFallbackOSEvent(`The OS ${os.platform()} of the platform is unexpected, falling back to auto-os.`));
return 'auto'
}
}
}
public static async fileIsOpen(filePath: string, eventStream?: IEventStream): Promise<boolean>
{
let fileHandle: fs.promises.FileHandle | null = null;
if (os.platform() === 'win32')
{
try
{
// eslint-disable-next-line no-bitwise
fileHandle = await fs.promises.open(filePath, fs.constants.O_RDONLY | 0x10000000); // 0x10000000 is the FILE_FLAG_NO_BUFFERING flag to try to hold an exclusive 'lock'
}
catch (error: any)
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (error?.code === 'EBUSY')
{
eventStream?.post(new FileIsBusy(`The file ${filePath} is busy due to another file handle`));
return true;
}
}
finally
{
await fileHandle?.close();
}
eventStream?.post(new FileIsNotBusy(`The file ${filePath} is not busy due to another file handle`));
return false;
}
else
{
try
{
return promisify(exec)(`lsof ${filePath}`).then(
fulfilled =>
{
const lines = fulfilled?.stdout?.toString().split('\n');
if (lines.length > 1) // lsof returns a header line and then the lines of open files
{
eventStream?.post(new FileIsBusy(`The file ${filePath} is busy due to another file handle as lsof shows`));
return Promise.resolve(true);
}
else
{
eventStream?.post(new FileIsBusy(`The file ${filePath} is busy due to another file handle as lsof is empty`));
return Promise.resolve(false);
}
},
rejected =>
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (rejected?.code?.toString() === 'EACCESS')
{
eventStream?.post(new FileIsBusy(`The file ${filePath} is presumed busy due to EACCESS`));
return Promise.resolve(true);
}
return Promise.resolve(false); // lsof returns a non-zero exit code if the file is not open by any process. EACCESS is thrown if the user does not have permission to run lsof on that file.
// ENOENT or others may be thrown if the file DNE, but we only care if its open.
}).finally(() => { return false; });
}
catch (error: any)
{
eventStream?.post(new SuppressedAcquisitionError(error, `Failed to check if file ${filePath} is open.`));
return false;
}
}
}
/**
*
* @returns true if the process is running with admin privileges
*/
public async isElevated(context: IAcquisitionWorkerContext, utilContext: IUtilityContext): Promise<boolean>
{
const executor = new CommandExecutor(context, utilContext);
if (os.platform() !== 'win32')
{
try
{
const commandResult = await executor.execute(CommandExecutor.makeCommand('id', ['-u']), { shell: true, dotnetInstallToolCacheTtlMs: SYSTEM_INFORMATION_CACHE_DURATION_MS }, false);
return commandResult.status === '0';
}
catch (error: any)
{
context.eventStream?.post(new SuppressedAcquisitionError(error, `Failed to run 'id' to check for privilege, running without privilege.`))
return false;
}
}
try
{
// If we can execute this command on Windows then we have admin rights.
const _ = await executor.execute(CommandExecutor.makeCommand('net', ['session']), { 'stdio': 'ignore', dotnetInstallToolCacheTtlMs: SYSTEM_INFORMATION_CACHE_DURATION_MS });
return true;
}
catch (error: any)
{
context.eventStream?.post(new SuppressedAcquisitionError(error, `Failed to run 'net' to check for privilege, running without privilege.`))
return false;
}
}
private sha512Hasher(filePath: string)
{
return new Promise<string>((resolve, reject) =>
{
const hash = crypto.createHash('sha512');
const fileStream = fs.createReadStream(filePath);
fileStream.on('error', err => reject(err));
fileStream.on('data', chunk => hash.update(chunk));
fileStream.on('end', () => resolve(hash.digest('hex')));
})
};
public async getFileHash(filePath: string): Promise<string | null>
{
const res = await this.sha512Hasher(filePath);
return res;
}
}