Skip to content

Commit

Permalink
config(qemu): show progress indicator for reclaim operations
Browse files Browse the repository at this point in the history
  • Loading branch information
osy committed Apr 30, 2024
1 parent f8f1627 commit 0008112
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Platform/UTMData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,15 @@ struct AlertMessage: Identifiable {
func reclaimSpace(for driveUrl: URL, withCompression isCompressed: Bool = false) async throws {
let baseUrl = driveUrl.deletingLastPathComponent()
let dstUrl = Self.newImage(from: driveUrl, to: baseUrl, withExtension: "qcow2")
try await UTMQemuImage.convert(from: driveUrl, toQcow2: dstUrl, withCompression: isCompressed)
defer {
busyProgress = nil
}
try await UTMQemuImage.convert(from: driveUrl, toQcow2: dstUrl, withCompression: isCompressed) { progress in
Task { @MainActor in
self.busyProgress = progress / 100
}
}
busyProgress = nil
do {
try fileManager.replaceItem(at: driveUrl, withItemAt: dstUrl, backupItemName: nil, resultingItemURL: nil)
} catch {
Expand Down
38 changes: 36 additions & 2 deletions Services/UTMQemuImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import Foundation
import QEMUKitInternal

@objc class UTMQemuImage: UTMProcess {
typealias ProgressCallback = (Float) -> Void

private var logOutput: String = ""
private var processExitContinuation: CheckedContinuation<Void, any Error>?

private var onProgress: ProgressCallback?

private init() {
super.init(arguments: [])
}
Expand Down Expand Up @@ -52,11 +55,14 @@ import QEMUKitInternal
}
}

static func convert(from url: URL, toQcow2 dest: URL, withCompression compressed: Bool = false) async throws {
static func convert(from url: URL, toQcow2 dest: URL, withCompression compressed: Bool = false, onProgress: ProgressCallback? = nil) async throws {
let qemuImg = UTMQemuImage()
let srcBookmark = try url.bookmarkData()
let dstBookmark = try dest.deletingLastPathComponent().bookmarkData()
qemuImg.pushArgv("convert")
if onProgress != nil {
qemuImg.pushArgv("-p")
}
if compressed {
qemuImg.pushArgv("-c")
qemuImg.pushArgv("-o")
Expand All @@ -69,8 +75,10 @@ import QEMUKitInternal
qemuImg.accessData(withBookmark: dstBookmark)
qemuImg.pushArgv(dest.path)
let logging = QEMULogging()
logging.delegate = qemuImg
qemuImg.standardOutput = logging.standardOutput
qemuImg.standardError = logging.standardError
qemuImg.onProgress = onProgress
try await qemuImg.start()
}

Expand Down Expand Up @@ -175,8 +183,34 @@ extension UTMQemuImageError: LocalizedError {
extension UTMQemuImage: QEMULoggingDelegate {
func logging(_ logging: QEMULogging, didRecieveOutputLine line: String) {
logOutput += line
if let onProgress = onProgress, line.contains("100%") {
if let progress = parseProgress(line) {
onProgress(progress)
}
}
}

func logging(_ logging: QEMULogging, didRecieveErrorLine line: String) {
}
}

extension UTMQemuImage {
private func parseProgress(_ line: String) -> Float? {
let pattern = "\\(([0-9]+\\.[0-9]+)/100\\%\\)"
do {
let regex = try NSRegularExpression(pattern: pattern)
if let match = regex.firstMatch(in: line, range: NSRange(location: 0, length: line.count)) {
let range = match.range(at: 1)
if let swiftRange = Range(range, in: line) {
let floatValueString = line[swiftRange]
if let floatValue = Float(floatValueString) {
return floatValue
}
}
}
} catch {

}
return nil
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "7b5e2ff74c08d3efce9babb7649d891f7ad18d9c868e75db7d9e26ce542055cc",
"originHash" : "c9482d61e795c27df5a7c772a0750aefc9164d93bd871138a9b229c9f08c6fab",
"pins" : [
{
"identity" : "altkit",
Expand Down Expand Up @@ -52,7 +52,7 @@
"location" : "https://github.com/utmapp/QEMUKit.git",
"state" : {
"branch" : "main",
"revision" : "06b806f61aeeea8efff99a98b058defcf3632e2e"
"revision" : "1019ed76278a1d5cbe871ff5e51c62b5d8c9a032"
}
},
{
Expand Down

0 comments on commit 0008112

Please sign in to comment.