Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 4872102

Browse files
committed
Bug 1428557 - Stop using GetNativePath in xpcom/. r=froydnj
MozReview-Commit-ID: GnSFQ1wprzb --HG-- extra : rebase_source : 7c25e0361d7f4e8aea730672a931cf78a581e062 extra : intermediate-source : e055aee7306b6636d27abe6fc0b02012b0075260 extra : source : d0831f473c8f85996c106693c9a4ce3783dd9447
1 parent 9bbb215 commit 4872102

8 files changed

Lines changed: 55 additions & 36 deletions

File tree

startupcache/test/TestStartupCache.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,18 @@ TestStartupCache::TestStartupCache()
6565
{
6666
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mSCFile));
6767
mSCFile->AppendNative(NS_LITERAL_CSTRING("test-startupcache.tmp"));
68+
#ifdef XP_WIN
69+
nsAutoString env(NS_LITERAL_STRING("MOZ_STARTUP_CACHE="));
70+
env.Append(mSCFile->NativePath());
71+
_wputenv(env.get());
72+
#else
6873
nsAutoCString path;
6974
mSCFile->GetNativePath(path);
7075
char* env = mozilla::Smprintf("MOZ_STARTUP_CACHE=%s", path.get()).release();
7176
PR_SetEnv(env);
7277
// We intentionally leak `env` here because it is required by PR_SetEnv
7378
MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(env);
79+
#endif
7480
StartupCache::GetSingleton()->InvalidateCache();
7581
}
7682
TestStartupCache::~TestStartupCache()

xpcom/base/nsMemoryInfoDumper.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,8 @@ nsMemoryInfoDumper::OpenDMDFile(const nsAString& aIdentifier, int aPid,
805805
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "OpenANSIFileDesc failed");
806806

807807
// Print the path, because on some platforms (e.g. Mac) it's not obvious.
808-
nsCString path;
809-
rv = dmdFile->GetNativePath(path);
810-
if (NS_WARN_IF(NS_FAILED(rv))) {
811-
return rv;
812-
}
813-
dmd::StatusMsg("opened %s for writing\n", path.get());
808+
dmd::StatusMsg("opened %s for writing\n",
809+
dmdFile->HumanReadablePath().get());
814810

815811
return rv;
816812
}

xpcom/build/LateWriteChecks.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
#include "mozilla/Unused.h"
1717
#include "nsAppDirectoryServiceDefs.h"
1818
#include "nsDirectoryServiceUtils.h"
19+
#include "nsLocalFile.h"
1920
#include "nsPrintfCString.h"
2021
#include "mozilla/StackWalk.h"
2122
#include "plstr.h"
2223
#include "prio.h"
2324

2425
#ifdef XP_WIN
25-
#define NS_T(str) L ## str
2626
#define NS_SLASH "\\"
2727
#include <fcntl.h>
2828
#include <io.h>
@@ -92,21 +92,22 @@ RecordStackWalker(uint32_t aFrameNumber, void* aPC, void* aSP, void* aClosure)
9292
*/
9393
class LateWriteObserver final : public IOInterposeObserver
9494
{
95+
using char_type = filesystem::Path::value_type;
9596
public:
96-
explicit LateWriteObserver(const char* aProfileDirectory)
97-
: mProfileDirectory(PL_strdup(aProfileDirectory))
97+
explicit LateWriteObserver(const char_type* aProfileDirectory)
98+
: mProfileDirectory(NS_strdup(aProfileDirectory))
9899
{
99100
}
100101
~LateWriteObserver()
101102
{
102-
PL_strfree(mProfileDirectory);
103+
free(mProfileDirectory);
103104
mProfileDirectory = nullptr;
104105
}
105106

106107
void Observe(IOInterposeObserver::Observation& aObservation) override;
107108

108109
private:
109-
char* mProfileDirectory;
110+
char_type* mProfileDirectory;
110111
};
111112

112113
void
@@ -130,9 +131,9 @@ LateWriteObserver::Observe(IOInterposeObserver::Observation& aOb)
130131
&rawStack);
131132
Telemetry::ProcessedStack stack = Telemetry::GetStackAndModules(rawStack);
132133

133-
nsPrintfCString nameAux("%s%s%s", mProfileDirectory,
134-
NS_SLASH, "Telemetry.LateWriteTmpXXXXXX");
135-
char* name;
134+
nsTAutoString<char_type> nameAux(mProfileDirectory);
135+
nameAux.AppendLiteral(NS_SLASH "Telemetry.LateWriteTmpXXXXXX");
136+
char_type* name;
136137
nameAux.GetMutableData(&name);
137138

