3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2025-06-28 02:58:47 +00:00

clean nested and arbitrary profile and target directories

This commit is contained in:
Arpad Borsos 2022-07-09 16:14:38 +02:00
parent 827b33fbd0
commit 86bae2494f
No known key found for this signature in database
GPG key ID: FC7BCA77824B3298
5 changed files with 621 additions and 388 deletions

View file

@ -7,6 +7,27 @@ import { CacheConfig, STATE_BINS } from "./config";
import { Packages } from "./workspace";
export async function cleanTargetDir(targetDir: string, packages: Packages) {
let dir: fs.Dir;
// remove all *files* from the profile directory
dir = await fs.promises.opendir(targetDir);
for await (const dirent of dir) {
if (dirent.isDirectory()) {
let dirName = path.join(dir.path, dirent.name);
// is it a profile dir, or a nested target dir?
let isNestedTarget = await exists(path.join(dirName, "CACHEDIR.TAG"));
try {
if (isNestedTarget) {
await cleanTargetDir(dirName, packages);
} else {
await cleanProfileTarget(dirName, packages);
}
} catch {}
} else if (dirent.name !== "CACHEDIR.TAG") {
await rm(dir.path, dirent);
}
}
await fs.promises.unlink(path.join(targetDir, "./.rustc_info.json"));
// TODO: remove all unknown files, clean all directories like profiles
@ -156,3 +177,12 @@ export async function rm(parent: string, dirent: fs.Dirent) {
}
} catch {}
}
async function exists(path: string) {
try {
await fs.promises.access(path);
return true;
} catch {
return false;
}
}