3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2026-03-04 07:40:24 +00:00

new restore technique

This commit is contained in:
Jonathan Kelley 2025-01-28 22:28:27 -08:00
parent 64b8867183
commit 01addf7215
No known key found for this signature in database
GPG key ID: 1FBB50F7EB0A08BE
6 changed files with 157 additions and 182 deletions

View file

@ -1,50 +1,30 @@
import * as core from "@actions/core";
// import * as core from "@actions/core";
// import * as io from "@actions/io";
import fs from "fs";
import path from "path";
// import { CARGO_HOME } from "./config";
import { exists } from "./utils";
// import { exists } from "./utils";
// import { Packages } from "./workspace";
export async function restoreIncremental(targetDir: string) {
core.debug(`restoring incremental directory "${targetDir}"`);
export async function saveMtimes(targetDirs: string[]): Promise<Map<string, number>> {
let cache = new Map<string, number>();
let stack = targetDirs.slice();
let 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"))) || (await exists(path.join(dirName, ".rustc_info.json")));
while (stack.length > 0) {
const dirName = stack.pop()!;
const dir = await fs.promises.opendir(dirName);
try {
if (isNestedTarget) {
await restoreIncremental(dirName);
} else {
await restoreIncrementalProfile(dirName);
} restoreIncrementalProfile
} catch { }
for await (const dirent of dir) {
if (dirent.isDirectory()) {
stack.push(path.join(dirName, dirent.name));
} else {
const fileName = path.join(dirName, dirent.name);
const { mtime } = await fs.promises.stat(fileName);
cache.set(fileName, mtime.getTime());
}
}
}
}
async function restoreIncrementalProfile(dirName: string) {
core.debug(`restoring incremental profile directory "${dirName}"`);
const incrementalJson = path.join(dirName, "incremental-restore.json");
if (await exists(incrementalJson)) {
const contents = await fs.promises.readFile(incrementalJson, "utf8");
const { modifiedTimes } = JSON.parse(contents);
core.debug(`restoring incremental profile directory "${dirName}" with ${modifiedTimes} files`);
// Write the mtimes to all the files in the profile directory
for (const fileName of Object.keys(modifiedTimes)) {
const mtime = modifiedTimes[fileName];
const filePath = path.join(dirName, fileName);
await fs.promises.utimes(filePath, new Date(mtime), new Date(mtime));
}
} else {
core.debug(`incremental-restore.json not found for ${dirName}`);
}
return cache;
}