Bug 1671367 - Add overloads of NS_NewLocal*FileStream functions returning a Result and use them. r=dom-workers-and-storage-reviewers,necko-reviewers,asuth
Differential Revision: https://phabricator.services.mozilla.com/D94138
This commit is contained in:
10
dom/cache/FileUtils.cpp
vendored
10
dom/cache/FileUtils.cpp
vendored
@@ -784,14 +784,12 @@ Result<int64_t, nsresult> LockedDirectoryPaddingGet(nsIFile& aBaseDir) {
|
||||
const auto& file,
|
||||
CloneFileAndAppend(aBaseDir, nsLiteralString(PADDING_FILE_NAME)));
|
||||
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
CACHE_TRY(NS_NewLocalFileInputStream(getter_AddRefs(stream), file));
|
||||
CACHE_TRY_UNWRAP(auto stream, NS_NewLocalFileInputStream(file));
|
||||
|
||||
nsCOMPtr<nsIInputStream> bufferedStream;
|
||||
CACHE_TRY(NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
|
||||
stream.forget(), 512));
|
||||
CACHE_TRY_INSPECT(const auto& bufferedStream,
|
||||
NS_NewBufferedInputStream(stream.forget(), 512));
|
||||
|
||||
nsCOMPtr<nsIObjectInputStream> objectStream =
|
||||
const nsCOMPtr<nsIObjectInputStream> objectStream =
|
||||
NS_NewObjectInputStream(bufferedStream);
|
||||
|
||||
CACHE_TRY_RETURN(
|
||||
|
||||
@@ -415,20 +415,20 @@ GetStructuredCloneReadInfoFromExternalBlob(uint64_t aIntData,
|
||||
const nsCOMPtr<nsIFile> nativeFile = file.FileInfo().GetFileForFileInfo();
|
||||
IDB_TRY(OkIf(nativeFile), Err(NS_ERROR_FAILURE));
|
||||
|
||||
// XXX NS_NewLocalFileInputStream does not follow the convention to place
|
||||
// its output parameter last (it has optional parameters which makes that
|
||||
// problematic), so we can't use ToResultInvoke, nor
|
||||
// IDB_TRY_UNWRAP/IDB_TRY_INSPECT.
|
||||
nsCOMPtr<nsIInputStream> fileInputStream;
|
||||
IDB_TRY(NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream),
|
||||
nativeFile));
|
||||
IDB_TRY_INSPECT(
|
||||
const auto& fileInputStream,
|
||||
NS_NewLocalFileInputStream(nativeFile)
|
||||
.andThen([aMaybeKey](auto fileInputStream)
|
||||
-> Result<nsCOMPtr<nsIInputStream>, nsresult> {
|
||||
if (aMaybeKey) {
|
||||
return nsCOMPtr<nsIInputStream>{MakeRefPtr<
|
||||
quota::DecryptingInputStream<IndexedDBCipherStrategy>>(
|
||||
WrapNotNull(std::move(fileInputStream)),
|
||||
kEncryptedStreamBlockSize, *aMaybeKey)};
|
||||
}
|
||||
|
||||
if (aMaybeKey) {
|
||||
fileInputStream =
|
||||
MakeRefPtr<quota::DecryptingInputStream<IndexedDBCipherStrategy>>(
|
||||
WrapNotNull(std::move(fileInputStream)),
|
||||
kEncryptedStreamBlockSize, *aMaybeKey);
|
||||
}
|
||||
return fileInputStream;
|
||||
}));
|
||||
|
||||
IDB_TRY(SnappyUncompressStructuredCloneData(*fileInputStream, data));
|
||||
}
|
||||
|
||||
@@ -1183,8 +1183,7 @@ nsresult UpdateUsageFile(nsIFile* aUsageFile, nsIFile* aUsageJournalFile,
|
||||
LS_TRY(aUsageJournalFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644));
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIOutputStream> stream;
|
||||
LS_TRY(NS_NewLocalFileOutputStream(getter_AddRefs(stream), aUsageFile));
|
||||
LS_TRY_INSPECT(const auto& stream, NS_NewLocalFileOutputStream(aUsageFile));
|
||||
|
||||
nsCOMPtr<nsIBinaryOutputStream> binaryStream =
|
||||
NS_NewObjectOutputStream(stream);
|
||||
@@ -1206,12 +1205,10 @@ Result<UsageInfo, nsresult> LoadUsageFile(nsIFile& aUsageFile) {
|
||||
|
||||
LS_TRY(OkIf(fileSize == kUsageFileSize), Err(NS_ERROR_FILE_CORRUPTED));
|
||||
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
LS_TRY(NS_NewLocalFileInputStream(getter_AddRefs(stream), &aUsageFile));
|
||||
LS_TRY_UNWRAP(auto stream, NS_NewLocalFileInputStream(&aUsageFile));
|
||||
|
||||
nsCOMPtr<nsIInputStream> bufferedStream;
|
||||
LS_TRY(NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
|
||||
stream.forget(), 16));
|
||||
LS_TRY_INSPECT(const auto& bufferedStream,
|
||||
NS_NewBufferedInputStream(stream.forget(), 16));
|
||||
|
||||
const nsCOMPtr<nsIBinaryInputStream> binaryStream =
|
||||
NS_NewObjectInputStream(bufferedStream);
|
||||
|
||||
@@ -2541,13 +2541,9 @@ Result<nsCOMPtr<nsIOutputStream>, nsresult> GetOutputStream(
|
||||
nsIFile& aFile, FileFlag aFileFlag) {
|
||||
AssertIsOnIOThread();
|
||||
|
||||
nsCOMPtr<nsIOutputStream> outputStream;
|
||||
switch (aFileFlag) {
|
||||
case kTruncateFileFlag: {
|
||||
QM_TRY(NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), &aFile));
|
||||
|
||||
break;
|
||||
}
|
||||
case kTruncateFileFlag:
|
||||
QM_TRY_RETURN(NS_NewLocalFileOutputStream(&aFile));
|
||||
|
||||
case kUpdateFileFlag: {
|
||||
QM_TRY_INSPECT(const bool& exists, MOZ_TO_RESULT_INVOKE(&aFile, Exists));
|
||||
@@ -2556,28 +2552,21 @@ Result<nsCOMPtr<nsIOutputStream>, nsresult> GetOutputStream(
|
||||
return nsCOMPtr<nsIOutputStream>();
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIFileStream> stream;
|
||||
QM_TRY(NS_NewLocalFileStream(getter_AddRefs(stream), &aFile));
|
||||
QM_TRY_INSPECT(const auto& stream, NS_NewLocalFileStream(&aFile));
|
||||
|
||||
outputStream = do_QueryInterface(stream);
|
||||
nsCOMPtr<nsIOutputStream> outputStream = do_QueryInterface(stream);
|
||||
QM_TRY(OkIf(outputStream), Err(NS_ERROR_FAILURE));
|
||||
|
||||
break;
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
case kAppendFileFlag: {
|
||||
QM_TRY(
|
||||
NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), &aFile,
|
||||
PR_WRONLY | PR_CREATE_FILE | PR_APPEND));
|
||||
|
||||
break;
|
||||
}
|
||||
case kAppendFileFlag:
|
||||
QM_TRY_RETURN(NS_NewLocalFileOutputStream(
|
||||
&aFile, PR_WRONLY | PR_CREATE_FILE | PR_APPEND));
|
||||
|
||||
default:
|
||||
MOZ_CRASH("Should never get here!");
|
||||
}
|
||||
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
Result<nsCOMPtr<nsIBinaryOutputStream>, nsresult> GetBinaryOutputStream(
|
||||
@@ -2718,12 +2707,10 @@ Result<nsCOMPtr<nsIBinaryInputStream>, nsresult> GetBinaryInputStream(
|
||||
|
||||
QM_TRY(file->Append(aFilename));
|
||||
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
QM_TRY(NS_NewLocalFileInputStream(getter_AddRefs(stream), file));
|
||||
QM_TRY_UNWRAP(auto stream, NS_NewLocalFileInputStream(file));
|
||||
|
||||
nsCOMPtr<nsIInputStream> bufferedStream;
|
||||
QM_TRY(NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
|
||||
stream.forget(), 512));
|
||||
QM_TRY_INSPECT(const auto& bufferedStream,
|
||||
NS_NewBufferedInputStream(stream.forget(), 512));
|
||||
|
||||
QM_TRY(OkIf(bufferedStream), Err(NS_ERROR_FAILURE));
|
||||
|
||||
|
||||
@@ -138,6 +138,18 @@ nsresult NS_NewLocalFileInputStream(nsIInputStream** result, nsIFile* file,
|
||||
return rv;
|
||||
}
|
||||
|
||||
Result<nsCOMPtr<nsIInputStream>, nsresult> NS_NewLocalFileInputStream(
|
||||
nsIFile* file, int32_t ioFlags /* = -1 */, int32_t perm /* = -1 */,
|
||||
int32_t behaviorFlags /* = 0 */) {
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
const nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file,
|
||||
ioFlags, perm, behaviorFlags);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
return stream;
|
||||
}
|
||||
return Err(rv);
|
||||
}
|
||||
|
||||
nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result, nsIFile* file,
|
||||
int32_t ioFlags /* = -1 */,
|
||||
int32_t perm /* = -1 */,
|
||||
@@ -152,6 +164,18 @@ nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result, nsIFile* file,
|
||||
return rv;
|
||||
}
|
||||
|
||||
Result<nsCOMPtr<nsIOutputStream>, nsresult> NS_NewLocalFileOutputStream(
|
||||
nsIFile* file, int32_t ioFlags /* = -1 */, int32_t perm /* = -1 */,
|
||||
int32_t behaviorFlags /* = 0 */) {
|
||||
nsCOMPtr<nsIOutputStream> stream;
|
||||
const nsresult rv = NS_NewLocalFileOutputStream(getter_AddRefs(stream), file,
|
||||
ioFlags, perm, behaviorFlags);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
return stream;
|
||||
}
|
||||
return Err(rv);
|
||||
}
|
||||
|
||||
nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result,
|
||||
const mozilla::ipc::FileDescriptor& fd) {
|
||||
nsCOMPtr<nsIFileOutputStream> out;
|
||||
@@ -1267,6 +1291,18 @@ nsresult NS_NewLocalFileStream(nsIFileStream** result, nsIFile* file,
|
||||
return rv;
|
||||
}
|
||||
|
||||
mozilla::Result<nsCOMPtr<nsIFileStream>, nsresult> NS_NewLocalFileStream(
|
||||
nsIFile* file, int32_t ioFlags /* = -1 */, int32_t perm /* = -1 */,
|
||||
int32_t behaviorFlags /* = 0 */) {
|
||||
nsCOMPtr<nsIFileStream> stream;
|
||||
const nsresult rv = NS_NewLocalFileStream(getter_AddRefs(stream), file,
|
||||
ioFlags, perm, behaviorFlags);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
return stream;
|
||||
}
|
||||
return Err(rv);
|
||||
}
|
||||
|
||||
nsresult NS_NewBufferedOutputStream(
|
||||
nsIOutputStream** aResult, already_AddRefed<nsIOutputStream> aOutputStream,
|
||||
uint32_t aBufferSize) {
|
||||
@@ -1303,6 +1339,17 @@ nsresult NS_NewBufferedOutputStream(
|
||||
return rv;
|
||||
}
|
||||
|
||||
Result<nsCOMPtr<nsIInputStream>, nsresult> NS_NewBufferedInputStream(
|
||||
already_AddRefed<nsIInputStream> aInputStream, uint32_t aBufferSize) {
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
const nsresult rv = NS_NewBufferedInputStream(
|
||||
getter_AddRefs(stream), std::move(aInputStream), aBufferSize);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
return stream;
|
||||
}
|
||||
return Err(rv);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
#define BUFFER_SIZE 8192
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <functional>
|
||||
#include "mozilla/Maybe.h"
|
||||
#include "mozilla/ResultExtensions.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsIInterfaceRequestor.h"
|
||||
#include "nsIInterfaceRequestorUtils.h"
|
||||
@@ -464,10 +465,18 @@ nsresult NS_NewLocalFileInputStream(nsIInputStream** result, nsIFile* file,
|
||||
int32_t ioFlags = -1, int32_t perm = -1,
|
||||
int32_t behaviorFlags = 0);
|
||||
|
||||
mozilla::Result<nsCOMPtr<nsIInputStream>, nsresult> NS_NewLocalFileInputStream(
|
||||
nsIFile* file, int32_t ioFlags = -1, int32_t perm = -1,
|
||||
int32_t behaviorFlags = 0);
|
||||
|
||||
nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result, nsIFile* file,
|
||||
int32_t ioFlags = -1, int32_t perm = -1,
|
||||
int32_t behaviorFlags = 0);
|
||||
|
||||
mozilla::Result<nsCOMPtr<nsIOutputStream>, nsresult>
|
||||
NS_NewLocalFileOutputStream(nsIFile* file, int32_t ioFlags = -1,
|
||||
int32_t perm = -1, int32_t behaviorFlags = 0);
|
||||
|
||||
nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result,
|
||||
const mozilla::ipc::FileDescriptor& fd);
|
||||
|
||||
@@ -486,10 +495,17 @@ nsresult NS_NewLocalFileStream(nsIFileStream** result, nsIFile* file,
|
||||
int32_t ioFlags = -1, int32_t perm = -1,
|
||||
int32_t behaviorFlags = 0);
|
||||
|
||||
mozilla::Result<nsCOMPtr<nsIFileStream>, nsresult> NS_NewLocalFileStream(
|
||||
nsIFile* file, int32_t ioFlags = -1, int32_t perm = -1,
|
||||
int32_t behaviorFlags = 0);
|
||||
|
||||
[[nodiscard]] nsresult NS_NewBufferedInputStream(
|
||||
nsIInputStream** aResult, already_AddRefed<nsIInputStream> aInputStream,
|
||||
uint32_t aBufferSize);
|
||||
|
||||
mozilla::Result<nsCOMPtr<nsIInputStream>, nsresult> NS_NewBufferedInputStream(
|
||||
already_AddRefed<nsIInputStream> aInputStream, uint32_t aBufferSize);
|
||||
|
||||
// note: the resulting stream can be QI'ed to nsISafeOutputStream iff the
|
||||
// provided stream supports it.
|
||||
nsresult NS_NewBufferedOutputStream(
|
||||
|
||||
@@ -781,9 +781,9 @@ Result<nsCOMPtr<nsIInputStream>, nsresult> ExtensionProtocolHandler::NewStream(
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIInputStream> inputStream;
|
||||
MOZ_TRY(NS_NewLocalFileInputStream(getter_AddRefs(inputStream), requestedFile,
|
||||
PR_RDONLY, -1,
|
||||
nsIFileInputStream::DEFER_OPEN));
|
||||
MOZ_TRY_VAR(inputStream,
|
||||
NS_NewLocalFileInputStream(requestedFile, PR_RDONLY, -1,
|
||||
nsIFileInputStream::DEFER_OPEN));
|
||||
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user