Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/ChameleonPrefPane/Sources/PartitionInfoElement.mm

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
13static 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{
23NSMutableArray* arr = [[[NSMutableArray alloc] init] autorelease];
24
25char line[256];
26ShellProcess p;
27
28p.open("ls -1 /dev/disk*");
29while( p.get_line(line, sizeof(line)-1))
30{
31size_t l = strlen(line);
32if (l==0 || strstr(line, "ls:")!=NULL) continue;
33if (line[l-1]) line[l-1]='\0';
34const char * p = strstr(line, "disk");
35NSString* s = [NSString stringWithUTF8String: p] ;
36[arr addObject: s];
37
38}
39p.close();
40
41return arr;
42}
43
44/// redirection table for disk swapping
45+(NSUInteger*) hdRedirTable
46{
47return sHdRedirTable;
48}
49
50/// redirection table for disk swapping
51/// extract disk number from bsdname spec
52-(int) diskNumber
53{
54int newDiskNum = (diskNum>=0 && diskNum <MAX_HD) ? sHdRedirTable[diskNum] : diskNum;
55return newDiskNum;
56}
57
58/// extract partition number from bsdname spec
59-(int) partitionNumber
60{
61return partNum; // todo extract d num from bsdname
62}
63
64-(NSString*) hdString
65{
66return [NSString stringWithFormat:@"hd(%d,%d)", [self diskNumber], [self partitionNumber]];
67}
68
69-(void) setDiskNumber: (int) d
70{
71diskNum = d;
72}
73
74-(void) setPartitionNumber:(int) p
75{
76partNum = p;
77}
78/// return true if partition extraction successfully executed, false otherwise
79-(bool) isValid
80{
81
82return (
83err==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*) stringValueWithKey: (NSString*) key
92{
93if (err) return @"";
94CFTypeRef o = [descDict objectForKey: key];
95if (o==nil) return @"";
96CFStringRef sref = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@"), o);
97NSString* s = [[((NSString*) sref) copy] autorelease];
98CFRelease(sref);
99return 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{
105err = 0;
106
107NSRange rDisk = [name rangeOfString:@"disk"];
108NSRange rS = [name rangeOfString:@"s" options:NSBackwardsSearch] ;
109
110if (name == nil
111|| [name length] < 7
112|| rDisk.location == NSNotFound
113|| rS.location <5)
114{
115err = EINVAL;
116return err;
117}
118
119// extract disk and partition number
120int pos = rDisk.location+rDisk.length;
121int pos2 = rS.location+rS.length;
122NSString* sDisk = [name substringWithRange:
123 NSMakeRange(pos, rS.location - pos) ];
124NSString* sPart = [name substringFromIndex: pos2 ];
125
126bool mustRelease = (session == nil) ? true : false;
127
128if (session == nil)
129{
130session = DASessionCreate(NULL);
131}
132
133if (session == NULL)err = EINVAL;
134
135if (err == 0) {
136DADiskRef disk = DADiskCreateFromBSDName(NULL, session, [name UTF8String] );
137if (disk == NULL) err = EINVAL;
138 else
139 {
140 [self cleanup];
141 CFDictionaryRefdr = DADiskCopyDescription(disk);
142 if (dr!=NULL)
143 {
144 descDict = (NSDictionary*) dr;
145 CFRelease(dr);
146 }
147 }
148 if (disk) CFRelease(disk);
149}
150
151if (err == 0) {
152diskNum = [sDiskintValue];
153partNum = [sPartintValue];
154
155self.vUUID = [self stringValueWithKey: @"DAVolumeUUID"];
156self.vName = [self stringValueWithKey: @"DAVolumeName"];
157self.vAliasName = self.vName; // by default renamed = original part name
158self.vKind = [self stringValueWithKey: @"DAVolumeKind"];
159self.mediaPath = [self stringValueWithKey: @"DAMediaPath"];
160self.devProtocol = [self stringValueWithKey: @"DADeviceProtocol"];
161self.bsdName = [NSString stringWithString:name];
162
163self.devInternal = (bool) [[descDict objectForKey: @"DADeviceInternal"] boolValue];
164self.mediaRemovable = (bool) [[descDict objectForKey: @"DAMediaRemovable"] boolValue];
165
166}
167
168// Clean up.
169
170if (session != NULL && mustRelease) CFRelease(session);
171
172return err;
173}
174
175+(NSMutableArray*)extractInfoWithBSDNames: (NSArray*) bsdNames
176{
177return [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{
183NSArray* partArr = [PartitionInfoElement createBSDPartitionList];
184if (arr == nil)
185arr = [[[NSMutableArray alloc ] init] autorelease];
186
187if (partArr!=nil && [partArr count]>0)
188{
189DASessionRef session = DASessionCreate(NULL);
190for (NSString* part in partArr)
191{
192PartitionInfoElement* elt =
193[[[PartitionInfoElement alloc] initWithBSDName: part withinSession: session] autorelease];
194if (elt!=nil && [[elt vName] length] >0 && [elt isValid] )
195[arr addObject: elt];
196}
197if (session!=nil) CFRelease(session);
198}
199return arr;
200}
201
202- (int) extractInfoWithBSDName: (NSString*) name
203{
204return [self extractInfoWithBSDName:name withinSession: nil];
205}
206
207-(bool) isBootable
208{
209bool bootable = false;
210NSFileManager* mgr = [NSFileManager defaultManager];
211NSString *fmt = @"/Volumes/%@%s";
212// that Windows is the name of WIN32 bootable disk dir ...
213if(
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 )
221bootable=true;
222else if ([vName rangeOfString:@"ext"].location != NSNotFound) // linux ?
223bootable = true;
224return bootable;
225}
226
227/// Return the partition image index according to its file system
228-(int) imageIndexFromFs
229{
230
231if ( [[self vKind] rangeOfString:@"hfs"].location != NSNotFound )
232return 0; // Mac
233if ( [[self vKind] rangeOfString:@"fat"].location != NSNotFound ||
234 [[self vKind] rangeOfString:@"ntfs"].location != NSNotFound ||
235 [[self vKind] rangeOfString:@"dos"].location != NSNotFound )
236return 1; // Windows
237if ( [[self vKind] rangeOfString:@"ext"].location != NSNotFound )
238return 2; // Linux
239
240return 10; // unknown
241}
242
243/// Volume label trimming utility function
244+(NSString*) removesSpacesFromLabel:(NSString*) label
245{
246if (label == nil || [label length]==0) return label;
247return [label stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
248}
249
250/// make the object description available for NSLog() debug purpose
251-(NSString*) description
252{
253NSString* 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 ")";
259NSString* value = [NSString stringWithFormat: format,
260 self.bsdName, self.devProtocol, self.devInternal,
261 self.vName, self.vKind, self.vUUID,
262 self.mediaPath, self.mediaRemovable ];
263return value;
264}
265
266
267/// initialize a partition element with DA partition info
268-(id) initWithBSDName:(NSString*) name
269{
270self = [super init];
271diskNum = partNum = -1;
272err = [self extractInfoWithBSDName: name withinSession: nil];
273return 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{
279self = [super init];
280diskNum = partNum = -1;
281err = [self extractInfoWithBSDName: name withinSession: session];
282
283return self;
284}
285
286-(void) cleanup
287{
288
289if (descDict != nil) CFRelease(descDict);
290if (bsdName != nil) [bsdName release];
291if (vUUID != nil) [vUUID release];
292if (vKind != nil) [vKind release];
293if (vName != nil) [vName release];
294if (mediaPath != nil) [mediaPath release];
295if (devProtocol != nil) [devProtocol release];
296if (vAliasName != nil) [vAliasName release];
297bsdName = vUUID = vKind = vName = mediaPath = devProtocol = vAliasName = nil;
298descDict = nil;
299}
300
301/// release created objects
302-(void) dealloc
303{
304[self cleanup];
305[super dealloc];
306}
307
308@end
309

Archive Download this file

Revision: 451