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 { 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)); } } }