Bug 1428557 - Stop using GetNativePath in xpcom/. r?froydnj draft
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Fri, 15 Dec 2017 06:53:37 +0900
changeset 750958 b096cc0e272b40870f782037b16a02d4219876bc
parent 750957 a5008c5f0eb936ccbcda51afe39c15af5e3fa1c9
child 750959 c338531fc4a8c5d37f985ca85bc3d37ebd2efba6
child 750960 acd8efa353cc57dbb42b2562bb88989d5618169f
push id97794
push userVYV03354@nifty.ne.jp
push dateSat, 03 Feb 2018 14:18:03 +0000
reviewersfroydnj
bugs1428557
milestone60.0a1
Bug 1428557 - Stop using GetNativePath in xpcom/. r?froydnj MozReview-Commit-ID: GnSFQ1wprzb
startupcache/test/TestStartupCache.cpp
xpcom/base/nsMemoryInfoDumper.cpp
xpcom/build/LateWriteChecks.cpp
xpcom/io/nsLocalFileUnix.cpp
xpcom/io/nsLocalFileUnix.h
xpcom/io/nsLocalFileWin.cpp
xpcom/io/nsLocalFileWin.h
xpcom/tests/gtest/TestFile.cpp
--- a/startupcache/test/TestStartupCache.cpp
+++ b/startupcache/test/TestStartupCache.cpp
@@ -60,22 +60,28 @@ protected:
 
   nsCOMPtr<nsIFile> mSCFile;
 };
 
 TestStartupCache::TestStartupCache()
 {
   NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mSCFile));
   mSCFile->AppendNative(NS_LITERAL_CSTRING("test-startupcache.tmp"));
+#ifdef XP_WIN
+  nsAutoString env(NS_LITERAL_STRING("MOZ_STARTUP_CACHE="));
+  env.Append(mSCFile->NativePath());
+  _wputenv(env.get());
+#else
   nsAutoCString path;
   mSCFile->GetNativePath(path);
   char* env = mozilla::Smprintf("MOZ_STARTUP_CACHE=%s", path.get()).release();
   PR_SetEnv(env);
   // We intentionally leak `env` here because it is required by PR_SetEnv
   MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(env);
+#endif
   StartupCache::GetSingleton()->InvalidateCache();
 }
 TestStartupCache::~TestStartupCache()
 {
   PR_SetEnv("MOZ_STARTUP_CACHE=");
   StartupCache::GetSingleton()->InvalidateCache();
 }
 
--- a/xpcom/base/nsMemoryInfoDumper.cpp
+++ b/xpcom/base/nsMemoryInfoDumper.cpp
@@ -800,22 +800,18 @@ nsMemoryInfoDumper::OpenDMDFile(const ns
                                  NS_LITERAL_CSTRING("memory-reports"));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   rv = dmdFile->OpenANSIFileDesc("wb", aOutFile);
   NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "OpenANSIFileDesc failed");
 
   // Print the path, because on some platforms (e.g. Mac) it's not obvious.
