Write a self-describing wtoffline/client.json origin marker (copy, hardlink, or in-place) on install, space-saving compaction, and hardlink-detaching updates, and read it with lightweight hardlink nlink sampling when inspecting existing folders. Allow deleting existing folders only when backed by a valid copy/hardlink marker outside Steam and disjoint from all registered source paths, and add an atomic restore-stock-and-remove option that restores the four stock core files and removes only wto_ws.{dll,cfg,log} and wtoffline/.
44 lines
1.8 KiB
TypeScript
44 lines
1.8 KiB
TypeScript
import { readdir } from 'node:fs/promises';
|
|
import { dirname, join } from 'node:path';
|
|
import { AppError } from '@shared/errors.ts';
|
|
import type { DataFolderProblem } from '@shared/schemas/install.ts';
|
|
import { copyPathContains } from './install-record.ts';
|
|
import { STOCK_BACKUP_FOLDER } from './stock-backup.ts';
|
|
|
|
/**
|
|
* Validate where the offline data folder sits relative to the client and stock folders.
|
|
* A subfolder inside the client (outside wtoffline/) is allowed, while setting dataPath to the
|
|
* client folder itself or an ancestor of it would mix game files into the data root.
|
|
*/
|
|
export function dataFolderProblem(
|
|
dataPath: string,
|
|
copyPath: string | null,
|
|
sourcePath: string | null,
|
|
externalStockPath: string | null,
|
|
): DataFolderProblem | null {
|
|
if (copyPath && (copyPathContains(dataPath, copyPath) ||
|
|
copyPathContains(join(copyPath, dirname(STOCK_BACKUP_FOLDER)), dataPath))) {
|
|
return 'overlaps_client';
|
|
}
|
|
if ((sourcePath && copyPathContains(sourcePath, dataPath)) ||
|
|
(externalStockPath && copyPathContains(externalStockPath, dataPath))) {
|
|
return 'overlaps_stock';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** A data tree must keep the server's saves, logs and caches inside the selected folder. */
|
|
export async function requireUnredirectedData(root: string): Promise<void> {
|
|
const folders = [root];
|
|
for (let folder = folders.pop(); folder !== undefined; folder = folders.pop()) {
|
|
const entries = await readdir(folder, { withFileTypes: true }).catch((error: NodeJS.ErrnoException) => {
|
|
if (folder === root && error.code === 'ENOENT') return [];
|
|
throw error;
|
|
});
|
|
for (const entry of entries) {
|
|
if (entry.isSymbolicLink()) throw new AppError('launch_location');
|
|
if (entry.isDirectory()) folders.push(join(folder, entry.name));
|
|
}
|
|
}
|
|
}
|