3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2025-04-26 21:35:35 +00:00

explicitly close dir handles, add more logging, cleanups

This commit is contained in:
Arpad Borsos 2022-09-04 13:10:36 +02:00
parent 213334cd98
commit be4be3720d
No known key found for this signature in database
GPG key ID: FC7BCA77824B3298
5 changed files with 217 additions and 148 deletions

116
dist/save/index.js vendored
View file

@ -64394,6 +64394,7 @@ async function getCmdOutput(cmd, args = [], options = {}) {
;// CONCATENATED MODULE: ./src/workspace.ts
const SAVE_TARGETS = new Set(["lib", "proc-macro"]);
class Workspace {
constructor(root, target) {
this.root = root;
@ -64409,7 +64410,7 @@ class Workspace {
if (pkg.manifest_path.startsWith(this.root)) {
continue;
}
const targets = pkg.targets.filter((t) => t.kind[0] === "lib").map((t) => t.name);
const targets = pkg.targets.filter((t) => t.kind.some((kind) => SAVE_TARGETS.has(kind))).map((t) => t.name);
packages.push({ name: pkg.name, version: pkg.version, targets, path: external_path_default().dirname(pkg.manifest_path) });
}
}
@ -64594,10 +64595,10 @@ async function getRustVersion() {
async function cleanTargetDir(targetDir, packages) {
let dir;
async function cleanTargetDir(targetDir, packages, checkTimestamp = false) {
core.debug(`cleaning target directory "${targetDir}"`);
// remove all *files* from the profile directory
dir = await external_fs_default().promises.opendir(targetDir);
let dir = await external_fs_default().promises.opendir(targetDir);
for await (const dirent of dir) {
if (dirent.isDirectory()) {
let dirName = external_path_default().join(dir.path, dirent.name);
@ -64605,10 +64606,10 @@ async function cleanTargetDir(targetDir, packages) {
let isNestedTarget = (await exists(external_path_default().join(dirName, "CACHEDIR.TAG"))) || (await exists(external_path_default().join(dirName, ".rustc_info.json")));
try {
if (isNestedTarget) {
await cleanTargetDir(dirName, packages);
await cleanTargetDir(dirName, packages, checkTimestamp);
}
else {
await cleanProfileTarget(dirName, packages);
await cleanProfileTarget(dirName, packages, checkTimestamp);
}
}
catch { }
@ -64617,21 +64618,15 @@ async function cleanTargetDir(targetDir, packages) {
await rm(dir.path, dirent);
}
}
await dir.close();
}
async function cleanProfileTarget(profileDir, packages) {
await rmRF(external_path_default().join(profileDir, "examples"));
await rmRF(external_path_default().join(profileDir, "incremental"));
let dir;
// remove all *files* from the profile directory
dir = await external_fs_default().promises.opendir(profileDir);
for await (const dirent of dir) {
if (dirent.isFile()) {
await rm(dir.path, dirent);
}
}
async function cleanProfileTarget(profileDir, packages, checkTimestamp = false) {
core.debug(`cleaning profile directory "${profileDir}"`);
let keepProfile = new Set(["build", ".fingerprint", "deps"]);
await rmExcept(profileDir, keepProfile);
const keepPkg = new Set(packages.map((p) => p.name));
await rmExcept(external_path_default().join(profileDir, "build"), keepPkg);
await rmExcept(external_path_default().join(profileDir, ".fingerprint"), keepPkg);
await rmExcept(external_path_default().join(profileDir, "build"), keepPkg, checkTimestamp);
await rmExcept(external_path_default().join(profileDir, ".fingerprint"), keepPkg, checkTimestamp);
const keepDeps = new Set(packages.flatMap((p) => {
const names = [];
for (const n of [p.name, ...p.targets]) {
@ -64640,7 +64635,7 @@ async function cleanProfileTarget(profileDir, packages) {
}
return names;
}));
await rmExcept(external_path_default().join(profileDir, "deps"), keepDeps);
await rmExcept(external_path_default().join(profileDir, "deps"), keepDeps, checkTimestamp);
}
async function getCargoBins() {
const bins = new Set();
@ -64667,6 +64662,7 @@ async function cleanBin() {
await rm(dir.path, dirent);
}
}
await dir.close();
}
async function cleanRegistry(packages) {
// `.cargo/registry/src`
@ -64683,9 +64679,11 @@ async function cleanRegistry(packages) {
if (await exists(external_path_default().join(dir.path, ".git"))) {
await rmRF(external_path_default().join(dir.path, ".cache"));
}
await dir.close();
// TODO: else, clean `.cache` based on the `packages`
}
}
await indexDir.close();
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
// `.cargo/registry/cache`
const cacheDir = await external_fs_default().promises.opendir(external_path_default().join(CARGO_HOME, "registry", "cache"));
@ -64700,8 +64698,10 @@ async function cleanRegistry(packages) {
await rm(dir.path, dirent);
}
}
await dir.close();
}
}
await cacheDir.close();
}
async function cleanGit(packages) {
const coPath = external_path_default().join(CARGO_HOME, "git", "checkouts");
@ -64722,49 +64722,71 @@ async function cleanGit(packages) {
}
// we have to keep both the clone, and the checkout, removing either will
// trigger a rebuild
let dir;
// clean the db
dir = await external_fs_default().promises.opendir(dbPath);
for await (const dirent of dir) {
if (!repos.has(dirent.name)) {
await rm(dir.path, dirent);
}
}
// clean the checkouts
dir = await external_fs_default().promises.opendir(coPath);
for await (const dirent of dir) {
const refs = repos.get(dirent.name);
if (!refs) {
await rm(dir.path, dirent);
continue;
}
if (!dirent.isDirectory()) {
continue;
}
const refsDir = await external_fs_default().promises.opendir(external_path_default().join(dir.path, dirent.name));
for await (const dirent of refsDir) {
if (!refs.has(dirent.name)) {
await rm(refsDir.path, dirent);
try {
let dir = await external_fs_default().promises.opendir(dbPath);
for await (const dirent of dir) {
if (!repos.has(dirent.name)) {
await rm(dir.path, dirent);
}
}
await dir.close();
}
catch { }
// clean the checkouts
try {
let dir = await external_fs_default().promises.opendir(coPath);
for await (const dirent of dir) {
const refs = repos.get(dirent.name);
if (!refs) {
await rm(dir.path, dirent);
continue;
}
if (!dirent.isDirectory()) {
continue;
}
const refsDir = await external_fs_default().promises.opendir(external_path_default().join(dir.path, dirent.name));
for await (const dirent of refsDir) {
if (!refs.has(dirent.name)) {
await rm(refsDir.path, dirent);
}
}
await refsDir.close();
}
await dir.close();
}
catch { }
}
const ONE_WEEK = 7 * 24 * 3600 * 1000;
async function rmExcept(dirName, keepPrefix) {
/**
* Removes all files or directories in `dirName`, except the ones matching
* any string in the `keepPrefix` set.
*
* The matching strips and trailing `-$hash` suffix.
*
* When the `checkTimestamp` flag is set, this will also remove anything older
* than one week.
*/
async function rmExcept(dirName, keepPrefix, checkTimestamp = false) {
const dir = await external_fs_default().promises.opendir(dirName);
for await (const dirent of dir) {
let name = dirent.name;
// strip the trailing hash
const idx = name.lastIndexOf("-");
if (idx !== -1) {
name = name.slice(0, idx);
}
const fileName = external_path_default().join(dir.path, dirent.name);
const { mtime } = await external_fs_default().promises.stat(fileName);
// we dont really know
if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > ONE_WEEK) {
let isOutdated = false;
if (checkTimestamp) {
const fileName = external_path_default().join(dir.path, dirent.name);
const { mtime } = await external_fs_default().promises.stat(fileName);
isOutdated = Date.now() - mtime.getTime() > ONE_WEEK;
}
if (!keepPrefix.has(name) || isOutdated) {
await rm(dir.path, dirent);
}
}
await dir.close();
}
async function rm(parent, dirent) {
try {