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);
}
@@ -258,16 +258,19 @@
"repair": "Repair client",
"repairAction": "Repair",
"repairHint": "Restore the original files from the backup. Game files will be generated again at launch.",
"delete": "Delete client",
"deleteAction": "Delete…",
"deleteHint": "The data folder is kept by default.",
"unlink": "Remove registration",
"unlinkAction": "Remove…",
"unlinkHint": "The launcher keeps the Steam game files and data folder in place.",
"unlinkLocation": "Remove the launcher's registration for {{path}}? Steam game files and data will stay in place.",
"deleteLocation": "Delete the offline client at {{path}}?",
"deleteData": "Also delete the data folder at {{path}}",
"confirm": "Confirm deletion",
"remove": "Remove client",
"removeAction": "Remove…",
"removeCopyHint": "Removes from the launcher list by default without deleting files on disk.",
"removeUnlinkHint": "Removes only from the launcher list without deleting game files on disk.",
"removeSteamHint": "Removes only from the launcher list. Steam game files and the data folder stay in place.",
"removeCopyNotice": "Removes {{path}} from the launcher list. All game files and data on disk are kept by default.",
"removeListOnlyNotice": "Removes {{path}} from the launcher list only. Game files on disk will not be deleted.",
"removeSteamNotice": "Removes {{path}} from the launcher list only. Steam game files and data on disk will not be deleted.",
"deleteFiles": "Also delete the client folder at {{path}}",
"deleteFilesWarning": "This will permanently delete all files in the copied client folder at {{path}}. This action cannot be undone.",
"deleteData": "Also delete the data folder at {{path}} (contains saves, settings, and logs)",
"confirmRemove": "Remove from list",
"confirmDelete": "Confirm deletion",
"phase": {
"checking": "Checking the client",
"copying": "Copying changed files",
@@ -280,7 +283,7 @@
"repair": "Original files restored.",
"update": "Client updated.",
"compact": "Space-saving conversion complete.",
"delete": "Client deleted.",
"delete": "Client removed.",
"cancelled": "Maintenance cancelled."
}
},
@@ -258,16 +258,19 @@
"repair": "修复客户端",
"repairAction": "修复",
"repairHint": "从原版文件备份还原文件。下次启动时重新生成游戏文件。",
"delete": "删除客户端",
"deleteAction": "删除…",
"deleteHint": "默认保留数据目录。",
"unlink": "解除登记",
"unlinkAction": "解除登记…",
"unlinkHint": "Steam 游戏文件和数据目录将留在原处。",
"unlinkLocation": "从启动器中解除 {{path}} 的登记?Steam 游戏文件和数据将留在原处。",
"deleteLocation": "确认删除 {{path}} 中的离线客户端?",
"deleteData": "同时删除 {{path}} 中的数据",
"confirm": "确认删除",
"remove": "移除客户端",
"removeAction": "移除…",
"removeCopyHint": "默认仅从启动器列表移除,不会删除磁盘文件。",
"removeUnlinkHint": "仅从启动器列表移除,不会删除磁盘上的游戏文件。",
"removeSteamHint": "仅从启动器列表移除,Steam 游戏文件和数据目录将留在原处。",
"removeCopyNotice": "将从启动器列表移除 {{path}}。默认保留磁盘上的所有游戏文件和数据。",
"removeListOnlyNotice": "将仅从启动器列表移除 {{path}},不会删除磁盘上的游戏文件。",
"removeSteamNotice": "将仅从启动器列表移除 {{path}},不会删除磁盘上的 Steam 游戏文件和数据目录。",
"deleteFiles": "同时删除客户端文件夹 {{path}}",
"deleteFilesWarning": "将永久删除客户端副本文件夹 {{path}} 中的全部文件,此操作无法撤销。",
"deleteData": "同时删除数据文件夹 {{path}}(包含存档、配置与日志)",
"confirmRemove": "从列表移除",
"confirmDelete": "确认删除",
"phase": {
"checking": "正在检查客户端",
"copying": "正在复制有变化的文件",
@@ -280,7 +283,7 @@
"repair": "原版文件已还原。",
"update": "客户端已更新。",
"compact": "省空间转换已完成。",
"delete": "客户端已删除。",
"delete": "客户端已移除。",
"cancelled": "维护已取消。"
}
},
@@ -20,6 +20,10 @@ export function CopyMaintenance({ record, maintenance, onError }: CopyMaintenanc
const [info, setInfo] = useState<MaintenanceInfo | null>(null);
const [removal, setRemoval] = useState<CopyRemovalOptions | null>(null);
useEffect(() => {
setRemoval(null);
}, [record.id]);
useEffect(() => {
let mounted = true;
setInfo(null);
@@ -60,7 +64,7 @@ export function CopyMaintenance({ record, maintenance, onError }: CopyMaintenanc
busy={busy}
options={removal}
onOptionsChange={setRemoval}
onRemove={(deleteData) => run('delete', { deleteData })}
onRemove={(options) => run('delete', options)}
/>
{active && <MaintenanceStatus progress={active} onCancel={maintenance.cancel} />}
{result && <p className="small" role="status">{t(`copies.maintenance.result.${result}`)}</p>}
@@ -69,44 +69,72 @@ export function CopyMaintenanceActions({ record, info, busy, onRun, onChooseSour
}
export interface CopyRemovalOptions {
readonly deleteFiles: boolean;
readonly deleteData: boolean;
}
interface CopyRemovalProps extends CopyMaintenanceControlsProps {
readonly options: CopyRemovalOptions | null;
readonly onOptionsChange: (options: CopyRemovalOptions | null) => void;
readonly onRemove: (deleteData: boolean) => void;
readonly onRemove: (options: CopyRemovalOptions) => void;
}
export function CopyRemoval({ record, info, busy, options, onOptionsChange, onRemove }: CopyRemovalProps) {
const { t } = useTranslation();
const steam = info?.steamLocations;
const unlinkOnly = steam?.copyInSteam === true;
const canDeleteData = !unlinkOnly && !steam?.dataInSteam;
const action = t(unlinkOnly ? 'copies.maintenance.unlink' : 'copies.maintenance.confirm');
const copyInSteam = steam?.copyInSteam === true;
const dataInSteam = steam?.dataInSteam === true;
const canDeleteFiles = info !== null && record.method === 'copy' && !copyInSteam;
const canDeleteData = info !== null && !copyInSteam && !dataInSteam;
const willDeleteFiles = canDeleteFiles && options?.deleteFiles === true;
const willDeleteData = canDeleteData && options?.deleteData === true;
const willDeleteAny = willDeleteFiles || willDeleteData;
const action = t(willDeleteAny ? 'copies.maintenance.confirmDelete' : 'copies.maintenance.confirmRemove');
const rowHint = copyInSteam
? t('copies.maintenance.removeSteamHint')
: record.method === 'copy'
? t('copies.maintenance.removeCopyHint')
: t('copies.maintenance.removeUnlinkHint');
const confirmNotice = copyInSteam
? t('copies.maintenance.removeSteamNotice', { path: steam?.copyPath ?? record.copyPath })
: canDeleteFiles
? t('copies.maintenance.removeCopyNotice', { path: record.copyPath })
: t('copies.maintenance.removeListOnlyNotice', { path: record.copyPath });
return (
<>
<Row label={t(unlinkOnly ? 'copies.maintenance.unlink' : 'copies.maintenance.delete')}
hint={t(unlinkOnly ? 'copies.maintenance.unlinkHint' : 'copies.maintenance.deleteHint')}>
<button type="button" className="button" disabled={busy || !info}
onClick={() => onOptionsChange({ deleteData: false })}>
{t(unlinkOnly ? 'copies.maintenance.unlinkAction' : 'copies.maintenance.deleteAction')}
<Row label={t('copies.maintenance.remove')} hint={rowHint}>
<button type="button" className="button" disabled={busy}
onClick={() => onOptionsChange({ deleteFiles: false, deleteData: false })}>
{t('copies.maintenance.removeAction')}
</button>
</Row>
{options && (
<section className="maintenance-confirm" role="group" aria-label={action}>
<p>{t(unlinkOnly ? 'copies.maintenance.unlinkLocation' : 'copies.maintenance.deleteLocation', { path: steam?.copyPath ?? record.copyPath })}</p>
<p>{confirmNotice}</p>
{canDeleteFiles && (
<label className="check">
<input type="checkbox" checked={options.deleteFiles} disabled={busy}
onChange={(event) => onOptionsChange({ ...options, deleteFiles: event.target.checked })} />
{t('copies.maintenance.deleteFiles', { path: record.copyPath })}
</label>
)}
{willDeleteFiles && (
<Notice tone="warning">
<p>{t('copies.maintenance.deleteFilesWarning', { path: record.copyPath })}</p>
</Notice>
)}
{canDeleteData && (
<label className="check">
<input type="checkbox" checked={options.deleteData} disabled={busy}
onChange={(event) => onOptionsChange({ deleteData: event.target.checked })} />
onChange={(event) => onOptionsChange({ ...options, deleteData: event.target.checked })} />
{t('copies.maintenance.deleteData', { path: record.dataPath })}
</label>
)}
<div className="copy-list-actions">
<button type="button" className="button" disabled={busy} onClick={() => onOptionsChange(null)}>{t('wizard.cancel')}</button>
<button type="button" className="button button-primary" disabled={busy} onClick={() => onRemove(canDeleteData && options.deleteData)}>
<button type="button" className="button button-primary" disabled={busy}
onClick={() => onRemove({ deleteFiles: willDeleteFiles, deleteData: willDeleteData })}>
{action}
</button>
</div>
@@ -4,7 +4,7 @@ import type { MaintenanceProgress, MaintenanceRequest } from '@shared/schemas/ma
import { AppIpcError, ipcInvoke, ipcOn } from '../../ipc.ts';
export type MaintenanceOperation = MaintenanceRequest['operation'];
export type MaintenanceOptions = Pick<IpcInput<'copies:maintain'>, 'deleteData' | 'steamLocations' | 'sourcePath'>;
export type MaintenanceOptions = Pick<IpcInput<'copies:maintain'>, 'deleteFiles' | 'deleteData' | 'steamLocations' | 'sourcePath'>;
export interface MaintenanceOutcome {
readonly copyId: string;
@@ -5,6 +5,7 @@ export const MaintenanceOperationSchema = z.enum(['check', 'repair', 'update', '
export const MaintenanceRequestSchema = z.object({
id: CopyIdSchema,
operation: MaintenanceOperationSchema,
deleteFiles: z.boolean().default(false),
deleteData: z.boolean().default(false),
sourcePath: z.string().min(1).optional(),
steamLocations: z.object({