123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- #import "PFMoveApplication.h"
- #import <AppKit/AppKit.h>
- #import <Foundation/Foundation.h>
- #import <Security/Security.h>
- #import <dlfcn.h>
- #import <sys/param.h>
- #import <sys/mount.h>
- #define _I10NS(nsstr) NSLocalizedStringFromTable(nsstr, @"MoveApplication", nil)
- #define kStrMoveApplicationCouldNotMove _I10NS(@"Could not move to Applications folder")
- #define kStrMoveApplicationQuestionTitle _I10NS(@"Move to Applications folder?")
- #define kStrMoveApplicationQuestionTitleHome _I10NS(@"Move to Applications folder in your Home folder?")
- #define kStrMoveApplicationQuestionMessage _I10NS(@"I can move myself to the Applications folder if you'd like.")
- #define kStrMoveApplicationButtonMove _I10NS(@"Move to Applications Folder")
- #define kStrMoveApplicationButtonDoNotMove _I10NS(@"Do Not Move")
- #define kStrMoveApplicationQuestionInfoWillRequirePasswd _I10NS(@"Note that this will require an administrator password.")
- #define kStrMoveApplicationQuestionInfoInDownloadsFolder _I10NS(@"This will keep your Downloads folder uncluttered.")
- #ifndef NSAppKitVersionNumber10_5
- #define NSAppKitVersionNumber10_5 949
- #endif
- #define PFUseSmallAlertSuppressCheckbox 1
- static NSString *AlertSuppressKey = @"moveToApplicationsFolderAlertSuppress";
- static NSString *PreferredInstallLocation(BOOL *isUserDirectory);
- static BOOL IsInApplicationsFolder(NSString *path);
- static BOOL IsInDownloadsFolder(NSString *path);
- static BOOL IsApplicationAtPathRunning(NSString *path);
- static BOOL IsApplicationAtPathNested(NSString *path);
- static NSString *ContainingDiskImageDevice(NSString *path);
- static BOOL Trash(NSString *path);
- static BOOL DeleteOrTrash(NSString *path);
- static BOOL AuthorizedInstall(NSString *srcPath, NSString *dstPath, BOOL *canceled);
- static BOOL CopyBundle(NSString *srcPath, NSString *dstPath);
- static NSString *ShellQuotedString(NSString *string);
- static void Relaunch(NSString *destinationPath);
- void PFMoveToApplicationsFolderIfNecessary(void) {
-
- if ([[NSUserDefaults standardUserDefaults] boolForKey:AlertSuppressKey]) return;
-
- NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
-
- BOOL isNestedApplication = IsApplicationAtPathNested(bundlePath);
-
-
- if (IsInApplicationsFolder(bundlePath) && !isNestedApplication) return;
-
- NSFileManager *fm = [NSFileManager defaultManager];
-
- NSString *diskImageDevice = ContainingDiskImageDevice(bundlePath);
-
- BOOL installToUserApplications = NO;
- NSString *applicationsDirectory = PreferredInstallLocation(&installToUserApplications);
- NSString *bundleName = [bundlePath lastPathComponent];
- NSString *destinationPath = [applicationsDirectory stringByAppendingPathComponent:bundleName];
-
- BOOL needAuthorization = ([fm isWritableFileAtPath:applicationsDirectory] == NO);
-
- needAuthorization |= ([fm fileExistsAtPath:destinationPath] && ![fm isWritableFileAtPath:destinationPath]);
-
- NSAlert *alert = [[[NSAlert alloc] init] autorelease];
- {
- NSString *informativeText = nil;
- [alert setMessageText:(installToUserApplications ? kStrMoveApplicationQuestionTitleHome : kStrMoveApplicationQuestionTitle)];
- informativeText = kStrMoveApplicationQuestionMessage;
- if (needAuthorization) {
- informativeText = [informativeText stringByAppendingString:@" "];
- informativeText = [informativeText stringByAppendingString:kStrMoveApplicationQuestionInfoWillRequirePasswd];
- }
- else if (IsInDownloadsFolder(bundlePath)) {
-
- informativeText = [informativeText stringByAppendingString:@" "];
- informativeText = [informativeText stringByAppendingString:kStrMoveApplicationQuestionInfoInDownloadsFolder];
- }
- [alert setInformativeText:informativeText];
-
- [alert addButtonWithTitle:kStrMoveApplicationButtonMove];
-
- NSButton *cancelButton = [alert addButtonWithTitle:kStrMoveApplicationButtonDoNotMove];
- [cancelButton setKeyEquivalent:[NSString stringWithFormat:@"%C", 0x1b]];
-
- [alert setShowsSuppressionButton:YES];
- if (PFUseSmallAlertSuppressCheckbox) {
- NSCell *cell = [[alert suppressionButton] cell];
- [cell setControlSize:NSSmallControlSize];
- [cell setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
- }
- }
-
- if (![NSApp isActive]) {
- [NSApp activateIgnoringOtherApps:YES];
- }
- if ([alert runModal] == NSAlertFirstButtonReturn) {
- NSLog(@"INFO -- Moving myself to the Applications folder");
-
- if (needAuthorization) {
- BOOL authorizationCanceled;
- if (!AuthorizedInstall(bundlePath, destinationPath, &authorizationCanceled)) {
- if (authorizationCanceled) {
- NSLog(@"INFO -- Not moving because user canceled authorization");
- return;
- }
- else {
- NSLog(@"ERROR -- Could not copy myself to /Applications with authorization");
- goto fail;
- }
- }
- }
- else {
-
- if ([fm fileExistsAtPath:destinationPath]) {
-
- if (IsApplicationAtPathRunning(destinationPath)) {
-
- NSLog(@"INFO -- Switching to an already running version");
- [[NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:[NSArray arrayWithObject:destinationPath]] waitUntilExit];
- exit(0);
- }
- else {
- if (!Trash([applicationsDirectory stringByAppendingPathComponent:bundleName]))
- goto fail;
- }
- }
- if (!CopyBundle(bundlePath, destinationPath)) {
- NSLog(@"ERROR -- Could not copy myself to %@", destinationPath);
- goto fail;
- }
- }
-
-
-
-
- if (!isNestedApplication && diskImageDevice == nil && !DeleteOrTrash(bundlePath)) {
- NSLog(@"WARNING -- Could not delete application after moving it to Applications folder");
- }
-
- Relaunch(destinationPath);
-
-
- if (diskImageDevice && !isNestedApplication) {
- NSString *script = [NSString stringWithFormat:@"(/bin/sleep 5 && /usr/bin/hdiutil detach %@) &", ShellQuotedString(diskImageDevice)];
- [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", script, nil]];
- }
- exit(0);
- }
-
- else if ([[alert suppressionButton] state] == NSOnState) {
- [[NSUserDefaults standardUserDefaults] setBool:YES forKey:AlertSuppressKey];
- }
- return;
- fail:
- {
-
- alert = [[[NSAlert alloc] init] autorelease];
- [alert setMessageText:kStrMoveApplicationCouldNotMove];
- [alert runModal];
- }
- }
- #pragma mark -
- #pragma mark Helper Functions
- static NSString *PreferredInstallLocation(BOOL *isUserDirectory) {
-
-
-
- #if 0
- NSFileManager *fm = [NSFileManager defaultManager];
- NSArray *userApplicationsDirs = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
- if ([userApplicationsDirs count] > 0) {
- NSString *userApplicationsDir = [userApplicationsDirs objectAtIndex:0];
- BOOL isDirectory;
- if ([fm fileExistsAtPath:userApplicationsDir isDirectory:&isDirectory] && isDirectory) {
-
- NSArray *contents = [fm contentsOfDirectoryAtPath:userApplicationsDir error:NULL];
-
- for (NSString *contentsPath in contents) {
- if ([[contentsPath pathExtension] isEqualToString:@"app"]) {
- if (isUserDirectory) *isUserDirectory = YES;
- return [userApplicationsDir stringByResolvingSymlinksInPath];
- }
- }
- }
- }
- #endif
-
- if (isUserDirectory) *isUserDirectory = NO;
- return [[NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSLocalDomainMask, YES) lastObject] stringByResolvingSymlinksInPath];
- }
- static BOOL IsInApplicationsFolder(NSString *path) {
-
- NSArray *applicationDirs = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSAllDomainsMask, YES);
- for (NSString *appDir in applicationDirs) {
- if ([path hasPrefix:appDir]) return YES;
- }
-
- if ([[path pathComponents] containsObject:@"Applications"]) return YES;
- return NO;
- }
- static BOOL IsInDownloadsFolder(NSString *path) {
- NSArray *downloadDirs = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSAllDomainsMask, YES);
- for (NSString *downloadsDirPath in downloadDirs) {
- if ([path hasPrefix:downloadsDirPath]) return YES;
- }
- return NO;
- }
- static BOOL IsApplicationAtPathRunning(NSString *bundlePath) {
- bundlePath = [bundlePath stringByStandardizingPath];
- #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
-
- if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
- for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) {
- NSString *runningAppBundlePath = [[[runningApplication bundleURL] path] stringByStandardizingPath];
- if ([runningAppBundlePath isEqualToString:bundlePath]) {
- return YES;
- }
- }
- return NO;
- }
- #endif
-
- NSString *script = [NSString stringWithFormat:@"/bin/ps ax -o comm | /usr/bin/grep %@/ | /usr/bin/grep -v grep >/dev/null", ShellQuotedString(bundlePath)];
- NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", script, nil]];
- [task waitUntilExit];
-
-
- return [task terminationStatus] == 0;
- }
- static BOOL IsApplicationAtPathNested(NSString *path) {
- NSString *containingPath = [path stringByDeletingLastPathComponent];
- NSArray *components = [containingPath pathComponents];
- for (NSString *component in components) {
- if ([[component pathExtension] isEqualToString:@"app"]) {
- return YES;
- }
- }
- return NO;
- }
- static NSString *ContainingDiskImageDevice(NSString *path) {
- NSString *containingPath = [path stringByDeletingLastPathComponent];
- struct statfs fs;
- if (statfs([containingPath fileSystemRepresentation], &fs) || (fs.f_flags & MNT_ROOTFS))
- return nil;
- NSString *device = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:fs.f_mntfromname length:strlen(fs.f_mntfromname)];
- NSTask *hdiutil = [[[NSTask alloc] init] autorelease];
- [hdiutil setLaunchPath:@"/usr/bin/hdiutil"];
- [hdiutil setArguments:[NSArray arrayWithObjects:@"info", @"-plist", nil]];
- [hdiutil setStandardOutput:[NSPipe pipe]];
- [hdiutil launch];
- [hdiutil waitUntilExit];
- NSData *data = [[[hdiutil standardOutput] fileHandleForReading] readDataToEndOfFile];
- NSDictionary *info = nil;
- #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
- if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
- info = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
- }
- else {
- #endif
- #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10
- info = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL];
- #endif
- #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
- }
- #endif
- if (![info isKindOfClass:[NSDictionary class]]) return nil;
- NSArray *images = (NSArray *)[info objectForKey:@"images"];
- if (![images isKindOfClass:[NSArray class]]) return nil;
- for (NSDictionary *image in images) {
- if (![image isKindOfClass:[NSDictionary class]]) return nil;
- id systemEntities = [image objectForKey:@"system-entities"];
- if (![systemEntities isKindOfClass:[NSArray class]]) return nil;
- for (NSDictionary *systemEntity in systemEntities) {
- if (![systemEntity isKindOfClass:[NSDictionary class]]) return nil;
- NSString *devEntry = [systemEntity objectForKey:@"dev-entry"];
- if (![devEntry isKindOfClass:[NSString class]]) return nil;
- if ([devEntry isEqualToString:device])
- return device;
- }
- }
- return nil;
- }
- static BOOL Trash(NSString *path) {
- BOOL result = NO;
- #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
- if (floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_8) {
- result = [[NSFileManager defaultManager] trashItemAtURL:[NSURL fileURLWithPath:path] resultingItemURL:NULL error:NULL];
- }
- #endif
- #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_11
- if (!result) {
- result = [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation
- source:[path stringByDeletingLastPathComponent]
- destination:@""
- files:[NSArray arrayWithObject:[path lastPathComponent]]
- tag:NULL];
- }
- #endif
-
-
-
-
- if (!result) {
- NSAppleScript *appleScript = [[[NSAppleScript alloc] initWithSource:
- [NSString stringWithFormat:@"\
- set theFile to POSIX file \"%@\" \n\
- tell application \"Finder\" \n\
- move theFile to trash \n\
- end tell", path]] autorelease];
- NSDictionary *errorDict = nil;
- NSAppleEventDescriptor *scriptResult = [appleScript executeAndReturnError:&errorDict];
- if (scriptResult == nil) {
- NSLog(@"Trash AppleScript error: %@", errorDict);
- }
- result = (scriptResult != nil);
- }
- if (!result) {
- NSLog(@"ERROR -- Could not trash '%@'", path);
- }
- return result;
- }
- static BOOL DeleteOrTrash(NSString *path) {
- NSError *error;
- if ([[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
- return YES;
- }
- else {
-
- if (![path containsString:@"/AppTranslocation/"])
- NSLog(@"WARNING -- Could not delete '%@': %@", path, [error localizedDescription]);
-
- return Trash(path);
- }
- }
- static BOOL AuthorizedInstall(NSString *srcPath, NSString *dstPath, BOOL *canceled) {
- if (canceled) *canceled = NO;
-
-
- if (![[dstPath pathExtension] isEqualToString:@"app"]) return NO;
-
- if ([[dstPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) return NO;
- if ([[srcPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) return NO;
- int pid, status;
- AuthorizationRef myAuthorizationRef;
-
- OSStatus err = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &myAuthorizationRef);
- if (err != errAuthorizationSuccess) return NO;
- AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
- AuthorizationRights myRights = {1, &myItems};
- AuthorizationFlags myFlags = (AuthorizationFlags)(kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize);
- err = AuthorizationCopyRights(myAuthorizationRef, &myRights, NULL, myFlags, NULL);
- if (err != errAuthorizationSuccess) {
- if (err == errAuthorizationCanceled && canceled)
- *canceled = YES;
- goto fail;
- }
- static OSStatus (*security_AuthorizationExecuteWithPrivileges)(AuthorizationRef authorization, const char *pathToTool,
- AuthorizationFlags options, char * const *arguments,
- FILE **communicationsPipe) = NULL;
- if (!security_AuthorizationExecuteWithPrivileges) {
-
-
-
-
-
- security_AuthorizationExecuteWithPrivileges = (OSStatus (*)(AuthorizationRef, const char*,
- AuthorizationFlags, char* const*,
- FILE **)) dlsym(RTLD_DEFAULT, "AuthorizationExecuteWithPrivileges");
- }
- if (!security_AuthorizationExecuteWithPrivileges) goto fail;
-
- {
- char *args[] = {"-rf", (char *)[dstPath fileSystemRepresentation], NULL};
- err = security_AuthorizationExecuteWithPrivileges(myAuthorizationRef, "/bin/rm", kAuthorizationFlagDefaults, args, NULL);
- if (err != errAuthorizationSuccess) goto fail;
-
- pid = wait(&status);
- if (pid == -1 || !WIFEXITED(status)) goto fail;
- }
-
- {
- char *args[] = {"-pR", (char *)[srcPath fileSystemRepresentation], (char *)[dstPath fileSystemRepresentation], NULL};
- err = security_AuthorizationExecuteWithPrivileges(myAuthorizationRef, "/bin/cp", kAuthorizationFlagDefaults, args, NULL);
- if (err != errAuthorizationSuccess) goto fail;
-
- pid = wait(&status);
- if (pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status)) goto fail;
- }
- AuthorizationFree(myAuthorizationRef, kAuthorizationFlagDefaults);
- return YES;
- fail:
- AuthorizationFree(myAuthorizationRef, kAuthorizationFlagDefaults);
- return NO;
- }
- static BOOL CopyBundle(NSString *srcPath, NSString *dstPath) {
- NSFileManager *fm = [NSFileManager defaultManager];
- NSError *error = nil;
- if ([fm copyItemAtPath:srcPath toPath:dstPath error:&error]) {
- return YES;
- }
- else {
- NSLog(@"ERROR -- Could not copy '%@' to '%@' (%@)", srcPath, dstPath, error);
- return NO;
- }
- }
- static NSString *ShellQuotedString(NSString *string) {
- return [NSString stringWithFormat:@"'%@'", [string stringByReplacingOccurrencesOfString:@"'" withString:@"'\\''"]];
- }
- static void Relaunch(NSString *destinationPath) {
-
-
- int pid = [[NSProcessInfo processInfo] processIdentifier];
-
- NSString *preOpenCmd = @"";
- NSString *quotedDestinationPath = ShellQuotedString(destinationPath);
-
-
-
- if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
-
- preOpenCmd = [NSString stringWithFormat:@"/usr/bin/xattr -d -r com.apple.quarantine %@", quotedDestinationPath];
- }
- else {
- preOpenCmd = [NSString stringWithFormat:@"/usr/bin/xattr -d com.apple.quarantine %@", quotedDestinationPath];
- }
- NSString *script = [NSString stringWithFormat:@"(while /bin/kill -0 %d >&/dev/null; do /bin/sleep 0.1; done; %@; /usr/bin/open %@) &", pid, preOpenCmd, quotedDestinationPath];
- [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", script, nil]];
- }
|