1 | //␊ |
2 | // PartitionInfoElement.mm␊ |
3 | // ChameleonPrefPane␊ |
4 | //␊ |
5 | // Powerful Partition Info class utility␊ |
6 | //␊ |
7 | // Created by Rekursor on 11-11-12.␊ |
8 | //␊ |
9 | ␊ |
10 | #import "PartitionInfoElement.h"␊ |
11 | #import "ShellProcess.h"␊ |
12 | ␊ |
13 | static NSUInteger sHdRedirTable[MAX_HD];␊ |
14 | ␊ |
15 | @implementation PartitionInfoElement␊ |
16 | ␊ |
17 | @synthesize descDict, bsdName, vUUID, vKind, vName, mediaPath, mediaRemovable, devInternal, devProtocol;␊ |
18 | @synthesize vAliasName, hidden;␊ |
19 | ␊ |
20 | /// Create a list of all bsd partitions␊ |
21 | +(NSArray*) createBSDPartitionList␊ |
22 | {␊ |
23 | ␉NSMutableArray* arr = [[NSMutableArray alloc] init];␊ |
24 | ␉␊ |
25 | ␉char line[256];␊ |
26 | ␉ShellProcess p;␊ |
27 | ␉␊ |
28 | ␉p.open("ls -1 /dev/disk*");␊ |
29 | ␉while( p.get_line(line, sizeof(line)-1))␊ |
30 | ␉{␊ |
31 | ␉␉size_t l = strlen(line);␊ |
32 | ␉␉if (l==0 || strstr(line, "ls:")!=NULL) continue;␊ |
33 | ␉␉if (line[l-1]) line[l-1]='\0';␊ |
34 | ␉␉const char * p = strstr(line, "disk");␊ |
35 | ␉␉NSString* s = [[NSString stringWithUTF8String: p] retain];␊ |
36 | ␉␉[arr addObject: s];␉␊ |
37 | ␉␉␊ |
38 | ␉}␊ |
39 | ␉p.close();␊ |
40 | ␉␊ |
41 | ␉return arr;␊ |
42 | }␊ |
43 | ␊ |
44 | /// redirection table for disk swapping␊ |
45 | +(NSUInteger*) hdRedirTable␊ |
46 | {␊ |
47 | ␉return sHdRedirTable;␊ |
48 | }␊ |
49 | ␊ |
50 | /// redirection table for disk swapping␊ |
51 | /// extract disk number from bsdname spec␊ |
52 | -(int) diskNumber␊ |
53 | {␊ |
54 | ␉int newDiskNum = (diskNum>=0 && diskNum <MAX_HD) ? sHdRedirTable[diskNum] : diskNum;␊ |
55 | ␉return newDiskNum;␊ |
56 | }␊ |
57 | ␊ |
58 | /// extract partition number from bsdname spec␊ |
59 | -(int) partitionNumber␊ |
60 | {␊ |
61 | ␉return partNum; // todo extract d num from bsdname␊ |
62 | }␊ |
63 | ␊ |
64 | -(NSString*) hdString␊ |
65 | {␊ |
66 | ␉return [NSString stringWithFormat:@"hd(%d,%d)", [self diskNumber], [self partitionNumber]];␊ |
67 | }␊ |
68 | ␊ |
69 | -(void) setDiskNumber: (int) d␊ |
70 | {␊ |
71 | ␉diskNum = d;␊ |
72 | }␊ |
73 | ␊ |
74 | -(void) setPartitionNumber:(int) p␊ |
75 | {␊ |
76 | ␉partNum = p;␊ |
77 | }␊ |
78 | /// return true if partition extraction successfully executed, false otherwise␊ |
79 | -(bool) isValid␊ |
80 | {␊ |
81 | ␉␊ |
82 | ␉return (␊ |
83 | ␉␉␉err==0␊ |
84 | ␉␉␉// && [self isBootable] don't hide non bootable disk for now to be in sync ␊ |
85 | ␉␉␉// with chameleon as much as possible␊ |
86 | ␉␉␉&& ![[self devProtocol] isEqual:@"Virtual Interface"]␊ |
87 | ␉␉␉);␊ |
88 | }␊ |
89 | ␊ |
90 | /// Extract a particular information from the disk partition dictionary from a key, generate a string value output␊ |
91 | -(NSString*) createStringValueWithKey: (NSString*) key␊ |
92 | {␊ |
93 | ␉if (err) return @"";␊ |
94 | ␉CFTypeRef o = [descDict objectForKey: key];␊ |
95 | ␉if (o==nil) return @"";␊ |
96 | ␉CFStringRef sref = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@"), o);␊ |
97 | ␉NSString* s = [[((NSString*) sref) copy] autorelease];␊ |
98 | ␉CFRelease(sref);␊ |
99 | ␉return s;␊ |
100 | }␊ |
101 | ␊ |
102 | /// main extraction method create a dictionary containing all needed information from disk arbitration API␊ |
103 | - (int) extractInfoWithBSDName: (NSString*) name withinSession:(DASessionRef) session␊ |
104 | {␊ |
105 | ␉err = 0;␊ |
106 | ␉␊ |
107 | ␉NSRange rDisk = [name rangeOfString:@"disk"];␊ |
108 | ␉NSRange rS = [name rangeOfString:@"s" options:NSBackwardsSearch] ;␊ |
109 | ␉␊ |
110 | ␉if (name == nil ␊ |
111 | ␉␉|| [name length] < 7 ␊ |
112 | ␉␉|| rDisk.location == NSNotFound ␊ |
113 | ␉␉|| rS.location <5) ␊ |
114 | ␉{␊ |
115 | ␉␉err = EINVAL;␊ |
116 | ␉␉return err;␊ |
117 | ␉}␊ |
118 | ␉␉␉␉␉␉␉␉␉␉␉ ␊ |
119 | ␉// extract disk and partition number␊ |
120 | ␉int pos = rDisk.location+rDisk.length;␊ |
121 | ␉int pos2 = rS.location+rS.length;␊ |
122 | ␉NSString* sDisk = [name substringWithRange:␊ |
123 | ␉␉␉␉␉ NSMakeRange(pos, rS.location - pos) ];␊ |
124 | ␉NSString* sPart = [name substringFromIndex: pos2 ];␊ |
125 | ␉␊ |
126 | ␉bool mustRelease = (session == nil) ? true : false;␊ |
127 | ␉␊ |
128 | ␉if (session == nil)␊ |
129 | ␉{␊ |
130 | ␉␉session = DASessionCreate(NULL);␊ |
131 | ␉}␊ |
132 | ␊ |
133 | DADiskRef disk;␊ |
134 | ␉␊ |
135 | ␉if (session == NULL)␉err = EINVAL;␊ |
136 | ␉␊ |
137 | ␉if (err == 0) {␊ |
138 | ␉␉disk = DADiskCreateFromBSDName(NULL, session, [name UTF8String] );␊ |
139 | ␉␉if (disk == NULL) err = EINVAL;␊ |
140 | ␉}␊ |
141 | ␉␊ |
142 | ␉[self cleanup];␊ |
143 | ␊ |
144 | ␉if (err == 0) {␊ |
145 | ␉␉CFDictionaryRef␉dr = DADiskCopyDescription(disk);␊ |
146 | ␉␉if (dr!=NULL)␊ |
147 | ␉␉{␊ |
148 | ␉␉␉self.descDict = [((NSDictionary*) dr) copy];␊ |
149 | ␉␉␉CFRelease(dr);␊ |
150 | ␉␉␉if (self.descDict == NULL) err = EINVAL;␊ |
151 | ␉␉}␊ |
152 | ␉}␊ |
153 | ␊ |
154 | ␉if (err == 0) {␊ |
155 | ␉␉diskNum = [sDisk␉intValue];␊ |
156 | ␉␉partNum = [sPart␉intValue];␊ |
157 | ␉␉␊ |
158 | ␉␉self.vUUID = [self createStringValueWithKey: @"DAVolumeUUID"];␊ |
159 | ␉␉self.vName = [self createStringValueWithKey: @"DAVolumeName"];␊ |
160 | ␉␉self.vAliasName = self.vName; // by default renamed = original part name ␊ |
161 | ␉␉self.vKind = [self createStringValueWithKey: @"DAVolumeKind"];␊ |
162 | ␉␉self.mediaPath = [self createStringValueWithKey: @"DAMediaPath"];␊ |
163 | ␉␉self.devProtocol = [self createStringValueWithKey: @"DADeviceProtocol"];␊ |
164 | ␉␉self.bsdName = [[NSString stringWithString:name] retain];␊ |
165 | ␉␉␊ |
166 | ␉␉self.devInternal = (bool) [[descDict objectForKey: @"DADeviceInternal"] boolValue];␊ |
167 | ␉␉self.mediaRemovable = (bool) [[descDict objectForKey: @"DAMediaRemovable"] boolValue];␊ |
168 | ␉␉␊ |
169 | ␉}␊ |
170 | ␉␊ |
171 | ␉// Clean up.␊ |
172 | ␉␊ |
173 | ␉if (disk != NULL)␉CFRelease(disk);␊ |
174 | ␉if (session != NULL && mustRelease) CFRelease(session);␊ |
175 | ␉␊ |
176 | ␉return err;␊ |
177 | }␊ |
178 | ␊ |
179 | +(NSMutableArray*)␉extractInfoWithBSDNames: (NSArray*) bsdNames␊ |
180 | {␊ |
181 | ␉return [PartitionInfoElement extractInfoWithBSDNames:bsdNames withArray: nil];␊ |
182 | }␊ |
183 | ␊ |
184 | /// main extraction method create a dictionary of PartitionInfoElement objects containing all needed information from disk arbitration API␊ |
185 | + (NSMutableArray*) extractInfoWithBSDNames: (NSArray*) names withArray:(NSMutableArray*) arr␊ |
186 | {␉␉␊ |
187 | ␉NSArray* partArr = [PartitionInfoElement createBSDPartitionList];␊ |
188 | ␉if (arr == nil)␊ |
189 | ␉␉arr = [[NSMutableArray alloc ] init];␊ |
190 | ␉␊ |
191 | ␉if (partArr!=nil && [partArr count]>0)␊ |
192 | ␉{␊ |
193 | ␉␉␉DASessionRef session = DASessionCreate(NULL);␊ |
194 | ␉␉␉for (NSString* part in partArr)␊ |
195 | ␉␉␉{␊ |
196 | ␉␉␉␉PartitionInfoElement* elt = ␊ |
197 | ␉␉␉␉[[PartitionInfoElement alloc] initWithBSDName: part withinSession: session];␊ |
198 | ␉␉␉␉if (elt!=nil && [[elt vName] length] >0 && [elt isValid] ) ␊ |
199 | ␉␉␉␉␉[arr addObject:elt];␊ |
200 | ␉␉␉}␊ |
201 | ␉␉␉if (session!=nil) CFRelease(session);␊ |
202 | ␉}␊ |
203 | ␉[partArr release];␊ |
204 | ␉return arr;␊ |
205 | }␊ |
206 | ␊ |
207 | - (int) extractInfoWithBSDName: (NSString*) name ␊ |
208 | {␊ |
209 | ␉return [self extractInfoWithBSDName:name withinSession: nil]; ␊ |
210 | }␊ |
211 | ␊ |
212 | -(bool) isBootable␊ |
213 | {␊ |
214 | ␉bool bootable = false;␊ |
215 | ␉NSFileManager* mgr = [NSFileManager defaultManager];␊ |
216 | ␉NSString *fmt = @"/Volumes/%@%s";␊ |
217 | ␉// that Windows is the name of WIN32 bootable disk dir ...␊ |
218 | ␉if(␊ |
219 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/System/Library/Extensions" ]] ||␊ |
220 | ␉ [mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/ntldr" ]] ||␊ |
221 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/bootmgr" ]] ||␊ |
222 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/boot/bcd" ]] ||␊ |
223 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/pagefile.sys" ]] ||␊ |
224 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/hiberfil.sys" ]] ␊ |
225 | ␉ )␊ |
226 | ␉␉bootable=true;␊ |
227 | ␉else if ([vName rangeOfString:@"ext"].location != NSNotFound) // linux ?␊ |
228 | ␉␉bootable = true;␊ |
229 | ␉return bootable;␊ |
230 | }␊ |
231 | ␊ |
232 | /// Return the partition image index according to its file system␊ |
233 | -(int) imageIndexFromFs␊ |
234 | {␊ |
235 | ␉␊ |
236 | ␉if ( [[self vKind] rangeOfString:@"hfs"].location != NSNotFound )␊ |
237 | ␉␉return 0; // Mac␊ |
238 | ␉if ( [[self vKind] rangeOfString:@"fat"].location != NSNotFound ||␊ |
239 | ␉␉ [[self vKind] rangeOfString:@"ntfs"].location != NSNotFound ||␊ |
240 | ␉␉ [[self vKind] rangeOfString:@"dos"].location != NSNotFound )␊ |
241 | ␉␉return 1; // Windows␊ |
242 | ␉if ( [[self vKind] rangeOfString:@"ext"].location != NSNotFound )␊ |
243 | ␉␉return 2; // Linux␊ |
244 | ␉␊ |
245 | ␉return 10; // unknown␊ |
246 | }␊ |
247 | ␊ |
248 | /// Volume label trimming utility function␊ |
249 | +(NSString*) removesSpacesFromLabel:(NSString*) label␊ |
250 | {␊ |
251 | ␉if (label == nil || [label length]==0) return label;␊ |
252 | ␉return [label stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];␊ |
253 | }␊ |
254 | ␊ |
255 | /// make the object description available for NSLog() debug purpose␊ |
256 | -(NSString*) description␊ |
257 | {␊ |
258 | ␉NSString* format =␊ |
259 | ␉@"(\n"␊ |
260 | ␉ " bsdName %@\n deviceProtocol %@\n deviceInternal %i\n"␊ |
261 | ␉ " volumeName %@\n volumeKind %@\n volumeUUID %@\n"␊ |
262 | ␉ " mediaPath %@\n mediaRemovable %i\n"␉␊ |
263 | ␉ ")";␊ |
264 | ␉NSString* value = [NSString stringWithFormat: format,␊ |
265 | ␉␉␉␉␉ self.bsdName, self.devProtocol, self.devInternal, ␊ |
266 | ␉␉␉␉␉ self.vName, self.vKind, self.vUUID, ␊ |
267 | ␉␉␉␉␉ self.mediaPath, self.mediaRemovable ];␊ |
268 | ␉return value;␊ |
269 | }␊ |
270 | ␊ |
271 | ␊ |
272 | /// initialize a partition element with DA partition info␊ |
273 | -(id) initWithBSDName:(NSString*) name ␊ |
274 | {␊ |
275 | ␉[super init];␊ |
276 | ␉diskNum = partNum = -1;␊ |
277 | ␉err = [self extractInfoWithBSDName: name withinSession: nil];␊ |
278 | ␉return self;␊ |
279 | }␊ |
280 | ␊ |
281 | /// initialize a partition element with DA partition info and an optional opened session␊ |
282 | -(id) initWithBSDName:(NSString*) name withinSession:(DASessionRef) session␊ |
283 | {␊ |
284 | ␉[super init];␊ |
285 | ␉diskNum = partNum = -1;␊ |
286 | ␉err = [self extractInfoWithBSDName: name withinSession: session];␊ |
287 | ␉␊ |
288 | ␉return self;␊ |
289 | }␊ |
290 | ␊ |
291 | -(void) cleanup␊ |
292 | {␊ |
293 | ␊ |
294 | ␉if (descDict != nil) CFRelease(descDict);␊ |
295 | ␉if (bsdName != nil) [bsdName release];␊ |
296 | ␉if (vUUID != nil) [vUUID release];␊ |
297 | ␉if (vKind != nil) [vKind release];␊ |
298 | ␉if (vName != nil) [vName release];␊ |
299 | ␉if (mediaPath != nil) [mediaPath release];␊ |
300 | ␉if (devProtocol != nil) [devProtocol release];␊ |
301 | ␉if (vAliasName != nil) [vAliasName release];␊ |
302 | ␉bsdName = vUUID = vKind = vName = mediaPath = devProtocol = vAliasName = nil;␊ |
303 | ␉descDict = nil;␊ |
304 | }␊ |
305 | ␊ |
306 | /// release created objects␊ |
307 | -(void) dealloc␊ |
308 | {␊ |
309 | ␉[self cleanup];␊ |
310 | ␉[super dealloc];␊ |
311 | }␊ |
312 | ␊ |
313 | @end␊ |
314 | |