mirror of
https://github.com/Swatinem/rust-cache
synced 2026-09-14 20:13:25 +00:00
fix: support Cargo V2 build dir layout (#371)
This commit is contained in:
+19
-6
@@ -245,6 +245,12 @@ const ONE_WEEK = 7 * 24 * 3600 * 1000;
|
|||||||
* Otherwise, it will remove everything that does not match any string in the
|
* Otherwise, it will remove everything that does not match any string in the
|
||||||
* `keepPrefix` set.
|
* `keepPrefix` set.
|
||||||
* The matching strips and trailing `-$hash` suffix.
|
* The matching strips and trailing `-$hash` suffix.
|
||||||
|
*
|
||||||
|
* Cargo's newer `build-dir` layout (rust-lang/cargo#17258) nests the hash as
|
||||||
|
* a subdirectory instead of appending it, so entries there are bare package
|
||||||
|
* names with no suffix to strip. Check for an exact match first so those
|
||||||
|
* names (which may themselves contain hyphens) aren't mistaken for a
|
||||||
|
* `<name>-<hash>` entry from the old layout and truncated incorrectly.
|
||||||
*/
|
*/
|
||||||
async function rmExcept(dirName: string, keepPrefix: Set<string>, checkTimestamp = false) {
|
async function rmExcept(dirName: string, keepPrefix: Set<string>, checkTimestamp = false) {
|
||||||
const dir = await fs.promises.opendir(dirName);
|
const dir = await fs.promises.opendir(dirName);
|
||||||
@@ -262,14 +268,21 @@ async function rmExcept(dirName: string, keepPrefix: Set<string>, checkTimestamp
|
|||||||
|
|
||||||
let name = dirent.name;
|
let name = dirent.name;
|
||||||
|
|
||||||
// strip the trailing hash
|
// in Cargo's V1 layout, all packages are suffixed by their hash.
|
||||||
const idx = name.lastIndexOf("-");
|
// in V2, all package hashes are subdirectories instead.
|
||||||
if (idx !== -1) {
|
// Check both possible naming standards for packages and accept either.
|
||||||
name = name.slice(0, idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// v2 package format
|
||||||
if (!keepPrefix.has(name)) {
|
if (!keepPrefix.has(name)) {
|
||||||
await rm(dir.path, dirent);
|
// now check for v1 package format
|
||||||
|
// strip the trailing hash
|
||||||
|
const idx = name.lastIndexOf("-");
|
||||||
|
if (idx !== -1) {
|
||||||
|
name = name.slice(0, idx);
|
||||||
|
}
|
||||||
|
if (!keepPrefix.has(name)) {
|
||||||
|
await rm(dir.path, dirent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user