Bug 1874226 - Use unique_ptr not Scoped in xpcom/*. r=xpcom-reviewers,necko-reviewers,valentin,emilio

Differential Revision: https://phabricator.services.mozilla.com/D200250
This commit is contained in:
Kelsey Gilbert 2024-02-07 18:30:22 +00:00
parent b2be0b2dcd
commit 34cdfc4f86
17 changed files with 75 additions and 106 deletions

View File

@ -4175,12 +4175,12 @@ nsresult ArrayBufferBuilder::MapToFileInPackage(const nsCString& aFile,
uint32_t offset = zip->GetDataOffset(zipItem);
uint32_t size = zipItem->RealSize();
mozilla::AutoFDClose pr_fd;
rv = aJarFile->OpenNSPRFileDesc(PR_RDONLY, 0, &pr_fd.rwget());
rv = aJarFile->OpenNSPRFileDesc(PR_RDONLY, 0, getter_Transfers(pr_fd));
if (NS_FAILED(rv)) {
return rv;
}
mMapPtr = JS::CreateMappedArrayBufferContents(
PR_FileDesc2NativeHandle(pr_fd), offset, size);
PR_FileDesc2NativeHandle(pr_fd.get()), offset, size);
if (mMapPtr) {
mLength = size;
return NS_OK;

View File

@ -34,7 +34,7 @@ Result<Ok, nsresult> AutoMemMap::init(nsIFile* file, int flags, int mode,
PRFileMapProtect prot) {
MOZ_ASSERT(!fd);
MOZ_TRY(file->OpenNSPRFileDesc(flags, mode, &fd.rwget()));
MOZ_TRY(file->OpenNSPRFileDesc(flags, mode, getter_Transfers(fd)));
return initInternal(prot);
}
@ -48,7 +48,7 @@ Result<Ok, nsresult> AutoMemMap::init(const FileDescriptor& file,
auto handle = file.ClonePlatformHandle();
fd = PR_ImportFile(PROsfd(handle.get()));
fd.reset(PR_ImportFile(PROsfd(handle.get())));
if (!fd) {
return Err(NS_ERROR_FAILURE);
}
@ -79,7 +79,7 @@ Result<Ok, nsresult> AutoMemMap::initInternal(PRFileMapProtect prot,
size_ = fileInfo.size;
}
fileMap = PR_CreateFileMap(fd, 0, prot);
fileMap = PR_CreateFileMap(fd.get(), 0, prot);
if (!fileMap) {
return Err(NS_ERROR_FAILURE);
}
@ -151,7 +151,7 @@ void AutoMemMap::reset() {
handle_ = nullptr;
}
#endif
fd.dispose();
fd = nullptr;
}
} // namespace loader

View File

@ -712,9 +712,10 @@ Result<Ok, nsresult> ScriptPreloader::WriteCache() {
}
{
AutoFDClose fd;
AutoFDClose raiiFd;
MOZ_TRY(cacheFile->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE, 0644,
&fd.rwget()));
getter_Transfers(raiiFd)));
const auto fd = raiiFd.get();
// We also need to hold mMonitor while we're touching scripts in
// mScripts, or they may be freed before we're done with them.

View File

@ -222,9 +222,10 @@ Result<Ok, nsresult> URLPreloader::WriteCache() {
}
{
AutoFDClose fd;
AutoFDClose raiiFd;
MOZ_TRY(cacheFile->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE, 0644,
&fd.rwget()));
getter_Transfers(raiiFd)));
const auto fd = raiiFd.get();
nsTArray<URLEntry*> entries;
for (const auto& entry : mCachedURLs.Values()) {

View File

@ -190,13 +190,13 @@ nsresult nsZipHandle::Init(nsIFile* file, nsZipHandle** ret, PRFileDesc** aFd) {
flags |= nsIFile::OS_READAHEAD;
#endif
LOG(("ZipHandle::Init %s", file->HumanReadablePath().get()));
nsresult rv = file->OpenNSPRFileDesc(flags, 0000, &fd.rwget());
nsresult rv = file->OpenNSPRFileDesc(flags, 0000, getter_Transfers(fd));
if (NS_FAILED(rv)) return rv;
int64_t size = PR_Available64(fd);
int64_t size = PR_Available64(fd.get());
if (size >= INT32_MAX) return NS_ERROR_FILE_TOO_BIG;
PRFileMap* map = PR_CreateFileMap(fd, size, PR_PROT_READONLY);
PRFileMap* map = PR_CreateFileMap(fd.get(), size, PR_PROT_READONLY);
if (!map) return NS_ERROR_FAILURE;
uint8_t* buf = (uint8_t*)PR_MemMap(map, 0, (uint32_t)size);
@ -216,10 +216,10 @@ nsresult nsZipHandle::Init(nsIFile* file, nsZipHandle** ret, PRFileDesc** aFd) {
#if defined(XP_WIN)
if (aFd) {
*aFd = fd.forget();
*aFd = fd.release();
}
#else
handle->mNSPRFileDesc = fd.forget();
handle->mNSPRFileDesc = std::move(fd);
#endif
handle->mFile.Init(file);
handle->mTotalLen = (uint32_t)size;
@ -344,7 +344,7 @@ nsresult nsZipHandle::GetNSPRFileDesc(PRFileDesc** aNSPRFileDesc) {
return NS_ERROR_ILLEGAL_VALUE;
}
*aNSPRFileDesc = mNSPRFileDesc;
*aNSPRFileDesc = mNSPRFileDesc.get();
if (!mNSPRFileDesc) {
return NS_ERROR_NOT_AVAILABLE;
}
@ -387,7 +387,8 @@ already_AddRefed<nsZipArchive> nsZipArchive::OpenArchive(nsIFile* aFile) {
RefPtr<nsZipHandle> handle;
#if defined(XP_WIN)
mozilla::AutoFDClose fd;
nsresult rv = nsZipHandle::Init(aFile, getter_AddRefs(handle), &fd.rwget());
nsresult rv =
nsZipHandle::Init(aFile, getter_AddRefs(handle), getter_Transfers(fd));
#else
nsresult rv = nsZipHandle::Init(aFile, getter_AddRefs(handle));
#endif

View File

@ -186,15 +186,16 @@ class ExtensionJARFileOpener final : public nsISupports {
MOZ_ASSERT(winFile);
if (NS_SUCCEEDED(rv)) {
rv = winFile->OpenNSPRFileDescShareDelete(PR_RDONLY, 0,
&prFileDesc.rwget());
getter_Transfers(prFileDesc));
}
#else
nsresult rv = mFile->OpenNSPRFileDesc(PR_RDONLY, 0, &prFileDesc.rwget());
nsresult rv =
mFile->OpenNSPRFileDesc(PR_RDONLY, 0, getter_Transfers(prFileDesc));
#endif /* XP_WIN */
if (NS_SUCCEEDED(rv)) {
mFD = FileDescriptor(FileDescriptor::PlatformHandleType(
PR_FileDesc2NativeHandle(prFileDesc)));
PR_FileDesc2NativeHandle(prFileDesc.get())));
}
nsCOMPtr<nsIRunnable> event =

