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:
Simon Giesecke
2020-11-17 19:30:05 +00:00
parent 3d8e212baf
commit 567587d2dd
7 changed files with 98 additions and 53 deletions

View File

@@ -784,14 +784,12 @@ Result<int64_t, nsresult> LockedDirectoryPaddingGet(nsIFile& aBaseDir) {
const auto& file, const auto& file,
CloneFileAndAppend(aBaseDir, nsLiteralString(PADDING_FILE_NAME))); CloneFileAndAppend(aBaseDir, nsLiteralString(PADDING_FILE_NAME)));
nsCOMPtr<nsIInputStream> stream; CACHE_TRY_UNWRAP(auto stream, NS_NewLocalFileInputStream(file));
CACHE_TRY(NS_NewLocalFileInputStream(getter_AddRefs(stream), file));
nsCOMPtr<nsIInputStream> bufferedStream; CACHE_TRY_INSPECT(const auto& bufferedStream,
CACHE_TRY(NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), NS_NewBufferedInputStream(stream.forget(), 512));
stream.forget(), 512));
nsCOMPtr<nsIObjectInputStream> objectStream = const nsCOMPtr<nsIObjectInputStream> objectStream =
NS_NewObjectInputStream(bufferedStream); NS_NewObjectInputStream(bufferedStream);
CACHE_TRY_RETURN( CACHE_TRY_RETURN(

View File

@@ -415,20 +415,20 @@ GetStructuredCloneReadInfoFromExternalBlob(uint64_t aIntData,
const nsCOMPtr<nsIFile> nativeFile = file.FileInfo().GetFileForFileInfo(); const nsCOMPtr<nsIFile> nativeFile = file.FileInfo().GetFileForFileInfo();
IDB_TRY(OkIf(nativeFile), Err(NS_ERROR_FAILURE)); IDB_TRY(OkIf(nativeFile), Err(NS_ERROR_FAILURE));
// XXX NS_NewLocalFileInputStream does not follow the convention to place IDB_TRY_INSPECT(
// its output parameter last (it has optional parameters which makes that const auto& fileInputStream,
// problematic), so we can't use ToResultInvoke, nor NS_NewLocalFileInputStream(nativeFile)
// IDB_TRY_UNWRAP/IDB_TRY_INSPECT. .andThen([aMaybeKey](auto fileInputStream)
nsCOMPtr<nsIInputStream> fileInputStream; -> Result<nsCOMPtr<nsIInputStream>, nsresult> {
IDB_TRY(NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), if (aMaybeKey) {
nativeFile)); return nsCOMPtr<nsIInputStream>{MakeRefPtr<
quota::DecryptingInputStream<IndexedDBCipherStrategy>>(
WrapNotNull(std::move(fileInputStream)),
kEncryptedStreamBlockSize, *aMaybeKey)};
}
if (aMaybeKey) { return fileInputStream;
fileInputStream = }));
MakeRefPtr<quota::DecryptingInputStream<IndexedDBCipherStrategy>>(
WrapNotNull(std::move(fileInputStream)),
kEncryptedStreamBlockSize, *aMaybeKey);
}
IDB_TRY(SnappyUncompressStructuredCloneData(*fileInputStream, data)); IDB_TRY(SnappyUncompressStructuredCloneData(*fileInputStream, data));
} }

View File

