From 83da8aad2945bf0b2b6b9f45c897b3794fc13098 Mon Sep 17 00:00:00 2001 From: CelloSerenity <195480169+CelloSerenity@users.noreply.github.com> Date: Sun, 26 Jul 2026 00:34:07 -0600 Subject: [PATCH] Make pairing file import atomic --- StikDebug/Support/PairingFileStore.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/StikDebug/Support/PairingFileStore.swift b/StikDebug/Support/PairingFileStore.swift index 5564ade..20beccb 100644 --- a/StikDebug/Support/PairingFileStore.swift +++ b/StikDebug/Support/PairingFileStore.swift @@ -36,12 +36,19 @@ enum PairingFileStore { static func replace(with sourceURL: URL, fileManager: FileManager = .default) throws { let destination = prepareURL(fileManager: fileManager) + + let tempURL = directoryURL.appendingPathComponent(UUID().uuidString + ".tmp") + try? fileManager.removeItem(at: tempURL) + try fileManager.copyItem(at: sourceURL, to: tempURL) + defer { try? fileManager.removeItem(at: tempURL) } + if fileManager.fileExists(atPath: destination.path) { - try fileManager.removeItem(at: destination) + _ = try fileManager.replaceItemAt(destination, withItemAt: tempURL) + } else { + try fileManager.moveItem(at: tempURL, to: destination) } removeLegacyCopies(fileManager: fileManager) - try fileManager.copyItem(at: sourceURL, to: destination) protectPairingFile(at: destination, fileManager: fileManager) }