2012-03-28 04:04:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "FileBlockCache.h"
|
2017-03-16 23:54:23 +03:00
|
|
|
#include "mozilla/SharedThreadPool.h"
|
2012-03-28 04:04:20 +04:00
|
|
|
#include "VideoUtils.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "prio.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2017-03-16 23:54:23 +03:00
|
|
|
#include "nsAnonymousTemporaryFile.h"
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-04-07 10:05:30 +03:00
|
|
|
#undef LOG
|
2017-03-16 23:54:23 +03:00
|
|
|
LazyLogModule gFileBlockCacheLog("FileBlockCache");
|
2017-04-07 10:05:30 +03:00
|
|
|
#define LOG(x, ...) MOZ_LOG(gFileBlockCacheLog, LogLevel::Debug, \
|
|
|
|
("%p " x, this, ##__VA_ARGS__))
|
2017-03-16 23:54:23 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
FileBlockCache::SetCacheFile(PRFileDesc* aFD)
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-04-07 10:05:30 +03:00
|
|
|
LOG("SetFD(aFD=%p) mIsOpen=%d", aFD, mIsOpen);
|
2017-03-16 23:54:23 +03:00
|
|
|
|
|
|
|
if (!aFD) {
|
|
|
|
// Failed to get a temporary file. Shutdown.
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoLock lock(mFileMutex);
|
2012-03-28 04:04:20 +04:00
|
|
|
mFD = aFD;
|
|
|
|
}
|
2017-04-07 08:51:47 +03:00
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoLock lock(mDataMutex);
|
2017-04-07 08:51:47 +03:00
|
|
|
if (!mIsOpen) {
|
|
|
|
// We've been closed while waiting for the file descriptor. Bail out.
|
|
|
|
// Rely on the destructor to close the file descriptor.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mInitialized = true;
|
|
|
|
if (mIsWriteScheduled) {
|
2017-05-17 06:30:32 +03:00
|
|
|
// A write was scheduled while waiting for FD. We need to run/dispatch a
|
2017-04-07 08:51:47 +03:00
|
|
|
// task to service the request.
|
|
|
|
mThread->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
2017-03-16 23:54:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileBlockCache::Init()
|
|
|
|
{
|
2017-04-07 10:05:30 +03:00
|
|
|
LOG("Init()");
|
2017-03-16 23:54:23 +03:00
|
|
|
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoLock mon(mDataMutex);
|
2017-03-16 23:54:23 +03:00
|
|
|
nsresult rv = NS_NewNamedThread("FileBlockCache",
|
|
|
|
getter_AddRefs(mThread),
|
|
|
|
nullptr,
|
|
|
|
SharedThreadPool::kStackSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mIsOpen = true;
|
|
|
|
|
|
|
|
if (XRE_IsParentProcess()) {
|
2017-05-17 06:30:32 +03:00
|
|
|
RefPtr<FileBlockCache> self = this;
|
|
|
|
rv = mThread->Dispatch(NS_NewRunnableFunction([self] {
|
|
|
|
PRFileDesc* fd = nullptr;
|
|
|
|
nsresult rv = NS_OpenAnonymousTemporaryFile(&fd);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
self->SetCacheFile(fd);
|
|
|
|
} else {
|
|
|
|
self->Close();
|
|
|
|
}
|
|
|
|
}), NS_DISPATCH_NORMAL);
|
2017-03-16 23:54:23 +03:00
|
|
|
} else {
|
|
|
|
// We must request a temporary file descriptor from the parent process.
|
|
|
|
RefPtr<FileBlockCache> self = this;
|
|
|
|
rv = dom::ContentChild::GetSingleton()->AsyncOpenAnonymousTemporaryFile(
|
|
|
|
[self](PRFileDesc* aFD) { self->SetCacheFile(aFD); });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Close();
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
2017-03-16 23:54:23 +03:00
|
|
|
|
|
|
|
return rv;
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FileBlockCache::FileBlockCache()
|
2017-05-17 07:36:26 +03:00
|
|
|
: mFileMutex("MediaCache.Writer.IO.Mutex"),
|
2012-07-30 18:20:58 +04:00
|
|
|
mFD(nullptr),
|
2012-03-28 04:04:20 +04:00
|
|
|
mFDCurrentPos(0),
|
2017-05-17 07:36:26 +03:00
|
|
|
mDataMutex("MediaCache.Writer.Data.Mutex"),
|
2012-03-28 04:04:20 +04:00
|
|
|
mIsWriteScheduled(false),
|
2017-05-17 08:10:57 +03:00
|
|
|
mIsReading(false),
|
2012-03-28 04:04:20 +04:00
|
|
|
mIsOpen(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileBlockCache::~FileBlockCache()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mIsOpen, "Should Close() FileBlockCache before destroying");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileBlockCache::Close()
|
|
|
|
{
|
2017-04-07 10:05:30 +03:00
|
|
|
LOG("Close()");
|
2017-03-16 23:54:23 +03:00
|
|
|
|
2017-05-17 07:00:39 +03:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoLock mon(mDataMutex);
|
2017-05-17 07:00:39 +03:00
|
|
|
if (!mIsOpen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIsOpen = false;
|
|
|
|
if (!mThread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
thread.swap(mThread);
|
2017-04-07 08:51:47 +03:00
|
|
|
}
|
2017-05-17 07:00:39 +03:00
|
|
|
|
|
|
|
PRFileDesc* fd;
|
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoLock lock(mFileMutex);
|
2017-05-17 07:00:39 +03:00
|
|
|
fd = mFD;
|
|
|
|
mFD = nullptr;
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
2017-05-17 07:00:39 +03:00
|
|
|
|
|
|
|
// Let the thread close the FD, and then trigger its own shutdown.
|
|
|
|
// Note that mThread is now empty, so no other task will be posted there.
|
|
|
|
// Also mThread and mFD are empty and therefore can be reused immediately.
|
|
|
|
nsresult rv = thread->Dispatch(NS_NewRunnableFunction([thread, fd] {
|
|
|
|
if (fd) {
|
|
|
|
PRStatus prrc;
|
|
|
|
prrc = PR_Close(fd);
|
|
|
|
if (prrc != PR_SUCCESS) {
|
|
|
|
NS_WARNING("PR_Close() failed.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We must shut down the thread in another runnable. This is called
|
|
|
|
// while we're shutting down the media cache, and nsIThread::Shutdown()
|
|
|
|
// can cause events to run before it completes, which could end up
|
|
|
|
// opening more streams, while the media cache is shutting down and
|
|
|
|
// releasing memory etc!
|
|
|
|
nsCOMPtr<nsIRunnable> event = new ShutdownThreadEvent(thread);
|
|
|
|
SystemGroup::Dispatch(
|
|
|
|
"ShutdownThreadEvent", TaskCategory::Other, event.forget());
|
|
|
|
}), NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 08:52:34 +03:00
|
|
|
template<typename Container, typename Value>
|
|
|
|
bool
|
|
|
|
ContainerContains(const Container& aContainer, const Value& value)
|
|
|
|
{
|
|
|
|
return std::find(aContainer.begin(), aContainer.end(), value)
|
|
|
|
!= aContainer.end();
|
|
|
|
}
|
|
|
|
|
2017-04-11 10:47:46 +03:00
|
|
|
nsresult
|
|
|
|
FileBlockCache::WriteBlock(uint32_t aBlockIndex,
|
|
|
|
Span<const uint8_t> aData1, Span<const uint8_t> aData2)
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoLock mon(mDataMutex);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
if (!mIsOpen)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Check if we've already got a pending write scheduled for this block.
|
|
|
|
mBlockChanges.EnsureLengthAtLeast(aBlockIndex + 1);
|
2012-07-30 18:20:58 +04:00
|
|
|
bool blockAlreadyHadPendingChange = mBlockChanges[aBlockIndex] != nullptr;
|
2017-04-11 10:47:46 +03:00
|
|
|
mBlockChanges[aBlockIndex] = new BlockChange(aData1, aData2);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
2016-03-20 08:52:34 +03:00
|
|
|
if (!blockAlreadyHadPendingChange || !ContainerContains(mChangeIndexList, aBlockIndex)) {
|
2012-03-28 04:04:20 +04:00
|
|
|
// We either didn't already have a pending change for this block, or we
|
|
|
|
// did but we didn't have an entry for it in mChangeIndexList (we're in the process
|
|
|
|
// of writing it and have removed the block's index out of mChangeIndexList
|
|
|
|
// in Run() but not finished writing the block to file yet). Add the blocks
|
|
|
|
// index to the end of mChangeIndexList to ensure the block is written as
|
|
|
|
// as soon as possible.
|
2016-03-20 08:52:34 +03:00
|
|
|
mChangeIndexList.push_back(aBlockIndex);
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
2016-03-20 08:52:34 +03:00
|
|
|
NS_ASSERTION(ContainerContains(mChangeIndexList, aBlockIndex), "Must have entry for new block");
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
EnsureWriteScheduled();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileBlockCache::EnsureWriteScheduled()
|
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
mDataMutex.AssertCurrentThreadOwns();
|
2017-03-16 23:54:23 +03:00
|
|
|
MOZ_ASSERT(mIsOpen);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
2017-05-17 08:10:57 +03:00
|
|
|
if (mIsWriteScheduled || mIsReading) {
|
2017-03-16 23:54:23 +03:00
|
|
|
return;
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
2017-03-16 23:54:23 +03:00
|
|
|
mIsWriteScheduled = true;
|
2017-04-07 08:51:47 +03:00
|
|
|
if (!mInitialized) {
|
|
|
|
// We're still waiting on a file descriptor. When it arrives,
|
|
|
|
// the write will be scheduled.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mThread->Dispatch(this, NS_DISPATCH_NORMAL);
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult FileBlockCache::Seek(int64_t aOffset)
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
mFileMutex.AssertCurrentThreadOwns();
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
if (mFDCurrentPos != aOffset) {
|
2017-03-16 23:54:23 +03:00
|
|
|
MOZ_ASSERT(mFD);
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t result = PR_Seek64(mFD, aOffset, PR_SEEK_SET);
|
2012-03-28 04:04:20 +04:00
|
|
|
if (result != aOffset) {
|
|
|
|
NS_WARNING("Failed to seek media cache file");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mFDCurrentPos = result;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-11 00:21:50 +04:00
|
|
|
nsresult FileBlockCache::ReadFromFile(int64_t aOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* aDest,
|
|
|
|
int32_t aBytesToRead,
|
|
|
|
int32_t& aBytesRead)
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-04-07 10:05:30 +03:00
|
|
|
LOG("ReadFromFile(offset=%" PRIu64 ", len=%u)", aOffset, aBytesToRead);
|
2017-05-17 07:36:26 +03:00
|
|
|
mFileMutex.AssertCurrentThreadOwns();
|
2017-03-16 23:54:23 +03:00
|
|
|
MOZ_ASSERT(mFD);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
nsresult res = Seek(aOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
2014-10-13 02:53:42 +04:00
|
|
|
|
2012-03-28 04:04:20 +04:00
|
|
|
aBytesRead = PR_Read(mFD, aDest, aBytesToRead);
|
|
|
|
if (aBytesRead <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mFDCurrentPos += aBytesRead;
|
2014-10-13 02:53:42 +04:00
|
|
|
|
2012-03-28 04:04:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult FileBlockCache::WriteBlockToFile(int32_t aBlockIndex,
|
|
|
|
const uint8_t* aBlockData)
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-04-07 10:05:30 +03:00
|
|
|
LOG("WriteBlockToFile(index=%u)", aBlockIndex);
|
2017-03-16 23:54:23 +03:00
|
|
|
|
2017-05-17 07:36:26 +03:00
|
|
|
mFileMutex.AssertCurrentThreadOwns();
|
2017-03-16 23:54:23 +03:00
|
|
|
MOZ_ASSERT(mFD);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
2013-01-11 00:21:50 +04:00
|
|
|
nsresult rv = Seek(BlockIndexToOffset(aBlockIndex));
|
2012-03-28 04:04:20 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t amount = PR_Write(mFD, aBlockData, BLOCK_SIZE);
|
2012-03-28 04:04:20 +04:00
|
|
|
if (amount < BLOCK_SIZE) {
|
|
|
|
NS_WARNING("Failed to write media cache block!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mFDCurrentPos += BLOCK_SIZE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult FileBlockCache::MoveBlockInFile(int32_t aSourceBlockIndex,
|
|
|
|
int32_t aDestBlockIndex)
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-04-07 10:05:30 +03:00
|
|
|
LOG("MoveBlockInFile(src=%u, dest=%u)", aSourceBlockIndex, aDestBlockIndex);
|
2017-03-16 23:54:23 +03:00
|
|
|
|
2017-05-17 07:36:26 +03:00
|
|
|
mFileMutex.AssertCurrentThreadOwns();
|
2012-03-28 04:04:20 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t buf[BLOCK_SIZE];
|
|
|
|
int32_t bytesRead = 0;
|
2013-01-11 00:21:50 +04:00
|
|
|
if (NS_FAILED(ReadFromFile(BlockIndexToOffset(aSourceBlockIndex),
|
2012-03-28 04:04:20 +04:00
|
|
|
buf,
|
|
|
|
BLOCK_SIZE,
|
|
|
|
bytesRead))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return WriteBlockToFile(aDestBlockIndex, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult FileBlockCache::Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoLock mon(mDataMutex);
|
2016-03-20 08:52:34 +03:00
|
|
|
NS_ASSERTION(!mChangeIndexList.empty(), "Only dispatch when there's work to do");
|
2012-03-28 04:04:20 +04:00
|
|
|
NS_ASSERTION(mIsWriteScheduled, "Should report write running or scheduled.");
|
2017-03-16 23:54:23 +03:00
|
|
|
MOZ_ASSERT(mFD);
|
|
|
|
|
2017-04-07 10:05:30 +03:00
|
|
|
LOG("Run() mFD=%p mIsOpen=%d", mFD, mIsOpen);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
2016-03-20 08:52:34 +03:00
|
|
|
while (!mChangeIndexList.empty()) {
|
2012-03-28 04:04:20 +04:00
|
|
|
if (!mIsOpen) {
|
|
|
|
// We've been closed, abort, discarding unwritten changes.
|
|
|
|
mIsWriteScheduled = false;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-05-17 08:10:57 +03:00
|
|
|
if (mIsReading) {
|
|
|
|
// We're trying to read; postpone all writes. (Reader will resume writes.)
|
|
|
|
mIsWriteScheduled = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-28 04:04:20 +04:00
|
|
|
// Process each pending change. We pop the index out of the change
|
|
|
|
// list, but leave the BlockChange in mBlockChanges until the change
|
|
|
|
// is written to file. This is so that any read which happens while
|
2017-05-17 07:36:26 +03:00
|
|
|
// we drop mDataMutex to write will refer to the data's source in
|
2012-03-28 04:04:20 +04:00
|
|
|
// memory, rather than the not-yet up to date data written to file.
|
|
|
|
// This also ensures we will insert a new index into mChangeIndexList
|
|
|
|
// when this happens.
|
|
|
|
|
|
|
|
// Hold a reference to the change, in case another change
|
|
|
|
// overwrites the mBlockChanges entry for this block while we drop
|
2017-05-17 07:36:26 +03:00
|
|
|
// mDataMutex to take mFileMutex.
|
2016-03-20 08:52:34 +03:00
|
|
|
int32_t blockIndex = mChangeIndexList.front();
|
|
|
|
mChangeIndexList.pop_front();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlockChange> change = mBlockChanges[blockIndex];
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(change,
|
|
|
|
"Change index list should only contain entries for blocks "
|
|
|
|
"with changes");
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoUnlock unlock(mDataMutex);
|
|
|
|
MutexAutoLock lock(mFileMutex);
|
2012-03-28 04:04:20 +04:00
|
|
|
if (change->IsWrite()) {
|
|
|
|
WriteBlockToFile(blockIndex, change->mData.get());
|
|
|
|
} else if (change->IsMove()) {
|
|
|
|
MoveBlockInFile(change->mSourceBlockIndex, blockIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If a new change has not been made to the block while we dropped
|
2017-05-17 07:36:26 +03:00
|
|
|
// mDataMutex, clear reference to the old change. Otherwise, the old
|
2012-03-28 04:04:20 +04:00
|
|
|
// reference has been cleared already.
|
|
|
|
if (mBlockChanges[blockIndex] == change) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mBlockChanges[blockIndex] = nullptr;
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsWriteScheduled = false;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult FileBlockCache::Read(int64_t aOffset,
|
|
|
|
uint8_t* aData,
|
|
|
|
int32_t aLength,
|
|
|
|
int32_t* aBytes)
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoLock mon(mDataMutex);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
2017-03-16 23:54:23 +03:00
|
|
|
if (!mIsOpen || (aOffset / BLOCK_SIZE) > INT32_MAX)
|
2012-03-28 04:04:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2017-05-17 08:10:57 +03:00
|
|
|
mIsReading = true;
|
|
|
|
auto exitRead = MakeScopeExit([&] {
|
|
|
|
mIsReading = false;
|
|
|
|
if (!mChangeIndexList.empty()) {
|
|
|
|
// mReading has stopped or prevented pending writes, resume them.
|
|
|
|
EnsureWriteScheduled();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesToRead = aLength;
|
|
|
|
int64_t offset = aOffset;
|
|
|
|
uint8_t* dst = aData;
|
2012-03-28 04:04:20 +04:00
|
|
|
while (bytesToRead > 0) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t blockIndex = static_cast<int32_t>(offset / BLOCK_SIZE);
|
|
|
|
int32_t start = offset % BLOCK_SIZE;
|
2013-01-15 16:22:03 +04:00
|
|
|
int32_t amount = std::min(BLOCK_SIZE - start, bytesToRead);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// If the block is not yet written to file, we can just read from
|
|
|
|
// the memory buffer, otherwise we need to read from file.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesRead = 0;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlockChange> change = mBlockChanges[blockIndex];
|
2012-03-28 04:04:20 +04:00
|
|
|
if (change && change->IsWrite()) {
|
|
|
|
// Block isn't yet written to file. Read from memory buffer.
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint8_t* blockData = change->mData.get();
|
2012-03-28 04:04:20 +04:00
|
|
|
memcpy(dst, blockData + start, amount);
|
|
|
|
bytesRead = amount;
|
|
|
|
} else {
|
|
|
|
if (change && change->IsMove()) {
|
|
|
|
// The target block is the destination of a not-yet-completed move
|
|
|
|
// action, so read from the move's source block from file. Note we
|
|
|
|
// *don't* follow a chain of moves here, as a move's source index
|
|
|
|
// is resolved when MoveBlock() is called, and the move's source's
|
|
|
|
// block could be have itself been subject to a move (or write)
|
|
|
|
// which happened *after* this move was recorded.
|
|
|
|
blockIndex = mBlockChanges[blockIndex]->mSourceBlockIndex;
|
|
|
|
}
|
|
|
|
// Block has been written to file, either as the source block of a move,
|
|
|
|
// or as a stable (all changes made) block. Read the data directly
|
|
|
|
// from file.
|
|
|
|
nsresult res;
|
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoUnlock unlock(mDataMutex);
|
|
|
|
MutexAutoLock lock(mFileMutex);
|
2013-01-11 00:21:50 +04:00
|
|
|
res = ReadFromFile(BlockIndexToOffset(blockIndex) + start,
|
2012-03-28 04:04:20 +04:00
|
|
|
dst,
|
|
|
|
amount,
|
|
|
|
bytesRead);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
|
|
|
}
|
|
|
|
dst += bytesRead;
|
|
|
|
offset += bytesRead;
|
|
|
|
bytesToRead -= bytesRead;
|
|
|
|
}
|
|
|
|
*aBytes = aLength - bytesToRead;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult FileBlockCache::MoveBlock(int32_t aSourceBlockIndex, int32_t aDestBlockIndex)
|
2012-03-28 04:04:20 +04:00
|
|
|
{
|
2017-05-17 07:36:26 +03:00
|
|
|
MutexAutoLock mon(mDataMutex);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
if (!mIsOpen)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
mBlockChanges.EnsureLengthAtLeast(std::max(aSourceBlockIndex, aDestBlockIndex) + 1);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// The source block's contents may be the destination of another pending
|
|
|
|
// move, which in turn can be the destination of another pending move,
|
|
|
|
// etc. Resolve the final source block, so that if one of the blocks in
|
|
|
|
// the chain of moves is overwritten, we don't lose the reference to the
|
|
|
|
// contents of the destination block.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t sourceIndex = aSourceBlockIndex;
|
2012-07-30 18:20:58 +04:00
|
|
|
BlockChange* sourceBlock = nullptr;
|
2012-03-28 04:04:20 +04:00
|
|
|
while ((sourceBlock = mBlockChanges[sourceIndex]) &&
|
|
|
|
sourceBlock->IsMove()) {
|
|
|
|
sourceIndex = sourceBlock->mSourceBlockIndex;
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mBlockChanges[aDestBlockIndex] == nullptr ||
|
2016-03-20 08:52:34 +03:00
|
|
|
!ContainerContains(mChangeIndexList, aDestBlockIndex)) {
|
2012-03-28 04:04:20 +04:00
|
|
|
// Only add another entry to the change index list if we don't already
|
|
|
|
// have one for this block. We won't have an entry when either there's
|
|
|
|
// no pending change for this block, or if there is a pending change for
|
|
|
|
// this block and we're in the process of writing it (we've popped the
|
|
|
|
// block's index out of mChangeIndexList in Run() but not finished writing
|
|
|
|
// the block to file yet.
|
2016-03-20 08:52:34 +03:00
|
|
|
mChangeIndexList.push_back(aDestBlockIndex);
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
2014-10-13 02:53:42 +04:00
|
|
|
|
2012-03-28 04:04:20 +04:00
|
|
|
// If the source block hasn't yet been written to file then the dest block
|
|
|
|
// simply contains that same write. Resolve this as a write instead.
|
|
|
|
if (sourceBlock && sourceBlock->IsWrite()) {
|
|
|
|
mBlockChanges[aDestBlockIndex] = new BlockChange(sourceBlock->mData.get());
|
|
|
|
} else {
|
|
|
|
mBlockChanges[aDestBlockIndex] = new BlockChange(sourceIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureWriteScheduled();
|
|
|
|
|
2016-03-20 08:52:34 +03:00
|
|
|
NS_ASSERTION(ContainerContains(mChangeIndexList, aDestBlockIndex),
|
2012-03-28 04:04:20 +04:00
|
|
|
"Should have scheduled block for change");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End namespace mozilla.
|
2017-04-07 10:05:30 +03:00
|
|
|
|
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef LOG
|