2015-04-09 20:25:05 +03: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 15:12:37 +04: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 10:23:20 +04: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 11:55:51 +04:00
|
|
|
#include <string.h>
|
2011-05-22 10:23:20 +04:00
|
|
|
#endif
|
2017-01-13 03:40:54 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2011-05-22 10:23:20 +04:00
|
|
|
|
2017-02-27 21:39:34 +03:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2017-06-22 20:56:04 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIFile.h"
|
2017-02-27 21:39:34 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#endif
|
|
|
|
|
2011-05-22 10:23:20 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
class BinaryPath
|
|
|
|
{
|
2011-05-22 10:23:20 +04:00
|
|
|
public:
|
|
|
|
#ifdef XP_WIN
|
2014-08-13 22:45:37 +04:00
|
|
|
static nsresult Get(const char* argv0, char aResult[MAXPATHLEN])
|
2011-05-22 10:23:20 +04:00
|
|
|
{
|
2013-12-04 16:19:09 +04:00
|
|
|
wchar_t wide_path[MAXPATHLEN];
|
2011-05-22 10:23:20 +04:00
|
|
|
nsresult rv = GetW(argv0, wide_path);
|
2014-08-13 22:45:37 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return rv;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wide_path, -1,
|
2013-10-11 00:41:00 +04:00
|
|
|
aResult, MAXPATHLEN, nullptr, nullptr);
|
2011-05-22 10:23:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-22 20:56:04 +03:00
|
|
|
static nsresult GetLong(wchar_t aResult[MAXPATHLEN])
|
|
|
|
{
|
|
|
|
static bool cached = false;
|
|
|
|
static wchar_t exeLongPath[MAXPATHLEN] = L"";
|
|
|
|
|
|
|
|
if (!cached) {
|
|
|
|
nsresult rv = GetW(nullptr, exeLongPath);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!::GetLongPathNameW(exeLongPath, exeLongPath, MAXPATHLEN)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wcscpy_s(aResult, MAXPATHLEN, exeLongPath)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 10:23:20 +04:00
|
|
|
private:
|
2014-08-13 22:45:37 +04:00
|
|
|
static nsresult GetW(const char* argv0, wchar_t aResult[MAXPATHLEN])
|
2011-05-22 10:23:20 +04:00
|
|
|
{
|
2017-06-22 20:56:04 +03:00
|
|
|
static bool cached = false;
|
|
|
|
static wchar_t moduleFileName[MAXPATHLEN] = L"";
|
|
|
|
|
|
|
|
if (!cached) {
|
|
|
|
if (!::GetModuleFileNameW(0, moduleFileName, MAXPATHLEN)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cached = true;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2017-06-22 20:56:04 +03:00
|
|
|
|
|
|
|
if (wcscpy_s(aResult, MAXPATHLEN, moduleFileName)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2011-05-22 10:23:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(XP_MACOSX)
|
2014-08-13 22:45:37 +04:00
|
|
|
static nsresult Get(const char* argv0, char aResult[MAXPATHLEN])
|
2011-05-22 10:23:20 +04:00
|
|
|
{
|
|
|
|
// Works even if we're not bundled.
|
|
|
|
CFBundleRef appBundle = CFBundleGetMainBundle();
|
2014-08-13 22:45:37 +04:00
|
|
|
if (!appBundle) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
|
|
|
|
CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
|
2014-08-13 22:45:37 +04:00
|
|
|
if (!executableURL) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2014-10-09 05:34:04 +04:00
|
|
|
if (CFURLGetFileSystemRepresentation(executableURL, false, (UInt8*)aResult,
|
2014-08-13 22:45:37 +04:00
|
|
|
MAXPATHLEN)) {
|
2014-10-09 05:19:45 +04: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 10:23:20 +04:00
|
|
|
rv = NS_OK;
|
2014-08-13 22:45:37 +04:00
|
|
|
} else {
|
2011-05-22 10:23:20 +04:00
|
|
|
rv = NS_ERROR_FAILURE;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2014-10-07 17:33:09 +04:00
|
|
|
|
2011-05-22 10:23:20 +04:00
|
|
|
CFRelease(executableURL);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-04-05 07:24:10 +04:00
|
|
|
#elif defined(ANDROID)
|
2014-08-13 22:45:37 +04:00
|
|
|
static nsresult Get(const char* argv0, char aResult[MAXPATHLEN])
|
2011-11-22 11:05:59 +04:00
|
|
|
{
|
|
|
|
// On Android, we use the GRE_HOME variable that is set by the Java
|
|
|
|
// bootstrap code.
|
2014-08-13 22:45:37 +04:00
|
|
|
const char* greHome = getenv("GRE_HOME");
|
|
|
|
if (!greHome) {
|
2011-11-22 11:05:59 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-11-22 11:05:59 +04:00
|
|
|
|
|
|
|
snprintf(aResult, MAXPATHLEN, "%s/%s", greHome, "dummy");
|
2014-08-13 22:45:37 +04:00
|
|
|
aResult[MAXPATHLEN - 1] = '\0';
|
2011-11-22 11:05:59 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 10:23:20 +04:00
|
|
|
#elif defined(XP_UNIX)
|
2014-08-13 22:45:37 +04:00
|
|
|
static nsresult Get(const char* aArgv0, char aResult[MAXPATHLEN])
|
2011-05-22 10:23:20 +04: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 11:55:51 +04:00
|
|
|
// PATH. Only do so if argv[0] looks like a path (contains a /).
|
2011-05-22 10:23:20 +04:00
|
|
|
// 2) manually walk through the PATH and look for ourself
|
|
|
|
// 3) give up
|
2014-08-13 22:45:37 +04:00
|
|
|
if (strchr(aArgv0, '/') && realpath(aArgv0, aResult) &&
|
|
|
|
stat(aResult, &fileStat) == 0) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return NS_OK;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
const char* path = getenv("PATH");
|
|
|
|
if (!path) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
char* pathdup = strdup(path);
|
|
|
|
if (!pathdup) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found = false;
|
2014-08-13 22:45:37 +04:00
|
|
|
char* token = strtok(pathdup, ":");
|
2011-05-22 10:23:20 +04:00
|
|
|
while (token) {
|
|
|
|
char tmpPath[MAXPATHLEN];
|
2014-08-13 22:45:37 +04:00
|
|
|
sprintf(tmpPath, "%s/%s", token, aArgv0);
|
2011-05-22 10:23:20 +04:00
|
|
|
if (realpath(tmpPath, aResult) && stat(aResult, &fileStat) == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
found = true;
|
2011-05-22 10:23:20 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-10-11 00:41:00 +04:00
|
|
|
token = strtok(nullptr, ":");
|
2011-05-22 10:23:20 +04:00
|
|
|
}
|
|
|
|
free(pathdup);
|
2014-08-13 22:45:37 +04:00
|
|
|
if (found) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return NS_OK;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error Oops, you need platform-specific code here
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2017-01-13 03:40:54 +03:00
|
|
|
static UniqueFreePtr<char> Get(const char *aArgv0)
|
|
|
|
{
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
if (NS_FAILED(Get(aArgv0, path))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
UniqueFreePtr<char> result;
|
|
|
|
result.reset(strdup(path));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-27 21:39:34 +03:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2014-08-13 22:45:37 +04:00
|
|
|
static nsresult GetFile(const char* aArgv0, nsIFile** aResult)
|
2011-05-22 10:23:20 +04:00
|
|
|
{
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> lf;
|
2011-05-22 10:23:20 +04:00
|
|
|
#ifdef XP_WIN
|
2013-12-04 16:19:09 +04:00
|
|
|
wchar_t exePath[MAXPATHLEN];
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult rv = GetW(aArgv0, exePath);
|
2011-05-22 10:23:20 +04:00
|
|
|
#else
|
|
|
|
char exePath[MAXPATHLEN];
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult rv = Get(aArgv0, exePath);
|
2011-05-22 10:23:20 +04:00
|
|
|
#endif
|
2014-08-13 22:45:37 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return rv;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
#ifdef XP_WIN
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = NS_NewLocalFile(nsDependentString(exePath), true,
|
2011-05-22 10:23:20 +04:00
|
|
|
getter_AddRefs(lf));
|
|
|
|
#else
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = NS_NewNativeLocalFile(nsDependentCString(exePath), true,
|
2011-05-22 10:23:20 +04:00
|
|
|
getter_AddRefs(lf));
|
|
|
|
#endif
|
2014-08-13 22:45:37 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return rv;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2011-05-22 10:23:20 +04:00
|
|
|
NS_ADDREF(*aResult = lf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-02-27 21:39:34 +03:00
|
|
|
#endif
|
2011-05-22 10:23:20 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2011-05-22 10:23:20 +04:00
|
|
|
|
|
|
|
#endif /* mozilla_BinaryPath_h */
|