QEMUHelper.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. @synthesize currentDirectoryPath;
  26. - (instancetype)init {
  27. if (self = [super init]) {
  28. self.urls = [NSMutableArray array];
  29. }
  30. return self;
  31. }
  32. - (void)dealloc {
  33. for (NSURL *url in self.urls) {
  34. [url stopAccessingSecurityScopedResource];
  35. }
  36. }
  37. - (void)accessDataWithBookmark:(NSData *)bookmark securityScoped:(BOOL)securityScoped completion:(void(^)(BOOL, NSData * _Nullable, NSString * _Nullable))completion {
  38. BOOL stale = NO;
  39. NSError *err;
  40. NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark
  41. options:(securityScoped ? NSURLBookmarkResolutionWithSecurityScope : 0)
  42. relativeToURL:nil
  43. bookmarkDataIsStale:&stale
  44. error:&err];
  45. if (!url) {
  46. NSLog(@"Failed to access bookmark data.");
  47. completion(NO, nil, nil);
  48. return;
  49. }
  50. if (stale || !securityScoped) {
  51. bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
  52. includingResourceValuesForKeys:nil
  53. relativeToURL:nil
  54. error:&err];
  55. // if we fail, try again with read-only access
  56. if (!bookmark) {
  57. bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
  58. includingResourceValuesForKeys:nil
  59. relativeToURL:nil
  60. error:&err];
  61. }
  62. if (!bookmark) {
  63. NSLog(@"Failed to create new bookmark!");
  64. completion(NO, bookmark, url.path);
  65. return;
  66. }
  67. }
  68. if ([url startAccessingSecurityScopedResource]) {
  69. [self.urls addObject:url];
  70. } else {
  71. NSLog(@"Failed to access security scoped resource for: %@", url);
  72. }
  73. completion(YES, bookmark, url.path);
  74. }
  75. - (void)stopAccessingPath:(nullable NSString *)path {
  76. if (!path) {
  77. return;
  78. }
  79. for (NSURL *url in _urls) {
  80. if ([url.path isEqualToString:path]) {
  81. [url stopAccessingSecurityScopedResource];
  82. [_urls removeObject:url];
  83. return;
  84. }
  85. }
  86. NSLog(@"Cannot find '%@' in existing scoped access.", path);
  87. }
  88. - (void)startQemu:(NSString *)binName standardOutput:(NSFileHandle *)standardOutput standardError:(NSFileHandle *)standardError libraryBookmark:(NSData *)libBookmark argv:(NSArray<NSString *> *)argv completion:(void(^)(BOOL,NSString *))completion {
  89. NSError *err;
  90. NSURL *libraryPath = [NSURL URLByResolvingBookmarkData:libBookmark
  91. options:0
  92. relativeToURL:nil
  93. bookmarkDataIsStale:nil
  94. error:&err];
  95. if (!libraryPath || ![[NSFileManager defaultManager] fileExistsAtPath:libraryPath.path]) {
  96. NSLog(@"Cannot resolve library path: %@", err);
  97. completion(NO, NSLocalizedString(@"Cannot find QEMU support libraries.", @"QEMUHelper"));
  98. return;
  99. }
  100. [self startQemuTask:binName standardOutput:standardOutput standardError:standardError libraryPath:libraryPath argv:argv completion:completion];
  101. }
  102. - (void)startQemuTask:(NSString *)binName standardOutput:(NSFileHandle *)standardOutput standardError:(NSFileHandle *)standardError libraryPath:(NSURL *)libraryPath argv:(NSArray<NSString *> *)argv completion:(void(^)(BOOL,NSString *))completion {
  103. NSError *err;
  104. NSTask *task = [NSTask new];
  105. NSMutableArray<NSString *> *newArgv = [argv mutableCopy];
  106. NSString *path = [libraryPath URLByAppendingPathComponent:binName].path;
  107. __weak typeof(self) _self = self;
  108. [newArgv insertObject:path atIndex:0];
  109. task.executableURL = [[[NSBundle mainBundle] URLForAuxiliaryExecutable:@"QEMULauncher.app"] URLByAppendingPathComponent:@"Contents/MacOS/QEMULauncher"];
  110. task.arguments = newArgv;
  111. task.standardOutput = standardOutput;
  112. task.standardError = standardError;
  113. NSMutableDictionary<NSString *, NSString *> *environment = [NSMutableDictionary dictionary];
  114. environment[@"TMPDIR"] = NSFileManager.defaultManager.temporaryDirectory.path;
  115. if (self.environment) {
  116. [environment addEntriesFromDictionary:self.environment];
  117. }
  118. task.environment = environment;
  119. if (self.currentDirectoryPath) {
  120. task.currentDirectoryURL = [NSURL fileURLWithPath:self.currentDirectoryPath];
  121. }
  122. task.qualityOfService = NSQualityOfServiceUserInitiated;
  123. task.terminationHandler = ^(NSTask *task) {
  124. _self.childTask = nil;
  125. [_self.connection.remoteObjectProxy processHasExited:task.terminationStatus message:nil];
  126. };
  127. if (![task launchAndReturnError:&err]) {
  128. NSLog(@"Error starting QEMU: %@", err);
  129. completion(NO, err.localizedDescription);
  130. } else {
  131. self.childTask = task;
  132. completion(YES, nil);
  133. }
  134. }
  135. - (void)terminate {
  136. [self.childTask terminate];
  137. self.childTask = nil;
  138. }
  139. @end