Skip to content

Commit

Permalink
Bug 1874226 - Use unique_ptr not Scoped in xpcom/*. r=xpcom-reviewers…
Browse files Browse the repository at this point in the history
…,necko-reviewers,valentin,emilio

Differential Revision: https://phabricator.services.mozilla.com/D200250
  • Loading branch information
kdashg committed Feb 6, 2024
1 parent 3462371 commit c9e48e7
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 106 deletions.
4 changes: 2 additions & 2 deletions dom/xhr/XMLHttpRequestMainThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions js/xpconnect/loader/AutoMemMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -151,7 +151,7 @@ void AutoMemMap::reset() {
handle_ = nullptr;
}
#endif
fd.dispose();
fd = nullptr;
}

} // namespace loader
Expand Down
5 changes: 3 additions & 2 deletions js/xpconnect/loader/ScriptPreloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions js/xpconnect/loader/URLPreloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
15 changes: 8 additions & 7 deletions modules/libjar/nsZipArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions netwerk/protocol/res/ExtensionProtocolHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
5 changes: 3 additions & 2 deletions startupcache/StartupCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
13 changes: 9 additions & 4 deletions toolkit/components/backgroundhangmonitor/HangDetails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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
Expand Down Expand Up @@ -574,8 +575,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);
}
Expand Down Expand Up @@ -648,9 +651,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);
}
Expand Down
2 changes: 1 addition & 1 deletion toolkit/xre/nsAppRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion widget/LSBUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions widget/gtk/nsSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,15 @@ 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;
}

// 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;
}
Expand Down
13 changes: 7 additions & 6 deletions xpcom/base/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 6 additions & 3 deletions xpcom/build/FileLocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "nsZipArchive.h"
#include "nsURLHelper.h"

#include "mozilla/UniquePtrExtensions.h"

namespace mozilla {

FileLocation::FileLocation() = default;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}

Expand All @@ -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();
Expand Down
43 changes: 9 additions & 34 deletions xpcom/glue/FileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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
Expand Down
Loading

0 comments on commit c9e48e7

Please sign in to comment.