From 7307f09457488dd545ca2306a5d2f01b31f14460 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Thu, 1 Sep 2016 12:31:42 -0700 Subject: [PATCH] base: use _NSGetExecutablePath in GetExecutablePath Bug: http://b/31240820 Change-Id: I0dbf95d3667c7ce7b474ddbb3f8e3ed69476d13e Test: mma && adb kill-server && adb devices --- base/file.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/base/file.cpp b/base/file.cpp index 03ce4ea7e..721ab2f7f 100644 --- a/base/file.cpp +++ b/base/file.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -31,7 +32,7 @@ #include "utils/Compat.h" #if defined(__APPLE__) -#import +#include #endif #if defined(_WIN32) #include @@ -210,15 +211,14 @@ std::string GetExecutablePath() { android::base::Readlink("/proc/self/exe", &path); return path; #elif defined(__APPLE__) - // TODO: use _NSGetExecutablePath instead (http://b/31240820)? - CFBundleRef mainBundle = CFBundleGetMainBundle(); - CFURLRef executableURL = CFBundleCopyExecutableURL(mainBundle); - CFStringRef executablePathString = CFURLCopyFileSystemPath(executableURL, kCFURLPOSIXPathStyle); - CFRelease(executableURL); - char path[PATH_MAX + 1]; - CFStringGetFileSystemRepresentation(executablePathString, path, sizeof(PATH_MAX)-1); - CFRelease(executablePathString); + uint32_t path_len = sizeof(path); + int rc = _NSGetExecutablePath(path, &path_len); + if (rc < 0) { + std::unique_ptr path_buf(new char[path_len]); + _NSGetExecutablePath(path_buf.get(), &path_len); + return path_buf.get(); + } return path; #elif defined(_WIN32) char path[PATH_MAX + 1];