fix: Windows malicious file analysis waiting problem (#398)

Co-authored-by: Steven <steven@ceriously.com>
This commit is contained in:
Henry 2024-02-27 10:16:55 +01:00 committed by GitHub
parent 967e2666b5
commit 295a1cdb9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 30 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import type {Dir} from 'fs';
import Module from 'module';
import path from 'path';
import semver from 'semver';
import {setTimeout as setTimeoutPromise} from 'timers/promises';
import * as engine from './Engine';
import * as debugUtils from './debugUtils';
@ -213,7 +214,11 @@ export async function installVersion(installTarget: string, locator: Locator, {s
await fs.promises.mkdir(path.dirname(installFolder), {recursive: true});
try {
if (process.platform === `win32`) {
await renameUnderWindows(tmpFolder, installFolder);
} else {
await fs.promises.rename(tmpFolder, installFolder);
}
} catch (err) {
if (
(err as nodeUtils.NodeError).code === `ENOTEMPTY` ||
@ -260,6 +265,30 @@ export async function installVersion(installTarget: string, locator: Locator, {s
};
}
async function renameUnderWindows(oldPath: fs.PathLike, newPath: fs.PathLike) {
// Windows malicious file analysis blocks files currently under analysis, so we need to wait for file release
const retries = 5;
for (let i = 0; i < retries; i++) {
try {
await fs.promises.rename(oldPath, newPath);
break;
} catch (err) {
if (
(
(err as nodeUtils.NodeError).code === `ENOENT` ||
(err as nodeUtils.NodeError).code === `EPERM`
) &&
i < (retries - 1)
) {
await setTimeoutPromise(100 * 2 ** i);
continue;
} else {
throw err;
}
}
}
}
/**
* Loads the binary, taking control of the current process.
*/