@@ -1183,8 +1183,7 @@ nsresult UpdateUsageFile(nsIFile* aUsageFile, nsIFile* aUsageJournalFile,
LS_TRY(aUsageJournalFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644)); LS_TRY(aUsageJournalFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644));
} }
nsCOMPtr<nsIOutputStream> stream; LS_TRY_INSPECT(const auto& stream, NS_NewLocalFileOutputStream(aUsageFile));
LS_TRY(NS_NewLocalFileOutputStream(getter_AddRefs(stream), aUsageFile));
nsCOMPtr<nsIBinaryOutputStream> binaryStream = nsCOMPtr<nsIBinaryOutputStream> binaryStream =
NS_NewObjectOutputStream(stream); NS_NewObjectOutputStream(stream);
@@ -1206,12 +1205,10 @@ Result<UsageInfo, nsresult> LoadUsageFile(nsIFile& aUsageFile) {
LS_TRY(OkIf(fileSize == kUsageFileSize), Err(NS_ERROR_FILE_CORRUPTED)); LS_TRY(OkIf(fileSize == kUsageFileSize), Err(NS_ERROR_FILE_CORRUPTED));
nsCOMPtr<nsIInputStream> stream; LS_TRY_UNWRAP(auto stream, NS_NewLocalFileInputStream(&aUsageFile));
LS_TRY(NS_NewLocalFileInputStream(getter_AddRefs(stream), &aUsageFile));
nsCOMPtr<nsIInputStream> bufferedStream; LS_TRY_INSPECT(const auto& bufferedStream,
LS_TRY(NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), NS_NewBufferedInputStream(stream.forget(), 16));
stream.forget(), 16));
const nsCOMPtr<nsIBinaryInputStream> binaryStream = const nsCOMPtr<nsIBinaryInputStream> binaryStream =
NS_NewObjectInputStream(bufferedStream); NS_NewObjectInputStream(bufferedStream);

View File

