2015-04-09 17:25:05 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-22 06:23:20 +00:00
|
|
|
|
|
|
|
#ifndef mozilla_BinaryPath_h
|
|
|
|
#define mozilla_BinaryPath_h
|
|
|
|
|
|
|
|
#include "nsXPCOMPrivate.h" // for MAXPATHLEN
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
#include <sys/stat.h>
|
2011-07-21 07:55:51 +00:00
|
|
|
#include <string.h>
|
2011-05-22 06:23:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
class BinaryPath
|
|
|
|
{
|
2011-05-22 06:23:20 +00:00
|
|
|
public:
|
|
|
|
#ifdef XP_WIN
|
2014-08-13 18:45:37 +00:00
|
|
|
static nsresult Get(const char* argv0, char aResult[MAXPATHLEN])
|
2011-05-22 06:23:20 +00:00
|
|
|
{
|
2013-12-04 12:19:09 +00:00
|
|
|
wchar_t wide_path[MAXPATHLEN];
|
2011-05-22 06:23:20 +00:00
|
|
|
nsresult rv = GetW(argv0, wide_path);
|
2014-08-13 18:45:37 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return rv;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wide_path, -1,
|
2013-10-10 20:41:00 +00:00
|
|
|
aResult, MAXPATHLEN, nullptr, nullptr);
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-08-13 18:45:37 +00:00
|
|
|
static nsresult GetW(const char* argv0, wchar_t aResult[MAXPATHLEN])
|
2011-05-22 06:23:20 +00:00
|
|
|
{
|
2014-08-13 18:45:37 +00:00
|
|
|
if (::GetModuleFileNameW(0, aResult, MAXPATHLEN)) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_OK;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(XP_MACOSX)
|
2014-08-13 18:45:37 +00:00
|
|
|
static nsresult Get(const char* argv0, char aResult[MAXPATHLEN])
|
2011-05-22 06:23:20 +00:00
|
|
|
{
|
|
|
|
// Works even if we're not bundled.
|
|
|
|
CFBundleRef appBundle = CFBundleGetMainBundle();
|
2014-08-13 18:45:37 +00:00
|
|
|
if (!appBundle) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
|
|
|
|
CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
|
2014-08-13 18:45:37 +00:00
|
|
|
if (!executableURL) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
|
|
|
|
nsresult rv;
|
2014-10-09 01:34:04 +00:00
|
|
|
if (CFURLGetFileSystemRepresentation(executableURL, false, (UInt8*)aResult,
|
2014-08-13 18:45:37 +00:00
|
|
|
MAXPATHLEN)) {
|
2014-10-09 01:19:45 +00:00
|
|
|
// Sanitize path in case the app was launched from Terminal via
|
|
|
|
// './firefox' for example.
|
|
|
|
size_t readPos = 0;
|
|
|
|
size_t writePos = 0;
|
|
|
|
while (aResult[readPos] != '\0') {
|
|
|
|
if (aResult[readPos] == '.' && aResult[readPos + 1] == '/') {
|
|
|
|
readPos += 2;
|
|
|
|
} else {
|
|
|
|
aResult[writePos] = aResult[readPos];
|
|
|
|
readPos++;
|
|
|
|
writePos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aResult[writePos] = '\0';
|
2011-05-22 06:23:20 +00:00
|
|
|
rv = NS_OK;
|
2014-08-13 18:45:37 +00:00
|
|
|
} else {
|
2011-05-22 06:23:20 +00:00
|
|
|
rv = NS_ERROR_FAILURE;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2014-10-07 13:33:09 +00:00
|
|
|
|
2011-05-22 06:23:20 +00:00
|
|
|
CFRelease(executableURL);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-04-05 03:24:10 +00:00
|
|
|
#elif defined(ANDROID)
|
2014-08-13 18:45:37 +00:00
|
|
|
static nsresult Get(const char* argv0, char aResult[MAXPATHLEN])
|
2011-11-22 07:05:59 +00:00
|
|
|
{
|
|
|
|
// On Android, we use the GRE_HOME variable that is set by the Java
|
|
|
|
// bootstrap code.
|
2014-08-13 18:45:37 +00:00
|
|
|
const char* greHome = getenv("GRE_HOME");
|
2012-04-05 03:24:10 +00:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
2014-08-13 18:45:37 +00:00
|
|
|
if (!greHome) {
|
2012-04-05 03:24:10 +00:00
|
|
|
greHome = "/system/b2g";
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2012-04-05 03:24:10 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
if (!greHome) {
|
2011-11-22 07:05:59 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-11-22 07:05:59 +00:00
|
|
|
|
|
|
|
snprintf(aResult, MAXPATHLEN, "%s/%s", greHome, "dummy");
|
2014-08-13 18:45:37 +00:00
|
|
|
aResult[MAXPATHLEN - 1] = '\0';
|
2011-11-22 07:05:59 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 06:23:20 +00:00
|
|
|
#elif defined(XP_UNIX)
|
2014-08-13 18:45:37 +00:00
|
|
|
static nsresult Get(const char* aArgv0, char aResult[MAXPATHLEN])
|
2011-05-22 06:23:20 +00:00
|
|
|
{
|
|
|
|
struct stat fileStat;
|
|
|
|
// on unix, there is no official way to get the path of the current binary.
|
|
|
|
// instead of using the MOZILLA_FIVE_HOME hack, which doesn't scale to
|
|
|
|
// multiple applications, we will try a series of techniques:
|
|
|
|
//
|
|
|
|
// 1) use realpath() on argv[0], which works unless we're loaded from the
|
2011-07-21 07:55:51 +00:00
|
|
|
// PATH. Only do so if argv[0] looks like a path (contains a /).
|
2011-05-22 06:23:20 +00:00
|
|
|
// 2) manually walk through the PATH and look for ourself
|
|
|
|
// 3) give up
|
2014-08-13 18:45:37 +00:00
|
|
|
if (strchr(aArgv0, '/') && realpath(aArgv0, aResult) &&
|
|
|
|
stat(aResult, &fileStat) == 0) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_OK;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
const char* path = getenv("PATH");
|
|
|
|
if (!path) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
char* pathdup = strdup(path);
|
|
|
|
if (!pathdup) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool found = false;
|
2014-08-13 18:45:37 +00:00
|
|
|
char* token = strtok(pathdup, ":");
|
2011-05-22 06:23:20 +00:00
|
|
|
while (token) {
|
|
|
|
char tmpPath[MAXPATHLEN];
|
2014-08-13 18:45:37 +00:00
|
|
|
sprintf(tmpPath, "%s/%s", token, aArgv0);
|
2011-05-22 06:23:20 +00:00
|
|
|
if (realpath(tmpPath, aResult) && stat(aResult, &fileStat) == 0) {
|
2011-10-17 14:59:28 +00:00
|
|
|
found = true;
|
2011-05-22 06:23:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-10-10 20:41:00 +00:00
|
|
|
token = strtok(nullptr, ":");
|
2011-05-22 06:23:20 +00:00
|
|
|
}
|
|
|
|
free(pathdup);
|
2014-08-13 18:45:37 +00:00
|
|
|
if (found) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_OK;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error Oops, you need platform-specific code here
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2014-08-13 18:45:37 +00:00
|
|
|
static nsresult GetFile(const char* aArgv0, nsIFile** aResult)
|
2011-05-22 06:23:20 +00:00
|
|
|
{
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> lf;
|
2011-05-22 06:23:20 +00:00
|
|
|
#ifdef XP_WIN
|
2013-12-04 12:19:09 +00:00
|
|
|
wchar_t exePath[MAXPATHLEN];
|
2014-08-13 18:45:37 +00:00
|
|
|
nsresult rv = GetW(aArgv0, exePath);
|
2011-05-22 06:23:20 +00:00
|
|
|
#else
|
|
|
|
char exePath[MAXPATHLEN];
|
2014-08-13 18:45:37 +00:00
|
|
|
nsresult rv = Get(aArgv0, exePath);
|
2011-05-22 06:23:20 +00:00
|
|
|
#endif
|
2014-08-13 18:45:37 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return rv;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
#ifdef XP_WIN
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = NS_NewLocalFile(nsDependentString(exePath), true,
|
2011-05-22 06:23:20 +00:00
|
|
|
getter_AddRefs(lf));
|
|
|
|
#else
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = NS_NewNativeLocalFile(nsDependentCString(exePath), true,
|
2011-05-22 06:23:20 +00:00
|
|
|
getter_AddRefs(lf));
|
|
|
|
#endif
|
2014-08-13 18:45:37 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-05-22 06:23:20 +00:00
|
|
|
return rv;
|
2014-08-13 18:45:37 +00:00
|
|
|
}
|
2011-05-22 06:23:20 +00:00
|
|
|
NS_ADDREF(*aResult = lf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace mozilla
|
2011-05-22 06:23:20 +00:00
|
|
|
|
|
|
|
#endif /* mozilla_BinaryPath_h */
|