2222
2323#import " TmpDiskManager.h"
2424
25+ @implementation TmpDiskFile
26+
27+ - (BOOL )exists {
28+ return [[NSFileManager defaultManager ]
29+ fileExistsAtPath: [TmpDiskManager pathForName: self .name]
30+ isDirectory: nil ];
31+ }
32+
33+ @end
34+
2535@implementation TmpDiskManager
2636
37+ + (NSString *)pathForName : (NSString *)name {
38+ return [NSString stringWithFormat: @" /Volumes/%@ " , name];
39+ }
40+
41+ + (void )autoCreateVolumesWithNames : (NSSet <NSString *> *)names {
42+
43+ for (TmpDiskFile *disk in [self knownVolumesWithNames: names]) {
44+ if (disk.exists ) {
45+ continue ;
46+ }
47+
48+ [self createTmpDiskWithName: disk.name
49+ size: disk.size
50+ autoCreate: NO
51+ indexed: disk.indexed
52+ hidden: disk.hidden
53+ folders: disk.folders
54+ onSuccess: nil ];
55+ }
56+ }
57+
58+ + (NSArray <TmpDiskFile *> *)knownVolumesWithNames : (NSSet <NSString *> *)names {
59+
60+ NSMutableArray *results = [[NSMutableArray alloc ] init ];
61+
62+ if ([[NSUserDefaults standardUserDefaults ] objectForKey: @" autoCreate" ]) {
63+
64+ NSArray *autoCreateArray =
65+ [[NSUserDefaults standardUserDefaults ] objectForKey: @" autoCreate" ];
66+
67+ for (NSDictionary *d in autoCreateArray) {
68+
69+ NSString *name = d[@" name" ];
70+
71+ if (names.count && ![names containsObject: name]) {
72+ continue ;
73+ }
74+
75+ NSNumber *size = d[@" size" ];
76+
77+ if (name && size) {
78+ TmpDiskFile *disk = [[TmpDiskFile alloc ] init ];
79+ disk.name = name;
80+ disk.size = size.unsignedLongLongValue ;
81+ disk.indexed = [d[@" indexed" ] boolValue ];
82+ disk.hidden = [d[@" hidden" ] boolValue ];
83+ disk.folders = [d objectForKey: @" folders" ] ?: @[];
84+ [results addObject: disk];
85+ }
86+ }
87+ }
88+
89+ return results;
90+ }
91+
92+ + (void )ejectVolumesWithNames : (NSSet <NSString *> *)names
93+ recreate : (BOOL )recreate {
94+ dispatch_group_t group = dispatch_group_create ();
95+
96+ for (TmpDiskFile *disk in [self knownVolumesWithNames: names]) {
97+ dispatch_group_enter (group);
98+ NSString *volumePath = [self pathForName: disk.name];
99+
100+ BOOL isRemovable, isWritable, isUnmountable;
101+ NSString *description, *type;
102+
103+ NSWorkspace *ws = [[NSWorkspace alloc ] init ];
104+
105+ // Make sure the Volume is unmountable first
106+ [ws getFileSystemInfoForPath: volumePath
107+ isRemovable: &isRemovable
108+ isWritable: &isWritable
109+ isUnmountable: &isUnmountable
110+ description: &description
111+ type: &type];
112+ if (isUnmountable) {
113+ [ws unmountAndEjectDeviceAtPath: volumePath];
114+
115+ if (recreate) {
116+ [self autoCreateVolumesWithNames: [NSSet setWithObject: disk.name]];
117+ }
118+ }
119+ dispatch_group_leave (group);
120+ }
121+ }
122+
123+ + (void )openVolumeWithName : (NSString *)name {
124+
125+ NSString *volumePath = [self pathForName: name];
126+
127+ NSWorkspace *ws = [[NSWorkspace alloc ] init ];
128+
129+ [ws openFile: volumePath];
130+ }
131+
27132+ (bool )createTmpDiskWithName : (NSString *)name size : (u_int64_t )size autoCreate : (bool )autoCreate indexed : (bool )indexed hidden : (bool )hidden folders : (NSArray *)folders onSuccess : (void (^)())success {
28133
29134 if (name.length == 0 ) {
@@ -33,7 +138,7 @@ + (bool)createTmpDiskWithName:(NSString*)name size:(u_int64_t)size autoCreate:(b
33138 return NO ;
34139 }
35140
36- if ([[NSFileManager defaultManager ] fileExistsAtPath: [NSString stringWithFormat: @" /Volumes/ %@ " , name] isDirectory: nil ]) {
141+ if ([[NSFileManager defaultManager ] fileExistsAtPath: [self pathForName: name] isDirectory: nil ]) {
37142 NSAlert *a = [NSAlert alertWithMessageText: @" Error Creating TmpDisk" defaultButton: @" OK" alternateButton: nil otherButton: nil informativeTextWithFormat: @" A Volume named %@ already exists." , name];
38143 [a runModal ];
39144
@@ -112,7 +217,7 @@ + (bool)createTmpDiskWithName:(NSString*)name size:(u_int64_t)size autoCreate:(b
112217
113218 NSArray *arguments;
114219 arguments = @[@" -c" ,
115- [NSString stringWithFormat: @" mdutil -i on /Volumes/ %@ " , name]];
220+ [NSString stringWithFormat: @" mdutil -i on %@ " , [ self pathForName: name] ]];
116221
117222 indexTask.arguments = arguments;
118223 }
0 commit comments