Skip to content

Commit

Permalink
Pretty up the agent UI by putting it in a sheet.
Browse files Browse the repository at this point in the history
Fix filesystem persistence across agent crashed by moving off of xattr tagging
and using a simple cache instead.
  • Loading branch information
Michael Gorbach committed Mar 24, 2008
1 parent e00037b commit 5ba8993
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 156 deletions.
1 change: 0 additions & 1 deletion MFClient.h
Expand Up @@ -33,7 +33,6 @@
+ (MFClient*)sharedClient;

// Initialization
- (BOOL)setup;
- (void)fillInitialStatus;
- (BOOL)establishCommunication;

Expand Down
94 changes: 4 additions & 90 deletions MFClient.m
Expand Up @@ -119,10 +119,12 @@ - (id) init

- (void)initializeIvars
{
persistentFilesystems = [NSMutableArray array];
temporaryFilesystems = [NSMutableArray array];
// persistentFilesystems = [NSMutableArray array];
// temporaryFilesystems = [NSMutableArray array];
plugins = [NSMutableArray array];
recents = [NSMutableArray array];
persistentFilesystems = [NSMutableArray array];
temporaryFilesystems = [NSMutableArray array];
}

- (void)fillInitialStatus
Expand Down Expand Up @@ -183,94 +185,6 @@ - (BOOL)establishCommunication
}
}

- (BOOL)setup
{
if ([self establishCommunication])
{
[self fillInitialStatus];
return YES;
}
else
{
BOOL agentRunning = NO;
NSArray* runingIDs = [[[NSWorkspace sharedWorkspace] launchedApplications] valueForKey:@"NSApplicationBundleIdentifier"];
// MFLogS(self, @"Runing ids are %@", runingIDs);
for(NSString* id in runingIDs)
{
if ([id isEqualToString: kMFAgentBundleIdentifier])
{
agentRunning = YES;
break;
}
}

if (agentRunning)
{

// Agent is runing. Wait a bit for it to set up IPC
MFLogS(self, @"Waiting for agent");
NSDate* stopDate = [[NSDate date] addTimeInterval: 5.0];
[[NSRunLoop currentRunLoop] runUntilDate: stopDate];
if ([self establishCommunication])
{
[self fillInitialStatus];
return YES;
}
else
{
return NO;
}
}
else
{
// Try to start the agent process
MFLogS(self, @"Agent not runing");
NSAlert* serverStartAlert = [NSAlert new];
[serverStartAlert setMessageText: @"The macfusion agent process is not started"];
[serverStartAlert setInformativeText: @"Would you like to start the agent?\nOtherwise, Macfusion will Quit."];
[serverStartAlert setShowsSuppressionButton: YES];
[serverStartAlert addButtonWithTitle:@"Start"];
[serverStartAlert addButtonWithTitle:@"Quit"];
[[serverStartAlert suppressionButton] setTitle: @"Start agent automatically on login"];
NSInteger returnValue = [serverStartAlert runModal];
// MFLogS(self, @"Return %d supression state %d", returnValue, [[serverStartAlert suppressionButton] state]);

if ([[serverStartAlert suppressionButton] state])
mfcSetStateForAgentLoginItem(YES);

if (returnValue == NSAlertSecondButtonReturn)
{
[NSApp terminate: self];
}
else if (returnValue == NSAlertFirstButtonReturn)
{
NSString* agentPath = mfcAgentBundlePath();
[[NSWorkspace sharedWorkspace] launchApplication: agentPath];
[[NSRunLoop currentRunLoop] runUntilDate:[[NSDate date] addTimeInterval: 1.5]];

if ([self establishCommunication])
{
[self fillInitialStatus];
return YES;
}
else
{
NSAlert* faliureAlert = [NSAlert alertWithMessageText:@"Could not start or connect to the macfusion agent"
defaultButton:@"OK"
alternateButton:@""
otherButton:@""
informativeTextWithFormat:@"Macfusion will Quit."];
[faliureAlert setAlertStyle: NSCriticalAlertStyle];
[faliureAlert runModal];
[NSApp terminate: self];
}
}
}

}

return NO;
}

#pragma mark Notification handling
- (void)handleStatusChangedNotification:(NSNotification*)note
Expand Down
2 changes: 1 addition & 1 deletion MFCommunicationServer.h
Expand Up @@ -28,7 +28,7 @@

