-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
UTMConfiguration+Drives.m
202 lines (173 loc) · 7.08 KB
/
UTMConfiguration+Drives.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// Copyright © 2020 osy. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "UTMConfiguration+Constants.h"
#import "UTMConfiguration+Drives.h"
#import "UTM-Swift.h"
extern const NSString *const kUTMConfigDrivesKey;
static const NSString *const kUTMConfigImagePathKey = @"ImagePath";
static const NSString *const kUTMConfigImageTypeKey = @"ImageType";
static const NSString *const kUTMConfigInterfaceTypeKey = @"InterfaceType";
static const NSString *const kUTMConfigRemovableKey = @"Removable";
static const NSString *const kUTMConfigCdromKey = @"Cdrom";
@interface UTMConfiguration ()
@property (nonatomic, readonly) NSMutableDictionary *rootDict;
@end
@implementation UTMConfiguration (Drives)
#pragma mark - Images Path
- (NSURL *)imagesPath {
if (self.existingPath) {
return [self.existingPath URLByAppendingPathComponent:[UTMConfiguration diskImagesDirectory] isDirectory:YES];
} else {
return [[NSFileManager defaultManager].temporaryDirectory URLByAppendingPathComponent:[UTMConfiguration diskImagesDirectory] isDirectory:YES];
}
}
#pragma mark - Migration
- (void)migrateDriveConfigurationIfNecessary {
// Migrate Cdrom => ImageType
[self.rootDict[kUTMConfigDrivesKey] enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (!obj[kUTMConfigImageTypeKey]) {
if ([obj[kUTMConfigCdromKey] boolValue]) {
[self setDriveImageType:UTMDiskImageTypeCD forIndex:idx];
} else {
[self setDriveImageType:UTMDiskImageTypeDisk forIndex:idx];
}
[obj removeObjectForKey:kUTMConfigCdromKey];
}
}];
}
#pragma mark - Orphan drives
// ensure user can delete the drive image from the interface if something wrong happens
- (NSArray<NSString *> *)orphanedDrives {
NSFileManager *manager = [NSFileManager defaultManager];
NSArray<NSURL *> *files = [manager contentsOfDirectoryAtURL:self.imagesPath includingPropertiesForKeys:nil options:0 error:nil];
if (files.count == 0) {
return nil; // empty or does not exist
}
// find existing drives
NSMutableSet<NSString *> *existing = [NSMutableSet set];
for (NSInteger i = 0; i < self.countDrives; i++) {
if (![self driveRemovableForIndex:i]) {
[existing addObject:[self driveImagePathForIndex:i]];
}
}
// add any missing drives
NSMutableArray<NSString *> *orphans = [NSMutableArray array];
for (NSURL *file in files) {
NSString *name = [file lastPathComponent];
if (![existing containsObject:name]) {
[orphans addObject:name];
}
}
return orphans;
}
- (void)recoverOrphanedDrives {
NSArray<NSString *> *orphans = self.orphanedDrives;
for (NSInteger i = 0; i < orphans.count; i++) {
[self newDrive:orphans[i] type:UTMDiskImageTypeNone interface:@""];
}
}
#pragma mark - Drives array handling
- (NSInteger)countDrives {
return [self.rootDict[kUTMConfigDrivesKey] count];
}
- (NSInteger)newDrive:(NSString *)name type:(UTMDiskImageType)type interface:(NSString *)interface {
NSInteger index = [self countDrives];
NSString *strType = [UTMConfiguration supportedImageTypes][type];
NSMutableDictionary *drive = [[NSMutableDictionary alloc] initWithDictionary:@{
kUTMConfigImagePathKey: name,
kUTMConfigImageTypeKey: strType,
kUTMConfigInterfaceTypeKey: interface
}];
[self propertyWillChange];
[self.rootDict[kUTMConfigDrivesKey] addObject:drive];
return index;
}
- (NSInteger)newRemovableDrive:(UTMDiskImageType)type interface:(NSString *)interface {
NSInteger index = [self countDrives];
NSString *strType = [UTMConfiguration supportedImageTypes][type];
NSMutableDictionary *drive = [[NSMutableDictionary alloc] initWithDictionary:@{
kUTMConfigRemovableKey: @(YES),
kUTMConfigImageTypeKey: strType,
kUTMConfigInterfaceTypeKey: interface
}];
[self propertyWillChange];
[self.rootDict[kUTMConfigDrivesKey] addObject:drive];
return index;
}
- (nullable NSString *)driveImagePathForIndex:(NSInteger)index {
if (index >= self.countDrives) {
return nil;
} else {
return self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigImagePathKey];
}
}
- (void)setImagePath:(NSString *)path forIndex:(NSInteger)index {
[self propertyWillChange];
self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigImagePathKey] = path;
}
- (nullable NSString *)driveInterfaceTypeForIndex:(NSInteger)index {
if (index >= self.countDrives) {
return nil;
} else {
return self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigInterfaceTypeKey];
}
}
- (void)setDriveInterfaceType:(NSString *)interfaceType forIndex:(NSInteger)index {
[self propertyWillChange];
self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigInterfaceTypeKey] = interfaceType;
}
- (UTMDiskImageType)driveImageTypeForIndex:(NSInteger)index {
if (index >= self.countDrives) {
return UTMDiskImageTypeDisk;
}
NSString *strType = self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigImageTypeKey];
NSInteger type = [[UTMConfiguration supportedImageTypes] indexOfObject:strType];
if (type == NSNotFound || type >= UTMDiskImageTypeMax) {
return UTMDiskImageTypeDisk;
} else {
return (UTMDiskImageType)type;
}
}
- (void)setDriveImageType:(UTMDiskImageType)type forIndex:(NSInteger)index {
NSString *strType = [UTMConfiguration supportedImageTypes][type];
[self propertyWillChange];
self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigImageTypeKey] = strType;
}
- (BOOL)driveRemovableForIndex:(NSInteger)index {
if (index >= self.countDrives) {
return NO;
} else {
return [self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigRemovableKey] boolValue];
}
}
- (void)setDriveRemovable:(BOOL)isRemovable forIndex:(NSInteger)index {
self.rootDict[kUTMConfigDrivesKey][index][kUTMConfigRemovableKey] = @(isRemovable);
if (isRemovable) {
[self.rootDict[kUTMConfigDrivesKey][index] removeObjectForKey:kUTMConfigImagePathKey];
}
[self propertyWillChange];
}
- (void)moveDriveIndex:(NSInteger)index to:(NSInteger)newIndex {
NSMutableDictionary *drive = self.rootDict[kUTMConfigDrivesKey][index];
[self propertyWillChange];
[self.rootDict[kUTMConfigDrivesKey] removeObjectAtIndex:index];
[self.rootDict[kUTMConfigDrivesKey] insertObject:drive atIndex:newIndex];
}
- (void)removeDriveAtIndex:(NSInteger)index {
[self propertyWillChange];
[self.rootDict[kUTMConfigDrivesKey] removeObjectAtIndex:index];
}
@end