Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: net/disk_cache/simple/simple_index_file.cc

Issue 14263005: Refactor our SimpleIndex file format and serialization to use Pickle instead of the previously bugg… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: syncing Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/disk_cache/simple/simple_index.h" 5 #include "net/disk_cache/simple/simple_index_file.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_util.h"
10 #include "base/hash.h"
11 #include "base/logging.h"
9 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/pickle.h"
10 #include "base/task_runner.h" 14 #include "base/task_runner.h"
11 #include "base/threading/worker_pool.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
14 #include "net/disk_cache/simple/simple_disk_format.h" 15 #include "net/disk_cache/simple/simple_disk_format.h"
16 #include "net/disk_cache/simple/simple_index_util.h"
15 #include "third_party/zlib/zlib.h" 17 #include "third_party/zlib/zlib.h"
16 18
17 namespace { 19 namespace {
18 20
19 const uint64 kMaxEntiresInIndex = 100000000; 21 const uint64 kMaxEntiresInIndex = 100000000;
20 22
21 bool CheckHeader(disk_cache::SimpleIndexFile::Header header) {
22 return header.number_of_entries <= kMaxEntiresInIndex &&
23 header.initial_magic_number ==
24 disk_cache::kSimpleIndexInitialMagicNumber &&
25 header.version == disk_cache::kSimpleVersion;
26 }
27
28 class FileAutoCloser {
29 public:
30 explicit FileAutoCloser(const base::PlatformFile& file) : file_(file) { }
31 ~FileAutoCloser() {
32 base::ClosePlatformFile(file_);
33 }
34 private:
35 base::PlatformFile file_;
36 DISALLOW_COPY_AND_ASSIGN(FileAutoCloser);
37 };
38
39 } // namespace 23 } // namespace
40 24
41 namespace disk_cache { 25 namespace disk_cache {
42 26
43 SimpleIndex::SimpleIndex( 27 SimpleIndexFile::IndexMetadata::IndexMetadata() :
44 const scoped_refptr<base::TaskRunner>& cache_thread, 28 initial_magic_number_(kSimpleIndexInitialMagicNumber),
45 const scoped_refptr<base::TaskRunner>& io_thread, 29 version_(kSimpleVersion),
46 const base::FilePath& path) 30 number_of_entries_(0),
47 : cache_size_(0), 31 cache_size_(0) {}
48 initialized_(false),
49 index_filename_(path.AppendASCII("simple-index")),
50 cache_thread_(cache_thread),
51 io_thread_(io_thread) {}
52 32
53 SimpleIndex::~SimpleIndex() { 33 SimpleIndexFile::IndexMetadata::IndexMetadata(
54 DCHECK(io_thread_checker_.CalledOnValidThread()); 34 uint64 number_of_entries, uint64 cache_size) :
35 initial_magic_number_(kSimpleIndexInitialMagicNumber),
36 version_(kSimpleVersion),
37 number_of_entries_(number_of_entries),
38 cache_size_(cache_size) {}
55 39
40 void SimpleIndexFile::IndexMetadata::Serialize(Pickle* pickle) const {
41 DCHECK(pickle);
42 pickle->WriteUInt64(initial_magic_number_);
43 pickle->WriteUInt32(version_);
44 pickle->WriteUInt64(number_of_entries_);
45 pickle->WriteUInt64(cache_size_);
56 } 46 }
57 47
58 void SimpleIndex::Initialize() { 48 bool SimpleIndexFile::IndexMetadata::DeSerialize(PickleIterator* it,
59 DCHECK(io_thread_checker_.CalledOnValidThread()); 49 IndexMetadata* out) {
60 MergeCallback merge_callback = base::Bind(&SimpleIndex::MergeInitializingSet, 50 DCHECK(it);
61 this->AsWeakPtr()); 51 DCHECK(out);
62 base::WorkerPool::PostTask(FROM_HERE, 52 return it->ReadUInt64(&(out->initial_magic_number_)) &&
63 base::Bind(&SimpleIndex::LoadFromDisk, 53 it->ReadUInt32(&(out->version_)) &&
64 index_filename_, 54 it->ReadUInt64(&(out->number_of_entries_))&&
65 io_thread_, 55 it->ReadUInt64(&(out->cache_size_));
66 merge_callback), 56 }
67 true); 57
58 bool SimpleIndexFile::IndexMetadata::CheckIndexMetadata() {
59 return number_of_entries_ <= kMaxEntiresInIndex &&
60 initial_magic_number_ == disk_cache::kSimpleIndexInitialMagicNumber &&
61 version_ == disk_cache::kSimpleVersion;
68 } 62 }
69 63
70 // static 64 // static
71 void SimpleIndex::LoadFromDisk( 65 void SimpleIndexFile::LoadFromDisk(
72 const base::FilePath& index_filename, 66 const base::FilePath& index_filename,
73 const scoped_refptr<base::TaskRunner>& io_thread, 67 const scoped_refptr<base::TaskRunner>& callback_runner,
74 const MergeCallback& merge_callback) { 68 const IndexCompletionCallback& completion_callback) {
75 // Open the index file. 69 std::string contents;
76 base::PlatformFileError error; 70 if(!file_util::ReadFileToString(index_filename,
77 base::PlatformFile index_file = base::CreatePlatformFile( 71 &contents)) {
Philippe 2013/04/15 13:57:57 Nit: this should fit on the line above.
felipeg 2013/04/15 14:39:07 Done.
78 index_filename, 72 LOG(WARNING) << "Could not read Simple Index file.";
79 base::PLATFORM_FILE_OPEN_ALWAYS | 73 return RestoreFromDisk(
80 base::PLATFORM_FILE_READ | 74 index_filename, callback_runner, completion_callback);
81 base::PLATFORM_FILE_WRITE,
82 NULL,
83 &error);
84 FileAutoCloser auto_close_index_file(index_file);
85 if (error != base::PLATFORM_FILE_OK) {
86 LOG(ERROR) << "Error opening file " << index_filename.value();
87 return RestoreFromDisk(index_filename, io_thread, merge_callback);
88 } 75 }
89 76
90 uLong incremental_crc = crc32(0L, Z_NULL, 0); 77 Pickle pickle (contents.data(), contents.size());
Philippe 2013/04/15 13:57:57 Nit: extra space after 'pickle'.
felipeg 2013/04/15 14:39:07 Done.
91 int64 index_file_offset = 0; 78 PickleIterator pickle_it(pickle);
92 SimpleIndexFile::Header header;
93 if (base::ReadPlatformFile(index_file,
94 index_file_offset,
95 reinterpret_cast<char*>(&header),
96 sizeof(header)) != sizeof(header)) {
97 return RestoreFromDisk(index_filename, io_thread, merge_callback);
98 }
99 index_file_offset += sizeof(header);
100 incremental_crc = crc32(incremental_crc,
101 reinterpret_cast<const Bytef*>(&header),
102 implicit_cast<uInt>(sizeof(header)));
103 79
104 if (!CheckHeader(header)) { 80 SimpleIndexFile::PickleHeader* header_p =
105 LOG(ERROR) << "Invalid header on Simple Cache Index."; 81 pickle.headerT<SimpleIndexFile::PickleHeader>();
106 return RestoreFromDisk(index_filename, io_thread, merge_callback); 82 const uint32 crc_read = header_p->crc;
83 const uint32 crc_calculated = CalculatePickleCRC(pickle);
84
85 if (crc_read != crc_calculated) {
86 LOG(WARNING) << "Invalid CRC in Simple Index file.";
87 return RestoreFromDisk(
88 index_filename, callback_runner, completion_callback);
107 } 89 }
108 90
109 const int entries_buffer_size = 91 SimpleIndexFile::IndexMetadata index_metadata;
110 header.number_of_entries * SimpleIndexFile::kEntryMetadataSize; 92 if (!SimpleIndexFile::IndexMetadata::DeSerialize(
93 &pickle_it, &index_metadata)) {
94 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index.";
95 return RestoreFromDisk(
96 index_filename, callback_runner, completion_callback);
97 }
111 98
112 scoped_ptr<char[]> entries_buffer(new char[entries_buffer_size]); 99 if (!index_metadata.CheckIndexMetadata()) {
113 if (base::ReadPlatformFile(index_file, 100 LOG(ERROR) << "Invalid index_metadata on Simple Cache Index.";
114 index_file_offset, 101 return RestoreFromDisk(
115 entries_buffer.get(), 102 index_filename, callback_runner, completion_callback);
116 entries_buffer_size) != entries_buffer_size) {
117 return RestoreFromDisk(index_filename, io_thread, merge_callback);
118 } 103 }
119 index_file_offset += entries_buffer_size;
120 incremental_crc = crc32(incremental_crc,
121 reinterpret_cast<const Bytef*>(entries_buffer.get()),
122 implicit_cast<uInt>(entries_buffer_size));
123
124 SimpleIndexFile::Footer footer;
125 if (base::ReadPlatformFile(index_file,
126 index_file_offset,
127 reinterpret_cast<char*>(&footer),
128 sizeof(footer)) != sizeof(footer)) {
129 return RestoreFromDisk(index_filename, io_thread, merge_callback);
130 }
131 const uint32 crc_read = footer.crc;
132 const uint32 crc_calculated = incremental_crc;
133 if (crc_read != crc_calculated)
134 return RestoreFromDisk(index_filename, io_thread, merge_callback);
135 104
136 scoped_ptr<EntrySet> index_file_entries(new EntrySet()); 105 scoped_ptr<EntrySet> index_file_entries(new EntrySet());
137 int entries_buffer_offset = 0; 106 while (index_file_entries->size() < index_metadata.GetNumberOfEntries()) {
138 while(entries_buffer_offset < entries_buffer_size) { 107 EntryMetadata entry_metadata;
139 SimpleIndexFile::EntryMetadata entry_metadata; 108 if (!EntryMetadata::DeSerialize(&pickle_it, &entry_metadata)) {
140 SimpleIndexFile::EntryMetadata::DeSerialize( 109 LOG(WARNING) << "Invalid EntryMetadata in Simple Index file.";
141 &entries_buffer.get()[entries_buffer_offset], &entry_metadata); 110 return RestoreFromDisk(
142 InsertInternal(index_file_entries.get(), entry_metadata); 111 index_filename, callback_runner, completion_callback);
143 entries_buffer_offset += SimpleIndexFile::kEntryMetadataSize; 112 }
113 InsertInEntrySet(index_file_entries.get(), entry_metadata);
144 } 114 }
145 DCHECK_EQ(header.number_of_entries, index_file_entries->size()); 115 callback_runner->PostTask(FROM_HERE,
146 116 base::Bind(completion_callback,
147 io_thread->PostTask(FROM_HERE, 117 base::Passed(&index_file_entries)));
148 base::Bind(merge_callback,
149 base::Passed(&index_file_entries)));
150 }
151
152 void SimpleIndex::Insert(const std::string& key) {
153 DCHECK(io_thread_checker_.CalledOnValidThread());
154 // Upon insert we don't know yet the size of the entry.
155 // It will be updated later when the SimpleEntryImpl finishes opening or
156 // creating the new entry, and then UpdateEntrySize will be called.
157 const uint64 hash_key = GetEntryHashKey(key);
158 InsertInternal(&entries_set_, SimpleIndexFile::EntryMetadata(
159 hash_key,
160 base::Time::Now(), 0));
161 if (!initialized_)
162 removed_entries_.erase(hash_key);
163 }
164
165 void SimpleIndex::Remove(const std::string& key) {
166 DCHECK(io_thread_checker_.CalledOnValidThread());
167 UpdateEntrySize(key, 0);
168 const uint64 hash_key = GetEntryHashKey(key);
169 entries_set_.erase(hash_key);
170
171 if (!initialized_)
172 removed_entries_.insert(hash_key);
173 }
174
175 bool SimpleIndex::Has(const std::string& key) const {
176 DCHECK(io_thread_checker_.CalledOnValidThread());
177 // If not initialized, always return true, forcing it to go to the disk.
178 return !initialized_ || entries_set_.count(GetEntryHashKey(key)) != 0;
179 }
180
181 bool SimpleIndex::UseIfExists(const std::string& key) {
182 DCHECK(io_thread_checker_.CalledOnValidThread());
183 // Always update the last used time, even if it is during initialization.
184 // It will be merged later.
185 EntrySet::iterator it = entries_set_.find(GetEntryHashKey(key));
186 if (it == entries_set_.end())
187 // If not initialized, always return true, forcing it to go to the disk.
188 return !initialized_;
189 it->second.SetLastUsedTime(base::Time::Now());
190 return true;
191 }
192
193 bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) {
194 DCHECK(io_thread_checker_.CalledOnValidThread());
195 EntrySet::iterator it = entries_set_.find(GetEntryHashKey(key));
196 if (it == entries_set_.end())
197 return false;
198
199 // Update the total cache size with the new entry size.
200 cache_size_ -= it->second.entry_size;
201 cache_size_ += entry_size;
202 it->second.entry_size = entry_size;
203
204 return true;
205 } 118 }
206 119
207 // static 120 // static
208 void SimpleIndex::InsertInternal( 121 void SimpleIndexFile::RestoreFromDisk(
209 EntrySet* entry_set,
210 const SimpleIndexFile::EntryMetadata& entry_metadata) {
211 // TODO(felipeg): Use a hash_set instead of a hash_map.
212 DCHECK(entry_set);
213 entry_set->insert(
214 std::make_pair(entry_metadata.GetHashKey(), entry_metadata));
215 }
216
217 // static
218 void SimpleIndex::RestoreFromDisk(
219 const base::FilePath& index_filename, 122 const base::FilePath& index_filename,
220 const scoped_refptr<base::TaskRunner>& io_thread, 123 const scoped_refptr<base::TaskRunner>& callback_runner,
221 const MergeCallback& merge_callback) { 124 const IndexCompletionCallback& completion_callback) {
222 using file_util::FileEnumerator; 125 using file_util::FileEnumerator;
223 LOG(INFO) << "Simple Cache Index is being restored from disk."; 126 LOG(INFO) << "Simple Cache Index is being restored from disk.";
224 127
225 file_util::Delete(index_filename, /* recursive = */ false); 128 file_util::Delete(index_filename, /* recursive = */ false);
226 scoped_ptr<EntrySet> index_file_entries(new EntrySet()); 129 scoped_ptr<EntrySet> index_file_entries(new EntrySet());
227 130
228 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. 131 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format.
229 COMPILE_ASSERT(kSimpleEntryFileCount == 3, 132 COMPILE_ASSERT(kSimpleEntryFileCount == 3,
230 file_pattern_must_match_file_count); 133 file_pattern_must_match_file_count);
231 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]"); 134 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
(...skipping 24 matching lines...) Expand all
256 // For POSIX systems, a last access time is available. However, it's not 159 // For POSIX systems, a last access time is available. However, it's not
257 // guaranteed to be more accurate than mtime. It is no worse though. 160 // guaranteed to be more accurate than mtime. It is no worse though.
258 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime); 161 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime);
259 #endif 162 #endif
260 if (last_used_time.is_null()) 163 if (last_used_time.is_null())
261 last_used_time = FileEnumerator::GetLastModifiedTime(find_info); 164 last_used_time = FileEnumerator::GetLastModifiedTime(find_info);
262 165
263 int64 file_size = FileEnumerator::GetFilesize(find_info); 166 int64 file_size = FileEnumerator::GetFilesize(find_info);
264 EntrySet::iterator it = index_file_entries->find(hash_key); 167 EntrySet::iterator it = index_file_entries->find(hash_key);
265 if (it == index_file_entries->end()) { 168 if (it == index_file_entries->end()) {
266 InsertInternal(index_file_entries.get(), SimpleIndexFile::EntryMetadata( 169 InsertInEntrySet(index_file_entries.get(), EntryMetadata(
267 hash_key, last_used_time, file_size)); 170 hash_key, last_used_time, file_size));
268 } else { 171 } else {
269 // Summing up the total size of the entry through all the *_[0-2] files 172 // Summing up the total size of the entry through all the *_[0-2] files
270 it->second.entry_size += file_size; 173 it->second.SetEntrySize(it->second.GetEntrySize() + file_size);
271 } 174 }
272 } 175 }
273 176
274 io_thread->PostTask(FROM_HERE, 177 callback_runner->PostTask(FROM_HERE,
275 base::Bind(merge_callback, 178 base::Bind(completion_callback,
276 base::Passed(&index_file_entries))); 179 base::Passed(&index_file_entries)));
277 }
278
279 void SimpleIndex::MergeInitializingSet(
280 scoped_ptr<EntrySet> index_file_entries) {
281 DCHECK(io_thread_checker_.CalledOnValidThread());
282 // First, remove the entries that are in the |removed_entries_| from both
283 // sets.
284 for (base::hash_set<uint64>::const_iterator it =
285 removed_entries_.begin(); it != removed_entries_.end(); ++it) {
286 entries_set_.erase(*it);
287 index_file_entries->erase(*it);
288 }
289
290 // Recalculate the cache size while merging the two sets.
291 cache_size_ = 0;
292 for (EntrySet::const_iterator it = index_file_entries->begin();
293 it != index_file_entries->end(); ++it) {
294 // If there is already an entry in the current entries_set_, we need to
295 // merge the new data there with the data loaded in the initialization.
296 EntrySet::iterator current_entry = entries_set_.find(it->first);
297 if (current_entry != entries_set_.end()) {
298 // When Merging, existing valid data in the |current_entry| will prevail.
299 SimpleIndexFile::EntryMetadata::Merge(
300 it->second, &(current_entry->second));
301 cache_size_ += current_entry->second.entry_size;
302 } else {
303 InsertInternal(&entries_set_, it->second);
304 cache_size_ += it->second.entry_size;
305 }
306 }
307
308 initialized_ = true;
309 }
310
311 void SimpleIndex::Serialize(std::string* out_buffer) {
312 DCHECK(io_thread_checker_.CalledOnValidThread());
313 DCHECK(out_buffer);
314 SimpleIndexFile::Header header;
315 SimpleIndexFile::Footer footer;
316
317 header.initial_magic_number = kSimpleIndexInitialMagicNumber;
318 header.version = kSimpleVersion;
319 header.number_of_entries = entries_set_.size();
320
321 out_buffer->reserve(
322 sizeof(header) +
323 sizeof(SimpleIndexFile::EntryMetadata) * entries_set_.size() +
324 sizeof(footer));
325
326 // The Header goes first.
327 out_buffer->append(reinterpret_cast<const char*>(&header),
328 sizeof(header));
329
330 // Then all the entries from |entries_set_|.
331 for (EntrySet::const_iterator it = entries_set_.begin();
332 it != entries_set_.end(); ++it) {
333 SimpleIndexFile::EntryMetadata::Serialize(it->second, out_buffer);
334 }
335
336 // Then, CRC.
337 footer.crc = crc32(crc32(0, Z_NULL, 0),
338 reinterpret_cast<const Bytef*>(out_buffer->data()),
339 implicit_cast<uInt>(out_buffer->size()));
340
341 out_buffer->append(reinterpret_cast<const char*>(&footer), sizeof(footer));
342 }
343
344 void SimpleIndex::WriteToDisk() {
345 DCHECK(io_thread_checker_.CalledOnValidThread());
346 scoped_ptr<std::string> buffer(new std::string());
347 Serialize(buffer.get());
348 cache_thread_->PostTask(FROM_HERE, base::Bind(
349 &SimpleIndex::UpdateFile,
350 index_filename_,
351 index_filename_.DirName().AppendASCII("index_temp"),
352 base::Passed(&buffer)));
353 } 180 }
354 181
355 // static 182 // static
356 void SimpleIndex::UpdateFile(const base::FilePath& index_filename, 183 scoped_ptr<Pickle> SimpleIndexFile::Serialize(
357 const base::FilePath& temp_filename, 184 const SimpleIndexFile::IndexMetadata& index_metadata,
358 scoped_ptr<std::string> buffer) { 185 const EntrySet& entries) {
186 scoped_ptr<Pickle> pickle(new Pickle(sizeof(SimpleIndexFile::PickleHeader)));
187
188 // Serialize the |index_metadata|.
Philippe 2013/04/15 13:57:57 Nit: I'm not sure this comment is really useful.
felipeg 2013/04/15 14:39:07 Done.
189 index_metadata.Serialize(pickle.get());
190 // Then all the entries from |entries| set.
191 for (EntrySet::const_iterator it = entries.begin();
192 it != entries.end(); ++it) {
193 it->second.Serialize(pickle.get());
194 }
195 SimpleIndexFile::PickleHeader* header_p =
196 pickle->headerT<SimpleIndexFile::PickleHeader>();
197 header_p->crc = CalculatePickleCRC(*pickle);
198 return pickle.Pass();
199 }
200
201 // static
202 void SimpleIndexFile::WriteToDisk(const base::FilePath& index_filename,
203 scoped_ptr<Pickle> pickle) {
204 const base::FilePath temp_filename =
205 index_filename.DirName().AppendASCII("index_temp");
359 int bytes_written = file_util::WriteFile( 206 int bytes_written = file_util::WriteFile(
360 temp_filename, buffer->data(), buffer->size()); 207 temp_filename,
361 DCHECK_EQ(bytes_written, implicit_cast<int>(buffer->size())); 208 reinterpret_cast<const char*>(pickle->data()),
362 if (bytes_written != static_cast<int>(buffer->size())) { 209 pickle->size());
210 DCHECK_EQ(bytes_written, implicit_cast<int>(pickle->size()));
211 if (bytes_written != static_cast<int>(pickle->size())) {
363 // TODO(felipeg): Add better error handling. 212 // TODO(felipeg): Add better error handling.
364 LOG(ERROR) << "Could not write Simple Cache index to temporary file: " 213 LOG(ERROR) << "Could not write Simple Cache index to temporary file: "
365 << temp_filename.value(); 214 << temp_filename.value();
366 file_util::Delete(temp_filename, /* recursive = */ false); 215 file_util::Delete(temp_filename, /* recursive = */ false);
367 return; 216 return;
368 } 217 }
369 // Swap temp and index_file. 218 // Swap temp and index_file.
370 bool result = file_util::ReplaceFile(temp_filename, index_filename); 219 bool result = file_util::ReplaceFile(temp_filename, index_filename);
371 DCHECK(result); 220 DCHECK(result);
372 } 221 }
373 222
223 // static
224 uint32 SimpleIndexFile::CalculatePickleCRC(const Pickle& pickle) {
225 return crc32(crc32(0, Z_NULL, 0),
226 reinterpret_cast<const Bytef*>(pickle.payload()),
227 implicit_cast<uInt>(pickle.payload_size()));
228 }
229
374 } // namespace disk_cache 230 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698