138139
// We want the sha1 of the entire file, so please don't write to fd
@@ -142,9 +143,9 @@ LateWriteObserver::Observe(IOInterposeObserver::Observation& aOb)
142143
HANDLE hFile;
143144
do {
144145
// mkstemp isn't supported so keep trying until we get a file
145-
_mktemp_s(name, strlen(name) + 1);
146-
hFile = CreateFileA(name, GENERIC_WRITE, 0, nullptr, CREATE_NEW,
147-
FILE_ATTRIBUTE_NORMAL, nullptr);
146+
_wmktemp_s(char16ptr_t(name), NS_strlen(name) + 1);
147+
hFile = CreateFileW(char16ptr_t(name), GENERIC_WRITE, 0, nullptr,
148+
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
148149
} while (GetLastError() == ERROR_FILE_EXISTS);
149150

150151
if (hFile == INVALID_HANDLE_VALUE) {
@@ -202,13 +203,12 @@ LateWriteObserver::Observe(IOInterposeObserver::Observation& aOb)
202203

203204
// We append the sha1 of the contents to the file name. This provides a simple
204205
// client side deduplication.
205-
nsPrintfCString finalName("%s%s", mProfileDirectory,
206-
"/Telemetry.LateWriteFinal-");
206+
nsAutoString finalName(NS_LITERAL_STRING("Telemetry.LateWriteFinal-"));
207207
for (int i = 0; i < 20; ++i) {
208208
finalName.AppendPrintf("%02x", sha1[i]);
209209
}
210-
PR_Delete(finalName.get());
211-
PR_Rename(name, finalName.get());
210+
RefPtr<nsLocalFile> file = new nsLocalFile(nameAux);
211+
file->RenameTo(nullptr, finalName);
212212
}
213213

214214
/******************************* Setup/Teardown *******************************/
@@ -223,9 +223,8 @@ InitLateWriteChecks()
223223
nsCOMPtr<nsIFile> mozFile;
224224
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mozFile));
225225
if (mozFile) {
226-
nsAutoCString nativePath;
227-
nsresult rv = mozFile->GetNativePath(nativePath);
228-
if (NS_SUCCEEDED(rv) && nativePath.get()) {
226+
PathString nativePath = mozFile->NativePath();
227+
if (nativePath.get()) {
229228
sLateWriteObserver = new LateWriteObserver(nativePath.get());
230229
}
231230
}

xpcom/io/nsLocalFileUnix.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ nsLocalFile::nsLocalFile()
227227
{
228228
}
229229

230+
nsLocalFile::nsLocalFile(const nsACString& aFilePath)
231+
{
232+
InitWithNativePath(aFilePath);
233+
}
234+
230235
nsLocalFile::nsLocalFile(const nsLocalFile& aOther)
231236
: mPath(aOther.mPath)
232237
{

xpcom/io/nsLocalFileUnix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class nsLocalFile final
9191
NS_DEFINE_STATIC_CID_ACCESSOR(NS_LOCAL_FILE_CID)
9292

9393
nsLocalFile();
94+
explicit nsLocalFile(const nsACString& aFilePath);
9495

9596
static nsresult nsLocalFileConstructor(nsISupports* aOuter,
9697
const nsIID& aIID,

xpcom/io/nsLocalFileWin.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,12 @@ nsLocalFile::nsLocalFile()
799799
{
800800
}
801801

802+
nsLocalFile::nsLocalFile(const nsAString& aFilePath)
803+
: mFollowSymlinks(false)
804+
{
805+
InitWithPath(aFilePath);
806+
}
807+
802808
nsresult
803809
nsLocalFile::nsLocalFileConstructor(nsISupports* aOuter, const nsIID& aIID,
804810
void** aInstancePtr)

xpcom/io/nsLocalFileWin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class nsLocalFile final
3333
NS_DEFINE_STATIC_CID_ACCESSOR(NS_LOCAL_FILE_CID)
3434

3535
nsLocalFile();
36+
explicit nsLocalFile(const nsAString& aFilePath);
3637

3738
static nsresult nsLocalFileConstructor(nsISupports* aOuter,
3839
const nsIID& aIID,

xpcom/tests/gtest/TestFile.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ static already_AddRefed<nsIFile> NewFile(nsIFile* aBase)
3131
return file.forget();
3232
}
3333

34-
static nsCString FixName(const char* aName)
34+
template <typename char_type>
35+
static nsTString<char_type> FixName(const char_type* aName)
3536
{
36-
nsCString name;
37+
nsTString<char_type> name;
3738
for (uint32_t i = 0; aName[i]; ++i) {
38-
char ch = aName[i];
39+
char_type ch = aName[i];
3940
// PR_GetPathSeparator returns the wrong value on Mac so don't use it
4041
#if defined(XP_WIN)
4142
if (ch == '/') {
@@ -379,22 +380,26 @@ static bool TestNormalizeNativePath(nsIFile* aBase, nsIFile* aStart)
379380
if (!file)
380381
return false;
381382

382-
nsAutoCString path;
383-
nsresult rv = file->GetNativePath(path);
384-
VerifyResult(rv, "GetNativePath");
383+
auto path = file->NativePath();
384+
#ifdef XP_WIN
385+
path.Append(FixName(u"/./.."));
386+
nsresult rv = file->InitWithPath(path);
387+
VerifyResult(rv, "InitWithPath");
388+
#else
385389
path.Append(FixName("/./.."));
386-
rv = file->InitWithNativePath(path);
390+
nsresult rv = file->InitWithNativePath(path);
387391
VerifyResult(rv, "InitWithNativePath");
392+
#endif
388393
rv = file->Normalize();
389394
VerifyResult(rv, "Normalize");
390-
rv = file->GetNativePath(path);
391-
VerifyResult(rv, "GetNativePath (after normalization)");
395+
path = file->NativePath();
392396

393-
nsAutoCString basePath;
394-
rv = aBase->GetNativePath(basePath);
397+
auto basePath = aBase->NativePath();
395398
VerifyResult(rv, "GetNativePath (base)");
396399

397-
EXPECT_TRUE(path.Equals(basePath)) << "Incorrect normalization: " << path.get() << " - " << basePath.get();
400+
EXPECT_TRUE(path.Equals(basePath)) << "Incorrect normalization: " <<
401+
file->HumanReadablePath().get() << " - " <<
402+
aBase->HumanReadablePath().get();
398403
if (!path.Equals(basePath)) {
399404
return false;
400405
}

0 commit comments

Comments
 (0)