Skip to content

Commit 5b17b41

Browse files
committed
Escape translocation on launch so we can update ourselves.
1 parent 239e046 commit 5b17b41

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Cocoa/GBApp.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#import <JoyKit/JoyKit.h>
99
#import <WebKit/WebKit.h>
1010
#import <mach-o/dyld.h>
11+
#include <sys/mount.h>
12+
#include <sys/xattr.h>
1113

1214
#define UPDATE_SERVER "https://sameboy.github.io"
1315

@@ -662,6 +664,14 @@ - (void)performUpgrade
662664
}
663665
[[NSFileManager defaultManager] removeItemAtPath:_downloadDirectory error:nil];
664666
[[NSFileManager defaultManager] removeItemAtPath:contentsTempPath error:nil];
667+
668+
// Remove the quarantine flag so we don't have to escape translocation
669+
NSString *bundlePath = [NSBundle mainBundle].bundlePath;
670+
removexattr(bundlePath.UTF8String, "com.apple.quarantine", 0);
671+
for (NSString *path in [[NSFileManager defaultManager] enumeratorAtPath:bundlePath]) {
672+
removexattr([bundlePath stringByAppendingPathComponent:path].UTF8String, "com.apple.quarantine", 0);
673+
};
674+
665675
_downloadDirectory = nil;
666676
atexit_b(^{
667677
execl(executablePath.UTF8String, executablePath.UTF8String, "--update-launch", NULL);
@@ -781,4 +791,27 @@ - (void)dealloc
781791
- (IBAction)nop:(id)sender
782792
{
783793
}
794+
795+
/* This runs before C constructors. If we need to escape translocation, we should
796+
do it ASAP to minimize our launch time. */
797+
798+
+ (void)load
799+
{
800+
if (@available(macOS 10.12, *)) {
801+
/* Detect and escape translocation so we can safely update ourselves */
802+
if ([[[NSBundle mainBundle] bundlePath] containsString:@"/AppTranslocation/"]) {
803+
const char *mountPath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent].UTF8String;
804+
struct statfs *mntbuf;
805+
int mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
806+
for (unsigned i = 0; i < mntsize; i++) {
807+
if (strcmp(mntbuf[i].f_mntonname, mountPath) == 0) {
808+
NSBundle *origBundle = [NSBundle bundleWithPath:@(mntbuf[i].f_mntfromname)];
809+
810+
execl(origBundle.executablePath.UTF8String, origBundle.executablePath.UTF8String, NULL);
811+
break;
812+
}
813+
}
814+
}
815+
}
816+
}
784817
@end

0 commit comments

Comments
 (0)