Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export function listConfigs(paths: DataPaths): VmConfig[] {
.sort((a, b) => a.name.localeCompare(b.name))
}

/** a config file for this name is there already, ignoring case like Windows does */
function taken(paths: DataPaths, name: string): boolean {
const file = `${name}.json`.toLowerCase()
return existsSync(paths.vms) && readdirSync(paths.vms).some((f) => f.toLowerCase() === file)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this also matches a leftover x.json that listConfigs skips because it doesn't parse, so the user gets "already exists" for a VM the library doesn't show. That's still better than overwriting it, and the message is enough to go and look in the vms folder. No change needed.

}

export function saveConfig(paths: DataPaths, cfg: VmConfig, previousName?: string): VmConfig {
const err = validateVmName(cfg.name)
if (err) throw new Error(err)
Expand All @@ -75,6 +81,10 @@ export function saveConfig(paths: DataPaths, cfg: VmConfig, previousName?: strin
if (renamed && !caseOnly && existsSync(target)) {
throw new Error(`A VM called "${clean.name}" already exists`)
}
// a new VM. On Windows kali.json is Kali.json, writing it would replace that VM's settings
if (!previousName && taken(paths, clean.name)) {
throw new Error(`A VM called "${clean.name}" already exists`)
}
// write to a temp file first so a crash never leaves a half-written config
const tmp = `${target}.tmp`
writeFileSync(tmp, JSON.stringify(clean, null, 2), 'utf8')
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/mockApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function installMockApi(): void {
list: async () => [...vms],
newConfig: async (name) => defaultConfig(name?.trim() || `New VM ${vms.length + 1}`, disks),
save: async (cfg, prev) => {
if (!prev && vms.some((v) => v.name.toLowerCase() === cfg.name.toLowerCase())) throw new Error(`A VM called "${cfg.name}" already exists`)
if (prev && prev !== cfg.name && profiles[prev]) {
profiles[cfg.name] = profiles[prev]
delete profiles[prev]
Expand Down
11 changes: 11 additions & 0 deletions tests/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ describe('saveConfig renames and deleteConfig', () => {
expect(readdirSync(paths.vms).sort()).toEqual(['Kali.json', 'Parrot.json'])
})

it('refuses a new VM whose name is taken, ignoring case', () => {
const paths = setup()
saveConfig(paths, { ...defaultConfig('Kali', paths.disks), ramMB: 4096 })
expect(() => saveConfig(paths, { ...defaultConfig('kali', paths.disks), ramMB: 1024 })).toThrow(/A VM called "kali" already exists/)
expect(() => saveConfig(paths, defaultConfig('Kali', paths.disks))).toThrow(/already exists/)
expect(listConfigs(paths).map((c) => [c.name, c.ramMB])).toEqual([['Kali', 4096]])
// saving the VM itself (previousName = its own name) still works
saveConfig(paths, { ...defaultConfig('Kali', paths.disks), ramMB: 2048 }, 'Kali')
expect(listConfigs(paths)[0].ramMB).toBe(2048)
})

it('renames to the same name in different case without losing the config', () => {
const paths = setup()
saveConfig(paths, { ...defaultConfig('kali', paths.disks), ramMB: 2048 })
Expand Down
Loading