mirror of
https://github.com/StikDebug/StikDebug.git
synced 2026-10-15 20:10:30 +00:00
Make pairing file import atomic
This commit is contained in:
parent
c1189928d4
commit
83da8aad29
1 changed files with 9 additions and 2 deletions
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue