Skip to content

Commit

Permalink
vm(qemu): fix restoring of remote bookmarks
Browse files Browse the repository at this point in the history
When we pass in the URL to the argument generator, we lose info about the
remote bookmark. Now, we form a temporary dictionary mapping the URL to its
remote bookmark and attempt to use it if available.
  • Loading branch information
osy committed Apr 22, 2023
1 parent a9895af commit d7a68ee
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions Managers/UTMQemuSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface UTMQemuSystem : UTMQemu

@property (nonatomic, nullable, copy) NSArray<NSURL *> *resources;
@property (nonatomic, nullable, weak) NSDictionary<NSURL *, NSData *> *remoteBookmarks;
@property (nonatomic) UTMQEMURendererBackend rendererBackend;

- (instancetype)init NS_UNAVAILABLE;
Expand Down
24 changes: 19 additions & 5 deletions Managers/UTMQemuSystem.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

#import <dlfcn.h>
#import "UTMLogging.h"
#import "UTMQemuSystem.h"

@interface UTMQemuSystem ()
Expand Down Expand Up @@ -97,15 +98,28 @@ - (BOOL)didLoadDylib:(void *)handle {
}

- (void)startWithCompletion:(void (^)(BOOL, NSString * _Nonnull))completion {
dispatch_group_t group = dispatch_group_create();
for (NSURL *resourceURL in self.resources) {
NSData *bookmark = [resourceURL bookmarkDataWithOptions:0
includingResourceValuesForKeys:nil
relativeToURL:nil
error:nil];
NSData *bookmark = self.remoteBookmarks[resourceURL];
BOOL securityScoped = YES;
if (!bookmark) {
bookmark = [resourceURL bookmarkDataWithOptions:0
includingResourceValuesForKeys:nil
relativeToURL:nil
error:nil];
securityScoped = NO;
}
if (bookmark) {
[self accessDataWithBookmark:bookmark];
dispatch_group_enter(group);
[self accessDataWithBookmark:bookmark securityScoped:securityScoped completion:^(BOOL success, NSData *bookmark, NSString *path) {
if (!success) {
UTMLog(@"Access QEMU bookmark failed for: %@", path);
}
dispatch_group_leave(group);
}];
}
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSString *name = [NSString stringWithFormat:@"qemu-%@-softmmu", self.architecture];
[self startQemu:name completion:completion];
}
Expand Down
2 changes: 2 additions & 0 deletions Managers/UTMQemuVirtualMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ - (void)_vmStartWithCompletion:(void (^)(NSError * _Nullable))completion {

NSArray<NSString *> *arguments = self.config.qemuArguments;
NSArray<NSURL *> *resources = self.config.qemuResources;
NSDictionary<NSURL *, NSData *> *remoteBookmarks = self.remoteBookmarks;
self.system = [[UTMQemuSystem alloc] initWithArguments:arguments architecture:self.config.qemuArchitecture];
self.system.resources = resources;
self.system.remoteBookmarks = remoteBookmarks;
self.system.logging = self.logging;
self.system.logging.delegate = self;

Expand Down
15 changes: 15 additions & 0 deletions Managers/UTMQemuVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,21 @@ extension UTMQemuVirtualMachine {
}
}
}

@MainActor @objc var remoteBookmarks: [URL: Data] {
var dict = [URL: Data]()
for file in registryEntry.externalDrives.values {
if let bookmark = file.remoteBookmark {
dict[file.url] = bookmark
}
}
for file in registryEntry.sharedDirectories {
if let bookmark = file.remoteBookmark {
dict[file.url] = bookmark
}
}
return dict
}
}

enum UTMQemuVirtualMachineError: Error {
Expand Down

0 comments on commit d7a68ee

Please sign in to comment.