@@ -2541,13 +2541,9 @@ Result<nsCOMPtr<nsIOutputStream>, nsresult> GetOutputStream(
nsIFile& aFile, FileFlag aFileFlag) { nsIFile& aFile, FileFlag aFileFlag) {
AssertIsOnIOThread(); AssertIsOnIOThread();
nsCOMPtr<nsIOutputStream> outputStream;
switch (aFileFlag) { switch (aFileFlag) {
case kTruncateFileFlag: { case kTruncateFileFlag:
QM_TRY(NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), &aFile)); QM_TRY_RETURN(NS_NewLocalFileOutputStream(&aFile));
break;
}
case kUpdateFileFlag: { case kUpdateFileFlag: {
QM_TRY_INSPECT(const bool& exists, MOZ_TO_RESULT_INVOKE(&aFile, Exists)); QM_TRY_INSPECT(const bool& exists, MOZ_TO_RESULT_INVOKE(&aFile, Exists));
@@ -2556,28 +2552,21 @@ Result<nsCOMPtr<nsIOutputStream>, nsresult> GetOutputStream(
return nsCOMPtr<nsIOutputStream>(); return nsCOMPtr<nsIOutputStream>();
} }
nsCOMPtr<nsIFileStream> stream; QM_TRY_INSPECT(const auto& stream, NS_NewLocalFileStream(&aFile));
QM_TRY(NS_NewLocalFileStream(getter_AddRefs(stream), &aFile));
outputStream = do_QueryInterface(stream); nsCOMPtr<nsIOutputStream> outputStream = do_QueryInterface(stream);
QM_TRY(OkIf(outputStream), Err(NS_ERROR_FAILURE)); QM_TRY(OkIf(outputStream), Err(NS_ERROR_FAILURE));
break; return outputStream;
} }
case kAppendFileFlag: { case kAppendFileFlag:
QM_TRY( QM_TRY_RETURN(NS_NewLocalFileOutputStream(
NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), &aFile, &aFile, PR_WRONLY | PR_CREATE_FILE | PR_APPEND));
PR_WRONLY | PR_CREATE_FILE | PR_APPEND));
break;
}
default: default:
MOZ_CRASH("Should never get here!"); MOZ_CRASH("Should never get here!");
} }
return outputStream;
} }
Result<nsCOMPtr<nsIBinaryOutputStream>, nsresult> GetBinaryOutputStream( Result<nsCOMPtr<nsIBinaryOutputStream>, nsresult> GetBinaryOutputStream(
@@ -2718,12 +2707,10 @@ Result<nsCOMPtr<nsIBinaryInputStream>, nsresult> GetBinaryInputStream(
QM_TRY(file->Append(aFilename)); QM_TRY(file->Append(aFilename));
nsCOMPtr<nsIInputStream> stream; QM_TRY_UNWRAP(auto stream, NS_NewLocalFileInputStream(file));
QM_TRY(NS_NewLocalFileInputStream(getter_AddRefs(stream), file));
nsCOMPtr<nsIInputStream> bufferedStream; QM_TRY_INSPECT(const auto& bufferedStream,
QM_TRY(NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), NS_NewBufferedInputStream(stream.forget(), 512));
stream.forget(), 512));
QM_TRY(OkIf(bufferedStream), Err(NS_ERROR_FAILURE)); QM_TRY(OkIf(bufferedStream), Err(NS_ERROR_FAILURE));

View File

@@ -138,6 +138,18 @@ nsresult NS_NewLocalFileInputStream(nsIInputStream** result, nsIFile* file,
return rv; 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, nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result, nsIFile* file,
int32_t ioFlags /* = -1 */, int32_t ioFlags /* = -1 */,
int32_t perm /* = -1 */, int32_t perm /* = -1 */,
@@ -152,6 +164,18 @@ nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result, nsIFile* file,
return rv; 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, nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result,
const mozilla::ipc::FileDescriptor& fd) { const mozilla::ipc::FileDescriptor& fd) {
nsCOMPtr<nsIFileOutputStream> out; nsCOMPtr<nsIFileOutputStream> out;
@@ -1267,6 +1291,18 @@ nsresult NS_NewLocalFileStream(nsIFileStream** result, nsIFile* file,
return rv; 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( nsresult NS_NewBufferedOutputStream(
nsIOutputStream** aResult, already_AddRefed<nsIOutputStream> aOutputStream, nsIOutputStream** aResult, already_AddRefed<nsIOutputStream> aOutputStream,
uint32_t aBufferSize) { uint32_t aBufferSize) {
@@ -1303,6 +1339,17 @@ nsresult NS_NewBufferedOutputStream(
return rv; 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 { namespace {
#define BUFFER_SIZE 8192 #define BUFFER_SIZE 8192

View File

@@ -9,6 +9,7 @@
#include <functional> #include <functional>
#include "mozilla/Maybe.h" #include "mozilla/Maybe.h"
#include "mozilla/ResultExtensions.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsIInterfaceRequestor.h" #include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.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 ioFlags = -1, int32_t perm = -1,
int32_t behaviorFlags = 0); 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, nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result, nsIFile* file,
int32_t ioFlags = -1, int32_t perm = -1, int32_t ioFlags = -1, int32_t perm = -1,
int32_t behaviorFlags = 0); 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, nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result,
const mozilla::ipc::FileDescriptor& fd); 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 ioFlags = -1, int32_t perm = -1,
int32_t behaviorFlags = 0); 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( [[nodiscard]] nsresult NS_NewBufferedInputStream(
nsIInputStream** aResult, already_AddRefed<nsIInputStream> aInputStream, nsIInputStream** aResult, already_AddRefed<nsIInputStream> aInputStream,
uint32_t aBufferSize); 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 // note: the resulting stream can be QI'ed to nsISafeOutputStream iff the
// provided stream supports it. // provided stream supports it.
nsresult NS_NewBufferedOutputStream( nsresult NS_NewBufferedOutputStream(

View File

@@ -781,9 +781,9 @@ Result<nsCOMPtr<nsIInputStream>, nsresult> ExtensionProtocolHandler::NewStream(
} }
nsCOMPtr<nsIInputStream> inputStream; nsCOMPtr<nsIInputStream> inputStream;
MOZ_TRY(NS_NewLocalFileInputStream(getter_AddRefs(inputStream), requestedFile, MOZ_TRY_VAR(inputStream,
PR_RDONLY, -1, NS_NewLocalFileInputStream(requestedFile, PR_RDONLY, -1,
nsIFileInputStream::DEFER_OPEN)); nsIFileInputStream::DEFER_OPEN));
return inputStream; return inputStream;
} }