Bug 1211090 - Add fallocate support to nsIOutputFileStream and use it. r=froydnj

This commit is contained in:
Gian-Carlo Pascutto 2015-12-21 17:50:53 +01:00
parent e82778f026
commit 73ac6a6ca3
3 changed files with 42 additions and 2 deletions

View File

@ -21,6 +21,8 @@
#include "nsReadLine.h"
#include "nsIClassInfoImpl.h"
#include "mozilla/ipc/InputStreamUtils.h"
#include "mozilla/unused.h"
#include "mozilla/FileUtils.h"
#include "nsNetCID.h"
#include "nsXULAppAPI.h"
@ -866,6 +868,20 @@ nsFileOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
mBehaviorFlags & nsIFileOutputStream::DEFER_OPEN);
}
NS_IMETHODIMP
nsFileOutputStream::Preallocate(int64_t aLength)
{
if (!mFD) {
return NS_ERROR_NOT_INITIALIZED;
}
if (!mozilla::fallocate(mFD, aLength)) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsAtomicFileOutputStream

View File

@ -87,7 +87,7 @@ interface nsIFileInputStream : nsIInputStream
/**
* An output stream that lets you stream to a file.
*/
[scriptable, uuid(e6f68040-c7ec-11d3-8cda-0060b0fc14a3)]
[scriptable, uuid(e734cac9-1295-4e6f-9684-3ac4e1f91063)]
interface nsIFileOutputStream : nsIOutputStream
{
/**
@ -104,6 +104,15 @@ interface nsIFileOutputStream : nsIOutputStream
void init(in nsIFile file, in long ioFlags, in long perm,
in long behaviorFlags);
/**
* @param length asks the operating system to allocate storage for
* this file of at least |length| bytes long, and
* set the file length to the corresponding size.
* @throws NS_ERROR_FAILURE if the preallocation fails.
* @throws NS_ERROR_NOT_INITIALIZED if the file is not opened.
*/
[noscript] void preallocate(in long long length);
/**
* See the same constant in nsIFileInputStream. The deferred open will
* be performed when one of the following is called:

View File

@ -18,10 +18,12 @@
#include "nsNetUtil.h"
#include "nsISeekableStream.h"
#include "nsIBufferedStreams.h"
#include "nsIFileStreams.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Telemetry.h"
#include "mozilla/FileUtils.h"
#include "mozilla/Logging.h"
#include "mozilla/unused.h"
using namespace mozilla;
@ -374,12 +376,25 @@ nsUrlClassifierPrefixSet::LoadFromFile(nsIFile* aFile)
NS_IMETHODIMP
nsUrlClassifierPrefixSet::StoreToFile(nsIFile* aFile)
{
// Now re-open
nsCOMPtr<nsIOutputStream> localOutFile;
nsresult rv = NS_NewLocalFileOutputStream(getter_AddRefs(localOutFile), aFile,
PR_WRONLY | PR_TRUNCATE | PR_CREATE_FILE);
NS_ENSURE_SUCCESS(rv, rv);
// Preallocate the file storage
{
nsCOMPtr<nsIFileOutputStream> fos(do_QueryInterface(localOutFile));
Telemetry::AutoTimer<Telemetry::URLCLASSIFIER_PS_FALLOCATE_TIME> timer;
int64_t size = 4 * sizeof(uint32_t);
uint32_t deltas = mTotalPrefixes - mIndexPrefixes.Length();
size += 2 * mIndexPrefixes.Length() * sizeof(uint32_t);
size += deltas * sizeof(uint16_t);
// Ignore failure, the preallocation is a hint and we write out the entire
// file later on
Unused << fos->Preallocate(size);
}
// Convert to buffered stream
nsCOMPtr<nsIOutputStream> out;
rv = NS_NewBufferedOutputStream(getter_AddRefs(out), localOutFile,