2
0
Эх сурвалжийг харах

Merge pull request #2285 from conath/2284-fix-crash

Fix crash when opening VM configuration #2284
osy 4 жил өмнө
parent
commit
53deefa64a

+ 15 - 9
Configuration/UTMConfiguration+Drives.m

@@ -72,7 +72,9 @@ static const NSString *const kUTMConfigCdromKey = @"Cdrom";
     // find existing drives
     // find existing drives
     NSMutableSet<NSString *> *existing = [NSMutableSet set];
     NSMutableSet<NSString *> *existing = [NSMutableSet set];
     for (NSInteger i = 0; i < self.countDrives; i++) {
     for (NSInteger i = 0; i < self.countDrives; i++) {
-        [existing addObject:[self driveImagePathForIndex:i]];
+        if (![self driveRemovableForIndex:i]) {
+            [existing addObject:[self driveImagePathForIndex:i]];
+        }
     }
     }
     // add any missing drives
     // add any missing drives
     NSMutableArray<NSString *> *orphans = [NSMutableArray array];
     NSMutableArray<NSString *> *orphans = [NSMutableArray array];
@@ -102,10 +104,10 @@ static const NSString *const kUTMConfigCdromKey = @"Cdrom";
     NSInteger index = [self countDrives];
     NSInteger index = [self countDrives];
     NSString *strType = [UTMConfiguration supportedImageTypes][type];
     NSString *strType = [UTMConfiguration supportedImageTypes][type];
     NSMutableDictionary *drive = [[NSMutableDictionary alloc] initWithDictionary:@{
     NSMutableDictionary *drive = [[NSMutableDictionary alloc] initWithDictionary:@{
-                                                                                   kUTMConfigImagePathKey: name,
-                                                                                   kUTMConfigImageTypeKey: strType,
-                                                                                   kUTMConfigInterfaceTypeKey: interface
-                                                                                   }];
+        kUTMConfigImagePathKey: name,
+        kUTMConfigImageTypeKey: strType,
+        kUTMConfigInterfaceTypeKey: interface
+    }];
     [self propertyWillChange];
     [self propertyWillChange];
     [self.rootDict[kUTMConfigDrivesKey] addObject:drive];
     [self.rootDict[kUTMConfigDrivesKey] addObject:drive];
     return index;
     return index;
@@ -115,10 +117,10 @@ static const NSString *const kUTMConfigCdromKey = @"Cdrom";
     NSInteger index = [self countDrives];
     NSInteger index = [self countDrives];
     NSString *strType = [UTMConfiguration supportedImageTypes][type];
     NSString *strType = [UTMConfiguration supportedImageTypes][type];
     NSMutableDictionary *drive = [[NSMutableDictionary alloc] initWithDictionary:@{
     NSMutableDictionary *drive = [[NSMutableDictionary alloc] initWithDictionary:@{
-                                                                                   kUTMConfigRemovableKey: @(YES),
-                                                                                   kUTMConfigImageTypeKey: strType,
-                                                                                   kUTMConfigInterfaceTypeKey: interface
-                                                                                   }];
+        kUTMConfigRemovableKey: @(YES),
+        kUTMConfigImageTypeKey: strType,
+        kUTMConfigInterfaceTypeKey: interface
+    }];
     [self propertyWillChange];
     [self propertyWillChange];
     [self.rootDict[kUTMConfigDrivesKey] addObject:drive];
     [self.rootDict[kUTMConfigDrivesKey] addObject:drive];
     return index;
     return index;
@@ -179,6 +181,10 @@ static const NSString *const kUTMConfigCdromKey = @"Cdrom";
 
 
 - (void)setDriveRemovable:(BOOL)isRemovable forIndex:(NSInteger)index {
 - (void)setDriveRemovable:(BOOL)isRemovable forIndex:(NSInteger)index {
     self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigRemovableKey] = @(isRemovable);
     self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigRemovableKey] = @(isRemovable);
