feat(copies): add verified space-saving copies

Offer same-volume hard links for sampled 2.61.0.43 resources after checking their content and keeping mutable files independent. Support converting existing copies and detach registered links during version updates.
This commit is contained in:
N0VAN0M4LY committed 2026-11-30 05:51:22 +08:00
1 parent 0881d62766
commit 2a97b0d46c
22 files changed
+3118 -111

No files matched your search

@@ -1,26 +1,11 @@
import { lstat, readdir } from 'node:fs/promises';
import { join } from 'node:path';
import type { FolderSize } from '@shared/schemas/install.ts';
import { planCopy } from '../install/copy-game.ts';
import { hardlinkResource } from '../install/hardlink-files.ts';
/**
* The size of a folder tree, counted the way the copy will copy it: regular files only. Links
* and junctions are not followed. Asynchronous, so the window stays responsive on a large
* installation.
*/
export async function measureFolder(root: string): Promise<FolderSize> {
let bytes = 0;
let files = 0;
const pending = [root];
while (pending.length) {
const folder = pending.pop()!;
for (const entry of await readdir(folder, { withFileTypes: true })) {
const path = join(folder, entry.name);
if (entry.isDirectory()) pending.push(path);
else if (entry.isFile()) {
bytes += (await lstat(path)).size;
files += 1;
}
}
}
return { bytes, files };
/** Estimate the installation size and the bytes eligible for content verification. */
export async function measureFolder(root: string, version?: string | null): Promise<FolderSize> {
const plan = await planCopy(root);
const linkableBytes = plan.files.reduce((total, file) =>
total + (hardlinkResource(version, file.relativePath, file.bytes)?.bytes ?? 0), 0);
return { bytes: plan.totalBytes, files: plan.files.length, linkableBytes };
}
@@ -1,5 +1,5 @@
import { readdir, realpath, stat, statfs } from 'node:fs/promises';
import { dirname, parse, relative, resolve, isAbsolute } from 'node:path';
import { dirname, relative, resolve, isAbsolute } from 'node:path';
import { isInSteamLibrary } from '@wto/patcher';
import type { TargetFolderInspection } from '@shared/schemas/install.ts';
@@ -15,10 +15,26 @@ export async function inspectTargetFolder(path: string, stockPath: string): Prom
problem: await targetProblem(target, stock),
inSteamLibrary: isInSteamLibrary(target),
freeBytes: await freeBytes(target),
sameDriveAsStock: parse(target).root.toLowerCase() === parse(stock).root.toLowerCase(),
sameDriveAsStock: await sameVolume(target, stock),
};
}
/** Compare actual volumes, including directories reached through junctions. */
export async function sameVolume(left: string, right: string): Promise<boolean> {
const [first, second] = await Promise.all([volumeDevice(left), volumeDevice(right)]);
return first !== null && first === second;
}
async function volumeDevice(path: string): Promise<bigint | null> {
for (let folder = resolve(path); ; folder = dirname(folder)) {
try {
return (await stat(folder, { bigint: true })).dev || null;
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT' || dirname(folder) === folder) return null;
}
}
}
async function targetProblem(target: string, stock: string): Promise<TargetFolderInspection['problem']> {
if (contains(stock, target) || contains(target, stock)) return 'overlaps_stock';
let entries: string[];
@@ -39,7 +55,7 @@ function contains(outer: string, inner: string): boolean {
}
/** Free bytes on the drive of the nearest existing folder at or above `path`. */
async function freeBytes(path: string): Promise<number | null> {
export async function freeBytes(path: string): Promise<number | null> {
for (let folder = path; ; folder = dirname(folder)) {
try {
const info = await statfs(folder);