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