Skip to content

Commit

Permalink
Merge branch 'fix-bundle-path-detection-sierra'
Browse files Browse the repository at this point in the history
  • Loading branch information
vslavik committed Sep 8, 2022
2 parents 70c5772 + 8714292 commit a7cc028
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion PFMoveApplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ + (NSBundle *)bundle {
static BOOL MoveInProgress = NO;

// Helper functions
static NSURL * GetOriginalBundleURL(void);
static NSString *PreferredInstallLocation(BOOL *isUserDirectory);
static BOOL IsInApplicationsFolder(NSString *path);
static BOOL IsInDownloadsFolder(NSString *path);
Expand Down Expand Up @@ -78,7 +79,7 @@ void PFMoveToApplicationsFolderIfNecessary(void) {
if ([[NSUserDefaults standardUserDefaults] boolForKey:AlertSuppressKey]) return;

// Path of the bundle
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *bundlePath = GetOriginalBundleURL().path;

// Check if the bundle is embedded in another application
BOOL isNestedApplication = IsApplicationAtPathNested(bundlePath);
Expand Down Expand Up @@ -239,6 +240,56 @@ BOOL PFMoveIsInProgress() {
#pragma mark -
#pragma mark Helper Functions

/// Gets the real bundle path of the application, not the Translocated one if applicable
static NSURL * GetOriginalBundleURL(void) {

NSURL * bundleURL = [NSBundle mainBundle].bundleURL;
NSLog(@"%s: Foundation says bundle URL is %@", __FUNCTION__, bundleURL);

// #define NSAppKitVersionNumber10_11 1404
if (floor(NSAppKitVersionNumber) <= 1404) {
return bundleURL;
}

void * handle = dlopen("/System/Library/Frameworks/Security.framework/Security", RTLD_LAZY);
if (handle == NULL) {
return bundleURL;
}

bool isTranslocated = false;

// Get (undocumented?) function symbols for looking up app translocation info
// Note: <Security/SecTranslocation.h> was available in the macOS 10.12 beta SDKs but seems to have been removed as of Xcode 8.1
// see https://opensource.apple.com/source/Security/Security-58286.70.7/OSX/libsecurity_translocate/lib/SecTranslocate.h.auto.html

typedef Boolean (*SecTranslocateIsTranslocatedURL_t)(CFURLRef path, bool *isTranslocated, CFErrorRef * __nullable error);
SecTranslocateIsTranslocatedURL_t mySecTranslocateIsTranslocatedURL = (SecTranslocateIsTranslocatedURL_t)dlsym(handle, "SecTranslocateIsTranslocatedURL");

typedef CFURLRef __nullable(*SecTranslocateCreateOriginalPathForURL_t)(CFURLRef translocatedPath, CFErrorRef * __nullable error);
SecTranslocateCreateOriginalPathForURL_t mySecTranslocateCreateOriginalPathForURL = (SecTranslocateCreateOriginalPathForURL_t)dlsym(handle, "SecTranslocateCreateOriginalPathForURL");

dlclose(handle);

if (mySecTranslocateIsTranslocatedURL == NULL || mySecTranslocateCreateOriginalPathForURL == NULL) {
NSLog(@"%s: We're running on macOS >= 10.12 but the SecTranslocate functions are not available", __FUNCTION__);
return bundleURL;
}

if (mySecTranslocateIsTranslocatedURL((__bridge CFURLRef)bundleURL, &isTranslocated, NULL) && isTranslocated)
{
CFURLRef originalURL = mySecTranslocateCreateOriginalPathForURL((__bridge CFURLRef)bundleURL, NULL);
if (originalURL != NULL)
{
NSURL * result = CFBridgingRelease(originalURL);
NSLog(@"%s: Security says bundle is Translocated from %@", __FUNCTION__, result);
return result;
}
}

NSLog(@"%s: Translocation not in effect", __FUNCTION__);
return bundleURL;
}

static NSString *PreferredInstallLocation(BOOL *isUserDirectory) {
// Return the preferred install location.
// Assume that if the user has a ~/Applications folder, they'd prefer their
Expand Down

0 comments on commit a7cc028

Please sign in to comment.