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] autorelease];␊ |
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] ;␊ |
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] ␊ |
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*) stringValueWithKey: (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 | ␉if (session == NULL)␉err = EINVAL;␊ |
134 | ␉␊ |
135 | ␉if (err == 0) {␊ |
136 | ␉␉DADiskRef disk = DADiskCreateFromBSDName(NULL, session, [name UTF8String] );␊ |
137 | ␉␉if (disk == NULL) err = EINVAL;␊ |
138 | else␊ |
139 | {␊ |
140 | [self cleanup];␊ |
141 | CFDictionaryRef␉dr = DADiskCopyDescription(disk);␊ |
142 | if (dr!=NULL)␊ |
143 | {␊ |
144 | descDict = (NSDictionary*) dr;␊ |
145 | CFRelease(dr);␊ |
146 | }␊ |
147 | }␊ |
148 | if (disk) CFRelease(disk);␊ |
149 | ␉}␊ |
150 | ␊ |
151 | ␉if (err == 0) {␊ |
152 | ␉␉diskNum = [sDisk␉intValue];␊ |
153 | ␉␉partNum = [sPart␉intValue];␊ |
154 | ␉␉␊ |
155 | ␉␉self.vUUID = [self stringValueWithKey: @"DAVolumeUUID"];␊ |
156 | ␉␉self.vName = [self stringValueWithKey: @"DAVolumeName"];␊ |
157 | ␉␉self.vAliasName = self.vName; // by default renamed = original part name ␊ |
158 | ␉␉self.vKind = [self stringValueWithKey: @"DAVolumeKind"];␊ |
159 | ␉␉self.mediaPath = [self stringValueWithKey: @"DAMediaPath"];␊ |
160 | ␉␉self.devProtocol = [self stringValueWithKey: @"DADeviceProtocol"];␊ |
161 | ␉␉self.bsdName = [NSString stringWithString:name];␊ |
162 | ␉␉␊ |
163 | ␉␉self.devInternal = (bool) [[descDict objectForKey: @"DADeviceInternal"] boolValue];␊ |
164 | ␉␉self.mediaRemovable = (bool) [[descDict objectForKey: @"DAMediaRemovable"] boolValue];␊ |
165 | ␉␉␊ |
166 | ␉}␊ |
167 | ␉␊ |
168 | ␉// Clean up.␊ |
169 | ␉␊ |
170 | ␉if (session != NULL && mustRelease) CFRelease(session);␊ |
171 | ␉␊ |
172 | ␉return err;␊ |
173 | }␊ |
174 | ␊ |
175 | +(NSMutableArray*)␉extractInfoWithBSDNames: (NSArray*) bsdNames␊ |
176 | {␊ |
177 | ␉return [PartitionInfoElement extractInfoWithBSDNames:bsdNames withArray: nil];␊ |
178 | }␊ |
179 | ␊ |
180 | /// main extraction method create a dictionary of PartitionInfoElement objects containing all needed information from disk arbitration API␊ |
181 | + (NSMutableArray*) extractInfoWithBSDNames: (NSArray*) names withArray:(NSMutableArray*) arr␊ |
182 | {␉␉␊ |
183 | ␉NSArray* partArr = [PartitionInfoElement createBSDPartitionList];␊ |
184 | ␉if (arr == nil)␊ |
185 | ␉␉arr = [[[NSMutableArray alloc ] init] autorelease];␊ |
186 | ␉␊ |
187 | ␉if (partArr!=nil && [partArr count]>0)␊ |
188 | ␉{␊ |
189 | ␉␉␉DASessionRef session = DASessionCreate(NULL);␊ |
190 | ␉␉␉for (NSString* part in partArr)␊ |
191 | ␉␉␉{␊ |
192 | ␉␉␉␉PartitionInfoElement* elt = ␊ |
193 | ␉␉␉␉[[[PartitionInfoElement alloc] initWithBSDName: part withinSession: session] autorelease];␊ |
194 | ␉␉␉␉if (elt!=nil && [[elt vName] length] >0 && [elt isValid] ) ␊ |
195 | ␉␉␉␉␉[arr addObject: elt];␊ |
196 | ␉␉␉}␊ |
197 | ␉␉␉if (session!=nil) CFRelease(session);␊ |
198 | ␉}␊ |
199 | ␉return arr;␊ |
200 | }␊ |
201 | ␊ |
202 | - (int) extractInfoWithBSDName: (NSString*) name ␊ |
203 | {␊ |
204 | ␉return [self extractInfoWithBSDName:name withinSession: nil]; ␊ |
205 | }␊ |
206 | ␊ |
207 | -(bool) isBootable␊ |
208 | {␊ |
209 | ␉bool bootable = false;␊ |
210 | ␉NSFileManager* mgr = [NSFileManager defaultManager];␊ |
211 | ␉NSString *fmt = @"/Volumes/%@%s";␊ |
212 | ␉// that Windows is the name of WIN32 bootable disk dir ...␊ |
213 | ␉if(␊ |
214 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/System/Library/Extensions" ]] ||␊ |
215 | ␉ [mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/ntldr" ]] ||␊ |
216 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/bootmgr" ]] ||␊ |
217 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/boot/bcd" ]] ||␊ |
218 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/pagefile.sys" ]] ||␊ |
219 | ␉␉[mgr fileExistsAtPath: [NSString stringWithFormat: fmt, vName, "/hiberfil.sys" ]] ␊ |
220 | ␉ )␊ |
221 | ␉␉bootable=true;␊ |
222 | ␉else if ([vName rangeOfString:@"ext"].location != NSNotFound) // linux ?␊ |
223 | ␉␉bootable = true;␊ |
224 | ␉return bootable;␊ |
225 | }␊ |
226 | ␊ |
227 | /// Return the partition image index according to its file system␊ |
228 | -(int) imageIndexFromFs␊ |
229 | {␊ |
230 | ␉␊ |
231 | ␉if ( [[self vKind] rangeOfString:@"hfs"].location != NSNotFound )␊ |
232 | ␉␉return 0; // Mac␊ |
233 | ␉if ( [[self vKind] rangeOfString:@"fat"].location != NSNotFound ||␊ |
234 | ␉␉ [[self vKind] rangeOfString:@"ntfs"].location != NSNotFound ||␊ |
235 | ␉␉ [[self vKind] rangeOfString:@"dos"].location != NSNotFound )␊ |
236 | ␉␉return 1; // Windows␊ |
237 | ␉if ( [[self vKind] rangeOfString:@"ext"].location != NSNotFound )␊ |
238 | ␉␉return 2; // Linux␊ |
239 | ␉␊ |
240 | ␉return 10; // unknown␊ |
241 | }␊ |
242 | ␊ |
243 | /// Volume label trimming utility function␊ |
244 | +(NSString*) removesSpacesFromLabel:(NSString*) label␊ |
245 | {␊ |
246 | ␉if (label == nil || [label length]==0) return label;␊ |
247 | ␉return [label stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];␊ |
248 | }␊ |
249 | ␊ |
250 | /// make the object description available for NSLog() debug purpose␊ |
251 | -(NSString*) description␊ |
252 | {␊ |
253 | ␉NSString* format =␊ |
254 | ␉@"(\n"␊ |
255 | ␉ " bsdName %@\n deviceProtocol %@\n deviceInternal %i\n"␊ |
256 | ␉ " volumeName %@\n volumeKind %@\n volumeUUID %@\n"␊ |
257 | ␉ " mediaPath %@\n mediaRemovable %i\n"␉␊ |
258 | ␉ ")";␊ |
259 | ␉NSString* value = [NSString stringWithFormat: format,␊ |
260 | ␉␉␉␉␉ self.bsdName, self.devProtocol, self.devInternal, ␊ |
261 | ␉␉␉␉␉ self.vName, self.vKind, self.vUUID, ␊ |
262 | ␉␉␉␉␉ self.mediaPath, self.mediaRemovable ];␊ |
263 | ␉return value;␊ |
264 | }␊ |
265 | ␊ |
266 | ␊ |
267 | /// initialize a partition element with DA partition info␊ |
268 | -(id) initWithBSDName:(NSString*) name ␊ |
269 | {␊ |
270 | ␉self = [super init];␊ |
271 | ␉diskNum = partNum = -1;␊ |
272 | ␉err = [self extractInfoWithBSDName: name withinSession: nil];␊ |
273 | ␉return self;␊ |
274 | }␊ |
275 | ␊ |
276 | /// initialize a partition element with DA partition info and an optional opened session␊ |
277 | -(id) initWithBSDName:(NSString*) name withinSession:(DASessionRef) session␊ |
278 | {␊ |
279 | ␉self = [super init];␊ |
280 | ␉diskNum = partNum = -1;␊ |
281 | ␉err = [self extractInfoWithBSDName: name withinSession: session];␊ |
282 | ␉␊ |
283 | ␉return self;␊ |
284 | }␊ |
285 | ␊ |
286 | -(void) cleanup␊ |
287 | {␊ |
288 | ␊ |
289 | ␉if (descDict != nil) CFRelease(descDict);␊ |
290 | ␉if (bsdName != nil) [bsdName release];␊ |
291 | ␉if (vUUID != nil) [vUUID release];␊ |
292 | ␉if (vKind != nil) [vKind release];␊ |
293 | ␉if (vName != nil) [vName release];␊ |
294 | ␉if (mediaPath != nil) [mediaPath release];␊ |
295 | ␉if (devProtocol != nil) [devProtocol release];␊ |
296 | ␉if (vAliasName != nil) [vAliasName release];␊ |
297 | ␉bsdName = vUUID = vKind = vName = mediaPath = devProtocol = vAliasName = nil;␊ |
298 | ␉descDict = nil;␊ |
299 | }␊ |
300 | ␊ |
301 | /// release created objects␊ |
302 | -(void) dealloc␊ |
303 | {␊ |
304 | ␉[self cleanup];␊ |
305 | ␉[super dealloc];␊ |
306 | }␊ |
307 | ␊ |
308 | @end␊ |
309 | |