Bug 1240005 - Users of do_CreateInstance or InitWithNativePath/InitWithPath not checking return value r=necko-reviewers,xpcom-reviewers,media-playback-reviewers,emilio,karlt,valentin

Differential Revision: https://phabricator.services.mozilla.com/D227280
This commit is contained in:
Oliver Old
2024-11-25 16:54:11 +00:00
parent 82cc01210e
commit 054846ec0f
42 changed files with 377 additions and 359 deletions

View File

@@ -36,6 +36,7 @@
#include "mozilla/StaticPrefs_signon.h"
#include "mozilla/TextUtils.h"
#include "mozilla/Try.h"
#include "mozilla/Unused.h"
#include "nsAttrValueInlines.h"
#include "nsCRTGlue.h"
#include "nsIFilePicker.h"
@@ -347,7 +348,7 @@ UploadLastDir::ContentPrefCallback::HandleCompletion(uint16_t aReason) {
if (!prefStr.IsEmpty()) {
nsresult rv = NS_NewLocalFile(prefStr, getter_AddRefs(localFile));
(void)NS_WARN_IF(NS_FAILED(rv));
Unused << NS_WARN_IF(NS_FAILED(rv));
}
if (localFile) {
@@ -2161,13 +2162,13 @@ void HTMLInputElement::MozSetFileNameArray(const Sequence<nsString>& aFileNames,
nsASCIICaseInsensitiveStringComparator)) {
// Converts the URL string into the corresponding nsIFile if possible
// A local file will be created if the URL string begins with file://
NS_GetFileFromURLSpec(NS_ConvertUTF16toUTF8(aFileNames[i]),
Unused << NS_GetFileFromURLSpec(NS_ConvertUTF16toUTF8(aFileNames[i]),
getter_AddRefs(file));
}
if (!file) {
// this is no "file://", try as local file
NS_NewLocalFile(aFileNames[i], getter_AddRefs(file));
Unused << NS_NewLocalFile(aFileNames[i], getter_AddRefs(file));
}
if (!file) {

View File

@@ -91,8 +91,8 @@ bool FFVPXRuntimeLinker::Init() {
if (path.IsEmpty()) {
return false;
}
nsCOMPtr<nsIFile> libFile = new nsLocalFile(path);
if (libFile->NativePath().IsEmpty()) {
nsCOMPtr<nsIFile> libFile;
if (NS_FAILED(NS_NewPathStringLocalFile(path, getter_AddRefs(libFile)))) {
return false;
}

View File

@@ -7,6 +7,7 @@
#include <utility>
#include "mozilla/Try.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsCRT.h"
#include "nsIFile.h"
@@ -90,9 +91,9 @@ class mozPersonalDictionarySave final : public mozilla::Runnable {
mozilla::MonitorAutoLock mon(mDict->mMonitorSave);
nsCOMPtr<nsIOutputStream> outStream;
NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStream), mFile,
PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
0664);
MOZ_TRY(NS_NewSafeLocalFileOutputStream(
getter_AddRefs(outStream), mFile,
PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE, 0664));
// Get a buffered output stream 4096 bytes big, to optimize writes.
nsCOMPtr<nsIOutputStream> bufferedOutputStream;
@@ -273,7 +274,10 @@ void mozPersonalDictionary::SyncLoadInternal() {
}
nsCOMPtr<nsIInputStream> inStream;
NS_NewLocalFileInputStream(getter_AddRefs(inStream), mFile);
rv = NS_NewLocalFileInputStream(getter_AddRefs(inStream), mFile);
if (NS_FAILED(rv)) {
return;
}
nsCOMPtr<nsIUnicharInputStream> convStream;
rv = NS_NewUnicharInputStream(inStream, getter_AddRefs(convStream));

View File

@@ -506,6 +506,7 @@ void GeckoChildProcessHost::Destroy() {
// static
mozilla::BinPathType BaseProcessLauncher::GetPathToBinary(
FilePath& exePath, GeckoProcessType processType) {
exePath = {};
BinPathType pathType = XRE_GetChildProcBinPathType(processType);
if (pathType == BinPathType::Self) {
@@ -543,21 +544,25 @@ mozilla::BinPathType BaseProcessLauncher::GetPathToBinary(
exePath = FilePath(char16ptr_t(gGREBinPath));
#elif MOZ_WIDGET_COCOA
nsCOMPtr<nsIFile> childProcPath;
NS_NewLocalFile(nsDependentString(gGREBinPath),
getter_AddRefs(childProcPath));
if (NS_SUCCEEDED(NS_NewLocalFile(nsDependentString(gGREBinPath),
getter_AddRefs(childProcPath)))) {
// We need to use an App Bundle on OS X so that we can hide
// the dock icon. See Bug 557225.
childProcPath->AppendNative(bundleName);
childProcPath->AppendNative("Contents"_ns);
childProcPath->AppendNative("MacOS"_ns);
if (NS_SUCCEEDED(childProcPath->AppendNative(bundleName)) &&
NS_SUCCEEDED(childProcPath->AppendNative("Contents"_ns)) &&
NS_SUCCEEDED(childProcPath->AppendNative("MacOS"_ns))) {
nsCString tempCPath;
childProcPath->GetNativePath(tempCPath);
if (NS_SUCCEEDED(childProcPath->GetNativePath(tempCPath))) {
exePath = FilePath(tempCPath.get());
}
}
}
#else
nsCString path;
NS_CopyUnicodeToNative(nsDependentString(gGREBinPath), path);
if (NS_SUCCEEDED(
NS_CopyUnicodeToNative(nsDependentString(gGREBinPath), path))) {
exePath = FilePath(path.get());
}
#endif
}

View File

@@ -21,6 +21,7 @@
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/IOInterposer.h"
#include "mozilla/Preferences.h"
#include "mozilla/Unused.h"
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
#include "nsServiceManagerUtils.h"
#include "nsComponentManagerUtils.h"
@@ -181,7 +182,7 @@ static bool GetLocationProperty(JSContext* cx, unsigned argc, Value* vp) {
# endif
nsCOMPtr<nsIFile> location;
nsresult rv = NS_NewLocalFile(filenameString, getter_AddRefs(location));
Unused << NS_NewLocalFile(filenameString, getter_AddRefs(location));
if (!location && gWorkingDirectory) {
// could be a relative path, try appending it to the cwd
@@ -189,7 +190,7 @@ static bool GetLocationProperty(JSContext* cx, unsigned argc, Value* vp) {
nsAutoString absolutePath(*gWorkingDirectory);
absolutePath.Append(filenameString);
rv = NS_NewLocalFile(absolutePath, getter_AddRefs(location));
Unused << NS_NewLocalFile(absolutePath, getter_AddRefs(location));
}
if (location) {
@@ -199,7 +200,7 @@ static bool GetLocationProperty(JSContext* cx, unsigned argc, Value* vp) {
location->Normalize();
RootedObject locationObj(cx);
RootedObject scope(cx, JS::CurrentGlobalOrNull(cx));
rv = nsXPConnect::XPConnect()->WrapNative(
nsresult rv = nsXPConnect::XPConnect()->WrapNative(
cx, scope, location, NS_GET_IID(nsIFile), locationObj.address());
if (NS_SUCCEEDED(rv) && locationObj) {
args.rval().setObject(*locationObj);

View File

@@ -72,7 +72,6 @@
#include "nsISupportsImpl.h"
#include "nsISupportsPrimitives.h"
#include "nsIZipReader.h"
#include "nsLocalFile.h"
#include "nsNetUtil.h"
#include "nsPrintfCString.h"
#include "nsProxyRelease.h"
@@ -2523,15 +2522,10 @@ nsPrefBranch::GetComplexValue(const char* aPrefName, const nsIID& aType,
if (aType.Equals(NS_GET_IID(nsIFile))) {
ENSURE_PARENT_PROCESS("GetComplexValue(nsIFile)", aPrefName);
nsCOMPtr<nsIFile> file = new nsLocalFile();
rv = file->SetPersistentDescriptor(utf8String);
if (NS_SUCCEEDED(rv)) {
file.forget(reinterpret_cast<nsIFile**>(aRetVal));
MOZ_TRY(NS_NewLocalFileWithPersistentDescriptor(
utf8String, reinterpret_cast<nsIFile**>(aRetVal)));
return NS_OK;
}
return rv;
}
if (aType.Equals(NS_GET_IID(nsIRelativeFilePref))) {
ENSURE_PARENT_PROCESS("GetComplexValue(nsIRelativeFilePref)", aPrefName);
@@ -2566,15 +2560,8 @@ nsPrefBranch::GetComplexValue(const char* aPrefName, const nsIID& aType,
}
nsCOMPtr<nsIFile> theFile;
rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(theFile));
if (NS_FAILED(rv)) {
return rv;
}
rv = theFile->SetRelativeDescriptor(fromFile, Substring(++keyEnd, strEnd));
if (NS_FAILED(rv)) {
return rv;
}
MOZ_TRY(NS_NewLocalFileWithRelativeDescriptor(
fromFile, Substring(++keyEnd, strEnd), getter_AddRefs(theFile)));
nsCOMPtr<nsIRelativeFilePref> relativePref = new nsRelativeFilePref();
Unused << relativePref->SetFile(theFile);

View File

@@ -142,10 +142,6 @@ nsresult net_GetFileFromURLSpec(const nsACString& aURL, nsIFile** result) {
nsresult rv;
nsCOMPtr<nsIFile> localFile;
rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(localFile));
if (NS_FAILED(rv)) return rv;
nsAutoCString directory, fileBaseName, fileExtension, path;
bool bHFSPath = false;
@@ -196,10 +192,5 @@ nsresult net_GetFileFromURLSpec(const nsACString& aURL, nsIFile** result) {
if (bHFSPath) convertHFSPathtoPOSIX(path, path);
// assuming path is encoded in the native charset
rv = localFile->InitWithNativePath(path);
if (NS_FAILED(rv)) return rv;
localFile.forget(result);
return NS_OK;
return NS_NewNativeLocalFile(path, result);
}

View File

@@ -56,10 +56,6 @@ nsresult net_GetFileFromURLSpec(const nsACString& aURL, nsIFile** result) {
nsresult rv;
nsCOMPtr<nsIFile> localFile;
rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(localFile));
if (NS_FAILED(rv)) return rv;
nsAutoCString directory, fileBaseName, fileExtension, path;
rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension);
@@ -85,25 +81,5 @@ nsresult net_GetFileFromURLSpec(const nsACString& aURL, nsIFile** result) {
NS_UnescapeURL(path);
if (path.Length() != strlen(path.get())) return NS_ERROR_FILE_INVALID_PATH;
if (IsUtf8(path)) {
// speed up the start-up where UTF-8 is the native charset
// (e.g. on recent Linux distributions)
if (NS_IsNativeUTF8()) {
rv = localFile->InitWithNativePath(path);
} else {
rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
}
// XXX In rare cases, a valid UTF-8 string can be valid as a native
// encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
// However, the chance is very low that a meaningful word in a legacy
// encoding is valid as UTF-8.
} else {
// if path is not in UTF-8, assume it is encoded in the native charset
rv = localFile->InitWithNativePath(path);
}
if (NS_FAILED(rv)) return rv;
localFile.forget(result);
return NS_OK;
return NS_NewNativeLocalFile(path, result);
}

View File

@@ -89,22 +89,14 @@ nsresult net_GetFileFromURLSpec(const nsACString& aURL, nsIFile** result) {
// remove leading '\'
if (path.CharAt(0) == '\\') path.Cut(0, 1);
nsCOMPtr<nsIFile> localFile;
if (IsUtf8(path)) {
rv =
NS_NewLocalFile(NS_ConvertUTF8toUTF16(path), getter_AddRefs(localFile));
return NS_NewUTF8LocalFile(path, result);
}
// XXX In rare cases, a valid UTF-8 string can be valid as a native
// encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
// However, the chance is very low that a meaningful word in a legacy
// encoding is valid as UTF-8.
else {
// if path is not in UTF-8, assume it is encoded in the native charset
rv = NS_NewNativeLocalFile(path, getter_AddRefs(localFile));
}
if (NS_FAILED(rv)) return rv;
localFile.forget(result);
return NS_OK;
return NS_NewNativeLocalFile(path, result);
}

View File

@@ -250,11 +250,7 @@ nsresult nsFileChannel::Init() {
// can point to different resources right after the first resource is loaded.
nsCOMPtr<nsIFile> file;
nsCOMPtr<nsIURI> targetURI;
#ifdef XP_WIN
nsAutoString fileTarget;
#else
nsAutoCString fileTarget;
#endif
AutoPathString fileTarget;
nsCOMPtr<nsIFile> resolvedFile;
bool symLink;
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(mFileURI);
@@ -262,12 +258,11 @@ nsresult nsFileChannel::Init() {
NS_SUCCEEDED(file->IsSymlink(&symLink)) && symLink &&
#ifdef XP_WIN
NS_SUCCEEDED(file->GetTarget(fileTarget)) &&
NS_SUCCEEDED(NS_NewLocalFile(fileTarget, getter_AddRefs(resolvedFile))) &&
#else
NS_SUCCEEDED(file->GetNativeTarget(fileTarget)) &&
NS_SUCCEEDED(
NS_NewNativeLocalFile(fileTarget, getter_AddRefs(resolvedFile))) &&
#endif
NS_SUCCEEDED(NS_NewPathStringLocalFile(fileTarget,
getter_AddRefs(resolvedFile))) &&
NS_SUCCEEDED(
NS_NewFileURI(getter_AddRefs(targetURI), resolvedFile, nullptr))) {
// Make an effort to match up the query strings.

View File

@@ -1327,14 +1327,9 @@ static nsresult GetNSSProfilePath(nsAutoCString& aProfilePath) {
// |profilePath| is encoded in UTF-8.
static nsresult AttemptToRenamePKCS11ModuleDB(const nsACString& profilePath) {
nsCOMPtr<nsIFile> profileDir;
# ifdef XP_WIN
// |profilePath| is encoded in UTF-8 because SQLite always takes UTF-8 file
// paths regardless of the current system code page.
MOZ_TRY(NS_NewLocalFile(u""_ns, getter_AddRefs(profileDir)));
MOZ_TRY(profileDir->InitWithPath(NS_ConvertUTF8toUTF16(profilePath)));
# else
MOZ_TRY(NS_NewNativeLocalFile(profilePath, getter_AddRefs(profileDir)));
# endif
MOZ_TRY(NS_NewUTF8LocalFile(profilePath, getter_AddRefs(profileDir)));
const char* moduleDBFilename = "pkcs11.txt";
nsAutoCString destModuleDBFilename(moduleDBFilename);
destModuleDBFilename.Append(".fips");

View File

@@ -186,7 +186,8 @@ nsresult StartupCache::Init() {
// cache in.
char* env = PR_GetEnv("MOZ_STARTUP_CACHE");
if (env && *env) {
rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), getter_AddRefs(mFile));
MOZ_TRY(
NS_NewNativeLocalFile(nsDependentCString(env), getter_AddRefs(mFile)));
} else {
nsCOMPtr<nsIFile> file;
rv = NS_GetSpecialDirectory("ProfLDS", getter_AddRefs(file));
@@ -203,14 +204,11 @@ nsresult StartupCache::Init() {
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS) return rv;
rv = file->AppendNative(nsLiteralCString(STARTUP_CACHE_NAME));
NS_ENSURE_SUCCESS(rv, rv);
mFile = file;
mFile = file.forget();
}
NS_ENSURE_TRUE(mFile, NS_ERROR_UNEXPECTED);
mObserverService = do_GetService("@mozilla.org/observer-service;1");
if (!mObserverService) {

View File

@@ -139,7 +139,7 @@ int RunGTestFunc(int* argc, char** argv) {
nsCOMPtr<nsIFile> file;
if (path) {
nsresult rv =
NS_NewLocalFile(NS_ConvertUTF8toUTF16(path), getter_AddRefs(file));
NS_NewUTF8LocalFile(nsDependentCString(path), getter_AddRefs(file));
if (NS_FAILED(rv)) {
printf_stderr("Ignoring invalid MOZ_GTEST_MINIDUMPS_PATH\n");
}

View File

@@ -5,6 +5,7 @@
#include "nsCommandLine.h"
#include "mozilla/CmdLineAndEnvUtils.h"
#include "mozilla/Try.h"
#include "WinRemoteMessage.h"
using namespace mozilla;
@@ -39,7 +40,8 @@ nsresult WinRemoteMessageReceiver::ParseV2(const nsAString& aBuffer) {
nsCOMPtr<nsIFile> workingDir;
if (cch < aBuffer.Length()) {
NS_NewLocalFile(Substring(aBuffer, cch), getter_AddRefs(workingDir));
MOZ_TRY(
NS_NewLocalFile(Substring(aBuffer, cch), getter_AddRefs(workingDir)));
}
int argc = parser.Argc();
@@ -78,8 +80,7 @@ nsresult WinRemoteMessageReceiver::ParseV3(const nsACString& aBuffer) {
return NS_ERROR_FAILURE;
}
nsresult rv = NS_NewLocalFile(
NS_ConvertUTF8toUTF16(Substring(aBuffer, pos, nextNul - pos)),
nsresult rv = NS_NewUTF8LocalFile(Substring(aBuffer, pos, nextNul - pos),
getter_AddRefs(workingDir));
NS_ENSURE_SUCCESS(rv, rv);

View File

@@ -258,8 +258,11 @@ using PathChar = filesystem::Path::value_type;
using PathCharPtr = const PathChar*;
static uint32_t ReadLastShutdownDuration(PathCharPtr filename) {
RefPtr<nsLocalFile> file =
new nsLocalFile(nsTDependentString<PathChar>(filename));
nsCOMPtr<nsIFile> file;
if (NS_FAILED(NS_NewPathStringLocalFile(DependentPathString(filename),
getter_AddRefs(file)))) {
return 0;
}
FILE* f;
if (NS_FAILED(file->OpenANSIFileDesc("r", &f)) || !f) {
return 0;
@@ -1751,9 +1754,12 @@ void RecordShutdownEndTimeStamp() {
return;
}
nsTAutoString<PathChar> tmpName(name);
AutoPathString tmpName(name);
tmpName.AppendLiteral(".tmp");
RefPtr<nsLocalFile> tmpFile = new nsLocalFile(tmpName);
nsCOMPtr<nsIFile> tmpFile;
if (NS_FAILED(NS_NewPathStringLocalFile(tmpName, getter_AddRefs(tmpFile)))) {
return;
}
FILE* f;
if (NS_FAILED(tmpFile->OpenANSIFileDesc("w", &f)) || !f) return;
// On a normal release build this should be called just before
@@ -1773,11 +1779,15 @@ void RecordShutdownEndTimeStamp() {
tmpFile->Remove(false);
return;
}
RefPtr<nsLocalFile> file = new nsLocalFile(name);
nsCOMPtr<nsIFile> file;
if (NS_FAILED(NS_NewPathStringLocalFile(name, getter_AddRefs(file)))) {
return;
}
nsAutoString leafName;
file->GetLeafName(leafName);
if (NS_SUCCEEDED(file->GetLeafName(leafName))) {
tmpFile->RenameTo(nullptr, leafName);
}
}
} // namespace mozilla

View File

@@ -28,6 +28,7 @@
#include "mozilla/StaticMutex.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/Unused.h"
#include "nsPrintfCString.h"
#include "nsThreadUtils.h"
@@ -414,33 +415,25 @@ static inline void my_u64tostring(uint64_t aValue, char* aBuffer,
}
#endif
#ifdef XP_WIN
static void CreateFileFromPath(const xpstring& path, nsIFile** file) {
NS_NewLocalFile(nsDependentString(path.c_str()), file);
Unused << NS_NewPathStringLocalFile(
DependentPathString(path.c_str(), path.size()), file);
}
[[nodiscard]]
static std::optional<xpstring> CreatePathFromFile(nsIFile* file) {
nsAutoString path;
AutoPathString path;
#ifdef XP_WIN
nsresult rv = file->GetPath(path);
if (NS_FAILED(rv)) {
return {};
}
return xpstring(static_cast<wchar_t*>(path.get()), path.Length());
}
#else
static void CreateFileFromPath(const xpstring& path, nsIFile** file) {
NS_NewNativeLocalFile(nsDependentCString(path.c_str()), file);
}
MAYBE_UNUSED static std::optional<xpstring> CreatePathFromFile(nsIFile* file) {
nsAutoCString path;
nsresult rv = file->GetNativePath(path);
#endif
if (NS_FAILED(rv)) {
return {};
}
return xpstring(path.get(), path.Length());
return xpstring(static_cast<xpstring::const_pointer>(path.get()),
path.Length());
}
#endif
static time_t GetCurrentTimeForCrashTime() {
#ifdef XP_LINUX
@@ -2830,13 +2823,19 @@ static void SetCrashEventsDir(nsIFile* aDir) {
static const XP_CHAR eventsDirectoryEnv[] =
XP_TEXT("MOZ_CRASHREPORTER_EVENTS_DIRECTORY");
nsCOMPtr<nsIFile> eventsDir = aDir;
nsCOMPtr<nsIFile> eventsDir;
const char* env = PR_GetEnv("CRASHES_EVENTS_DIR");
if (env && *env) {
NS_NewNativeLocalFile(nsDependentCString(env), getter_AddRefs(eventsDir));
if (NS_SUCCEEDED(NS_NewNativeLocalFile(nsDependentCString(env),
getter_AddRefs(eventsDir)))) {
EnsureDirectoryExists(eventsDir);
}
}
if (!eventsDir) {
eventsDir = aDir;
}
std::optional<xpstring> path = CreatePathFromFile(eventsDir);
if (!path) {
@@ -2984,19 +2983,10 @@ static bool GetPendingDir(nsIFile** dir) {
return false;
}
nsCOMPtr<nsIFile> pending;
#ifdef XP_WIN
nsresult rv = NS_NewLocalFile(nsDependentString(pendingDirectory.c_str()),
getter_AddRefs(pending));
#else
nsresult rv = NS_NewNativeLocalFile(
nsDependentCString(pendingDirectory.c_str()), getter_AddRefs(pending));
#endif
if (NS_FAILED(rv)) {
return false;
}
pending.swap(*dir);
return true;
MOZ_ASSERT(!*dir);
return NS_SUCCEEDED(NS_NewPathStringLocalFile(
DependentPathString(pendingDirectory.c_str(), pendingDirectory.size()),
dir));
}
// The "limbo" dir is where minidumps go to wait for something else to
@@ -3346,6 +3336,7 @@ static void OnChildProcessDumpRequested(
if (!isSafeToDump) return;
CreateFileFromPath(aFilePath, getter_AddRefs(minidump));
MOZ_ASSERT(minidump);
ProcessId pid = aClientInfo.pid();
if (ShouldReport()) {
@@ -3810,6 +3801,7 @@ bool CreateMinidumpsAndPair(ProcessHandle aTargetHandle,
nsCOMPtr<nsIFile> targetMinidump;
CreateFileFromPath(xpstring(minidumpPath), getter_AddRefs(targetMinidump));
MOZ_ASSERT(targetMinidump);
// Create a dump of this process.
if (!google_breakpad::ExceptionHandler::WriteMinidump(
@@ -3829,6 +3821,7 @@ bool CreateMinidumpsAndPair(ProcessHandle aTargetHandle,
nsCOMPtr<nsIFile> incomingDump;
CreateFileFromPath(xpstring(minidumpPath), getter_AddRefs(incomingDump));
MOZ_ASSERT(incomingDump);
RenameAdditionalHangMinidump(incomingDump, targetMinidump, aIncomingPairName);

View File

@@ -785,10 +785,8 @@ bool nsToolkitProfileService::IsProfileForCurrentInstall(
}
nsCOMPtr<nsIFile> lastGreDir;
rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(lastGreDir));
NS_ENSURE_SUCCESS(rv, false);
rv = lastGreDir->SetPersistentDescriptor(lastGreDirStr);
rv = NS_NewLocalFileWithPersistentDescriptor(lastGreDirStr,
getter_AddRefs(lastGreDir));
NS_ENSURE_SUCCESS(rv, false);
#ifdef XP_WIN
@@ -1149,13 +1147,12 @@ nsresult nsToolkitProfileService::Init() {
}
nsCOMPtr<nsIFile> rootDir;
rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(rootDir));
NS_ENSURE_SUCCESS(rv, rv);
if (isRelative) {
rv = rootDir->SetRelativeDescriptor(mAppData, filePath);
rv = NS_NewLocalFileWithRelativeDescriptor(mAppData, filePath,
getter_AddRefs(rootDir));
} else {
rv = rootDir->SetPersistentDescriptor(filePath);
rv = NS_NewLocalFileWithPersistentDescriptor(filePath,
getter_AddRefs(rootDir));
}
if (NS_FAILED(rv)) continue;
@@ -2789,11 +2786,9 @@ nsresult nsToolkitProfileService::GetLocalDirFromRootDir(nsIFile* aRootDir,
nsCOMPtr<nsIFile> localDir;
if (isRelative) {
rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(localDir));
NS_ENSURE_SUCCESS(rv, rv);
rv = localDir->SetRelativeDescriptor(
nsToolkitProfileService::gService->mTempData, path);
rv = NS_NewLocalFileWithRelativeDescriptor(
nsToolkitProfileService::gService->mTempData, path,
getter_AddRefs(localDir));
NS_ENSURE_SUCCESS(rv, rv);
} else {
localDir = aRootDir;
@@ -2827,20 +2822,11 @@ nsresult XRE_GetFileFromPath(const char* aPath, nsIFile** aResult) {
nullptr, (const UInt8*)aPath, pathLen, true);
if (!fullPath) return NS_ERROR_FAILURE;
nsCOMPtr<nsIFile> lf;
nsresult rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(lf));
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsILocalFileMac> lfMac = do_QueryInterface(lf, &rv);
if (NS_SUCCEEDED(rv)) {
rv = lfMac->InitWithCFURL(fullPath);
if (NS_SUCCEEDED(rv)) {
lf.forget(aResult);
}
}
}
nsCOMPtr<nsILocalFileMac> lfMac;
nsresult rv = NS_NewLocalFileWithCFURL(fullPath, getter_AddRefs(lfMac));
lfMac.forget(aResult);
CFRelease(fullPath);
return rv;
#elif defined(XP_UNIX)
char fullPath[MAXPATHLEN];
@@ -2854,7 +2840,6 @@ nsresult XRE_GetFileFromPath(const char* aPath, nsIFile** aResult) {
return NS_ERROR_FAILURE;
return NS_NewLocalFile(nsDependentString(fullPath), aResult);
#else
# error Platform-specific logic needed here.
#endif

View File

@@ -284,7 +284,7 @@ already_AddRefed<nsIFile> GetNormalizedAppFile(nsIFile* aAppFile) {
nsCOMPtr<nsILocalFileMac> macFile = do_QueryInterface(appFile);
if (macFile && NS_SUCCEEDED(macFile->GetFSRef(&ref)) &&
NS_SUCCEEDED(NS_NewLocalFileWithFSRef(&ref, getter_AddRefs(macFile)))) {
appFile = static_cast<nsIFile*>(macFile);
appFile = macFile.forget();
} else {
NS_WARNING("Failed to resolve install directory.");
}

View File

@@ -32,6 +32,7 @@
#include "mozilla/StaticPrefs_webgl.h"
#include "mozilla/StaticPrefs_widget.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Try.h"
#include "mozilla/Utf8.h"
#include "mozilla/intl/LocaleService.h"
#include "mozilla/JSONWriter.h"
@@ -3440,10 +3441,7 @@ static bool CheckCompatibility(nsIFile* aProfileDir, const nsCString& aVersion,
if (NS_FAILED(rv)) return false;
nsCOMPtr<nsIFile> lf;
rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(lf));
if (NS_FAILED(rv)) return false;
rv = lf->SetPersistentDescriptor(buf);
rv = NS_NewLocalFileWithPersistentDescriptor(buf, getter_AddRefs(lf));
if (NS_FAILED(rv)) return false;
bool eq;
@@ -3454,10 +3452,7 @@ static bool CheckCompatibility(nsIFile* aProfileDir, const nsCString& aVersion,
rv = parser.GetString("Compatibility", "LastAppDir", buf);
if (NS_FAILED(rv)) return false;
rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(lf));
if (NS_FAILED(rv)) return false;
rv = lf->SetPersistentDescriptor(buf);
rv = NS_NewLocalFileWithPersistentDescriptor(buf, getter_AddRefs(lf));
if (NS_FAILED(rv)) return false;
rv = lf->Equals(aAppDir, &eq);

View File

@@ -49,6 +49,7 @@
#include "mozilla/Preferences.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Try.h"
#include "mozilla/XREAppData.h"
#include "nsPrintfCString.h"
@@ -962,7 +963,7 @@ nsresult nsXREDirProvider::GetUpdateRootDir(nsIFile** aResult,
}
nsAutoString updatePathStr;
updatePathStr.Assign(updatePath.get());
updRoot->InitWithPath(updatePathStr);
MOZ_TRY(updRoot->InitWithPath(updatePathStr));
updRoot.forget(aResult);
return NS_OK;
#else
@@ -1028,7 +1029,6 @@ nsresult nsXREDirProvider::SetUserDataProfileDirectory(nsCOMPtr<nsIFile>& aFile,
nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
bool aLocal) {
// Copied from nsAppFileLocationProvider (more or less)
nsresult rv;
nsCOMPtr<nsIFile> localDir;
if (aLocal && gDataDirHomeLocal) {
@@ -1053,18 +1053,12 @@ nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
OSErr err = ::FSFindFolder(kUserDomain, folderType, kCreateFolder, &fsRef);
NS_ENSURE_FALSE(err, NS_ERROR_FAILURE);
rv = NS_NewNativeLocalFile(""_ns, getter_AddRefs(localDir));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsILocalFileMac> dirFileMac = do_QueryInterface(localDir);
NS_ENSURE_TRUE(dirFileMac, NS_ERROR_UNEXPECTED);
rv = dirFileMac->InitWithFSRef(&fsRef);
NS_ENSURE_SUCCESS(rv, rv);
localDir = dirFileMac;
nsCOMPtr<nsILocalFileMac> dirFileMac;
MOZ_TRY(NS_NewLocalFileWithFSRef(&fsRef, getter_AddRefs(dirFileMac)));
localDir = dirFileMac.forget();
#elif defined(XP_IOS)
nsAutoCString userDir;
nsresult rv;
if (GetUIKitDirectory(aLocal, userDir)) {
rv = NS_NewNativeLocalFile(userDir, getter_AddRefs(localDir));
} else {
@@ -1072,6 +1066,7 @@ nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
}
NS_ENSURE_SUCCESS(rv, rv);
#elif defined(XP_WIN)
nsresult rv;
nsString path;
if (aLocal) {
rv = GetShellFolderPath(FOLDERID_LocalAppData, path);
@@ -1085,7 +1080,7 @@ nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
}
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_NewLocalFile(path, getter_AddRefs(localDir));
MOZ_TRY(NS_NewLocalFile(path, getter_AddRefs(localDir)));
#elif defined(XP_UNIX)
const char* homeDir = getenv("HOME");
if (!homeDir || !*homeDir) return NS_ERROR_FAILURE;
@@ -1098,23 +1093,23 @@ nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
// If $XDG_CACHE_HOME is defined use it, otherwise use $HOME/.cache.
const char* cacheHome = getenv("XDG_CACHE_HOME");
if (cacheHome && *cacheHome) {
rv = NS_NewNativeLocalFile(nsDependentCString(cacheHome),
getter_AddRefs(localDir));
MOZ_TRY(NS_NewNativeLocalFile(nsDependentCString(cacheHome),
getter_AddRefs(localDir)));
} else {
rv = NS_NewNativeLocalFile(nsDependentCString(homeDir),
getter_AddRefs(localDir));
if (NS_SUCCEEDED(rv)) rv = localDir->AppendNative(".cache"_ns);
MOZ_TRY(NS_NewNativeLocalFile(nsDependentCString(homeDir),
getter_AddRefs(localDir)));
MOZ_TRY(localDir->AppendNative(".cache"_ns));
}
} else {
rv = NS_NewNativeLocalFile(nsDependentCString(homeDir),
getter_AddRefs(localDir));
MOZ_TRY(NS_NewNativeLocalFile(nsDependentCString(homeDir),
getter_AddRefs(localDir)));
}
#else
# error "Don't know how to get product dir on your platform"
#endif
NS_IF_ADDREF(*aFile = localDir);
return rv;
localDir.forget(aFile);
return NS_OK;
}
nsresult nsXREDirProvider::GetSysUserExtensionsDirectory(nsIFile** aFile) {

View File

@@ -136,18 +136,18 @@ class ScopedXPCOM final : public nsIDirectoryServiceProvider2 {
}
already_AddRefed<nsIFile> GetGREDirectory() {
if (mGRED) {
nsCOMPtr<nsIFile> copy = mGRED;
return copy.forget();
if (!mGRED) {
char* env = PR_GetEnv("MOZ_XRE_DIR");
if (!env) {
return nullptr;
}
nsCOMPtr<nsIFile> greD;
if (char* env = PR_GetEnv("MOZ_XRE_DIR")) {
NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), getter_AddRefs(greD));
nsresult rv =
NS_NewNativeLocalFile(nsDependentCString(env), getter_AddRefs(mGRED));
NS_ENSURE_SUCCESS(rv, nullptr);
}
mGRED = greD;
return greD.forget();
return do_AddRef(mGRED);
}
already_AddRefed<nsIFile> GetGREBinDirectory() {

View File

@@ -4424,21 +4424,15 @@ static CFTypeRefPtr<CFURLRef> GetPasteLocation(NSPasteboard* aPasteboard) {
[UTIHelper
stringFromPboardType:
(NSString*)kPasteboardTypeFileURLPromise]]) {
nsCOMPtr<nsIFile> targFile;
NS_NewLocalFile(u""_ns, getter_AddRefs(targFile));
nsCOMPtr<nsILocalFileMac> macLocalFile = do_QueryInterface(targFile);
if (!macLocalFile) {
NS_ERROR("No Mac local file");
continue;
}
CFTypeRefPtr<CFURLRef> url = GetPasteLocation(aPasteboard);
if (!url) {
continue;
}
if (!NS_SUCCEEDED(macLocalFile->InitWithCFURL(url.get()))) {
NS_ERROR("failed InitWithCFURL");
nsCOMPtr<nsILocalFileMac> macLocalFile;
if (NS_FAILED(NS_NewLocalFileWithCFURL(url.get(),
getter_AddRefs(macLocalFile)))) {
NS_ERROR("failed NS_NewLocalFileWithCFURL");
continue;
}

View File

@@ -419,12 +419,10 @@ nsIFilePicker::ResultCode nsFilePicker::GetLocalFiles(
continue;
}
nsCOMPtr<nsIFile> localFile;
NS_NewLocalFile(u""_ns, getter_AddRefs(localFile));
nsCOMPtr<nsILocalFileMac> macLocalFile = do_QueryInterface(localFile);
if (macLocalFile &&
NS_SUCCEEDED(macLocalFile->InitWithCFURL((CFURLRef)url))) {
outFiles.AppendObject(localFile);
nsCOMPtr<nsILocalFileMac> macLocalFile;
if (NS_SUCCEEDED(NS_NewLocalFileWithCFURL((CFURLRef)url,
getter_AddRefs(macLocalFile)))) {
outFiles.AppendObject(macLocalFile);
}
}
@@ -478,13 +476,10 @@ nsIFilePicker::ResultCode nsFilePicker::GetLocalFolder(nsIFile** outFile) {
// get the path for the folder (we allow just 1, so that's all we get)
NSURL* theURL = [[thePanel URLs] objectAtIndex:0];
if (theURL) {
nsCOMPtr<nsIFile> localFile;
NS_NewLocalFile(u""_ns, getter_AddRefs(localFile));
nsCOMPtr<nsILocalFileMac> macLocalFile = do_QueryInterface(localFile);
if (macLocalFile &&
NS_SUCCEEDED(macLocalFile->InitWithCFURL((CFURLRef)theURL))) {
*outFile = localFile;
NS_ADDREF(*outFile);
nsCOMPtr<nsILocalFileMac> macLocalFile;
if (NS_SUCCEEDED(NS_NewLocalFileWithCFURL((CFURLRef)theURL,
getter_AddRefs(macLocalFile)))) {
macLocalFile.forget(outFile);
retVal = returnOK;
}
}
@@ -571,13 +566,10 @@ nsIFilePicker::ResultCode nsFilePicker::PutLocalFile(nsIFile** outFile) {
NSURL* fileURL = [thePanel URL];
if (fileURL) {
nsCOMPtr<nsIFile> localFile;
NS_NewLocalFile(u""_ns, getter_AddRefs(localFile));
nsCOMPtr<nsILocalFileMac> macLocalFile = do_QueryInterface(localFile);
if (macLocalFile &&
NS_SUCCEEDED(macLocalFile->InitWithCFURL((CFURLRef)fileURL))) {
*outFile = localFile;
NS_ADDREF(*outFile);
nsCOMPtr<nsILocalFileMac> macLocalFile;
if (NS_SUCCEEDED(NS_NewLocalFileWithCFURL((CFURLRef)fileURL,
getter_AddRefs(macLocalFile)))) {
macLocalFile.forget(outFile);
// We tell if we are replacing or not by just looking to see if the file
// exists. The user could not have hit OK and not meant to replace the
// file.

View File

@@ -682,10 +682,13 @@ bool MPRISServiceHandler::RenewLocalImageFile(const char* aImageData,
MOZ_ASSERT(mLocalImageFile);
nsCOMPtr<nsIOutputStream> out;
nsresult rv =
NS_NewLocalFileOutputStream(getter_AddRefs(out), mLocalImageFile,
PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
uint32_t written;
nsresult rv = out->Write(aImageData, aDataSize, &written);
if (NS_SUCCEEDED(rv)) {
rv = out->Write(aImageData, aDataSize, &written);
}
if (NS_FAILED(rv) || written != aDataSize) {
LOGMPRIS("Failed to write an image file");
RemoveAllLocalImages();

View File

@@ -180,6 +180,7 @@
#include "mozilla/Telemetry.h"
#include "mozilla/ThreadLocal.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Unused.h"
#include "nsCycleCollectionNoteRootCallback.h"
#include "nsCycleCollectionParticipant.h"
#include "nsCycleCollector.h"
@@ -1621,7 +1622,8 @@ class nsCycleCollectorLogSinkToFile final : public nsICycleCollectorLogSink {
// wouldn't work.
nsIFile* logFile = nullptr;
if (char* env = PR_GetEnv("MOZ_CC_LOG_DIRECTORY")) {
NS_NewNativeLocalFile(nsCString(env), &logFile);
Unused << NS_WARN_IF(
NS_FAILED(NS_NewNativeLocalFile(nsCString(env), &logFile)));
}
// On Android or B2G, this function will open a file named

View File

@@ -410,7 +410,8 @@ nsresult nsDumpUtils::OpenTempFile(const nsACString& aFilename, nsIFile** aFile,
// rather than the temp directory which is not.
if (!*aFile) {
if (char* env = PR_GetEnv("DOWNLOADS_DIRECTORY")) {
NS_NewNativeLocalFile(nsCString(env), aFile);
Unused << NS_WARN_IF(
NS_FAILED(NS_NewNativeLocalFile(nsCString(env), aFile)));
}
}
#endif

View File

@@ -122,8 +122,7 @@ bool nsMacUtilsImpl::GetAppPath(nsCString& aAppPath) {
}
nsCOMPtr<nsIFile> app;
nsresult rv =
NS_NewLocalFile(NS_ConvertUTF8toUTF16(appPath), getter_AddRefs(app));
nsresult rv = NS_NewNativeLocalFile(appPath, getter_AddRefs(app));
if (NS_FAILED(rv)) {
return false;
}
@@ -324,7 +323,7 @@ static nsresult GetDirFromBundlePlist(const nsAString& aKey, nsIFile** aDir) {
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIFile> dir;
rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(dirPath), getter_AddRefs(dir));
rv = NS_NewNativeLocalFile(dirPath, getter_AddRefs(dir));
NS_ENSURE_SUCCESS(rv, rv);
rv = dir->Normalize();

View File

@@ -620,11 +620,6 @@ nsMemoryInfoDumper::DumpMemoryReportsToNamedFile(
return rv;
}
reportsFile->InitWithPath(aFilename);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
bool exists;
rv = reportsFile->Exists(&exists);
if (NS_WARN_IF(NS_FAILED(rv))) {

View File

@@ -301,15 +301,9 @@ class BinaryPath {
if (NS_FAILED(rv)) {
return rv;
}
# ifdef XP_WIN
rv = NS_NewLocalFile(nsDependentString(exePath), getter_AddRefs(lf));
# else
rv = NS_NewNativeLocalFile(nsDependentCString(exePath), getter_AddRefs(lf));
# endif
if (NS_FAILED(rv)) {
return rv;
}
NS_ADDREF(*aResult = lf);
MOZ_TRY(NS_NewPathStringLocalFile(DependentPathString(exePath),
getter_AddRefs(lf)));
lf.forget(aResult);
return NS_OK;
}
#endif

View File

@@ -206,9 +206,11 @@ void LateWriteObserver::Observe(
for (int i = 0; i < 20; ++i) {
finalName.AppendPrintf("%02x", sha1[i]);
}
RefPtr<nsLocalFile> file = new nsLocalFile(nameAux);
RefPtr<nsIFile> file;
if (NS_SUCCEEDED(NS_NewPathStringLocalFile(nameAux, getter_AddRefs(file)))) {
file->RenameTo(nullptr, finalName);
}
}
/******************************* Setup/Teardown *******************************/

View File

@@ -41,7 +41,7 @@ extern bool gXPCOMMainThreadEventsAreDoomed;
/**
* Initialises XPCOM. You must call one of the NS_InitXPCOM methods
* before proceeding to use xpcom. The one exception is that you may
* call NS_NewLocalFile to create a nsIFile.
* call the NS_NewLocalFile family of functions to create an nsIFile.
*
* @note Use <CODE>NS_NewLocalFile</CODE> or <CODE>NS_NewNativeLocalFile</CODE>
* to create the file object you supply as the bin directory path in this
@@ -131,28 +131,37 @@ XPCOM_API(nsresult) NS_GetComponentManager(nsIComponentManager** aResult);
*/
XPCOM_API(nsresult) NS_GetComponentRegistrar(nsIComponentRegistrar** aResult);
/**
* Public Method to create an instance of a nsIFile. This function
* may be called prior to NS_InitXPCOM.
*
* @param aPath
* A string which specifies a full file path to a
* location. Relative paths will be treated as an
* error (NS_ERROR_FILE_UNRECOGNIZED_PATH).
* |NS_NewNativeLocalFile|'s path must be in the
* filesystem charset.
* @param aResult Interface pointer to a new instance of an nsIFile
*
* @return NS_OK for success;
* other error codes indicate a failure.
*/
#ifdef __cplusplus
XPCOM_API(nsresult)
/**
* Public method to create an instance of a nsIFile. This function may be called
* prior to NS_InitXPCOM.
*
* @param aPath A string which specifies a full file path to a location.
* Relative paths will be treated as an error (NS_ERROR_FILE_UNRECOGNIZED_PATH).
* Path must be in UTF-16 encoding.
*
* @param aResult Interface pointer to a new instance of an nsIFile
*
* @return NS_OK for success; other error codes indicate a failure.
*/
XPCOM_API([[nodiscard]] nsresult)
NS_NewLocalFile(const nsAString& aPath, nsIFile** aResult);
XPCOM_API(nsresult)
/**
* Public method to create an instance of a nsIFile. This function may be called
* prior to NS_InitXPCOM.
*
* @param aPath A string which specifies a full file path to a location.
* Relative paths will be treated as an error (NS_ERROR_FILE_UNRECOGNIZED_PATH).
* Path must be in the system's code page encoding for Windows and native path
* encoding for all other platforms.
*
* @param aResult Interface pointer to a new instance of an nsIFile
*
* @return NS_OK for success; other error codes indicate a failure.
*/
XPCOM_API([[nodiscard]] nsresult)
NS_NewNativeLocalFile(const nsACString& aPath, nsIFile** aResult);
// Use NS_NewLocalFile if you already have a UTF-16 string.
@@ -162,6 +171,61 @@ class NS_ConvertUTF16toUTF8;
nsresult NS_NewNativeLocalFile(const NS_ConvertUTF16toUTF8& aPath,
nsIFile** aResult) = delete;
/**
* Public method to create an instance of a nsIFile. This function may be called
* prior to NS_InitXPCOM.
*
* @param aPath A string which specifies a full file path to a location.
* Relative paths will be treated as an error (NS_ERROR_FILE_UNRECOGNIZED_PATH).
* Path must be in UTF-8 encoding.
*
* @param aResult Interface pointer to a new instance of an nsIFile
*
* @return NS_OK for success; other error codes indicate a failure.
*/
XPCOM_API([[nodiscard]] nsresult)
NS_NewUTF8LocalFile(const nsACString& aPath, nsIFile** aResult);
/**
* Public method to create an instance of a nsIFile. This function may be called
* prior to NS_InitXPCOM.
*
* @param aFile File from which to construct the new file.
* @param aResult Interface pointer to a new instance of an nsIFile.
*
* @return NS_OK for success; other error codes indicate a failure.
*/
XPCOM_API([[nodiscard]] nsresult)
NS_NewLocalFileWithFile(nsIFile* aFile, nsIFile** aResult);
/**
* Public method to create an instance of a nsIFile. This function may be called
* prior to NS_InitXPCOM.
*
* @param aFromFile File to which the descriptor is relative.
* @param aRelativeDesc Relative descriptor string for file.
* @param aResult Interface pointer to a new instance of an nsIFile.
*
* @return NS_OK for success; other error codes indicate a failure.
*/
XPCOM_API([[nodiscard]] nsresult)
NS_NewLocalFileWithRelativeDescriptor(nsIFile* aFromFile,
const nsACString& aRelativeDesc,
nsIFile** aResult);
/**
* Public method to create an instance of a nsIFile. This function may be called
* prior to NS_InitXPCOM.
*
* @param aPersistentDescriptor Persistent descriptor string for file.
* @param aResult Interface pointer to a new instance of an nsIFile.
*
* @return NS_OK for success; other error codes indicate a failure.
*/
XPCOM_API([[nodiscard]] nsresult)
NS_NewLocalFileWithPersistentDescriptor(const nsACString& aPersistentDescriptor,
nsIFile** aResult);
#endif
/**

View File

@@ -704,12 +704,10 @@ nsresult GetOSXFolderType(short aDomain, OSType aFolderType,
nsresult rv = NS_ERROR_FAILURE;
if (aFolderType == kTemporaryFolderType) {
NS_NewLocalFile(u""_ns, aLocalFile);
nsCOMPtr<nsILocalFileMac> localMacFile(do_QueryInterface(*aLocalFile));
if (localMacFile) {
rv = localMacFile->InitWithCFURL(
CocoaFileUtils::GetTemporaryFolder().get());
}
nsCOMPtr<nsILocalFileMac> localMacFile;
rv = NS_NewLocalFileWithCFURL(CocoaFileUtils::GetTemporaryFolder().get(),
getter_AddRefs(localMacFile));
localMacFile.forget(aLocalFile);
return rv;
}
@@ -717,11 +715,9 @@ nsresult GetOSXFolderType(short aDomain, OSType aFolderType,
FSRef fsRef;
err = ::FSFindFolder(aDomain, aFolderType, kCreateFolder, &fsRef);
if (err == noErr) {
NS_NewLocalFile(u""_ns, aLocalFile);
nsCOMPtr<nsILocalFileMac> localMacFile(do_QueryInterface(*aLocalFile));
if (localMacFile) {
rv = localMacFile->InitWithFSRef(&fsRef);
}
nsCOMPtr<nsILocalFileMac> localMacFile;
rv = NS_NewLocalFileWithFSRef(&fsRef, getter_AddRefs(localMacFile));
localMacFile.forget(aLocalFile);
}
return rv;
}

View File

@@ -183,17 +183,14 @@ nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
nsCOMPtr<nsIFile> localDir;
#if defined(MOZ_WIDGET_COCOA)
NS_NewLocalFile(u""_ns, getter_AddRefs(localDir));
if (!localDir) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsILocalFileMac> localDirMac(do_QueryInterface(localDir));
rv = localDirMac->InitWithCFURL(
CocoaFileUtils::GetProductDirectory(aLocal).get());
nsCOMPtr<nsILocalFileMac> localDirMac;
rv = NS_NewLocalFileWithCFURL(
CocoaFileUtils::GetProductDirectory(aLocal).get(),
getter_AddRefs(localDirMac));
if (NS_FAILED(rv)) {
return rv;
}
localDir = localDirMac.forget();
#elif defined(XP_WIN)
nsCOMPtr<nsIProperties> directoryService =
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);

View File

@@ -13,10 +13,30 @@ struct PRLibrary;
#include "mozilla/Path.h"
#include "nsCOMPtr.h"
#include "nsStringFwd.h"
// Platform-native path string types.
namespace mozilla {
using PathString = nsTString<filesystem::Path::value_type>;
using PathSubstring = nsTSubstring<filesystem::Path::value_type>;
using PathString = nsTString<filesystem::Path::value_type>;
using AutoPathString = nsTAutoString<filesystem::Path::value_type>;
using DependentPathString = nsTDependentString<filesystem::Path::value_type>;
} // namespace mozilla
/**
* Public method to create an instance of a nsIFile. This function may be called
* prior to NS_InitXPCOM.
*
* @param aPath A string which specifies a full file path to a location.
* Relative paths will be treated as an error (NS_ERROR_FILE_UNRECOGNIZED_PATH).
* Path must be in the platform's native path encoding.
*
* @param aResult Interface pointer to a new instance of an nsIFile
*
* @return NS_OK for success; other error codes indicate a failure.
*/
XPCOM_API([[nodiscard]] nsresult)
NS_NewPathStringLocalFile(const mozilla::PathSubstring& aPath,
nsIFile** aResult);
%}
[ptr] native PRFileDescStar(PRFileDesc);
@@ -431,8 +451,10 @@ interface nsIFile : nsISupports
* initWithNativePath, the filePath must be in the native
* filesystem charset.
*/
[must_use]
void initWithPath(in AString filePath);
[noscript] void initWithNativePath(in ACString filePath);
[noscript, must_use]
void initWithNativePath(in ACString filePath);
/**
* initWithFile

View File

@@ -6,6 +6,7 @@
#include "nsLocalFile.h" // includes platform-specific headers
#include "mozilla/Try.h"
#include "nsString.h"
#include "nsCOMPtr.h"
#include "nsReadableUtils.h"
@@ -145,6 +146,30 @@ const char* const sExecutableExts[] = {
// clang-format on
};
nsresult NS_NewLocalFileWithFile(nsIFile* aFile, nsIFile** aResult) {
nsCOMPtr<nsIFile> file = new nsLocalFile();
MOZ_TRY(file->InitWithFile(aFile));
file.forget(aResult);
return NS_OK;
}
nsresult NS_NewLocalFileWithRelativeDescriptor(nsIFile* aFromFile,
const nsACString& aRelativeDesc,
nsIFile** aResult) {
nsCOMPtr<nsIFile> file = new nsLocalFile();
MOZ_TRY(file->SetRelativeDescriptor(aFromFile, aRelativeDesc));
file.forget(aResult);
return NS_OK;
}
nsresult NS_NewLocalFileWithPersistentDescriptor(
const nsACString& aPersistentDescriptor, nsIFile** aResult) {
nsCOMPtr<nsIFile> file = new nsLocalFile();
MOZ_TRY(file->SetPersistentDescriptor(aPersistentDescriptor));
file.forget(aResult);
return NS_OK;
}
#if !defined(MOZ_WIDGET_COCOA) && !defined(XP_WIN)
NS_IMETHODIMP
nsLocalFile::InitWithFile(nsIFile* aFile) {

View File

@@ -279,10 +279,6 @@ nsDirEnumeratorUnix::Close() {
nsLocalFile::nsLocalFile() : mCachedStat() {}
nsLocalFile::nsLocalFile(const nsACString& aFilePath) : mCachedStat() {
InitWithNativePath(aFilePath);
}
nsLocalFile::nsLocalFile(const nsLocalFile& aOther) : mPath(aOther.mPath) {}
#ifdef MOZ_WIDGET_COCOA
@@ -2431,6 +2427,16 @@ nsresult NS_NewNativeLocalFile(const nsACString& aPath, nsIFile** aResult) {
return NS_OK;
}
nsresult NS_NewUTF8LocalFile(const nsACString& aPath, nsIFile** aResult) {
static_assert(NS_IsNativeUTF8());
return NS_NewNativeLocalFile(aPath, aResult);
}
nsresult NS_NewPathStringLocalFile(const PathSubstring& aPath,
nsIFile** aResult) {
return NS_NewNativeLocalFile(aPath, aResult);
}
//-----------------------------------------------------------------------------
// unicode support
//-----------------------------------------------------------------------------

View File

@@ -45,7 +45,6 @@ class nsLocalFile final
NS_DEFINE_STATIC_CID_ACCESSOR(NS_LOCAL_FILE_CID)
nsLocalFile();
explicit nsLocalFile(const nsACString& aFilePath);
static nsresult nsLocalFileConstructor(const nsIID& aIID,
void** aInstancePtr);

View File

@@ -879,11 +879,6 @@ NS_IMPL_ISUPPORTS_INHERITED(nsDirEnumerator, nsSimpleEnumerator,
nsLocalFile::nsLocalFile()
: mDirty(true), mResolveDirty(true), mUseDOSDevicePathSyntax(false) {}
nsLocalFile::nsLocalFile(const nsAString& aFilePath)
: mUseDOSDevicePathSyntax(false) {
InitWithPath(aFilePath);
}
nsresult nsLocalFile::nsLocalFileConstructor(const nsIID& aIID,
void** aInstancePtr) {
if (NS_WARN_IF(!aInstancePtr)) {
@@ -2152,22 +2147,17 @@ nsresult nsLocalFile::CopyMove(nsIFile* aParentDir, const nsAString& aNewName,
MakeDirty();
nsAutoString newParentPath;
newParentDir->GetPath(newParentPath);
if (newParentPath.IsEmpty()) {
return NS_ERROR_FAILURE;
}
MOZ_ALWAYS_SUCCEEDS(newParentDir->GetPath(newParentPath));
nsAutoString newLeafName;
if (aNewName.IsEmpty()) {
nsAutoString aFileName;
GetLeafName(aFileName);
InitWithPath(newParentPath);
Append(aFileName);
MOZ_ALWAYS_SUCCEEDS(GetLeafName(newLeafName));
} else {
InitWithPath(newParentPath);
Append(aNewName);
newLeafName = aNewName;
}
MOZ_ALWAYS_SUCCEEDS(InitWithPath(newParentPath));
MOZ_ALWAYS_SUCCEEDS(Append(newLeafName));
}
return NS_OK;
@@ -3462,6 +3452,15 @@ nsresult NS_NewLocalFile(const nsAString& aPath, nsIFile** aResult) {
return NS_OK;
}
nsresult NS_NewUTF8LocalFile(const nsACString& aPath, nsIFile** aResult) {
return NS_NewLocalFile(NS_ConvertUTF8toUTF16(aPath), aResult);
}
nsresult NS_NewPathStringLocalFile(const PathSubstring& aPath,
nsIFile** aResult) {
return NS_NewLocalFile(aPath, aResult);
}
//-----------------------------------------------------------------------------
// Native (lossy) interface
//-----------------------------------------------------------------------------

View File

@@ -28,7 +28,6 @@ class nsLocalFile final : public nsILocalFileWin {
NS_DEFINE_STATIC_CID_ACCESSOR(NS_LOCAL_FILE_CID)
nsLocalFile();
explicit nsLocalFile(const nsAString& aFilePath);
static nsresult nsLocalFileConstructor(const nsIID& aIID,
void** aInstancePtr);

View File

@@ -150,18 +150,18 @@ class ScopedXPCOM : public nsIDirectoryServiceProvider2 {
}
already_AddRefed<nsIFile> GetGREDirectory() {
if (mGRED) {
nsCOMPtr<nsIFile> copy = mGRED;
return copy.forget();
if (!mGRED) {
char* env = PR_GetEnv("MOZ_XRE_DIR");
if (!env) {
return nullptr;
}
nsCOMPtr<nsIFile> greD;
if (char* env = PR_GetEnv("MOZ_XRE_DIR")) {
NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), getter_AddRefs(greD));
nsresult rv =
NS_NewNativeLocalFile(nsDependentCString(env), getter_AddRefs(mGRED));
NS_ENSURE_SUCCESS(rv, nullptr);
}
mGRED = greD;
return greD.forget();
return do_AddRef(mGRED);
}
already_AddRefed<nsIFile> GetGREBinDirectory() {

View File

@@ -10,7 +10,6 @@
#ifdef XP_WIN
# include "nsILocalFileWin.h"
#endif
#include "nsLocalFile.h"
#include "nsComponentManagerUtils.h"
#include "nsString.h"
#include "nsDirectoryServiceDefs.h"
@@ -45,9 +44,9 @@ static void SetUseDOSDevicePathSyntax(nsIFile* aFile) {
static already_AddRefed<nsIFile> NewFile(nsIFile* aBase) {
nsresult rv;
nsCOMPtr<nsIFile> file = new nsLocalFile();
rv = file->InitWithFile(aBase);
VerifyResult(rv, "InitWithFile");
nsCOMPtr<nsIFile> file;
rv = NS_NewLocalFileWithFile(aBase, getter_AddRefs(file));
VerifyResult(rv, "NS_NewLocalFileWithFile");
#ifdef XP_WIN
SetUseDOSDevicePathSyntax(file);

View File

@@ -162,13 +162,17 @@ TEST(TestFilePreferencesUnix, Simple)
#if defined(XP_UNIX) && !defined(ANDROID)
nsAutoCString homePath;
NS_GetSpecialDirectory(NS_OS_HOME_DIR, getter_AddRefs(newPath));
newPath->GetNativePath(homePath);
rv = NS_GetSpecialDirectory(NS_OS_HOME_DIR, getter_AddRefs(newPath));
ASSERT_EQ(rv, NS_OK);
rv = newPath->GetNativePath(homePath);
ASSERT_EQ(rv, NS_OK);
newPath->InitWithNativePath("~"_ns);
rv = newPath->InitWithNativePath("~"_ns);
ASSERT_EQ(rv, NS_OK);
ASSERT_TRUE(newPath->NativePath().Equals(homePath));
newPath->InitWithNativePath("~/foo"_ns);
rv = newPath->InitWithNativePath("~/foo"_ns);
ASSERT_EQ(rv, NS_OK);
ASSERT_TRUE(newPath->NativePath().Equals(homePath + "/foo"_ns));
nsLiteralCString homeBase =
@@ -178,10 +182,12 @@ TEST(TestFilePreferencesUnix, Simple)
"/home"_ns;
# endif
newPath->InitWithNativePath("~foo"_ns);
rv = newPath->InitWithNativePath("~foo"_ns);
ASSERT_EQ(rv, NS_OK);
ASSERT_TRUE(newPath->NativePath().Equals(homeBase + "/foo"_ns));
newPath->InitWithNativePath("~foo/bar"_ns);
rv = newPath->InitWithNativePath("~foo/bar"_ns);
ASSERT_EQ(rv, NS_OK);
ASSERT_TRUE(newPath->NativePath().Equals(homeBase + "/foo/bar"_ns));
#endif