From 0955d2d7bf105da632c175eb06cc3f9610f75f36 Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:29:52 -0500 Subject: [PATCH 1/3] isos: removePartFiles deletes unfinished ISO copies A copy writes .iso.part and renames it when done. If the app is closed or crashes mid-copy the .part file stays, invisible in the app, taking GBs. removePartFiles deletes *.iso.part in the ISOs folder and skips anything it can't delete. --- src/main/isos.ts | 25 ++++++++++++++++++++++++- tests/isos.test.ts | 17 ++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/isos.ts b/src/main/isos.ts index c65f996..31c7fa8 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' @@ -58,6 +58,29 @@ async function copyInto(src: string, target: string): Promise { return target } +/** + * Deletes .iso.part files left in the ISOs folder by a copy the app didn't finish (closed or + * crashed mid-copy). 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 removePartFiles(isosDir: string): string[] { + let names: string[] + try { + names = readdirSync(isosDir) + } catch { + return [] // folder missing + } + return names.filter((f) => { + if (!f.toLowerCase().endsWith('.iso.part')) 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..670f5b1 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 { importIso, listIsos, removePartFiles, watchFolder } from '../src/main/isos' let dir: string beforeEach(() => { @@ -166,3 +166,18 @@ describe('importIso while a copy is running', () => { expect(readFileSync(b, 'utf8')).toBe('small') }) }) + +describe('removePartFiles', () => { + it('deletes unfinished copies and leaves everything else', () => { + writeFileSync(join(dir, 'kali.iso'), 'iso') + writeFileSync(join(dir, 'kali (2).iso.part'), 'half') + writeFileSync(join(dir, 'BIG.ISO.PART'), 'half') + writeFileSync(join(dir, 'notes.part'), 'mine') + expect(removePartFiles(dir).sort()).toEqual(['BIG.ISO.PART', 'kali (2).iso.part']) + expect(readdirSync(dir).sort()).toEqual(['kali.iso', 'notes.part']) + }) + + it('copes with a missing folder', () => { + expect(removePartFiles(join(dir, 'nope'))).toEqual([]) + }) +}) From f6a08a4581edf226d89849cf970869737acee801 Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:29:52 -0500 Subject: [PATCH 2/3] Remove leftover ISO copies when the app starts At startup no copy can be running, and the single instance lock keeps a second copy of the app from starting one, so .part files there are always leftovers. --- src/main/ipc.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index e37e60d..1cbddf1 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, removePartFiles, 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 = removePartFiles(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')) From 286e22d5961f2e9855e2355da3d0555365a02d1b Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:31:10 -0500 Subject: [PATCH 3/3] ISO copies: use our own .vmm-copy suffix, clean up only that Firefox downloads to .iso.part, so deleting *.iso.part at startup could kill a download in progress in the ISOs folder. Copies now write .iso.vmm-copy and only that suffix is cleaned up. Renamed removePartFiles to removeUnfinishedCopies to match. --- src/main/ipc.ts | 4 ++-- src/main/isos.ts | 16 +++++++++++----- tests/isos.test.ts | 22 ++++++++++++---------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 1cbddf1..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, removePartFiles, 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' @@ -52,7 +52,7 @@ export function registerIpc(ctx: IpcContext): Backend { // 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 = removePartFiles(paths.isos) + 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 diff --git a/src/main/isos.ts b/src/main/isos.ts index 31c7fa8..59cf8cd 100644 --- a/src/main/isos.ts +++ b/src/main/isos.ts @@ -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) @@ -59,11 +65,11 @@ async function copyInto(src: string, target: string): Promise { } /** - * Deletes .iso.part files left in the ISOs folder by a copy the app didn't finish (closed or - * crashed mid-copy). Only safe while no copy can be running, so call it once at startup. + * 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 removePartFiles(isosDir: string): string[] { +export function removeUnfinishedCopies(isosDir: string): string[] { let names: string[] try { names = readdirSync(isosDir) @@ -71,7 +77,7 @@ export function removePartFiles(isosDir: string): string[] { return [] // folder missing } return names.filter((f) => { - if (!f.toLowerCase().endsWith('.iso.part')) return false + if (!f.toLowerCase().endsWith(`.iso${COPY_SUFFIX}`)) return false try { rmSync(join(isosDir, f)) return true diff --git a/tests/isos.test.ts b/tests/isos.test.ts index 670f5b1..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, removePartFiles, 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') @@ -167,17 +167,19 @@ describe('importIso while a copy is running', () => { }) }) -describe('removePartFiles', () => { - it('deletes unfinished copies and leaves everything else', () => { +describe('removeUnfinishedCopies', () => { + it('deletes our unfinished copies and leaves everything else', () => { writeFileSync(join(dir, 'kali.iso'), 'iso') - writeFileSync(join(dir, 'kali (2).iso.part'), 'half') - writeFileSync(join(dir, 'BIG.ISO.PART'), 'half') - writeFileSync(join(dir, 'notes.part'), 'mine') - expect(removePartFiles(dir).sort()).toEqual(['BIG.ISO.PART', 'kali (2).iso.part']) - expect(readdirSync(dir).sort()).toEqual(['kali.iso', 'notes.part']) + 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(removePartFiles(join(dir, 'nope'))).toEqual([]) + expect(removeUnfinishedCopies(join(dir, 'nope'))).toEqual([]) }) })