Bug 685236 - Stop using GetNativePath in xpcom/. r?froydnj draft
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Fri, 15 Dec 2017 06:53:37 +0900
changeset 715685 f6b35c3ef28a1a79267b0fb11bc630b8022d2353
parent 715684 d5211b4aa5964a27b0ddb0dc4f2d5e80fb0e7f32
child 715686 0314260d6adfa36a32160c1409c545ab9d9346d7
push id94228
push userVYV03354@nifty.ne.jp
push dateThu, 04 Jan 2018 11:56:10 +0000
reviewersfroydnj
bugs685236
milestone59.0a1
Bug 685236 - Stop using GetNativePath in xpcom/. r?froydnj MozReview-Commit-ID: GnSFQ1wprzb
startupcache/test/TestStartupCache.cpp
xpcom/base/nsMemoryInfoDumper.cpp
xpcom/build/LateWriteChecks.cpp
--- a/startupcache/test/TestStartupCache.cpp
+++ b/startupcache/test/TestStartupCache.cpp
@@ -58,22 +58,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,17 @@ 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->DisplayPath().get());
 
   return rv;
 }
 
 nsresult
 nsMemoryInfoDumper::DumpDMDToFile(FILE* aFile)
 {
   RefPtr<nsGZFileWriter> gzWriter = new nsGZFileWriter();
--- a/xpcom/build/LateWriteChecks.cpp
+++ b/xpcom/build/LateWriteChecks.cpp
@@ -87,30 +87,31 @@ 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);
 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();
@@ -124,31 +125,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);
@@ -196,40 +197,44 @@ 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-");
+  nsTAutoString<char_type> finalName(mProfileDirectory);
+  finalName.AppendLiteral("/Telemetry.LateWriteFinal-");
   for (int i = 0; i < 20; ++i) {
     finalName.AppendPrintf("%02x", sha1[i]);
   }
+#ifdef XP_WIN
+  DeleteFileW(char16ptr_t(finalName.get()));
+  MoveFileW(char16ptr_t(name), char16ptr_t(finalName.get()));
+#else
   PR_Delete(finalName.get());
   PR_Rename(name, finalName.get());
+#endif
 }
 
 /******************************* 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()) {
+    filesystem::Path::string_type nativePath = mozFile->NativePath();
+    if (nativePath.get()) {
       sLateWriteObserver = new LateWriteObserver(nativePath.get());
     }
   }
 }
 
 void
 BeginLateWriteChecks()
 {