- (MFFilesystemController*)filesystemController;
- (MFPluginController*)pluginController;
- (void)startServingRunloop;
- (void)startServing;
- (NSError*)recentError;

@end
2 changes: 1 addition & 1 deletion MFCommunicationServer.m
Expand Up @@ -200,7 +200,7 @@ - (void)doInitializationComplete:(NSTimer*)timer
}
}

- (void)startServingRunloop
- (void)startServing
{
[self registerNotifications];
[self vendDisributedObject];
Expand Down
1 change: 1 addition & 0 deletions MFCore.h
Expand Up @@ -36,6 +36,7 @@ BOOL mfcSetStateForMenulingLoginItem(BOOL state);

// Clients
BOOL mfcClientIsUIElement();
void mfcLaunchAgent();


// FUSE versioning
Expand Down
6 changes: 6 additions & 0 deletions MFCore.m
Expand Up @@ -184,4 +184,10 @@ BOOL mfcClientIsUIElement()
NSDictionary* info = [[NSBundle mainBundle] infoDictionary];
BOOL uiElement = [[info objectForKey: @"LSUIElement"] boolValue];
return uiElement;
}

void mfcLaunchAgent()
{
NSString* path = mfcAgentBundlePath();
[NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:[NSArray arrayWithObject: path]];
}
1 change: 1 addition & 0 deletions MFFilesystemController.h
Expand Up @@ -23,6 +23,7 @@
NSMutableArray* recents;
NSMutableArray* mountedPaths;
NSMutableDictionary* tokens;
NSMutableDictionary* mountPathPersistenceCache;

DASessionRef appearSession;
DASessionRef disappearSession;
Expand Down
48 changes: 27 additions & 21 deletions MFFilesystemController.m
Expand Up @@ -24,6 +24,7 @@

#define FSDEF_EXTENSION @"macfusion"
#define RECENTS_PATH @"~/Library/Application Support/Macfusion/recents.plist"
#define CACHE_PATH @"~/Library/Application Support/Macfusion/cache.plist"

@interface MFFilesystemController (PrivateAPI)
- (void)setUpVolumeMonitoring;
Expand Down Expand Up @@ -93,6 +94,7 @@ - (id) init
mountedPaths = [NSMutableArray array];
recents = [NSMutableArray array];
tokens = [NSMutableDictionary dictionary];
[self loadFilesystems];
[self loadRecentFilesystems];
[self setUpVolumeMonitoring];
MFLogS(self, @"Init complete!");
Expand Down Expand Up @@ -147,6 +149,10 @@ - (NSArray*)pathsToFilesystemDefs
- (void)loadFilesystems
{
NSArray* filesystemPaths = [self pathsToFilesystemDefs];
NSDictionary* cacheDict = [NSDictionary dictionaryWithContentsOfFile:
[CACHE_PATH stringByExpandingTildeInPath]];
// MFLogS(self, @"Loaded Cache dict %@", cacheDict);

for(NSString* fsPath in filesystemPaths)
{
MFLogS(self, @"Loading fs at %@", fsPath);
Expand All @@ -157,15 +163,11 @@ - (void)loadFilesystems
if (fs)
{
[self storeFilesystem: fs ];
NSString* path = fs.mountPath;
for(NSString* mountedPath in mountedPaths)
if ([[[NSFileManager defaultManager] directoryContentsAtPath: fs.mountPath] count] > 0
&& ([[cacheDict objectForKey: fs.uuid] isEqualToString: fs.mountPath]))
{
if ([path isEqualToString: mountedPath] &&
[[self getUUIDXattrAtPath: mountedPath] isEqualToString: fs.uuid])
{
// MFLogS(self, @"Pre-mount condition hit on %@", fs.uuid);
[fs handleMountNotification];
}
MFLogSO(self, fs, @"Detected Already Mounted fs %@", fs);
[fs handleMountNotification];
}
}
else
Expand Down Expand Up @@ -433,6 +435,21 @@ - (MFServerFS*)filesystemForToken:(NSString*)token
return [tokens objectForKey: token];
}

# pragma mark Filesystem Persistence
- (void)updateFSPersistenceCache
{
NSArray* mountedFilesystems = [self.filesystems filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"self.status == %@", kMFStatusFSMounted]];
// MFLogS(self, @"Updating Cache. Mounted Filesystems %@", mountedFilesystems);
NSDictionary* mountedDict = [NSDictionary dictionaryWithObjects:(NSArray*)[mountedFilesystems valueForKey:@"mountPath"]
forKeys:(NSArray*)[mountedFilesystems valueForKey:@"uuid"]];
// MFLogS(self, @"Mounted Cache Dictionary %@", mountedDict);
NSString* cachePath = [@"~/Library/Application Support/Macfusion/cache.plist"
stringByExpandingTildeInPath];
[mountedDict writeToFile:cachePath
atomically:YES];
}