View File

@ -522,9 +522,10 @@ Result<Ok, nsresult> StartupCache::WriteToDisk() {
return Err(NS_ERROR_UNEXPECTED);
}
AutoFDClose fd;
AutoFDClose raiiFd;
MOZ_TRY(mFile->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
0644, &fd.rwget()));
0644, getter_Transfers(raiiFd)));
const auto fd = raiiFd.get();
nsTArray<StartupCacheEntry::KeyValuePair> entries(mTable.count());
for (auto iter = mTable.iter(); !iter.done(); iter.next()) {

View File

@ -10,6 +10,7 @@
#include "nsPrintfCString.h"
#include "js/Array.h" // JS::NewArrayObject
#include "js/PropertyAndElement.h" // JS_DefineElement
#include "mozilla/FileUtils.h"
#include "mozilla/gfx/GPUParent.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/ContentParent.h" // For RemoteTypePrefix
@ -576,8 +577,10 @@ Result<Ok, nsresult> ReadEntry(PRFileDesc* aFile, HangStack& aStack) {
}
Result<HangDetails, nsresult> ReadHangDetailsFromFile(nsIFile* aFile) {
AutoFDClose fd;
nsresult rv = aFile->OpenNSPRFileDesc(PR_RDONLY, 0644, &fd.rwget());
AutoFDClose raiiFd;
nsresult rv =
aFile->OpenNSPRFileDesc(PR_RDONLY, 0644, getter_Transfers(raiiFd));
const auto fd = raiiFd.get();
if (NS_FAILED(rv)) {
return Err(rv);
}
@ -650,9 +653,11 @@ Result<Ok, nsresult> WriteHangDetailsToFile(HangDetails& aDetails,
return Err(NS_ERROR_INVALID_POINTER);
}
AutoFDClose fd;
AutoFDClose raiiFd;
nsresult rv = aFile->OpenNSPRFileDesc(
PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0644, &fd.rwget());
PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0644, getter_Transfers(raiiFd));
const auto fd = raiiFd.get();
if (NS_FAILED(rv)) {
return Err(rv);
}

View File

@ -4553,7 +4553,7 @@ bool XREMain::CheckLastStartupWasCrash() {
// the startup crash detection window.
AutoFDClose fd;
Unused << crashFile.inspect()->OpenNSPRFileDesc(
PR_WRONLY | PR_CREATE_FILE | PR_EXCL, 0666, &fd.rwget());
PR_WRONLY | PR_CREATE_FILE | PR_EXCL, 0666, getter_Transfers(fd));
return !fd;
}

