Skip to content

Commit 6cc8ada

Browse files
committed
Merge pull request imothee#6 from entonio/recreate-disks
Added options to recreate disks individually or all at once
2 parents b9d0d91 + 7cf67eb commit 6cc8ada

File tree

11 files changed

+951
-3692
lines changed

11 files changed

+951
-3692
lines changed

TmpDisk.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
418A28E81442D05100D4BCB0 /* TmpDiskMenuItemView.m in Sources */ = {isa = PBXBuildFile; fileRef = 418A28E71442D05100D4BCB0 /* TmpDiskMenuItemView.m */; };
3434
418DD4C2147DF2E400488297 /* AutoCreateManagerWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 418DD4C1147DF2E400488297 /* AutoCreateManagerWindow.m */; };
3535
418DF6A0147B57CD0069EEAC /* TmpDiskManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 418DF69F147B57CD0069EEAC /* TmpDiskManager.m */; };
36+
A96E4E7A1C5FDCEA0086FE71 /* recreate_a.png in Resources */ = {isa = PBXBuildFile; fileRef = A96E4E781C5FDCEA0086FE71 /* recreate_a.png */; };
37+
A96E4E7B1C5FDCEA0086FE71 /* recreate.png in Resources */ = {isa = PBXBuildFile; fileRef = A96E4E791C5FDCEA0086FE71 /* recreate.png */; };
3638
/* End PBXBuildFile section */
3739

3840
/* Begin PBXContainerItemProxy section */
@@ -100,6 +102,8 @@
100102
418DF69E147B57CD0069EEAC /* TmpDiskManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TmpDiskManager.h; path = Classes/TmpDiskManager.h; sourceTree = "<group>"; };
101103
418DF69F147B57CD0069EEAC /* TmpDiskManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TmpDiskManager.m; path = Classes/TmpDiskManager.m; sourceTree = "<group>"; };
102104
41A5021C146268F600BC762D /* TmpDisk.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = TmpDisk.entitlements; sourceTree = "<group>"; };
105+
A96E4E781C5FDCEA0086FE71 /* recreate_a.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = recreate_a.png; sourceTree = "<group>"; };
106+
A96E4E791C5FDCEA0086FE71 /* recreate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = recreate.png; sourceTree = "<group>"; };
103107
/* End PBXFileReference section */
104108

105109
/* Begin PBXFrameworksBuildPhase section */
@@ -128,9 +132,11 @@
128132
4103FF9014440A2900618177 /* Images */ = {
129133
isa = PBXGroup;
130134
children = (
135+
A96E4E781C5FDCEA0086FE71 /* recreate_a.png */,
136+
A96E4E791C5FDCEA0086FE71 /* recreate.png */,
137+
4103FF9114440A2900618177 /* eject.png */,
131138
412EC7E6146DF1C600D569F1 /* eject_a.png */,
132139
410B9B2D1456E7EC003FE088 /* status.png */,
133-
4103FF9114440A2900618177 /* eject.png */,
134140
);
135141
path = Images;
136142
sourceTree = "<group>";
@@ -334,6 +340,8 @@
334340
410B9B331456F18C003FE088 /* icon.icns in Resources */,
335341
412EC7E7146DF1C600D569F1 /* eject_a.png in Resources */,
336342
418320CE14722EB5000D8488 /* dsa_pub.pem in Resources */,
343+
A96E4E7A1C5FDCEA0086FE71 /* recreate_a.png in Resources */,
344+
A96E4E7B1C5FDCEA0086FE71 /* recreate.png in Resources */,
337345
41283F8C147C88B7005EDE88 /* AutoCreateManager.xib in Resources */,
338346
);
339347
runOnlyForDeploymentPostprocessing = 0;

TmpDisk/AppDelegate.m

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -57,44 +57,13 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
5757
int dsize = argSize.intValue;
5858
u_int64_t size = (((u_int64_t) dsize) * 1024 * 1024 / 512);
5959

