diff --git a/dom/xhr/XMLHttpRequestMainThread.cpp b/dom/xhr/XMLHttpRequestMainThread.cpp index b43305b236d2..dc98ca03f922 100644 --- a/dom/xhr/XMLHttpRequestMainThread.cpp +++ b/dom/xhr/XMLHttpRequestMainThread.cpp @@ -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; diff --git a/js/xpconnect/loader/AutoMemMap.cpp b/js/xpconnect/loader/AutoMemMap.cpp index 424a8b42dd16..7ce26a9a3373 100644 --- a/js/xpconnect/loader/AutoMemMap.cpp +++ b/js/xpconnect/loader/AutoMemMap.cpp @@ -34,7 +34,7 @@ Result 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 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 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 diff --git a/js/xpconnect/loader/ScriptPreloader.cpp b/js/xpconnect/loader/ScriptPreloader.cpp index 9520fb3b370c..11a71ac68ab8 100644 --- a/js/xpconnect/loader/ScriptPreloader.cpp +++ b/js/xpconnect/loader/ScriptPreloader.cpp @@ -712,9 +712,10 @@ Result 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. diff --git a/js/xpconnect/loader/URLPreloader.cpp b/js/xpconnect/loader/URLPreloader.cpp index 695f524d42a0..ad7354e5a0ff 100644 --- a/js/xpconnect/loader/URLPreloader.cpp +++ b/js/xpconnect/loader/URLPreloader.cpp @@ -222,9 +222,10 @@ Result 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 entries; for (const auto& entry : mCachedURLs.Values()) { diff --git a/modules/libjar/nsZipArchive.cpp b/modules/libjar/nsZipArchive.cpp index 9ab949c89561..e0ca373e493f 100644 --- a/modules/libjar/nsZipArchive.cpp +++ b/modules/libjar/nsZipArchive.cpp @@ -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::OpenArchive(nsIFile* aFile) { RefPtr 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 diff --git a/netwerk/protocol/res/ExtensionProtocolHandler.cpp b/netwerk/protocol/res/ExtensionProtocolHandler.cpp index 4f56578100cc..d3b1a3916452 100644 --- a/netwerk/protocol/res/ExtensionProtocolHandler.cpp +++ b/netwerk/protocol/res/ExtensionProtocolHandler.cpp @@ -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 event = diff --git a/startupcache/StartupCache.cpp b/startupcache/StartupCache.cpp index 02744d34d251..9c066a195123 100644 --- a/startupcache/StartupCache.cpp +++ b/startupcache/StartupCache.cpp @@ -522,9 +522,10 @@ Result 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 entries(mTable.count()); for (auto iter = mTable.iter(); !iter.done(); iter.next()) { diff --git a/toolkit/components/backgroundhangmonitor/HangDetails.cpp b/toolkit/components/backgroundhangmonitor/HangDetails.cpp index b52f753f0ceb..de6ee056f6f3 100644 --- a/toolkit/components/backgroundhangmonitor/HangDetails.cpp +++ b/toolkit/components/backgroundhangmonitor/HangDetails.cpp @@ -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 ReadEntry(PRFileDesc* aFile, HangStack& aStack) { } Result 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 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); } diff --git a/toolkit/xre/nsAppRunner.cpp b/toolkit/xre/nsAppRunner.cpp index 90827be7b17c..495f3bc2fd6b 100644 --- a/toolkit/xre/nsAppRunner.cpp +++ b/toolkit/xre/nsAppRunner.cpp @@ -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; } diff --git a/widget/LSBUtils.cpp b/widget/LSBUtils.cpp index 38a02b758b00..9ebffacd7ef8 100644 --- a/widget/LSBUtils.cpp +++ b/widget/LSBUtils.cpp @@ -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" diff --git a/widget/gtk/nsSound.cpp b/widget/gtk/nsSound.cpp index f25a9e9274d6..0530c7322410 100644 --- a/widget/gtk/nsSound.cpp +++ b/widget/gtk/nsSound.cpp @@ -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; } diff --git a/xpcom/base/Logging.cpp b/xpcom/base/Logging.cpp index fa1527346561..61899b77c763 100644 --- a/xpcom/base/Logging.cpp +++ b/xpcom/base/Logging.cpp @@ -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(ftell(file)); + uint64_t fileSize = static_cast(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 line = MakeUnique(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; } diff --git a/xpcom/build/FileLocation.cpp b/xpcom/build/FileLocation.cpp index bb090c2b94d0..5162546f1d49 100644 --- a/xpcom/build/FileLocation.cpp +++ b/xpcom/build/FileLocation.cpp @@ -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(); diff --git a/xpcom/glue/FileUtils.h b/xpcom/glue/FileUtils.h index 6b322fb83988..8e66ee5860ee 100644 --- a/xpcom/glue/FileUtils.h +++ b/xpcom/glue/FileUtils.h @@ -17,7 +17,7 @@ #include "prio.h" #include "prlink.h" -#include "mozilla/Scoped.h" +#include // unique_ptr #include "nsIFile.h" #include #include @@ -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 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 AutoFDClose; +using AutoFDClose = UniquePtr; /* 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 ScopedCloseFile; +using ScopedCloseFile = UniquePtr; /** * Fallocate efficiently and continuously allocates files via fallocate-type diff --git a/xpcom/glue/standalone/nsXPCOMGlue.cpp b/xpcom/glue/standalone/nsXPCOMGlue.cpp index dc0a905830c7..e8d48193e255 100644 --- a/xpcom/glue/standalone/nsXPCOMGlue.cpp +++ b/xpcom/glue/standalone/nsXPCOMGlue.cpp @@ -11,6 +11,7 @@ #include #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 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)); } diff --git a/xpcom/io/FileUtilsWin.cpp b/xpcom/io/FileUtilsWin.cpp index 9c0bc6faa2dc..5bf7e4c968ac 100644 --- a/xpcom/io/FileUtilsWin.cpp +++ b/xpcom/io/FileUtilsWin.cpp @@ -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 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); diff --git a/xpcom/io/FileUtilsWin.h b/xpcom/io/FileUtilsWin.h index 4a59820d083b..548aed6dd72a 100644 --- a/xpcom/io/FileUtilsWin.h +++ b/xpcom/io/FileUtilsWin.h @@ -9,7 +9,6 @@ #include -#include "mozilla/Scoped.h" #include "nsString.h" namespace mozilla {