# pragma mark Accessors and Getters
- (void)storeFilesystem:(MFServerFS*)fs
{
Expand Down Expand Up @@ -469,19 +486,6 @@ - (NSDictionary*)filesystemsDictionary
return (NSDictionary*)filesystemsDictionary;
}

- (NSString*)getUUIDXattrAtPath:(NSString*)path
{
NSString* resultString = nil;
char* dataBuffer = malloc(100 * sizeof(char));
int result = getxattr([path cStringUsingEncoding: NSUTF8StringEncoding],
"org.mgorbach.macfusion.xattr.uuid", dataBuffer, 100*sizeof(char),
0, 0);
if (result > 0)
resultString = [NSString stringWithCString:dataBuffer length:result];
free(dataBuffer);
return resultString;
}

- (void)addMountedPath:(NSString*)path
{
// MFLogS(self, @"Adding mounted path %@", path);
Expand All @@ -496,6 +500,7 @@ - (void)addMountedPath:(NSString*)path
{
[fs handleMountNotification];
[self invalidateTokensForFS: fs];
[self updateFSPersistenceCache];
}
if (! [fs isPersistent] )
{
Expand All @@ -514,6 +519,7 @@ - (void)removeMountedPath:(NSString*)path
NSArray* matchingFilesystems = [filesystems filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"self.mountPath == %@", path]];
[matchingFilesystems makeObjectsPerformSelector:@selector(handleUnmountNotification)];
[self updateFSPersistenceCache];

NSArray* matchingTemporaryFilesystems = [matchingFilesystems filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"self.isPersistent != YES"]];
Expand Down
12 changes: 3 additions & 9 deletions MFMainController.m
Expand Up @@ -56,19 +56,13 @@ - (id)copyWithZone:(NSZone*)zone

#pragma mark Runloop and initialization methods

- (void)startRunloop
{
NSRunLoop* runloop = [NSRunLoop currentRunLoop];
[runloop run];
}

- (void)initialize
{
MFPluginController* pluginController = [MFPluginController sharedController];
MFFilesystemController* filesystemController = [MFFilesystemController sharedController];
[pluginController loadPlugins];
[filesystemController loadFilesystems];
[[MFCommunicationServer sharedServer] startServingRunloop];

[MFFilesystemController sharedController];
[[MFCommunicationServer sharedServer] startServing];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
Expand Down
14 changes: 0 additions & 14 deletions MFServerFS.m
Expand Up @@ -348,18 +348,6 @@ - (NSTask*)taskForLaunch

# pragma mark Mounting mechanics

- (void)tagMountPoint
{
NSString* value = self.uuid;
int response;

response = setxattr([self.mountPath cStringUsingEncoding: NSUTF8StringEncoding],
[@"org.mgorbach.macfusion.xattr.uuid" cStringUsingEncoding:NSUTF8StringEncoding],
[value cStringUsingEncoding: NSUTF8StringEncoding],
[value lengthOfBytesUsingEncoding: NSUTF8StringEncoding] *sizeof(char), 0, 0);
// MFLogS(self, @"Mount point tag responds %d", response);
}

- (BOOL)setupMountPoint
{
NSFileManager* fm = [NSFileManager defaultManager];
Expand Down Expand Up @@ -414,7 +402,6 @@ - (BOOL)setupMountPoint
}
else
{
[self tagMountPoint];
return YES;
}
}
Expand Down Expand Up @@ -544,7 +531,6 @@ - (void)handleMountNotification
{
// MFLogS(self, @"Mount notification received");
self.status = kMFStatusFSMounted;
[self tagMountPoint];
}

- (void)handleTaskDidTerminate:(NSNotification*)note
Expand Down

0 comments on commit 5ba8993

Please sign in to comment.