Bug 1428557 - Implement wrappers in FileUtils. r?froydnj draft
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Fri, 02 Feb 2018 23:25:10 +0900
changeset 750960 acd8efa353cc57dbb42b2562bb88989d5618169f
parent 750958 b096cc0e272b40870f782037b16a02d4219876bc
push id97795
push userVYV03354@nifty.ne.jp
push dateSat, 03 Feb 2018 14:38:24 +0000
reviewersfroydnj
bugs1428557
milestone60.0a1
Bug 1428557 - Implement wrappers in FileUtils. r?froydnj These methods will be used in subsequent patches. MozReview-Commit-ID: EqCpRbn2Y5Y
xpcom/glue/FileUtils.cpp
xpcom/glue/FileUtils.h
--- 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