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..9c65ce1f1e1cafb22edd36c9c0406b908a47d94f |
--- /dev/null |
+++ b/net/disk_cache/simple/simple_synchronous_entry.cc |
@@ -0,0 +1,331 @@ |
+// 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); |
rvargas (doing something else)
2013/02/13 01:48:46
There's a size mismatch here that should be handle
gavinp
2013/02/14 15:29:55
Done. I used std::numeric_limits<> to guard the ca
|
+} |
+ |
+int64 FileOffsetFromDataOffset(size_t key_size, int offset) { |
rvargas (doing something else)
2013/02/13 01:48:46
Do you expect to have large files?
gavinp
2013/02/14 15:29:55
No.
rvargas (doing something else)
2013/02/14 21:09:30
My point is that then you can return the size that
|
+ 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)); |
rvargas (doing something else)
2013/02/13 01:48:46
Sounds simpler (on both sides) to remove the last
gavinp
2013/02/14 15:29:55
Done.
|
+} |
+ |
+// 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); |
rvargas (doing something else)
2013/02/13 01:48:46
should not dcheck something coming from the OS
gavinp
2013/02/14 15:29:55
Done.
|
+ } |
+ 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) |
rvargas (doing something else)
2013/02/13 01:48:46
What's the purpose of this?
gavinp
2013/02/14 15:29:55
Removed.
|
+ 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 || |
+ (truncate && offset == 0)) |
+ 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)); |
+ 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), |
rvargas (doing something else)
2013/02/13 01:48:46
nit: initialization list on the next line
gavinp
2013/02/14 15:29:55
Done.
|
+ 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; |
rvargas (doing something else)
2013/02/13 01:48:46
overwrite would be an error
gavinp
2013/02/14 15:29:55
Done.
|
+ else |
+ flags |= PLATFORM_FILE_OPEN; |
+ PlatformFileError error; |
+ files_[i] = CreatePlatformFile(filename, flags, NULL, &error); |
+ if (error != PLATFORM_FILE_OK) { |
pasko-google - do not use
2013/02/13 11:36:24
please log the error here
gavinp
2013/02/14 15:29:55
Done.
|
+ 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); |
+ 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) { |
+ // TODO(gavinp): This seems very bad; for now we log at WARNING, but we |
+ // should give consideration to not saturating the log with these if that |
+ // becomes a problem. |
+ LOG(WARNING) << "Magic number did not match, saw " |
+ << header.initial_magic_number << " expecting " |
rvargas (doing something else)
2013/02/13 01:48:46
nit: consider removing the saw vs expected parts.
gavinp
2013/02/14 15:29:55
Done.
|
+ << kSimpleInitialMagicNumber; |
+ } |
+ |
+ if (header.version != kSimpleVersion) { |
+ LOG(INFO) << "Unreadable version. Got " << header.version |
rvargas (doing something else)
2013/02/13 01:48:46
nit: remove the variable parts?.
Shouldn't this be
gavinp
2013/02/14 15:29:55
Done.
|
+ << " but can only read " << kSimpleVersion; |
+ return false; |
+ } |
+ |
+ char key[4096]; |
+ DCHECK_LE(header.key_length, sizeof(key)); |
rvargas (doing something else)
2013/02/13 01:48:46
this doesn't work (in general).
gavinp
2013/02/14 15:29:55
Now replaced with the thing that works in general.
|
+ if (ReadPlatformFile(files_[i], sizeof(header), key, header.key_length) != |
+ implicit_cast<int>(header.key_length)) { |
rvargas (doing something else)
2013/02/13 01:48:46
why is key_length not an int?
gavinp
2013/02/14 15:29:55
Because there are no negative length keys.
rvargas (doing something else)
2013/02/14 21:09:30
That's not the principle that determines signed vs
gavinp
2013/02/15 16:04:01
Good point; I"d been assuming file formats were "d
|
+ LOG(ERROR) << "Cannot read key from entry."; |
rvargas (doing something else)
2013/02/13 01:48:46
nit: I would expect all failures from this method
gavinp
2013/02/14 15:29:55
Done.
|
+ return false; |
+ } |
+ if (header.key_length != key_.size() || |
+ std::memcmp(static_cast<const void*>(key_.data()), |
+ static_cast<const void*>(key), |
rvargas (doing something else)
2013/02/13 01:48:46
shouldn't need the casts here
gavinp
2013/02/14 15:29:55
Done.
|
+ key_.size()) != 0) { |
+ // TODO(gavinp): Since the way we use Entry SHA to name entries means this |
+ // is expected to occur at some frequency, add unit_tests that this does |
+ // is handled gracefully at higher levels. |
+ LOG(INFO) << "Key mismatch on open, expecting " << key_ |
+ << " but file stored " << std::string(key, header.key_length); |
+ return false; |
+ } |
+ |
+ if (base::Hash(key, header.key_length) != header.key_hash) { |
+ LOG(ERROR) << "Hash mismatch on key."; |
+ return false; |
+ } |
+ } |
+ |
+ initialized_ = true; |
+ return true; |
+} |
+ |
+bool SimpleSynchronousEntry::InitializeForCreate() { |
+ DCHECK(!initialized_); |
+ if (!OpenOrCreateFiles(true)) { |
+ LOG(ERROR) << "Could not create platform files for key " << key_; |
+ 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)) { |
+ // TODO(gavinp): Clean up created files. |
+ LOG(WARNING) << "Could not write headers to new cache entry."; |
+ return false; |
+ } |
+ |
+ if (WritePlatformFile(files_[i], sizeof(header), key_.data(), |
+ key_.size()) != implicit_cast<int>(key_.size())) { |
+ // TODO(gavinp): Clean up created files. |
+ LOG(WARNING) << "Could not write keys to new cache entry."; |
+ return false; |
+ } |
+ } |
+ |
+ initialized_ = true; |
+ return true; |
+} |
+ |
+} // namespace disk_cache |