QEMUHelper.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Copyright © 2020 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #import "QEMUHelper.h"
  17. #import "QEMUHelperDelegate.h"
  18. #import <stdio.h>
  19. @interface QEMUHelper ()
  20. @property NSMutableArray<NSURL *> *urls;
  21. @property NSTask *childTask;
  22. @end
  23. @implementation QEMUHelper
  24. @synthesize environment;
  25. - (instancetype)init {
  26. if (self = [super init]) {
  27. self.urls = [NSMutableArray array];
  28. }
  29. return self;
  30. }
  31. - (void)dealloc {
  32. for (NSURL *url in self.urls) {
  33. [url stopAccessingSecurityScopedResource];
  34. }
  35. }
  36. - (void)accessDataWithBookmark:(NSData *)bookmark securityScoped:(BOOL)securityScoped completion:(void(^)(BOOL, NSData * _Nullable, NSString * _Nullable))completion {
  37. BOOL stale = NO;
  38. NSError *err;
  39. NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark
  40. options:(securityScoped ? NSURLBookmarkResolutionWithSecurityScope : 0)
  41. relativeToURL:nil
  42. bookmarkDataIsStale:&stale
  43. error:&err];
  44. if (!url) {
  45. NSLog(@"Failed to access bookmark data.");
  46. completion(NO, nil, nil);
  47. return;
  48. }
  49. if (stale || !securityScoped) {
  50. bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
  51. includingResourceValuesForKeys:nil
  52. relativeToURL:nil
  53. error:&err];
  54. // if we fail, try again with read-only access
  55. if (!bookmark) {
  56. bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
  57. includingResourceValuesForKeys:nil
  58. relativeToURL:nil
  59. error:&err];
  60. }
  61. if (!bookmark) {
  62. NSLog(@"Failed to create new bookmark!");
  63. completion(NO, bookmark, url.path);
  64. return;
  65. }
  66. }
  67. if ([url startAccessingSecurityScopedResource]) {
  68. [self.urls addObject:url];
  69. } else {
  70. NSLog(@"Failed to access security scoped resource for: %@", url);
  71. }
  72. completion(YES, bookmark, url.path);
  73. }
  74. - (void)stopAccessingPath:(nullable NSString *)path {
  75. if (!path) {
  76. return;
  77. }
  78. for (NSURL *url in _urls) {
  79. if ([url.path isEqualToString:path]) {
  80. [url stopAccessingSecurityScopedResource];
  81. [_urls removeObject:url];
  82. return;
  83. }
  84. }
  85. NSLog(@"Cannot find '%@' in existing scoped access.", path);
  86. }
  87. - (void)startQemu:(NSString *)binName standardOutput:(NSFileHandle *)standardOutput standardError:(NSFileHandle *)standardError libraryBookmark:(NSData *)libBookmark argv:(NSArray<NSString *> *)argv completion:(void(^)(BOOL,NSString *))completion {
  88. NSError *err;
  89. NSURL *libraryPath = [NSURL URLByResolvingBookmarkData:libBookmark
  90. options:0
  91. relativeToURL:nil
  92. bookmarkDataIsStale:nil
  93. error:&err];
  94. if (!libraryPath || ![[NSFileManager defaultManager] fileExistsAtPath:libraryPath.path]) {
  95. NSLog(@"Cannot resolve library path: %@", err);
  96. completion(NO, NSLocalizedString(@"Cannot find QEMU support libraries.", @"QEMUHelper"));
  97. return;
  98. }
  99. [self startQemuTask:binName standardOutput:standardOutput standardError:standardError libraryPath:libraryPath argv:argv completion:completion];
  100. }
  101. - (void)startQemuTask:(NSString *)binName standardOutput:(NSFileHandle *)standardOutput standardError:(NSFileHandle *)standardError libraryPath:(NSURL *)libraryPath argv:(NSArray<NSString *> *)argv completion:(void(^)(BOOL,NSString *))completion {
  102. NSError *err;
  103. NSTask *task = [NSTask new];
  104. NSMutableArray<NSString *> *newArgv = [argv mutableCopy];
  105. NSString *path = [libraryPath URLByAppendingPathComponent:binName].path;
  106. __weak typeof(self) _self = self;
  107. [newArgv insertObject:path atIndex:0];
  108. task.executableURL = [[[NSBundle mainBundle] URLForAuxiliaryExecutable:@"QEMULauncher.app"] URLByAppendingPathComponent:@"Contents/MacOS/QEMULauncher"];
  109. task.arguments = newArgv;
  110. task.standardOutput = standardOutput;
  111. task.standardError = standardError;
  112. NSMutableDictionary<NSString *, NSString *> *environment = [NSMutableDictionary dictionary];
  113. environment[@"TMPDIR"] = NSFileManager.defaultManager.temporaryDirectory.path;
  114. if (self.environment) {
  115. [environment addEntriesFromDictionary:self.environment];
  116. }
  117. task.environment = environment;
  118. task.qualityOfService = NSQualityOfServiceUserInitiated;
  119. task.terminationHandler = ^(NSTask *task) {
  120. _self.childTask = nil;
  121. [_self.connection.remoteObjectProxy qemuHasExited:task.terminationStatus message:nil];
  122. };
  123. if (![task launchAndReturnError:&err]) {
  124. NSLog(@"Error starting QEMU: %@", err);
  125. completion(NO, err.localizedDescription);
  126. } else {
  127. self.childTask = task;
  128. completion(YES, nil);
  129. }
  130. }
  131. - (void)terminate {
  132. [self.childTask terminate];
  133. self.childTask = nil;
  134. }
  135. @end