Don't mess with messy temp dir and just download into extension dir

Temp dirs are messy. Dealing with them requires handling quite a bunch of
edge cases. As proposed by lnicola this seems better to just put the temp files
in the extension dir and not care much about suddenly leaving garbage.
Instead we get shorter and less platform-caveat-y code.
We will also assume users don't try to issue a download in different vscode windows simultaneously
This commit is contained in:
Veetaha 2020-06-24 13:19:14 +03:00
parent 5d84e27ba9
commit 62ebaa822b

View File

@ -2,8 +2,6 @@ import fetch from "node-fetch";
import * as vscode from "vscode"; import * as vscode from "vscode";
import * as stream from "stream"; import * as stream from "stream";
import * as fs from "fs"; import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as util from "util"; import * as util from "util";
import { log, assert } from "./util"; import { log, assert } from "./util";
@ -68,32 +66,31 @@ interface DownloadOpts {
} }
export async function download(opts: DownloadOpts) { export async function download(opts: DownloadOpts) {
// Put the artifact into a temporary folder to prevent partially downloaded files when user kills vscode // Put artifact into a temporary file (in the same dir for simplicity)
await withTempDir(async tempDir => { // to prevent partially downloaded files when user kills vscode
const tempFile = path.join(tempDir, path.basename(opts.dest)); const tempFile = `${opts.dest}.tmp`;
await vscode.window.withProgress( await vscode.window.withProgress(
{ {
location: vscode.ProgressLocation.Notification, location: vscode.ProgressLocation.Notification,
cancellable: false, cancellable: false,
title: opts.progressTitle title: opts.progressTitle
}, },
async (progress, _cancellationToken) => { async (progress, _cancellationToken) => {
let lastPercentage = 0; let lastPercentage = 0;
await downloadFile(opts.url, tempFile, opts.mode, (readBytes, totalBytes) => { await downloadFile(opts.url, tempFile, opts.mode, (readBytes, totalBytes) => {
const newPercentage = (readBytes / totalBytes) * 100; const newPercentage = (readBytes / totalBytes) * 100;
progress.report({ progress.report({
message: newPercentage.toFixed(0) + "%", message: newPercentage.toFixed(0) + "%",
increment: newPercentage - lastPercentage increment: newPercentage - lastPercentage
});
lastPercentage = newPercentage;
}); });
}
);
await moveFile(tempFile, opts.dest); lastPercentage = newPercentage;
}); });
}
);
await fs.promises.rename(tempFile, opts.dest);
} }
/** /**
@ -137,34 +134,3 @@ async function downloadFile(
// https://github.com/rust-analyzer/rust-analyzer/issues/3167 // https://github.com/rust-analyzer/rust-analyzer/issues/3167
}); });
} }
async function withTempDir(scope: (tempDirPath: string) => Promise<void>) {
// Based on the great article: https://advancedweb.hu/secure-tempfiles-in-nodejs-without-dependencies/
// `.realpath()` should handle the cases where os.tmpdir() contains symlinks
const osTempDir = await fs.promises.realpath(os.tmpdir());
const tempDir = await fs.promises.mkdtemp(path.join(osTempDir, "rust-analyzer"));
try {
return await scope(tempDir);
} finally {
// We are good citizens :D
void fs.promises.rmdir(tempDir, { recursive: true }).catch(log.error);
}
};
async function moveFile(src: fs.PathLike, dest: fs.PathLike) {
try {
await fs.promises.rename(src, dest);
} catch (err) {
if (err.code === 'EXDEV') {
// We are probably moving the file across partitions/devices
await fs.promises.copyFile(src, dest);
await fs.promises.unlink(src);
} else {
log.error(`Failed to rename the file ${src} -> ${dest}`, err);
throw err;
}
}
}