Skip to content

Commit 2d47233

Browse files
committed
data: do not block main thread for long copy operations
1 parent 180c67e commit 2d47233

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

Platform/UTMData.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ struct AlertMessage: Identifiable {
452452
let newName: String = newDefaultVMName(base: vm.detailsTitleLabel)
453453
let newPath = UTMQemuVirtualMachine.virtualMachinePath(for: newName, in: documentsURL)
454454

455-
try copyItemWithCopyfile(at: vm.pathUrl, to: newPath)
455+
try await copyItemWithCopyfile(at: vm.pathUrl, to: newPath)
456456
guard let newVM = try? VMData(url: newPath) else {
457457
throw UTMDataError.cloneFailed
458458
}
@@ -471,20 +471,20 @@ struct AlertMessage: Identifiable {
471471
/// - Parameters:
472472
/// - vm: VM to copy
473473
/// - url: Location to copy to (must be writable)
474-
func export(vm: VMData, to url: URL) throws {
474+
func export(vm: VMData, to url: URL) async throws {
475475
let sourceUrl = vm.pathUrl
476476
if fileManager.fileExists(atPath: url.path) {
477477
try fileManager.removeItem(at: url)
478478
}
479-
try copyItemWithCopyfile(at: sourceUrl, to: url)
479+
try await copyItemWithCopyfile(at: sourceUrl, to: url)
480480
}
481481

482482
/// Save a copy of the VM and all data to arbitary location and delete the original data
483483
/// - Parameters:
484484
/// - vm: VM to move
485485
/// - url: Location to move to (must be writable)
486486
func move(vm: VMData, to url: URL) async throws {
487-
try export(vm: vm, to: url)
487+
try await export(vm: vm, to: url)
488488
guard let newVM = try? VMData(url: url) else {
489489
throw UTMDataError.shortcutCreationFailed
490490
}
@@ -616,13 +616,13 @@ struct AlertMessage: Identifiable {
616616
listSelect(vm: vm)
617617
}
618618

619-
func copyItemWithCopyfile(at srcURL: URL, to dstURL: URL) throws {
620-
// let state = copyfile_state_alloc()
621-
let status = copyfile(srcURL.path, dstURL.path, nil, copyfile_flags_t(COPYFILE_ALL | COPYFILE_RECURSIVE | COPYFILE_CLONE | COPYFILE_DATA_SPARSE))
622-
// copyfile_state_free(state)
623-
if status < 0 {
624-
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno))
625-
}
619+
func copyItemWithCopyfile(at srcURL: URL, to dstURL: URL) async throws {
620+
try await Task.detached(priority: .userInitiated) {
621+
let status = copyfile(srcURL.path, dstURL.path, nil, copyfile_flags_t(COPYFILE_ALL | COPYFILE_RECURSIVE | COPYFILE_CLONE | COPYFILE_DATA_SPARSE))
622+
if status < 0 {
623+
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno))
624+
}
625+
}.value
626626
}
627627

628628
// MARK: - Downloading VMs

0 commit comments

Comments
 (0)