View File

@ -122,7 +122,7 @@ bool GetLSBRelease(nsACString& aDistributor, nsACString& aDescription,
}
char dist[256], desc[256], release[256], codename[256];
if (fscanf(stream,
if (fscanf(stream.get(),
"Distributor ID:\t%255[^\n]\n"
"Description:\t%255[^\n]\n"
"Release:\t%255[^\n]\n"

View File

@ -268,7 +268,7 @@ NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader* aLoader,
mozilla::AutoFDClose fd;
rv = canberraFile->OpenNSPRFileDesc(PR_WRONLY, PR_IRUSR | PR_IWUSR,
&fd.rwget());
getter_Transfers(fd));
if (NS_FAILED(rv)) {
return rv;
}
@ -276,7 +276,7 @@ NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader* aLoader,
// XXX: Should we do this on another thread?
uint32_t length = dataLen;
while (length > 0) {
int32_t amount = PR_Write(fd, data, length);
int32_t amount = PR_Write(fd.get(), data, length);
if (amount < 0) {
return NS_ERROR_FAILURE;
}

View File

@ -176,14 +176,14 @@ bool LimitFileToLessThanSize(const char* aFilename, uint32_t aSize,
return false;
}
if (fseek(file, 0, SEEK_END)) {
if (fseek(file.get(), 0, SEEK_END)) {
// If we can't seek for some reason, better to just not limit the log at
// all and hope to sort out large logs upon further analysis.
return false;
}
// `ftell` returns a positive `long`, which might be more than 32 bits.
uint64_t fileSize = static_cast<uint64_t>(ftell(file));
uint64_t fileSize = static_cast<uint64_t>(ftell(file.get()));
if (fileSize <= aSize) {
return true;
@ -192,7 +192,7 @@ bool LimitFileToLessThanSize(const char* aFilename, uint32_t aSize,
uint64_t minBytesToDrop = fileSize - aSize;
uint64_t numBytesDropped = 0;
if (fseek(file, 0, SEEK_SET)) {
if (fseek(file.get(), 0, SEEK_SET)) {
// Same as above: if we can't seek, hope for the best.
return false;
}
@ -250,11 +250,12 @@ bool LimitFileToLessThanSize(const char* aFilename, uint32_t aSize,
// `fgets` always null terminates. If the line is too long, it won't
// include a trailing '\n' but will be null-terminated.
UniquePtr<char[]> line = MakeUnique<char[]>(aLongLineSize + 1);
while (fgets(line.get(), aLongLineSize + 1, file)) {
while (fgets(line.get(), aLongLineSize + 1, file.get())) {
if (numBytesDropped >= minBytesToDrop) {
if (fputs(line.get(), temp) < 0) {
if (fputs(line.get(), temp.get()) < 0) {
NS_WARNING(
nsPrintfCString("fputs failed: ferror %d\n", ferror(temp)).get());
nsPrintfCString("fputs failed: ferror %d\n", ferror(temp.get()))
.get());
failedToWrite = true;
break;
}

View File

@ -8,6 +8,8 @@
#include "nsZipArchive.h"
#include "nsURLHelper.h"
#include "mozilla/UniquePtrExtensions.h"
namespace mozilla {
FileLocation::FileLocation() = default;
@ -147,7 +149,8 @@ bool FileLocation::Equals(const FileLocation& aFile) const {
nsresult FileLocation::GetData(Data& aData) {
if (!IsZip()) {
return mBaseFile->OpenNSPRFileDesc(PR_RDONLY, 0444, &aData.mFd.rwget());
return mBaseFile->OpenNSPRFileDesc(PR_RDONLY, 0444,
getter_Transfers(aData.mFd));
}
aData.mZip = mBaseZip;
if (!aData.mZip) {
@ -166,7 +169,7 @@ nsresult FileLocation::GetData(Data& aData) {
nsresult FileLocation::Data::GetSize(uint32_t* aResult) {
if (mFd) {
PRFileInfo64 fileInfo;
if (PR_SUCCESS != PR_GetOpenFileInfo64(mFd, &fileInfo)) {
if (PR_SUCCESS != PR_GetOpenFileInfo64(mFd.get(), &fileInfo)) {
return NS_ErrorAccordingToNSPR();
}
@ -187,7 +190,7 @@ nsresult FileLocation::Data::GetSize(uint32_t* aResult) {
nsresult FileLocation::Data::Copy(char* aBuf, uint32_t aLen) {
if (mFd) {
for (uint32_t totalRead = 0; totalRead < aLen;) {
int32_t read = PR_Read(mFd, aBuf + totalRead,
int32_t read = PR_Read(mFd.get(), aBuf + totalRead,
XPCOM_MIN(aLen - totalRead, uint32_t(INT32_MAX)));
if (read < 0) {
return NS_ErrorAccordingToNSPR();

View File

@ -17,7 +17,7 @@
#include "prio.h"
#include "prlink.h"
#include "mozilla/Scoped.h"
#include <memory> // unique_ptr
#include "nsIFile.h"
#include <errno.h>
#include <limits.h>
@ -32,51 +32,26 @@ typedef int filedesc_t;
typedef const char* pathstr_t;
#endif
/**
* ScopedCloseFD is a RAII wrapper for POSIX file descriptors
*
* Instances |close()| their fds when they go out of scope.
*/
struct ScopedCloseFDTraits {
typedef int type;
static type empty() { return -1; }
static void release(type aFd) {
if (aFd != -1) {
close(aFd);
}
}
};
typedef Scoped<ScopedCloseFDTraits> ScopedClose;
#if defined(MOZILLA_INTERNAL_API)
/**
* AutoFDClose is a RAII wrapper for PRFileDesc.
*
* Instances |PR_Close| their fds when they go out of scope.
**/
struct ScopedClosePRFDTraits {
typedef PRFileDesc* type;
static type empty() { return nullptr; }
static void release(type aFd) {
struct PRCloseDeleter {
void operator()(PRFileDesc* aFd) {
if (aFd) {
PR_Close(aFd);
}
}
};
typedef Scoped<ScopedClosePRFDTraits> AutoFDClose;
using AutoFDClose = UniquePtr<PRFileDesc, PRCloseDeleter>;
/* RAII wrapper for FILE descriptors */
struct ScopedCloseFileTraits {
typedef FILE* type;
static type empty() { return nullptr; }
static void release(type aFile) {
if (aFile) {
fclose(aFile);
struct FCloseDeleter {
void operator()(FILE* p) {
if (p) {
fclose(p);
}
}
};
typedef Scoped<ScopedCloseFileTraits> ScopedCloseFile;
using ScopedCloseFile = UniquePtr<FILE, FCloseDeleter>;
/**
* Fallocate efficiently and continuously allocates files via fallocate-type

View File

@ -11,6 +11,7 @@
#include <stdio.h>
#include "mozilla/FileUtils.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Try.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/UniquePtrExtensions.h"
@ -166,18 +167,6 @@ inline FILE* TS_tfopen(const char* aPath, const char* aMode) {
}
#endif
/* RAII wrapper for FILE descriptors */
struct ScopedCloseFileTraits {
typedef FILE* type;
static type empty() { return nullptr; }
static void release(type aFile) {
if (aFile) {
fclose(aFile);
}
}
};
typedef Scoped<ScopedCloseFileTraits> ScopedCloseFile;
#if !defined(MOZ_LINKER) && !defined(__ANDROID__)
static void XPCOMGlueUnload() {
while (sTop) {
@ -275,8 +264,12 @@ static XPCOMGlueLoadResult XPCOMGlueLoad(
strcat(xpcomDir, ".gtest");
}
ScopedCloseFile flist;
flist = TS_tfopen(xpcomDir, READ_TEXTMODE);
const auto flist = TS_tfopen(xpcomDir, READ_TEXTMODE);
const auto cleanup = MakeScopeExit([&]() {
if (flist) {
fclose(flist);
}
});
if (!flist) {
return Err(AsVariant(NS_ERROR_FAILURE));
}

View File

@ -11,25 +11,10 @@
#include "base/process_util.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Unused.h"
#include "nsWindowsHelpers.h"
namespace {
// Scoped type used by HandleToFilename
struct ScopedMappedViewTraits {
typedef void* type;
static void* empty() { return nullptr; }
static void release(void* aPtr) {
if (aPtr) {
mozilla::Unused << UnmapViewOfFile(aPtr);
}
}
};
typedef mozilla::Scoped<ScopedMappedViewTraits> ScopedMappedView;
} // namespace
namespace mozilla {
bool HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset,
@ -44,11 +29,14 @@ bool HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset,
if (!fileMapping) {
return false;
}
ScopedMappedView view(MapViewOfFile(fileMapping, FILE_MAP_READ,
aOffset.HighPart, aOffset.LowPart, 1));
const auto view = MapViewOfFile(fileMapping, FILE_MAP_READ, aOffset.HighPart,
aOffset.LowPart, 1);
if (!view) {
return false;
}
const auto cleanup =
MakeScopeExit([&]() { mozilla::Unused << UnmapViewOfFile(view); });
nsAutoString mappedFilename;
DWORD len = 0;
SetLastError(ERROR_SUCCESS);

View File

@ -9,7 +9,6 @@
#include <windows.h>
#include "mozilla/Scoped.h"
#include "nsString.h"
namespace mozilla {