fix(launcher): separate client removal from deleting disk files

Split deleteFiles from deleteData in copy maintenance requests with safe false defaults. Block disk deletion for non-copy (in-place/existing) and Steam-library clients on both main and renderer sides, and allow pure list removal to bypass stockBackup/root checks so missing or damaged client entries can always be removed.
This commit is contained in:
N0VAN0M4LY committed 2026-12-04 19:55:05 +08:00
1 parent 12a573a333
commit 9e6e13895e
7 files changed
+146 -53

No files matched your search

@@ -1,5 +1,5 @@
import { readdir, rm } from 'node:fs/promises';
import { isAbsolute, join, parse, relative } from 'node:path';
import { isAbsolute, join, parse, relative, resolve } from 'node:path';
import { app } from 'electron';
import { isInSteamLibrary } from '@wto/patcher';
import { AppError } from '@shared/errors.ts';
@@ -90,10 +90,18 @@ export async function maintainCopy(
}
}
/** Only launcher-created copies outside any Steam library may have their game folder deleted on disk. */
export function canDeleteCopyFiles(record: InstallRecord, root: string): boolean {
return record.method === 'copy' && !isInSteamLibrary(record.copyPath) && !isInSteamLibrary(root);
}
async function performMaintenance(request: MaintenanceRequest, signal: AbortSignal, report: Report): Promise<Copies> {
const { id, operation, steamLocations } = request;
const record = await requireCopy(id);
if (operation === 'set-source') return setCopySource(record, request.sourcePath);
if (operation === 'delete') {
return remove(record, request.deleteFiles === true, request.deleteData === true, signal, report);
}
const root = await checkedRoot(record);
const dataPath = await canonicalCopyPath(record.dataPath);
@@ -101,7 +109,7 @@ async function performMaintenance(request: MaintenanceRequest, signal: AbortSign
if (await gameProcessRunning(signal) || await labServerUses(root)) throw new AppError('maintenance_busy');
signal.throwIfAborted();
if (operation !== 'confirm-steam' && operation !== 'delete' && needsConfirmation) {
if (operation !== 'confirm-steam' && needsConfirmation) {
throw new AppError('launch_steam_confirmation_required');
}
@@ -123,9 +131,6 @@ async function performMaintenance(request: MaintenanceRequest, signal: AbortSign
return updateFromSource(record, root, signal, report);
case 'compact':
return compactCopy(record, root, signal, report);
case 'delete':
report('applying');
return remove(record, root, request.deleteData);
}
}
@@ -184,24 +189,73 @@ async function recordCheck(record: InstallRecord, folder: string): Promise<Copie
}));
}
async function remove(record: InstallRecord, root: string, deleteData: boolean): Promise<Copies> {
if (isInSteamLibrary(record.copyPath) || isInSteamLibrary(root)) {
if (deleteData) failLocation('delete_data_blocked_in_steam', { copyPath: record.copyPath, root });
async function remove(
record: InstallRecord,
deleteFiles: boolean,
deleteData: boolean,
signal: AbortSignal,
report: Report,
): Promise<Copies> {
const canonicalRoot = await canonicalCopyPath(record.copyPath);
if (await gameProcessRunning(signal) || await labServerUses(canonicalRoot)) {
throw new AppError('maintenance_busy');
}
signal.throwIfAborted();
// Pure list removal touches no files on disk and skips copyPath/stockBackup checks
// so missing, moved, or damaged client registrations can always be removed safely.
if (!deleteFiles && !deleteData) {
report('applying');
return removeCopyRecord(record.id);
}
const data = deleteData ? await safeFolder(record.dataPath) : await canonicalCopyPath(record.dataPath);
const copyInSteam = isInSteamLibrary(record.copyPath) || isInSteamLibrary(canonicalRoot);
if (copyInSteam) {
failLocation(deleteFiles ? 'delete_files_blocked_in_steam' : 'delete_data_blocked_in_steam', {
method: record.method,
copyPath: record.copyPath,
root: canonicalRoot,
});
}
if (deleteFiles && !canDeleteCopyFiles(record, canonicalRoot)) {
failLocation('delete_files_blocked_for_method', {
method: record.method,
copyPath: record.copyPath,
root: canonicalRoot,
});
}
const root = deleteFiles ? await checkedRoot(record) : canonicalRoot;
if (deleteFiles && !canDeleteCopyFiles(record, root)) {
failLocation('delete_files_blocked', { method: record.method, copyPath: record.copyPath, root });
}
const data = deleteData ? await safeFolder(record.dataPath, false) : await canonicalCopyPath(record.dataPath);
const source = record.sourcePath ? await canonicalCopyPath(record.sourcePath) : null;
if (deleteData && ((source && (copyPathContains(data, source) || copyPathContains(source, data))) ||
(relative(data, root) !== '' && copyPathContains(data, root)))) {
failLocation('delete_data_overlaps_source_or_copy', { root, data, source });
const stockBackup = await canonicalCopyPath(record.stockBackupPath);
if (deleteData) {
const rawRoot = resolve(record.copyPath);
const overlapsSource = source !== null && (copyPathContains(data, source) || copyPathContains(source, data));
const containsCopy = deleteFiles
? relative(data, root) !== '' && copyPathContains(data, root)
: copyPathContains(data, root) || copyPathContains(data, rawRoot) || copyPathContains(data, stockBackup);
if (overlapsSource || containsCopy) {
failLocation('delete_data_overlaps_source_or_copy', { root, data, source, stockBackup, deleteFiles });
}
}
if (!deleteData && copyPathContains(root, data)) {
await removeExcept(root, data);
} else {
await rm(root, { recursive: true, force: true });
report('applying');
if (deleteFiles) {
if (!deleteData && copyPathContains(root, data)) {
await removeExcept(root, data);
} else {
await rm(root, { recursive: true, force: true });
}
}
if (deleteData && (!deleteFiles || !copyPathContains(root, data))) {
await rm(data, { recursive: true, force: true });
}
if (deleteData && !copyPathContains(root, data)) await rm(data, { recursive: true, force: true });
return removeCopyRecord(record.id);
}