Don't mkdir restore dir in updater

This commit is contained in:
Fedor Indutny 2022-03-04 11:59:47 -08:00 committed by GitHub
parent effe5aae6f
commit df7cdfacc7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 21 deletions

View file

@ -446,7 +446,6 @@ export abstract class Updater {
const targetUpdatePath = join(cacheDir, fileName);
const tempDir = await createTempDir();
const restoreDir = await createTempDir();
const tempUpdatePath = join(tempDir, fileName);
const tempBlockMapPath = join(tempDir, blockMapFileName);
@ -556,7 +555,12 @@ export abstract class Updater {
return undefined;
}
this.logger.info(
'downloadUpdate: Downloaded update, moving into cache dir'
);
// Backup old files
const restoreDir = await getTempDir();
await rename(cacheDir, restoreDir);
// Move the files into the final position
@ -569,9 +573,18 @@ export abstract class Updater {
throw error;
}
try {
await deleteTempDir(restoreDir);
} catch (error) {
this.logger.warn(
'downloadUpdate: Failed to remove backup folder, ignoring',
Errors.toLogFormat(error)
);
}
return { updateFilePath: targetUpdatePath, signature };
} finally {
await Promise.all([deleteTempDir(tempDir), deleteTempDir(restoreDir)]);
await deleteTempDir(tempDir);
}
}
@ -781,14 +794,25 @@ function getBaseTempDir() {
}
export async function createTempDir(): Promise<string> {
const baseTempDir = getBaseTempDir();
const uniqueName = getGuid();
const targetDir = join(baseTempDir, uniqueName);
const targetDir = await getTempDir();
await mkdirpPromise(targetDir);
return targetDir;
}
export async function getTempDir(): Promise<string> {
const baseTempDir = getBaseTempDir();
const uniqueName = getGuid();
// Create parent folder if not already present
if (!(await pathExists(baseTempDir))) {
await mkdirpPromise(baseTempDir);
}
return join(baseTempDir, uniqueName);
}
function getUpdateCacheDir() {
// We only use tmpdir() when this code is run outside of an Electron app (as in: tests)
return app ? getUpdateCachePath(app.getPath('userData')) : tmpdir();