-  nsCString path;
-  rv = dmdFile->GetNativePath(path);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-  dmd::StatusMsg("opened %s for writing\n", path.get());
+  dmd::StatusMsg("opened %s for writing\n",
+                 dmdFile->HumanReadablePath().get());
 
   return rv;
 }
 
 nsresult
 nsMemoryInfoDumper::DumpDMDToFile(FILE* aFile)
 {
   RefPtr<nsGZFileWriter> gzWriter = new nsGZFileWriter();
--- a/xpcom/build/LateWriteChecks.cpp
+++ b/xpcom/build/LateWriteChecks.cpp
@@ -11,23 +11,23 @@
 #include "mozilla/ProcessedStack.h"
 #include "mozilla/SHA1.h"
 #include "mozilla/Scoped.h"
 #include "mozilla/StaticPtr.h"
 #include "mozilla/Telemetry.h"
 #include "mozilla/Unused.h"
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsDirectoryServiceUtils.h"
+#include "nsLocalFile.h"
 #include "nsPrintfCString.h"
 #include "mozilla/StackWalk.h"
 #include "plstr.h"
 #include "prio.h"
 
 #ifdef XP_WIN
-#define NS_T(str) L ## str
 #define NS_SLASH "\\"
 #include <fcntl.h>
 #include <io.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <windows.h>
 #else
@@ -87,31 +87,32 @@ RecordStackWalker(uint32_t aFrameNumber,
 /**************************** Late-Write Observer  ****************************/
 
 /**
  * An implementation of IOInterposeObserver to be registered with IOInterposer.
  * This observer logs all writes as late writes.
  */
 class LateWriteObserver final : public IOInterposeObserver
 {
+  using char_type = filesystem::Path::value_type;
 public:
-  explicit LateWriteObserver(const char* aProfileDirectory)
-    : mProfileDirectory(PL_strdup(aProfileDirectory))
+  explicit LateWriteObserver(const char_type* aProfileDirectory)
+    : mProfileDirectory(NS_strdup(aProfileDirectory))
   {
   }
   ~LateWriteObserver()
   {
-    PL_strfree(mProfileDirectory);
+    free(mProfileDirectory);
     mProfileDirectory = nullptr;
   }
 
   void Observe(IOInterposeObserver::Observation& aObservation) override;
 
 private:
-  char* mProfileDirectory;
+  char_type* mProfileDirectory;
 };
 
 void
 LateWriteObserver::Observe(IOInterposeObserver::Observation& aOb)
 {
   // Crash if that is the shutdown check mode
   if (gShutdownChecks == SCM_CRASH) {
     MOZ_CRASH();
@@ -125,31 +126,31 @@ LateWriteObserver::Observe(IOInterposeOb
   // Write the stack and loaded libraries to a file. We can get here
   // concurrently from many writes, so we use multiple temporary files.
   std::vector<uintptr_t> rawStack;
 
   MozStackWalk(RecordStackWalker, /* skipFrames */ 0, /* maxFrames */ 0,
                &rawStack);
   Telemetry::ProcessedStack stack = Telemetry::GetStackAndModules(rawStack);
 
-  nsPrintfCString nameAux("%s%s%s", mProfileDirectory,
-                          NS_SLASH, "Telemetry.LateWriteTmpXXXXXX");
-  char* name;
+  nsTAutoString<char_type> nameAux(mProfileDirectory);
+  nameAux.AppendLiteral(NS_SLASH "Telemetry.LateWriteTmpXXXXXX");
+  char_type* name;
   nameAux.GetMutableData(&name);
 
   // We want the sha1 of the entire file, so please don't write to fd
   // directly; use sha1Stream.
   FILE* stream;
 #ifdef XP_WIN
   HANDLE hFile;
   do {
     // mkstemp isn't supported so keep trying until we get a file
-    _mktemp_s(name, strlen(name) + 1);
-    hFile = CreateFileA(name, GENERIC_WRITE, 0, nullptr, CREATE_NEW,
-                        FILE_ATTRIBUTE_NORMAL, nullptr);
+    _wmktemp_s(char16ptr_t(name), NS_strlen(name) + 1);
+    hFile = CreateFileW(char16ptr_t(name), GENERIC_WRITE, 0, nullptr,
+                        CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
   } while (GetLastError() == ERROR_FILE_EXISTS);
 
   if (hFile == INVALID_HANDLE_VALUE) {
     MOZ_CRASH("Um, how did we get here?");
   }
 
   // http://support.microsoft.com/kb/139640
   int fd = _open_osfhandle((intptr_t)hFile, _O_APPEND);
@@ -197,40 +198,38 @@ LateWriteObserver::Observe(IOInterposeOb
 
   // Note: These files should be deleted by telemetry once it reads them. If
   // there were no telemetry runs by the time we shut down, we just add files
   // to the existing ones instead of replacing them. Given that each of these
   // files is a bug to be fixed, that is probably the right thing to do.
 
   // We append the sha1 of the contents to the file name. This provides a simple
   // client side deduplication.
-  nsPrintfCString finalName("%s%s", mProfileDirectory,
-                            "/Telemetry.LateWriteFinal-");
+  nsAutoString finalName(NS_LITERAL_STRING("Telemetry.LateWriteFinal-"));
   for (int i = 0; i < 20; ++i) {
     finalName.AppendPrintf("%02x", sha1[i]);
   }
-  PR_Delete(finalName.get());
-  PR_Rename(name, finalName.get());
+  RefPtr<nsLocalFile> file = new nsLocalFile(nameAux);
+  file->RenameTo(nullptr, finalName);
 }
 
 /******************************* Setup/Teardown *******************************/
 
 static StaticAutoPtr<LateWriteObserver> sLateWriteObserver;
 
 namespace mozilla {
 
 void
 InitLateWriteChecks()
 {
   nsCOMPtr<nsIFile> mozFile;
   NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mozFile));
   if (mozFile) {
-    nsAutoCString nativePath;
-    nsresult rv = mozFile->GetNativePath(nativePath);
-    if (NS_SUCCEEDED(rv) && nativePath.get()) {
+    PathString nativePath = mozFile->NativePath();
+    if (nativePath.get()) {
       sLateWriteObserver = new LateWriteObserver(nativePath.get());
     }
   }
 }
 
 void
 BeginLateWriteChecks()
 {
--- a/xpcom/io/nsLocalFileUnix.cpp
+++ b/xpcom/io/nsLocalFileUnix.cpp
@@ -222,16 +222,21 @@ nsDirEnumeratorUnix::Close()
   }
   return NS_OK;
 }
 
 nsLocalFile::nsLocalFile()
 {
 }
 
+nsLocalFile::nsLocalFile(const nsACString& aFilePath)
+{
+  InitWithNativePath(aFilePath);
+}
+
 nsLocalFile::nsLocalFile(const nsLocalFile& aOther)
   : mPath(aOther.mPath)
 {
 }
 
 #ifdef MOZ_WIDGET_COCOA
 NS_IMPL_ISUPPORTS(nsLocalFile,
                   nsILocalFileMac,
--- a/xpcom/io/nsLocalFileUnix.h
+++ b/xpcom/io/nsLocalFileUnix.h
@@ -86,16 +86,17 @@ class nsLocalFile final
   : public nsIFile
 #endif
   , public nsIHashable
 {
 public:
   NS_DEFINE_STATIC_CID_ACCESSOR(NS_LOCAL_FILE_CID)
 
   nsLocalFile();
+  explicit nsLocalFile(const nsACString& aFilePath);
 
   static nsresult nsLocalFileConstructor(nsISupports* aOuter,
                                          const nsIID& aIID,
                                          void** aInstancePtr);
 
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIFILE
 #ifdef MOZ_WIDGET_COCOA
--- a/xpcom/io/nsLocalFileWin.cpp
+++ b/xpcom/io/nsLocalFileWin.cpp
@@ -794,16 +794,22 @@ NS_IMPL_ISUPPORTS(nsDirEnumerator, nsISi
 
 nsLocalFile::nsLocalFile()
   : mDirty(true)
   , mResolveDirty(true)
   , mFollowSymlinks(false)
 {
 }
 
+nsLocalFile::nsLocalFile(const nsAString& aFilePath)
+  : mFollowSymlinks(false)
+{
+  InitWithPath(aFilePath);
+}
+
 nsresult
 nsLocalFile::nsLocalFileConstructor(nsISupports* aOuter, const nsIID& aIID,
                                     void** aInstancePtr)
 {
   if (NS_WARN_IF(!aInstancePtr)) {
     return NS_ERROR_INVALID_ARG;
   }
   if (NS_WARN_IF(aOuter)) {
--- a/xpcom/io/nsLocalFileWin.h
+++ b/xpcom/io/nsLocalFileWin.h
@@ -28,16 +28,17 @@
 class nsLocalFile final
   : public nsILocalFileWin
   , public nsIHashable
 {
 public:
   NS_DEFINE_STATIC_CID_ACCESSOR(NS_LOCAL_FILE_CID)
 
   nsLocalFile();
+  explicit nsLocalFile(const nsAString& aFilePath);
 
   static nsresult nsLocalFileConstructor(nsISupports* aOuter,
                                          const nsIID& aIID,
                                          void** aInstancePtr);
 
   // nsISupports interface
   NS_DECL_THREADSAFE_ISUPPORTS
 
--- a/xpcom/tests/gtest/TestFile.cpp
+++ b/xpcom/tests/gtest/TestFile.cpp
@@ -26,21 +26,22 @@ static already_AddRefed<nsIFile> NewFile
   nsCOMPtr<nsIFile> file =
     do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
   VerifyResult(rv, "Creating nsIFile");
   rv = file->InitWithFile(aBase);
   VerifyResult(rv, "InitWithFile");
   return file.forget();
 }
 
-static nsCString FixName(const char* aName)
+template <typename char_type>
+static nsTString<char_type> FixName(const char_type* aName)
 {
-  nsCString name;
+  nsTString<char_type> name;
   for (uint32_t i = 0; aName[i]; ++i) {
-    char ch = aName[i];
+    char_type ch = aName[i];
     // PR_GetPathSeparator returns the wrong value on Mac so don't use it
 #if defined(XP_WIN)
     if (ch == '/') {
       ch = '\\';
     }
 #endif
     name.Append(ch);
   }
@@ -374,32 +375,36 @@ static bool TestParent(nsIFile* aBase, n
 
 // Test nsIFile::Normalize and native path setting/getting
 static bool TestNormalizeNativePath(nsIFile* aBase, nsIFile* aStart)
 {
   nsCOMPtr<nsIFile> file = NewFile(aStart);
   if (!file)
     return false;
 
-  nsAutoCString path;
-  nsresult rv = file->GetNativePath(path);
-  VerifyResult(rv, "GetNativePath");
+  auto path = file->NativePath();
+#ifdef XP_WIN
+  path.Append(FixName(u"/./.."));
+  nsresult rv = file->InitWithPath(path);
+  VerifyResult(rv, "InitWithPath");
+#else
   path.Append(FixName("/./.."));
-  rv = file->InitWithNativePath(path);
+  nsresult rv = file->InitWithNativePath(path);
   VerifyResult(rv, "InitWithNativePath");
+#endif
   rv = file->Normalize();
   VerifyResult(rv, "Normalize");
-  rv = file->GetNativePath(path);
-  VerifyResult(rv, "GetNativePath (after normalization)");
+  path = file->NativePath();
 
-  nsAutoCString basePath;
-  rv = aBase->GetNativePath(basePath);
+  auto basePath = aBase->NativePath();
   VerifyResult(rv, "GetNativePath (base)");
 
-  EXPECT_TRUE(path.Equals(basePath)) << "Incorrect normalization: " << path.get() << " - " << basePath.get();
+  EXPECT_TRUE(path.Equals(basePath)) << "Incorrect normalization: " <<
+    file->HumanReadablePath().get() << " - " <<
+    aBase->HumanReadablePath().get();
   if (!path.Equals(basePath)) {
     return false;
   }
 
   return true;
 }
 
 TEST(TestFile, Tests)