60-
if(![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/Volumes/%@", argName] isDirectory:nil]) {
60+
if(![[NSFileManager defaultManager] fileExistsAtPath:[TmpDiskManager pathForName:argName] isDirectory:nil]) {
6161
[TmpDiskManager createTmpDiskWithName:argName size:size autoCreate:NO indexed:NO hidden:NO folders:[[NSArray alloc] init] onSuccess:nil];
6262
}
6363

6464
}
65-
66-
67-
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"autoCreate"]) {
68-
69-
NSArray *autoCreateArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"autoCreate"];
70-
71-
for (NSDictionary *d in autoCreateArray) {
72-
73-
NSString *name = d[@"name"];
74-
NSNumber *size = d[@"size"];
75-
NSNumber *indexed = d[@"indexed"];
76-
NSNumber *hidden = d[@"hidden"];
77-
NSArray *folders = nil;
78-
79-
if ([d objectForKey:@"folders"]) {
80-
folders = [d objectForKey:@"folders"];
81-
}else {
82-
folders = [[NSArray alloc] init];
83-
}
84-
85-
if (name && size) {
86-
87-
if([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/Volumes/%@", name] isDirectory:nil]) {
88-
continue;
89-
}
90-
91-
[TmpDiskManager createTmpDiskWithName:name size:size.unsignedLongLongValue autoCreate:NO indexed:indexed.boolValue hidden:hidden.boolValue folders:folders onSuccess:nil];
92-
93-
}
94-
95-
}
96-
97-
}
65+
66+
[TmpDiskManager autoCreateVolumesWithNames:nil];
9867
}
9968

10069
- (void)awakeFromNib {
@@ -142,54 +111,49 @@ - (void)newTmpDiskCreated:(NSNotification *)notification {
142111
continue;
143112
}
144113

145-
NSMenuItem *mi = [[TmpDiskMenuItem alloc] initWithTitle:s action:@selector(tmpDiskSelected:) keyEquivalent:@"" ejectBlock:^(NSString* s){
114+
NSMenuItem *mi = [[TmpDiskMenuItem alloc] initWithTitle:s action:@selector(tmpDiskSelected:) keyEquivalent:@""
115+
recreateBlock:^(NSString* s){
146116

147-
// Eject Block passed to menu to run when the eject button is clicked for a TmpDisk
117+
// Recreate Block passed to menu to run when the recreate button is clicked for a TmpDisk
148118

149119
[statusMenu cancelTrackingWithoutAnimation];
150120

121+
[self ejectVolumeWithName:s recreate:YES];
151122

152-
NSString *volumePath = [NSString stringWithFormat:@"/Volumes/%@", s];
153-
154-
BOOL isRemovable, isWritable, isUnmountable;
155-
NSString *description, *type;
123+
return;
124+
125+
}
126+
ejectBlock:^(NSString* s){
156127

157-
NSWorkspace *ws = [[NSWorkspace alloc] init];
128+
// Eject Block passed to menu to run when the eject button is clicked for a TmpDisk
158129

159-
// Make sure the Volume is inmountable first
160-
[ws getFileSystemInfoForPath:volumePath
161-
isRemovable:&isRemovable
162-
isWritable:&isWritable
163-
isUnmountable:&isUnmountable
164-
description:&description
165-
type:&type];
166-
if (isUnmountable) {
167-
[ws unmountAndEjectDeviceAtPath:volumePath];
168-
}
130+
[statusMenu cancelTrackingWithoutAnimation];
169131

132+
[self ejectVolumeWithName:s recreate:NO];
170133

171134
return;
172-
173-
}];
174-
135+
136+
}
137+
];
138+
175139
[diskMenu addItem:mi];
176-
140+
177141
}
178-
142+
143+
}
144+
145+
- (void)ejectVolumeWithName:(NSString *)name recreate:(BOOL)recreate {
146+
[TmpDiskManager ejectVolumesWithNames:[NSSet setWithObject:name]
147+
recreate:recreate];
179148
}
180149