+    if (isRemovable) {
+        [self.rootDict[kUTMConfigDrivesKey][index] removeObjectForKey:kUTMConfigImagePathKey];
+    }
+    [self propertyWillChange];
 }
 }
 
 
 - (void)moveDriveIndex:(NSInteger)index to:(NSInteger)newIndex {
 - (void)moveDriveIndex:(NSInteger)index to:(NSInteger)newIndex {

+ 9 - 9
Platform/UTMData.swift

@@ -325,7 +325,7 @@ class UTMData: ObservableObject {
     
     
     // MARK: - Disk drive functions
     // MARK: - Disk drive functions
     
     
-    func importDrive(_ drive: URL, forConfig: UTMConfiguration, copy: Bool = true) throws {
+    func importDrive(_ drive: URL, for config: UTMConfiguration, copy: Bool = true) throws {
         _ = drive.startAccessingSecurityScopedResource()
         _ = drive.startAccessingSecurityScopedResource()
         defer { drive.stopAccessingSecurityScopedResource() }
         defer { drive.stopAccessingSecurityScopedResource() }
         
         
@@ -340,7 +340,7 @@ class UTMData: ObservableObject {
         
         
         let name = drive.lastPathComponent
         let name = drive.lastPathComponent
         let imageType: UTMDiskImageType = drive.pathExtension.lowercased() == "iso" ? .CD : .disk
         let imageType: UTMDiskImageType = drive.pathExtension.lowercased() == "iso" ? .CD : .disk
-        let imagesPath = forConfig.imagesPath
+        let imagesPath = config.imagesPath
         let dstPath = imagesPath.appendingPathComponent(name)
         let dstPath = imagesPath.appendingPathComponent(name)
         if !fileManager.fileExists(atPath: imagesPath.path) {
         if !fileManager.fileExists(atPath: imagesPath.path) {
             try fileManager.createDirectory(at: imagesPath, withIntermediateDirectories: false, attributes: nil)
             try fileManager.createDirectory(at: imagesPath, withIntermediateDirectories: false, attributes: nil)
@@ -352,23 +352,23 @@ class UTMData: ObservableObject {
         }
         }
         DispatchQueue.main.async {
         DispatchQueue.main.async {
             let interface: String
             let interface: String
-            if let target = forConfig.systemTarget {
+            if let target = config.systemTarget {
                 interface = UTMConfiguration.defaultDriveInterface(forTarget: target, type: imageType)
                 interface = UTMConfiguration.defaultDriveInterface(forTarget: target, type: imageType)
             } else {
             } else {
                 interface = "none"
                 interface = "none"
             }
             }
-            forConfig.newDrive(name, type: imageType, interface: interface)
+            config.newDrive(name, type: imageType, interface: interface)
         }
         }
     }
     }
     
     
-    func createDrive(_ drive: VMDriveImage, forConfig: UTMConfiguration) throws {
+    func createDrive(_ drive: VMDriveImage, for config: UTMConfiguration) throws {
         var name: String = ""
         var name: String = ""
         if !drive.removable {
         if !drive.removable {
             guard drive.size > 0 else {
             guard drive.size > 0 else {
                 throw NSLocalizedString("Invalid drive size.", comment: "UTMData")
                 throw NSLocalizedString("Invalid drive size.", comment: "UTMData")
             }
             }
-            name = newDefaultDriveName(type: drive.imageType, forConfig: forConfig)
-            let imagesPath = forConfig.imagesPath
+            name = newDefaultDriveName(type: drive.imageType, forConfig: config)
+            let imagesPath = config.imagesPath
             let dstPath = imagesPath.appendingPathComponent(name)
             let dstPath = imagesPath.appendingPathComponent(name)
             if !fileManager.fileExists(atPath: imagesPath.path) {
             if !fileManager.fileExists(atPath: imagesPath.path) {
                 try fileManager.createDirectory(at: imagesPath, withIntermediateDirectories: false, attributes: nil)
                 try fileManager.createDirectory(at: imagesPath, withIntermediateDirectories: false, attributes: nil)
@@ -383,9 +383,9 @@ class UTMData: ObservableObject {
         DispatchQueue.main.async {
         DispatchQueue.main.async {
             let interface = drive.interface ?? "none"
             let interface = drive.interface ?? "none"
             if drive.removable {
             if drive.removable {
-                forConfig.newRemovableDrive(drive.imageType, interface: interface)
+                config.newRemovableDrive(drive.imageType, interface: interface)
             } else {
             } else {
-                forConfig.newDrive(name, type: drive.imageType, interface: interface)
+                config.newDrive(name, type: drive.imageType, interface: interface)
             }
             }
         }
         }
     }
     }

+ 1 - 3
Platform/iOS/Legacy/VMConfigDriveDetailViewController.m

@@ -168,9 +168,7 @@
         }
         }
     } else {
     } else {
         [self.configuration setDriveRemovable:self.removable forIndex:self.driveIndex];
         [self.configuration setDriveRemovable:self.removable forIndex:self.driveIndex];
-        if (self.removable) {
-            [self.configuration setImagePath:@"" forIndex:self.driveIndex];
-        } else {
+        if (!self.removable) {
             [self.configuration setImagePath:self.imageName forIndex:self.driveIndex];
             [self.configuration setImagePath:self.imageName forIndex:self.driveIndex];
         }
         }
         [self.configuration setDriveInterfaceType:self.driveInterfaceType forIndex:self.driveIndex];
         [self.configuration setDriveInterfaceType:self.driveInterfaceType forIndex:self.driveIndex];

+ 2 - 2
Platform/iOS/VMConfigDrivesView.swift

@@ -86,7 +86,7 @@ struct VMConfigDrivesView: View {
         data.busyWork {
         data.busyWork {
             switch result {
             switch result {
             case .success(let url):
             case .success(let url):
-                try data.importDrive(url, forConfig: config)
+                try data.importDrive(url, for: config)
                 break
                 break
             case .failure(let err):
             case .failure(let err):
                 throw err
                 throw err
@@ -96,7 +96,7 @@ struct VMConfigDrivesView: View {
     
     
     private func newDrive(driveImage: VMDriveImage) {
     private func newDrive(driveImage: VMDriveImage) {
         data.busyWork {
         data.busyWork {
-            try data.createDrive(driveImage, forConfig: config)
+            try data.createDrive(driveImage, for: config)
         }
         }
     }
     }
     
     

+ 2 - 2
Platform/macOS/VMConfigDrivesView.swift

@@ -68,7 +68,7 @@ struct VMConfigDrivesView: View {
         data.busyWork {
         data.busyWork {
             switch result {
             switch result {
             case .success(let url):
             case .success(let url):
-                try data.importDrive(url, forConfig: config)
+                try data.importDrive(url, for: config)
                 break
                 break
             case .failure(let err):
             case .failure(let err):
                 throw err
                 throw err
@@ -79,7 +79,7 @@ struct VMConfigDrivesView: View {
     private func addNewDrive(_ newDrive: VMDriveImage) {
     private func addNewDrive(_ newDrive: VMDriveImage) {
         newDrivePopover = false // hide popover
         newDrivePopover = false // hide popover
         data.busyWork {
         data.busyWork {
-            try data.createDrive(newDrive, forConfig: config)
+            try data.createDrive(newDrive, for: config)
         }
         }
     }
     }
 }
 }