Chromium Code Reviews| Index: net/disk_cache/simple/simple_synchronous_entry.cc |
| diff --git a/net/disk_cache/simple/simple_synchronous_entry.cc b/net/disk_cache/simple/simple_synchronous_entry.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1127eaa79befe4a83301c4c649ef90422b56c3a4 |
| --- /dev/null |
| +++ b/net/disk_cache/simple/simple_synchronous_entry.cc |
| @@ -0,0 +1,309 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/disk_cache/simple/simple_synchronous_entry.h" |
| + |
| +#include <algorithm> |
| +#include <cstring> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/file_util.h" |
| +#include "base/hash.h" |
| +#include "base/location.h" |
| +#include "base/message_loop_proxy.h" |
| +#include "base/sha1.h" |
| +#include "base/stringprintf.h" |
| +#include "base/task_runner.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/disk_cache/simple/simple_disk_format.h" |
| + |
| +using base::ClosePlatformFile; |
| +using base::GetPlatformFileInfo; |
| +using base::PlatformFileError; |
| +using base::PlatformFileInfo; |
| +using base::PLATFORM_FILE_CREATE_ALWAYS; |
| +using base::PLATFORM_FILE_OK; |
| +using base::PLATFORM_FILE_OPEN; |
| +using base::PLATFORM_FILE_READ; |
| +using base::PLATFORM_FILE_WRITE; |
| +using base::ReadPlatformFile; |
| +using base::TaskRunner; |
| +using base::Time; |
| +using base::TruncatePlatformFile; |
| +using base::WritePlatformFile; |
| + |
| +namespace { |
| + |
| +std::string GetFilenameForKeyAndIndex(const std::string& key, int index) { |
| + const std::string sha_hash = base::SHA1HashString(key); |
| + return StringPrintf("%02x%02x%02x%02x%02x_%1d", |
| + implicit_cast<unsigned char>(sha_hash[0]), |
| + implicit_cast<unsigned char>(sha_hash[1]), |
| + implicit_cast<unsigned char>(sha_hash[2]), |
| + implicit_cast<unsigned char>(sha_hash[3]), |
| + implicit_cast<unsigned char>(sha_hash[4]), index); |
| +} |
| + |
| +int32 DataSizeFromKeyAndFileSize(size_t key_size, int64 file_size) { |
| + return file_size - key_size - sizeof(disk_cache::SimpleFileHeader); |
| +} |
| + |
| +int64 FileOffsetFromDataOffset(size_t key_size, int offset) { |
| + const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + |
| + key_size; |
| + return headers_size + offset; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace disk_cache { |
| + |
| +// static |
| +void SimpleSynchronousEntry::OpenEntry( |
| + const FilePath& path, |
| + const std::string& key, |
| + const scoped_refptr<TaskRunner>& callback_runner, |
| + const SynchronousEntryCallback& callback) { |
| + SimpleSynchronousEntry* sync_entry = |
| + new SimpleSynchronousEntry(callback_runner, path, key); |
| + |
| + if (!sync_entry->InitializeForOpen()) { |
| + delete sync_entry; |
| + sync_entry = NULL; |
| + } |
| + callback_runner->PostTask(FROM_HERE, |
| + base::Bind(callback, sync_entry, |
| + sync_entry ? net::OK : net::ERR_FAILED)); |
| +} |
| + |
| +// static |
| +void SimpleSynchronousEntry::CreateEntry( |
| + const FilePath& path, |
| + const std::string& key, |
| + const scoped_refptr<TaskRunner>& callback_runner, |
| + const SynchronousEntryCallback& callback) { |
| + SimpleSynchronousEntry* sync_entry = |
| + new SimpleSynchronousEntry(callback_runner, path, key); |
| + |
| + if (!sync_entry->InitializeForCreate()) { |
| + delete sync_entry; |
| + sync_entry = NULL; |
| + } |
| + callback_runner->PostTask(FROM_HERE, |
| + base::Bind(callback, sync_entry, |
| + sync_entry ? net::OK : net::ERR_FAILED)); |
| +} |
| + |
| +// static |
| +void SimpleSynchronousEntry::DoomEntry( |
| + const FilePath& path, |
| + const std::string& key, |
| + scoped_refptr<TaskRunner> callback_runner, |
| + const net::CompletionCallback& callback) { |
| + for (int i = 0; i < kIndexCount; ++i) { |
| + bool delete_result = |
| + file_util::Delete(path.AppendASCII(GetFilenameForKeyAndIndex(key, i)), |
| + false); |
| + DCHECK(delete_result); |
| + } |
| + if (!callback.is_null()) |
| + callback_runner->PostTask(FROM_HERE, base::Bind(callback, net::OK)); |
| +} |
| + |
| +void SimpleSynchronousEntry::DoomAndClose() { |
| + scoped_refptr<TaskRunner> callback_runner = callback_runner_; |
| + FilePath path = path_; |
| + std::string key = key_; |
| + |
| + Close(); |
| + // |this| is now deleted. |
| + |
| + DoomEntry(path, key, callback_runner, net::CompletionCallback()); |
| +} |
| + |
| +void SimpleSynchronousEntry::Close() { |
| + for (int i = 0; i < kIndexCount; ++i) { |
| + bool result = ClosePlatformFile(files_[i]); |
| + DCHECK(result); |
| + } |
| + delete this; |
| +} |
| + |
| +void SimpleSynchronousEntry::ReadData( |
| + int index, |
| + int offset, |
| + net::IOBuffer* buf, |
| + int buf_len, |
| + const SynchronousEntryCallback& callback) { |
| + DCHECK(initialized_); |
| + if (status_[index].mode != EntryStatus::ENTRY_READER) |
| + status_[index].data_offset = 0; |
| + DCHECK_EQ(status_[index].data_offset, offset); |
| + status_[index].mode = EntryStatus::ENTRY_READER; |
| + |
| + int64 file_offset = FileOffsetFromDataOffset(key_.size(), offset); |
| + int bytes_read = ReadPlatformFile(files_[index], file_offset, |
| + buf->data(), buf_len); |
| + if (bytes_read > 0) { |
| + last_used_ = Time::Now(); |
| + status_[index].data_offset += bytes_read; |
| + } |
| + int result = (bytes_read >= 0) ? bytes_read : net::ERR_FAILED; |
| + callback_runner_->PostTask(FROM_HERE, base::Bind(callback, this, result)); |
| +} |
| + |
| +void SimpleSynchronousEntry::WriteData( |
| + int index, |
| + int offset, |
| + net::IOBuffer* buf, |
| + int buf_len, |
| + const SynchronousEntryCallback& callback, |
| + bool truncate) { |
| + DCHECK(initialized_); |
| + if (status_[index].mode != EntryStatus::ENTRY_WRITER) |
| + status_[index].data_offset = 0; |
| + DCHECK_EQ(status_[index].data_offset, offset); |
| + status_[index].mode = EntryStatus::ENTRY_WRITER; |
| + |
| + int64 file_offset = FileOffsetFromDataOffset(key_.size(), offset); |
| + if (buf_len > 0) { |
| + if (WritePlatformFile(files_[index], file_offset, buf->data(), buf_len) != |
| + buf_len) { |
| + callback_runner_->PostTask(FROM_HERE, |
| + base::Bind(callback, this, net::ERR_FAILED)); |
|
pasko-google - do not use
2013/02/11 13:59:25
I am wondering why some errors are logged and some
gavinp
2013/02/11 17:55:50
The rule is that there's logging in code that has
|
| + return; |
| + } |
| + data_size_[index] = std::max(data_size_[index], offset + buf_len); |
| + status_[index].data_offset += buf_len; |
| + } |
| + if (truncate) { |
| + data_size_[index] = offset + buf_len; |
| + if (!TruncatePlatformFile(files_[index], file_offset + buf_len)) { |
| + callback_runner_->PostTask(FROM_HERE, |
| + base::Bind(callback, this, net::ERR_FAILED)); |
| + return; |
| + } |
| + } |
| + last_modified_ = Time::Now(); |
| + callback_runner_->PostTask(FROM_HERE, base::Bind(callback, this, buf_len)); |
| +} |
| + |
| +SimpleSynchronousEntry::EntryStatus::EntryStatus() |
| + : mode(ENTRY_UNINITIALIZED), |
| + data_offset(0) { |
| +} |
| + |
| +SimpleSynchronousEntry::SimpleSynchronousEntry( |
| + const scoped_refptr<TaskRunner>& callback_runner, |
| + const FilePath& path, |
| + const std::string& key) : callback_runner_(callback_runner), |
| + path_(path), |
| + key_(key), |
| + initialized_(false) { |
| +} |
| + |
| +SimpleSynchronousEntry::~SimpleSynchronousEntry() { |
| +} |
| + |
| +bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) { |
| + for (int i = 0; i < kIndexCount; ++i) { |
| + FilePath filename = path_.AppendASCII(GetFilenameForKeyAndIndex(key_, i)); |
| + int flags = PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; |
| + if (create) |
| + flags |= PLATFORM_FILE_CREATE_ALWAYS; |
| + else |
| + flags |= PLATFORM_FILE_OPEN; |
| + PlatformFileError error; |
| + files_[i] = CreatePlatformFile(filename, flags, NULL, &error); |
| + if (error != PLATFORM_FILE_OK) { |
| + while (--i >= 0) { |
| + bool did_close = ClosePlatformFile(files_[i]); |
| + DCHECK(did_close); |
| + } |
| + return false; |
| + } |
| + } |
| + |
| + for (int i = 0; i < kIndexCount; ++i) { |
| + PlatformFileInfo file_info; |
| + CHECK(GetPlatformFileInfo(files_[i], &file_info)); |
| + last_used_ = std::max(last_used_, file_info.last_accessed); |
|
pasko-google - do not use
2013/02/11 13:59:25
I am wondering how last_used is actually used. Her
gavinp
2013/02/11 17:55:50
Yeah. Tricky. Could force this issue with filesyst
|
| + last_modified_ = std::max(last_modified_, file_info.last_modified); |
| + data_size_[i] = DataSizeFromKeyAndFileSize(key_.size(), file_info.size); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool SimpleSynchronousEntry::InitializeForOpen() { |
| + DCHECK(!initialized_); |
| + if (!OpenOrCreateFiles(false)) |
| + return false; |
| + |
| + for (int i = 0; i < kIndexCount; ++i) { |
| + SimpleFileHeader header; |
| + if (ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), |
| + sizeof(header)) != sizeof(header)) { |
| + return false; |
| + } |
| + if (header.initial_magic_number != kSimpleInitialMagicNumber || |
| + header.version != kSimpleVersion) { |
| + DVLOG(6) << "Error: No magic number."; |
|
pasko-google - do not use
2013/02/11 13:59:25
This is a pretty serious error, so sounds rather L
gavinp
2013/02/11 17:55:50
I'll leave it LOG(WARNING) for now with a TODO. We
|
| + return false; |
| + } |
| + |
| + char key[1024]; |
| + DCHECK_LE(header.key_length, sizeof(key)); |
| + if (ReadPlatformFile(files_[i], sizeof(header), key, header.key_length) != |
| + implicit_cast<int>(header.key_length)) { |
| + DVLOG(6) << "Error: No key."; |
| + return false; |
| + } |
| + if (std::memcmp(static_cast<const void*>(key_.data()), |
| + static_cast<const void*>(key), |
| + key_.size()) != 0) { |
| + DVLOG(6) << "Error: Bad key."; |
| + return false; |
| + } |
| + |
| + if (base::Hash(key, header.key_length) == header.key_hash) { |
| + DVLOG(6) << "Error: Bad key hash."; |
| + return false; |
| + } |
| + } |
| + |
| + initialized_ = true; |
| + return true; |
| +} |
| + |
| +bool SimpleSynchronousEntry::InitializeForCreate() { |
| + DCHECK(!initialized_); |
| + if (!OpenOrCreateFiles(true)) |
| + return false; |
| + |
| + for (int i = 0; i < kIndexCount; ++i) { |
| + SimpleFileHeader header; |
| + header.initial_magic_number = kSimpleInitialMagicNumber; |
| + header.version = kSimpleVersion; |
| + |
| + header.key_length = key_.size(); |
| + header.key_hash = base::Hash(key_); |
| + |
| + if (WritePlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), |
| + sizeof(header)) != sizeof(header)) { |
|
pasko-google - do not use
2013/02/11 13:59:25
would be helpful: LOG(WARNING) or explanation wher
gavinp
2013/02/11 17:55:50
Done.
|
| + return false; |
| + } |
| + |
| + if (WritePlatformFile(files_[i], sizeof(header), key_.data(), |
| + key_.size()) != implicit_cast<int>(key_.size())) { |
| + return false; |
| + } |
| + } |
| + |
| + initialized_ = true; |
| + return true; |
| +} |
| + |
| +} // namespace disk_cache |