Bug 1428557 - Implement wrappers in FileUtils. r?froydnj
These methods will be used in subsequent patches.
MozReview-Commit-ID: EqCpRbn2Y5Y
--- a/xpcom/glue/FileUtils.cpp
+++ b/xpcom/glue/FileUtils.cpp
@@ -1,21 +1,23 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
+#include "mozilla/FileUtils.h"
+
#include <errno.h>
#include <stdio.h>
#include "nscore.h"
#include "private/pprio.h"
+#include "prmem.h"
#include "mozilla/Assertions.h"
-#include "mozilla/FileUtils.h"
#if defined(XP_MACOSX)
#include <fcntl.h>
#include <unistd.h>
#include <mach/machine.h>
#include <mach-o/fat.h>
#include <mach-o/loader.h>
#include <sys/mman.h>
@@ -156,17 +158,72 @@ mozilla::ReadAheadFile(nsIFile* aFile, c
nsAutoCString nativePath;
if (!aFile || NS_FAILED(aFile->GetNativePath(nativePath))) {
return;
}
ReadAheadFile(nativePath.get(), aOffset, aCount, aOutFd);
#endif
}
-#endif // !defined(XPCOM_GLUE)
+mozilla::PathString
+mozilla::GetLibraryName(mozilla::pathstr_t aDirectory, const char* aLib)
+{
+#ifdef XP_WIN
+ nsAutoString fullName;
+ if (aDirectory) {
+ fullName.Assign(aDirectory);
+ fullName.Append('\\');
+ }
+ AppendUTF8toUTF16(aLib, fullName);
+ if (!strstr(aLib, ".dll")) {
+ fullName.AppendLiteral(".dll");
+ }
+ return fullName;
+#else
+ char* temp = PR_GetLibraryName(aDirectory, aLib);
+ if (!temp) {
+ return EmptyCString();
+ }
+ nsAutoCString libname(temp);
+ PR_FreeLibraryName(temp);
+ return libname;
+#endif
+}
+
+mozilla::PathString
+mozilla::GetLibraryFilePathname(mozilla::pathstr_t aName, PRFuncPtr aAddr)
+{
+#ifdef XP_WIN
+ HMODULE handle = GetModuleHandleW(char16ptr_t(aName));
+ if (!handle) {
+ return EmptyString();
+ }
+
+ nsAutoString path;
+ path.SetLength(MAX_PATH);
+ DWORD len = GetModuleFileNameW(handle, char16ptr_t(path.BeginWriting()),
+ path.Length());
+ if (!len) {
+ return EmptyString();
+ }
+
+ path.SetLength(len);
+ return path;
+#else
+ char* temp = PR_GetLibraryFilePathname(aName, aAddr);
+ if (!temp) {
+ return EmptyCString();
+ }
+ nsAutoCString path(temp);
+ PR_Free(temp); // PR_GetLibraryFilePathname() uses PR_Malloc().
+ return path;
+#endif
+}
+
+#endif // defined(MOZILLA_INTERNAL_API)
#if defined(LINUX) && !defined(ANDROID)
static const unsigned int bufsize = 4096;
#ifdef __LP64__
typedef Elf64_Ehdr Elf_Ehdr;
typedef Elf64_Phdr Elf_Phdr;
--- a/xpcom/glue/FileUtils.h
+++ b/xpcom/glue/FileUtils.h
@@ -10,16 +10,17 @@
#include "nscore.h" // nullptr
#if defined(XP_UNIX)
# include <unistd.h>
#elif defined(XP_WIN)
# include <io.h>
#endif
#include "prio.h"
+#include "prlink.h"
#include "mozilla/Scoped.h"
#include "nsIFile.h"
#include <errno.h>
#include <limits.h>
namespace mozilla {
@@ -114,16 +115,22 @@ void ReadAheadLib(nsIFile* aFile);
* @param aCount Number of bytes to preload (SIZE_MAX implies file size)
* @param aOutFd Pointer to file descriptor. If specified, ReadAheadFile will
* return its internal, opened file descriptor instead of closing it.
*/
void ReadAheadFile(nsIFile* aFile, const size_t aOffset = 0,
const size_t aCount = SIZE_MAX,
filedesc_t* aOutFd = nullptr);
+/*
+ * Wrappers for PR_GetLibraryName and PR_GetLibraryFilePathname.
+ */
+PathString GetLibraryName(pathstr_t aDirectory, const char* aLib);
+PathString GetLibraryFilePathname(pathstr_t aName, PRFuncPtr aAddr);
+
#endif // MOZILLA_INTERNAL_API
/**
* Use readahead to preload shared libraries into the file cache before loading.
* WARNING: This function should not be used without a telemetry field trial
* demonstrating a clear performance improvement!
*
* @param aFilePath path to shared library