181150
- (void)tmpDiskSelected:(id)sender {
182-
151+
183152
// When a tmpDisk menu option is selected, we open the volume in Finder
184-
153+
185154
NSString *s = [sender title];
186-
187-
NSString *volumePath = [NSString stringWithFormat:@"/Volumes/%@", s];
188-
189-
NSWorkspace *ws = [[NSWorkspace alloc] init];
190-
191-
[ws openFile:volumePath];
192-
155+
156+
[TmpDiskManager openVolumeWithName:s];
193157
}
194158

195159
@end

TmpDisk/Classes/TmpDiskManager.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,44 @@
66
// Copyright (c) 2011 Ink Scribbles Pty Ltd. All rights reserved.
77
//
88
// This file is part of TmpDisk.
9-
//
9+
//
1010
// TmpDisk is free software: you can redistribute it and/or modify
1111
// it under the terms of the GNU General Public License as published by
1212
// the Free Software Foundation, either version 3 of the License, or
1313
// (at your option) any later version.
14-
//
14+
//
1515
// TmpDisk is distributed in the hope that it will be useful,
1616
// but WITHOUT ANY WARRANTY; without even the implied warranty of
1717
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1818
// GNU General Public License for more details.
19-
//
19+
//
2020
// You should have received a copy of the GNU General Public License
2121
// along with TmpDisk. If not, see <http://www.gnu.org/licenses/>.
2222

2323
#import <Foundation/Foundation.h>
2424

25+
@interface TmpDiskFile : NSObject
26+
27+
@property(nonatomic, strong) NSString *name;
28+
@property(nonatomic, assign) uint64_t size;
29+
@property(nonatomic, assign) BOOL hidden;
30+
@property(nonatomic, assign) BOOL indexed;
31+
@property(nonatomic, strong) id folders;
32+
33+
@property(readonly) BOOL exists;
34+
35+
@end
36+
2537
@interface TmpDiskManager : NSObject
2638

39+
+ (NSString *)pathForName:(NSString *)name;
40+
41+
+ (void)autoCreateVolumesWithNames:(NSSet<NSString *> *)names;
42+
+ (NSArray<TmpDiskFile *> *)knownVolumesWithNames:(NSSet<NSString *> *)names;
43+
44+
+ (void)ejectVolumesWithNames:(NSSet<NSString *> *)names recreate:(BOOL)recreate;
45+
+ (void)openVolumeWithName:(NSString *)name;
46+
2747
+ (bool)createTmpDiskWithName:(NSString*)name size:(u_int64_t)size autoCreate:(bool)autoCreate indexed:(bool)indexed hidden:(bool)hidden folders:(NSArray*)folders onSuccess:(void (^)())success;
2848

2949
@end

TmpDisk/Classes/TmpDiskManager.m

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,113 @@
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
}

TmpDisk/Classes/TmpDiskMenuItem.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@
2323
#import <AppKit/AppKit.h>
2424
#import "TmpDiskMenuItemView.h"
2525

26+
typedef void (^RecreateBlock)(NSString*);
2627
typedef void (^EjectBlock)(NSString*);
2728

2829
@interface TmpDiskMenuItem : NSMenuItem {
2930

31+
RecreateBlock recreateBlock;
3032
EjectBlock ejectBlock;
31-
33+
3234
}
3335

3436

3537
- (instancetype)initWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)charCode eject:(SEL)ejSelector NS_DESIGNATED_INITIALIZER;
3638

37-
- (instancetype)initWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)charCode ejectBlock:(void (^)(NSString*))block NS_DESIGNATED_INITIALIZER;
39+
- (instancetype)initWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)charCode recreateBlock:(void (^)(NSString*))block ejectBlock:(void (^)(NSString*))block NS_DESIGNATED_INITIALIZER;
40+
41+
- (void)runEjectBlock:(id)sender;
3842

39-
- (void)runSelectBlock:(id)sender;
43+
- (void)runRecreateBlock:(id)sender;
4044

4145
@end

0 commit comments

Comments
 (0)