Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 272 lines (236 sloc) 8.762 kB
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
1 //
2 // MFCore.m
3 // MacFusion2
4 //
b798dd6 Advanced options enabled. License headers added. Added item to launch…
Michael Gorbach authored
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
16
17 #import "MFCore.h"
18 #define self @"MFCORE"
19
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
20 NSString* mfcMainBundlePath()
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
21 {
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
22 NSString* mybundleID = [[NSBundle mainBundle] bundleIdentifier];
23 NSString* pathToReturn = nil;
24
25 if ( [mybundleID isEqualToString: kMFMainBundleIdentifier] )
26 pathToReturn = [[NSBundle mainBundle] bundlePath];
27 if ( [mybundleID isEqualToString: kMFAgentBundleIdentifier] ||
28 [mybundleID isEqualToString: kMFMenulingBundleIdentifier] )
29 {
30 NSString* relativePath = @"/../../../";
31 NSString* fullPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: relativePath];
32 pathToReturn = [fullPath stringByStandardizingPath];
33 }
34
35 return pathToReturn;
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
36 }
37
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
38 NSString* mfcMenulingBundlePath()
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
39 {
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
40 NSString* mainBundlePath = mfcMainBundlePath();
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
41 return [mainBundlePath stringByAppendingPathComponent:@"/Contents/Resources/MacfusionMenuling.app"];
42 }
43
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
44 NSString* mfcAgentBundlePath()
fb4ed6c Change the agent into .app itself, to prevent terminal from showing u…
Michael Gorbach authored
45 {
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
46 NSString* mainBundlePath = mfcMainBundlePath();
47 return [mainBundlePath stringByAppendingPathComponent:@"/Contents/Resources/macfusionAgent.app"];
fb4ed6c Change the agent into .app itself, to prevent terminal from showing u…
Michael Gorbach authored
48 }
49
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
50 NSArray* mfcSecretClientsForFileystem( MFFilesystem* fs )
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
51 {
52 NSMutableArray* clientList = [NSMutableArray array];
53 if ([[fs delegate] respondsToSelector:@selector(secretsClientsList)])
54 {
55 NSArray* fsList = [[fs delegate] secretsClientsList];
56 if (fsList)
57 [clientList addObjectsFromArray: fsList];
58 }
59
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
60 NSBundle* mainUIBundle = [NSBundle bundleWithPath: mfcMainBundlePath()];
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
61 [clientList addObject: [mainUIBundle executablePath]];
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
62 NSBundle* menulingUIBundle = [NSBundle bundleWithPath: mfcMenulingBundlePath()];
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
63 [clientList addObject: [menulingUIBundle executablePath]];
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
64 [clientList addObject: mfcAgentBundlePath()];
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
65 return [clientList copy];
66 }
67
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
68 BOOL mfcGetStateOfLoginItemWithPath( NSString* path )
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
69 {
70 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
71 BOOL present = FALSE;
72 UInt32 seedValue;
73 NSArray *loginItems = (NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, &seedValue);
74 for(id loginItem in loginItems)
75 {
76 LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)loginItem;
77 NSURL* theURL = [NSURL new];
78 LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*)&theURL, NULL);
79 present = ([[theURL path] isEqualToString: path]);
80 if (present)
81 break;
82 }
83
84 CFRelease(loginItemsRef);
85 return present;
86 }
87
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
88 BOOL mfcGetStateForAgentLoginItem()
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
89 {
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
90 NSString* agentPath = mfcAgentBundlePath();
91 return mfcGetStateOfLoginItemWithPath( agentPath );
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
92 }
93
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
94 BOOL mfcSetStateForAgentLoginItem(BOOL state)
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
95 {
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
96 NSString* agentPath = mfcAgentBundlePath();
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
97 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
98
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
99 if (mfcGetStateOfLoginItemWithPath(agentPath) == state)
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
100 return NO;
101
102 UInt32 seedValue;
103 NSArray *loginItems = (NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, &seedValue);
104 for(id loginItem in loginItems)
105 {
106 LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)loginItem;
107 NSURL* theURL = [NSURL new];
108 LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*)&theURL, NULL);
109 NSString* checkPath = [[theURL path] lastPathComponent];
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
110 if ([checkPath isLike: @"*macfusionAgent*"])
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
111 {
112 LSSharedFileListItemRemove(loginItemsRef, itemRef);
113 }
114 }
115
116 if(state == YES)
117 {
118 LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemBeforeFirst, NULL, NULL,
119 (CFURLRef)[NSURL fileURLWithPath: agentPath], NULL, NULL);
120 }
121
122 CFRelease(loginItemsRef);
123 return YES;
124 }
125
75d77a4 Some renaming of C functions. Fix path resolution problems that lead …
Michael Gorbach authored
126 NSString* mfcGetMacFuseVersion()
1cb84dd Security system, UI refinement and other fixes. Ready for beta 1
Michael Gorbach authored
127 {
128 NSDictionary* fuseData = [NSDictionary dictionaryWithContentsOfFile:
129 @"/Library/Filesystems/fusefs.fs/Contents/Info.plist"];
130 return [fuseData objectForKey: @"CFBundleVersion"];
5ce0dda Adding some modifications to both clients to all recovery from an age…
Michael Gorbach authored
131 }
132
133 BOOL mfcClientIsUIElement()
134 {
135 NSDictionary* info = [[NSBundle mainBundle] infoDictionary];
136 BOOL uiElement = [[info objectForKey: @"LSUIElement"] boolValue];
137 return uiElement;
5ba8993 Pretty up the agent UI by putting it in a sheet.
Michael Gorbach authored
138 }
139
140 void mfcLaunchAgent()
141 {
142 NSString* path = mfcAgentBundlePath();
143 [NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:[NSArray arrayWithObject: path]];
f49f0da Work on a function to maintain the version sync of macfusion's multip…
Michael Gorbach authored
144 }
145
146 void mfcLaunchMenuling()
147 {
148 NSString* path = mfcMenulingBundlePath();
149 [NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:[NSArray arrayWithObject: path]];
150 }
151
152 // Checks the integrity of Macfusion2's multi-process system
153 // Make sure we are all running from the same bundle and speaking the same language
154 void mfcCheckIntegrity()
155 {
156 ProcessSerialNumber currentPSN = { 0, kNoProcess };
157 CFStringRef processName;
158 FSRef bundleFSRef;
4449887 Some overall cleanup. Addressing of ticket 40 (upgrade case)
Michael Gorbach authored
159 OSErr error;
160 id runningAgentPath=nil, runningMenulingPath=nil;
f49f0da Work on a function to maintain the version sync of macfusion's multip…
Michael Gorbach authored
161 pid_t runningAgentPID, runningMenulingPID;
162
163 while(GetNextProcess( &currentPSN ) == noErr
164 && currentPSN.lowLongOfPSN != kNoProcess )
165 {
166 NSString* processPath;
167 CopyProcessName( &currentPSN, &processName );
168 pid_t processPID;
169
170 if ( [ (NSString*)processName isEqualToString: @"macfusionAgent" ] ||
171 [ (NSString*)processName isEqualToString: @"macfusionMenuling"] )
172 {
4449887 Some overall cleanup. Addressing of ticket 40 (upgrade case)
Michael Gorbach authored
173 error = GetProcessBundleLocation( &currentPSN, &bundleFSRef);
f49f0da Work on a function to maintain the version sync of macfusion's multip…
Michael Gorbach authored
174 GetProcessPID( &currentPSN , &processPID);
175
4449887 Some overall cleanup. Addressing of ticket 40 (upgrade case)
Michael Gorbach authored
176 if (error == noErr)
f49f0da Work on a function to maintain the version sync of macfusion's multip…
Michael Gorbach authored
177 {
178 CFURLRef bundleURLRef = CFURLCreateFromFSRef( kCFAllocatorDefault, &bundleFSRef);
179 processPath = [ (NSURL*)bundleURLRef path ];
180 CFRelease( bundleURLRef );
181 }
182 else
183 {
184 processPath = (NSString*)[NSNull null];
185 // Set the processPath to NSNull if we failed getting it
186 // This can happen if the process is running from the trash (i.e. it's been deleted)
187 }
188 }
189
190 if ( [ (NSString*)processName isEqualToString: @"macfusionAgent"] )
191 {
192 runningAgentPath = processPath;
193 runningAgentPID = processPID;
194 }
195
196 if ( [ (NSString*)processName isEqualToString: @"macfusionMenuling"] )
197 {
198 runningMenulingPath = processPath;
199 runningMenulingPID = processPID;
200 }
201
202 CFRelease( processName );
203 }
204
205 if (runningAgentPath == [NSNull null] ||
206 ( runningAgentPath && ![runningAgentPath isEqualToString: mfcAgentBundlePath()] ) )
207 {
208 // Agent is in the trash or running from the wrong path. Kill it & restart it.
4449887 Some overall cleanup. Addressing of ticket 40 (upgrade case)
Michael Gorbach authored
209 MFLogS( self, @"Killing old or bad agent, and restarting." );
f49f0da Work on a function to maintain the version sync of macfusion's multip…
Michael Gorbach authored
210 kill( runningAgentPID, SIGKILL);
211 mfcLaunchAgent();
212 }
213
214 if (runningMenulingPath == [NSNull null] ||
215 (runningMenulingPath && ![runningMenulingPath isEqualToString: mfcMenulingBundlePath()] ) )
216 {
217 // Menuling is in the trash or running from the wrong path. Kill it & restart it.
4449887 Some overall cleanup. Addressing of ticket 40 (upgrade case)
Michael Gorbach authored
218 MFLogS( self, @"Killing old or bad menuling, and restarting." );
f49f0da Work on a function to maintain the version sync of macfusion's multip…
Michael Gorbach authored
219 kill( runningMenulingPID, SIGKILL );
220 mfcLaunchMenuling();
221 }
222 }
4449887 Some overall cleanup. Addressing of ticket 40 (upgrade case)
Michael Gorbach authored
223
8224b2b Fix bugs. Polish UI. Remove sparkle framework temporarily
Michael Gorbach authored
224 # pragma mark Process Killing
225
226 // Kill all Macfusion Processes other than me
227 void mfcKaboomMacfusion()
228 {
229 NSPredicate* macfusionAppsPredicate = [NSPredicate
230 predicateWithFormat:
231 @"self.NSApplicationBundleIdentifier CONTAINS \
232 org.mgorbach.macfusion2 AND self.NSApplicationPath != %@",
233 mfcMainBundlePath()];
234
235 NSArray* macfusionApps = [[[NSWorkspace sharedWorkspace] launchedApplications] filteredArrayUsingPredicate:
236 macfusionAppsPredicate];
237 NSArray* macfusionAppsPIDs = [macfusionApps valueForKey: @"NSApplicationProcessIdentifier"];
238 NSLog(@"Killing pids %@", macfusionAppsPIDs);
239 for(NSNumber* pid in macfusionAppsPIDs)
240 {
241 kill( [pid intValue], SIGKILL );
242 }
243 }
244
4449887 Some overall cleanup. Addressing of ticket 40 (upgrade case)
Michael Gorbach authored
245 # pragma mark Trashing
246
247 void trashFSEventCallBack(ConstFSEventStreamRef streamRef,
248 void *clientCallBackInfo,
249 size_t numEvents,
250 void *eventPaths,
251 const FSEventStreamEventFlags eventFlags[],
252 const FSEventStreamEventId eventIds[])
253 {
254 if (![[NSFileManager defaultManager] fileExistsAtPath: mfcMainBundlePath()])
255 {
256 MFLogS(self, @"I have been deleted. Goodbye!");
257 exit(0);
258 }
259 }
260
261 void mfcSetupTrashMonitoring()
262 {
263 FSEventStreamRef eventStream = FSEventStreamCreate( NULL, trashFSEventCallBack, NULL,
264 (CFArrayRef)[NSArray arrayWithObject:
265 [mfcMainBundlePath() stringByDeletingLastPathComponent]],
266 kFSEventStreamEventIdSinceNow,
267 0, kFSEventStreamCreateFlagUseCFTypes );
268 FSEventStreamScheduleWithRunLoop( eventStream, [[NSRunLoop currentRunLoop] getCFRunLoop],
269 kCFRunLoopDefaultMode );
270 FSEventStreamStart( eventStream );
271 }
Something went wrong with that request. Please try again.