diff --git a/src/main/ipc.ts b/src/main/ipc.ts index e37e60d..34a0c73 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -8,7 +8,7 @@ import { agentHibernate } from './agent' import { cloneVm, compactDisk, resizeDisk } from './disks' import { runAutostart } from './autostart' import { GuestSetupServer } from './guestSetup' -import { importIso, listIsos, watchFolder } from './isos' +import { importIso, listIsos, removeUnfinishedCopies, watchFolder } from './isos' import { detectAccel, findQemu, qemuVersion, type DataPaths } from './paths' import { Profiles } from './profiles' import { checkDataRoot, loadSettings, normalizeSettings, saveSettings } from './settings' @@ -50,6 +50,11 @@ export function registerIpc(ctx: IpcContext): Backend { runner.on('state', (s) => send('vm:state', s)) runner.on('crash', (c) => send('vm:crash', c)) + // a copy the app was closed in the middle of. Nothing copies yet, the single instance lock + // keeps a second copy of the app from doing it either + const leftovers = removeUnfinishedCopies(paths.isos) + if (leftovers.length) console.log(`removed unfinished ISO copies: ${leftovers.join(', ')}`) + // new or removed ISOs show up without restarting the app const stopIsoWatch = watchFolder(paths.isos, () => send('host:changed')) diff --git a/src/main/isos.ts b/src/main/isos.ts index c65f996..59cf8cd 100644 --- a/src/main/isos.ts +++ b/src/main/isos.ts @@ -1,4 +1,4 @@ -import { existsSync, readdirSync, statSync, watch } from 'node:fs' +import { existsSync, readdirSync, rmSync, statSync, watch } from 'node:fs' import { copyFile, rename, rm } from 'node:fs/promises' import { basename, extname, join } from 'node:path' import type { VmConfig } from '@shared/config' @@ -42,12 +42,18 @@ export async function importIso(src: string, isosDir: string, mode: 'copy' | 're return done } +/** + * Suffix of a copy in progress. Not .part: Firefox downloads to .iso.part, and the + * startup cleanup must never touch a download another program is writing. + */ +export const COPY_SUFFIX = '.vmm-copy' + /** copies running right now, by lowercased target path */ const copying = new Map }>() async function copyInto(src: string, target: string): Promise { // copy under a name the ISO list ignores, so a half-copied file never shows up as bootable - const part = `${target}.part` + const part = `${target}${COPY_SUFFIX}` try { await copyFile(src, part) await rename(part, target) @@ -58,6 +64,29 @@ async function copyInto(src: string, target: string): Promise { return target } +/** + * Deletes the files a copy the app didn't finish (closed or crashed mid-copy) left in the + * ISOs folder. Only ones with our own suffix, so other programs' downloads are safe. Only safe while no copy can be running, so call it once at startup. + * Returns the names it removed. A file that can't be deleted is skipped and tried next time. + */ +export function removeUnfinishedCopies(isosDir: string): string[] { + let names: string[] + try { + names = readdirSync(isosDir) + } catch { + return [] // folder missing + } + return names.filter((f) => { + if (!f.toLowerCase().endsWith(`.iso${COPY_SUFFIX}`)) return false + try { + rmSync(join(isosDir, f)) + return true + } catch { + return false + } + }) +} + const same = (a: string, b: string) => a.toLowerCase() === b.toLowerCase() // Windows paths /** diff --git a/tests/isos.test.ts b/tests/isos.test.ts index 5e885e9..99d26b0 100644 --- a/tests/isos.test.ts +++ b/tests/isos.test.ts @@ -3,7 +3,7 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { defaultConfig } from '@shared/config' -import { importIso, listIsos, watchFolder } from '../src/main/isos' +import { COPY_SUFFIX, importIso, listIsos, removeUnfinishedCopies, watchFolder } from '../src/main/isos' let dir: string beforeEach(() => { @@ -110,7 +110,7 @@ describe('importIso', () => { expect(readdirSync(isos)).toEqual([]) }) - it('copies into the ISOs folder without leaving a .part file', async () => { + it('copies into the ISOs folder without leaving a temp file', async () => { const file = join(src, 'debian.iso') writeFileSync(file, 'abc') const out = await importIso(file, isos, 'copy') @@ -166,3 +166,20 @@ describe('importIso while a copy is running', () => { expect(readFileSync(b, 'utf8')).toBe('small') }) }) + +describe('removeUnfinishedCopies', () => { + it('deletes our unfinished copies and leaves everything else', () => { + writeFileSync(join(dir, 'kali.iso'), 'iso') + writeFileSync(join(dir, `kali (2).iso${COPY_SUFFIX}`), 'half') + writeFileSync(join(dir, `BIG.ISO${COPY_SUFFIX.toUpperCase()}`), 'half') + // Firefox writes its downloads like this, one may be in progress right now + writeFileSync(join(dir, 'debian.iso.part'), 'downloading') + writeFileSync(join(dir, 'notes.vmm-copy'), 'not an iso') + expect(removeUnfinishedCopies(dir).sort()).toEqual([`BIG.ISO${COPY_SUFFIX.toUpperCase()}`, `kali (2).iso${COPY_SUFFIX}`]) + expect(readdirSync(dir).sort()).toEqual(['debian.iso.part', 'kali.iso', 'notes.vmm-copy']) + }) + + it('copes with a missing folder', () => { + expect(removeUnfinishedCopies(join(dir, 'nope'))).toEqual([]) + }) +})