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

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

Issue 14295012: Revert 194347 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | net/disk_cache/simple/simple_index_file.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.h"
6 6
7 #include <utility>
8
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
11 #include "base/file_util.h"
12 #include "base/logging.h"
13 #include "base/message_loop.h" 9 #include "base/message_loop.h"
14 #include "base/pickle.h"
15 #include "base/task_runner.h" 10 #include "base/task_runner.h"
16 #include "base/threading/worker_pool.h" 11 #include "base/threading/worker_pool.h"
12 #include "net/base/io_buffer.h"
17 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
18 #include "net/disk_cache/simple/simple_entry_format.h" 14 #include "net/disk_cache/simple/simple_disk_format.h"
19 #include "net/disk_cache/simple/simple_index_file.h" 15 #include "third_party/zlib/zlib.h"
20 #include "net/disk_cache/simple/simple_util.h" 16
17 namespace {
18
19 const uint64 kMaxEntiresInIndex = 100000000;
20
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
21 40
22 namespace disk_cache { 41 namespace disk_cache {
23 42
24 EntryMetadata::EntryMetadata() :
25 hash_key_(0),
26 last_used_time_(0),
27 entry_size_(0)
28 {}
29
30
31 EntryMetadata::EntryMetadata(uint64 hash_key,
32 base::Time last_used_time,
33 uint64 entry_size) :
34 hash_key_(hash_key),
35 last_used_time_(last_used_time.ToInternalValue()),
36 entry_size_(entry_size)
37 {}
38
39 base::Time EntryMetadata::GetLastUsedTime() const {
40 return base::Time::FromInternalValue(last_used_time_);
41 }
42
43 void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) {
44 last_used_time_ = last_used_time.ToInternalValue();
45 }
46
47 void EntryMetadata::Serialize(Pickle* pickle) const {
48 DCHECK(pickle);
49 COMPILE_ASSERT(sizeof(EntryMetadata) ==
50 (sizeof(uint64) + sizeof(int64) + sizeof(uint64)),
51 EntryMetadata_has_three_member_variables);
52 pickle->WriteUInt64(hash_key_);
53 pickle->WriteInt64(last_used_time_);
54 pickle->WriteUInt64(entry_size_);
55 }
56
57 bool EntryMetadata::Deserialize(PickleIterator* it) {
58 DCHECK(it);
59 return it->ReadUInt64(&hash_key_) &&
60 it->ReadInt64(&last_used_time_) &&
61 it->ReadUInt64(&entry_size_);
62 }
63
64 void EntryMetadata::MergeWith(const EntryMetadata& from) {
65 DCHECK_EQ(hash_key_, from.hash_key_);
66 if (last_used_time_ == 0)
67 last_used_time_ = from.last_used_time_;
68 if (entry_size_ == 0)
69 entry_size_ = from.entry_size_;
70 }
71
72 SimpleIndex::SimpleIndex( 43 SimpleIndex::SimpleIndex(
73 const scoped_refptr<base::TaskRunner>& cache_thread, 44 const scoped_refptr<base::TaskRunner>& cache_thread,
74 const scoped_refptr<base::TaskRunner>& io_thread, 45 const scoped_refptr<base::TaskRunner>& io_thread,
75 const base::FilePath& path) 46 const base::FilePath& path)
76 : cache_size_(0), 47 : cache_size_(0),
77 initialized_(false), 48 initialized_(false),
78 index_filename_(path.AppendASCII("simple-index")), 49 index_filename_(path.AppendASCII("simple-index")),
79 cache_thread_(cache_thread), 50 cache_thread_(cache_thread),
80 io_thread_(io_thread) {} 51 io_thread_(io_thread) {}
81 52
82 SimpleIndex::~SimpleIndex() { 53 SimpleIndex::~SimpleIndex() {
83 DCHECK(io_thread_checker_.CalledOnValidThread()); 54 DCHECK(io_thread_checker_.CalledOnValidThread());
84 55
85 } 56 }
86 57
87 void SimpleIndex::Initialize() { 58 void SimpleIndex::Initialize() {
88 DCHECK(io_thread_checker_.CalledOnValidThread()); 59 DCHECK(io_thread_checker_.CalledOnValidThread());
89 IndexCompletionCallback merge_callback = 60 MergeCallback merge_callback = base::Bind(&SimpleIndex::MergeInitializingSet,
90 base::Bind(&SimpleIndex::MergeInitializingSet, AsWeakPtr()); 61 this->AsWeakPtr());
91 base::WorkerPool::PostTask(FROM_HERE, 62 base::WorkerPool::PostTask(FROM_HERE,
92 base::Bind(&SimpleIndex::LoadFromDisk, 63 base::Bind(&SimpleIndex::LoadFromDisk,
93 index_filename_, 64 index_filename_,
94 io_thread_, 65 io_thread_,
95 merge_callback), 66 merge_callback),
96 true); 67 true);
97 } 68 }
98 69
70 // static
71 void SimpleIndex::LoadFromDisk(
72 const base::FilePath& index_filename,
73 const scoped_refptr<base::TaskRunner>& io_thread,
74 const MergeCallback& merge_callback) {
75 // Open the index file.
76 base::PlatformFileError error;
77 base::PlatformFile index_file = base::CreatePlatformFile(
78 index_filename,
79 base::PLATFORM_FILE_OPEN_ALWAYS |
80 base::PLATFORM_FILE_READ |
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 }
89
90 uLong incremental_crc = crc32(0L, Z_NULL, 0);
91 int64 index_file_offset = 0;
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
104 if (!CheckHeader(header)) {
105 LOG(ERROR) << "Invalid header on Simple Cache Index.";
106 return RestoreFromDisk(index_filename, io_thread, merge_callback);
107 }
108
109 const int entries_buffer_size =
110 header.number_of_entries * SimpleIndexFile::kEntryMetadataSize;
111
112 scoped_ptr<char[]> entries_buffer(new char[entries_buffer_size]);
113 if (base::ReadPlatformFile(index_file,
114 index_file_offset,
115 entries_buffer.get(),
116 entries_buffer_size) != entries_buffer_size) {
117 return RestoreFromDisk(index_filename, io_thread, merge_callback);
118 }
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
136 scoped_ptr<EntrySet> index_file_entries(new EntrySet());
137 int entries_buffer_offset = 0;
138 while(entries_buffer_offset < entries_buffer_size) {
139 SimpleIndexFile::EntryMetadata entry_metadata;
140 SimpleIndexFile::EntryMetadata::DeSerialize(
141 &entries_buffer.get()[entries_buffer_offset], &entry_metadata);
142 InsertInternal(index_file_entries.get(), entry_metadata);
143 entries_buffer_offset += SimpleIndexFile::kEntryMetadataSize;
144 }
145 DCHECK_EQ(header.number_of_entries, index_file_entries->size());
146
147 io_thread->PostTask(FROM_HERE,
148 base::Bind(merge_callback,
149 base::Passed(&index_file_entries)));
150 }
151
99 void SimpleIndex::Insert(const std::string& key) { 152 void SimpleIndex::Insert(const std::string& key) {
100 DCHECK(io_thread_checker_.CalledOnValidThread()); 153 DCHECK(io_thread_checker_.CalledOnValidThread());
101 // Upon insert we don't know yet the size of the entry. 154 // Upon insert we don't know yet the size of the entry.
102 // It will be updated later when the SimpleEntryImpl finishes opening or 155 // It will be updated later when the SimpleEntryImpl finishes opening or
103 // creating the new entry, and then UpdateEntrySize will be called. 156 // creating the new entry, and then UpdateEntrySize will be called.
104 const uint64 hash_key = simple_util::GetEntryHashKey(key); 157 const uint64 hash_key = GetEntryHashKey(key);
105 InsertInEntrySet(EntryMetadata(hash_key, base::Time::Now(), 0), 158 InsertInternal(&entries_set_, SimpleIndexFile::EntryMetadata(
106 &entries_set_); 159 hash_key,
160 base::Time::Now(), 0));
107 if (!initialized_) 161 if (!initialized_)
108 removed_entries_.erase(hash_key); 162 removed_entries_.erase(hash_key);
109 } 163 }
110 164
111 void SimpleIndex::Remove(const std::string& key) { 165 void SimpleIndex::Remove(const std::string& key) {
112 DCHECK(io_thread_checker_.CalledOnValidThread()); 166 DCHECK(io_thread_checker_.CalledOnValidThread());
113 UpdateEntrySize(key, 0); 167 UpdateEntrySize(key, 0);
114 const uint64 hash_key = simple_util::GetEntryHashKey(key); 168 const uint64 hash_key = GetEntryHashKey(key);
115 entries_set_.erase(hash_key); 169 entries_set_.erase(hash_key);
116 170
117 if (!initialized_) 171 if (!initialized_)
118 removed_entries_.insert(hash_key); 172 removed_entries_.insert(hash_key);
119 } 173 }
120 174
121 bool SimpleIndex::Has(const std::string& key) const { 175 bool SimpleIndex::Has(const std::string& key) const {
122 DCHECK(io_thread_checker_.CalledOnValidThread()); 176 DCHECK(io_thread_checker_.CalledOnValidThread());
123 // If not initialized, always return true, forcing it to go to the disk. 177 // If not initialized, always return true, forcing it to go to the disk.
124 return !initialized_ || 178 return !initialized_ || entries_set_.count(GetEntryHashKey(key)) != 0;
125 entries_set_.count(simple_util::GetEntryHashKey(key)) != 0;
126 } 179 }
127 180
128 bool SimpleIndex::UseIfExists(const std::string& key) { 181 bool SimpleIndex::UseIfExists(const std::string& key) {
129 DCHECK(io_thread_checker_.CalledOnValidThread()); 182 DCHECK(io_thread_checker_.CalledOnValidThread());
130 // Always update the last used time, even if it is during initialization. 183 // Always update the last used time, even if it is during initialization.
131 // It will be merged later. 184 // It will be merged later.
132 EntrySet::iterator it = entries_set_.find(simple_util::GetEntryHashKey(key)); 185 EntrySet::iterator it = entries_set_.find(GetEntryHashKey(key));
133 if (it == entries_set_.end()) 186 if (it == entries_set_.end())
134 // If not initialized, always return true, forcing it to go to the disk. 187 // If not initialized, always return true, forcing it to go to the disk.
135 return !initialized_; 188 return !initialized_;
136 it->second.SetLastUsedTime(base::Time::Now()); 189 it->second.SetLastUsedTime(base::Time::Now());
137 return true; 190 return true;
138 } 191 }
139 192
140 bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) { 193 bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) {
141 DCHECK(io_thread_checker_.CalledOnValidThread()); 194 DCHECK(io_thread_checker_.CalledOnValidThread());
142 EntrySet::iterator it = entries_set_.find(simple_util::GetEntryHashKey(key)); 195 EntrySet::iterator it = entries_set_.find(GetEntryHashKey(key));
143 if (it == entries_set_.end()) 196 if (it == entries_set_.end())
144 return false; 197 return false;
145 198
146 // Update the total cache size with the new entry size. 199 // Update the total cache size with the new entry size.
147 cache_size_ -= it->second.GetEntrySize(); 200 cache_size_ -= it->second.entry_size;
148 cache_size_ += entry_size; 201 cache_size_ += entry_size;
149 it->second.SetEntrySize(entry_size); 202 it->second.entry_size = entry_size;
150 203
151 return true; 204 return true;
152 } 205 }
153 206
154 // static 207 // static
155 void SimpleIndex::InsertInEntrySet( 208 void SimpleIndex::InsertInternal(
156 const disk_cache::EntryMetadata& entry_metadata, 209 EntrySet* entry_set,
157 EntrySet* entry_set) { 210 const SimpleIndexFile::EntryMetadata& entry_metadata) {
211 // TODO(felipeg): Use a hash_set instead of a hash_map.
158 DCHECK(entry_set); 212 DCHECK(entry_set);
159 entry_set->insert( 213 entry_set->insert(
160 std::make_pair(entry_metadata.GetHashKey(), entry_metadata)); 214 std::make_pair(entry_metadata.GetHashKey(), entry_metadata));
161 } 215 }
162 216
163 // static 217 // static
164 void SimpleIndex::LoadFromDisk( 218 void SimpleIndex::RestoreFromDisk(
165 const base::FilePath& index_filename, 219 const base::FilePath& index_filename,
166 const scoped_refptr<base::TaskRunner>& io_thread, 220 const scoped_refptr<base::TaskRunner>& io_thread,
167 const IndexCompletionCallback& completion_callback) { 221 const MergeCallback& merge_callback) {
168 scoped_ptr<EntrySet> index_file_entries =
169 SimpleIndexFile::LoadFromDisk(index_filename);
170
171 if (!index_file_entries.get())
172 index_file_entries = SimpleIndex::RestoreFromDisk(index_filename);
173
174 io_thread->PostTask(FROM_HERE,
175 base::Bind(completion_callback,
176 base::Passed(&index_file_entries)));
177 }
178
179 // static
180 scoped_ptr<SimpleIndex::EntrySet> SimpleIndex::RestoreFromDisk(
181 const base::FilePath& index_filename) {
182 using file_util::FileEnumerator; 222 using file_util::FileEnumerator;
183 LOG(INFO) << "Simple Cache Index is being restored from disk."; 223 LOG(INFO) << "Simple Cache Index is being restored from disk.";
184 224
185 file_util::Delete(index_filename, /* recursive = */ false); 225 file_util::Delete(index_filename, /* recursive = */ false);
186 scoped_ptr<EntrySet> index_file_entries(new EntrySet()); 226 scoped_ptr<EntrySet> index_file_entries(new EntrySet());
187 227
188 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. 228 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format.
189 COMPILE_ASSERT(kSimpleEntryFileCount == 3, 229 COMPILE_ASSERT(kSimpleEntryFileCount == 3,
190 file_pattern_must_match_file_count); 230 file_pattern_must_match_file_count);
191
192 const int kFileSuffixLenght = std::string("_0").size();
193 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]"); 231 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
194 FileEnumerator enumerator(index_filename.DirName(), 232 FileEnumerator enumerator(index_filename.DirName(),
195 false /* recursive */, 233 false /* recursive */,
196 FileEnumerator::FILES, 234 FileEnumerator::FILES,
197 file_pattern); 235 file_pattern);
198 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); 236 for (base::FilePath file_path = enumerator.Next(); !file_path.empty();
199 file_path = enumerator.Next()) { 237 file_path = enumerator.Next()) {
200 const base::FilePath::StringType base_name = file_path.BaseName().value(); 238 const base::FilePath::StringType base_name = file_path.BaseName().value();
201 // Converting to std::string is OK since we never use UTF8 wide chars in our 239 // Converting to std::string is OK since we never use UTF8 wide chars in our
202 // file names. 240 // file names.
203 const std::string hash_name(base_name.begin(), base_name.end()); 241 const std::string hash_name(base_name.begin(), base_name.end());
204 const std::string hash_key_string = 242 const std::string hash_key_string =
205 hash_name.substr(0, hash_name.size() - kFileSuffixLenght); 243 hash_name.substr(0, kEntryHashKeyAsHexStringSize);
206 uint64 hash_key = 0; 244 uint64 hash_key = 0;
207 if (!simple_util::GetEntryHashKeyFromHexString( 245 if (!GetEntryHashKeyFromHexString(hash_key_string, &hash_key)) {
208 hash_key_string, &hash_key)) {
209 LOG(WARNING) << "Invalid Entry Hash Key filename while restoring " 246 LOG(WARNING) << "Invalid Entry Hash Key filename while restoring "
210 << "Simple Index from disk: " << hash_name; 247 << "Simple Index from disk: " << hash_name;
211 // TODO(felipeg): Should we delete the invalid file here ? 248 // TODO(felipeg): Should we delete the invalid file here ?
212 continue; 249 continue;
213 } 250 }
214 251
215 FileEnumerator::FindInfo find_info = {}; 252 FileEnumerator::FindInfo find_info = {};
216 enumerator.GetFindInfo(&find_info); 253 enumerator.GetFindInfo(&find_info);
217 base::Time last_used_time; 254 base::Time last_used_time;
218 #if defined(OS_POSIX) 255 #if defined(OS_POSIX)
219 // For POSIX systems, a last access time is available. However, it's not 256 // For POSIX systems, a last access time is available. However, it's not
220 // guaranteed to be more accurate than mtime. It is no worse though. 257 // guaranteed to be more accurate than mtime. It is no worse though.
221 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime); 258 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime);
222 #endif 259 #endif
223 if (last_used_time.is_null()) 260 if (last_used_time.is_null())
224 last_used_time = FileEnumerator::GetLastModifiedTime(find_info); 261 last_used_time = FileEnumerator::GetLastModifiedTime(find_info);
225 262
226 int64 file_size = FileEnumerator::GetFilesize(find_info); 263 int64 file_size = FileEnumerator::GetFilesize(find_info);
227 EntrySet::iterator it = index_file_entries->find(hash_key); 264 EntrySet::iterator it = index_file_entries->find(hash_key);
228 if (it == index_file_entries->end()) { 265 if (it == index_file_entries->end()) {
229 InsertInEntrySet(EntryMetadata(hash_key, last_used_time, file_size), 266 InsertInternal(index_file_entries.get(), SimpleIndexFile::EntryMetadata(
230 index_file_entries.get()); 267 hash_key, last_used_time, file_size));
231 } else { 268 } else {
232 // Summing up the total size of the entry through all the *_[0-2] files 269 // Summing up the total size of the entry through all the *_[0-2] files
233 it->second.SetEntrySize(it->second.GetEntrySize() + file_size); 270 it->second.entry_size += file_size;
234 } 271 }
235 } 272 }
236 return index_file_entries.Pass();
237 }
238 273
239 274 io_thread->PostTask(FROM_HERE,
240 // static 275 base::Bind(merge_callback,
241 void SimpleIndex::WriteToDiskInternal(const base::FilePath& index_filename, 276 base::Passed(&index_file_entries)));
242 scoped_ptr<Pickle> pickle) {
243 SimpleIndexFile::WriteToDisk(index_filename, *pickle);
244 } 277 }
245 278
246 void SimpleIndex::MergeInitializingSet( 279 void SimpleIndex::MergeInitializingSet(
247 scoped_ptr<EntrySet> index_file_entries) { 280 scoped_ptr<EntrySet> index_file_entries) {
248 DCHECK(io_thread_checker_.CalledOnValidThread()); 281 DCHECK(io_thread_checker_.CalledOnValidThread());
249 // First, remove the entries that are in the |removed_entries_| from both 282 // First, remove the entries that are in the |removed_entries_| from both
250 // sets. 283 // sets.
251 for (base::hash_set<uint64>::const_iterator it = 284 for (base::hash_set<uint64>::const_iterator it =
252 removed_entries_.begin(); it != removed_entries_.end(); ++it) { 285 removed_entries_.begin(); it != removed_entries_.end(); ++it) {
253 entries_set_.erase(*it); 286 entries_set_.erase(*it);
254 index_file_entries->erase(*it); 287 index_file_entries->erase(*it);
255 } 288 }
256 289
257 // Recalculate the cache size while merging the two sets. 290 // Recalculate the cache size while merging the two sets.
258 cache_size_ = 0; 291 cache_size_ = 0;
259 for (EntrySet::const_iterator it = index_file_entries->begin(); 292 for (EntrySet::const_iterator it = index_file_entries->begin();
260 it != index_file_entries->end(); ++it) { 293 it != index_file_entries->end(); ++it) {
261 // If there is already an entry in the current entries_set_, we need to 294 // If there is already an entry in the current entries_set_, we need to
262 // merge the new data there with the data loaded in the initialization. 295 // merge the new data there with the data loaded in the initialization.
263 EntrySet::iterator current_entry = entries_set_.find(it->first); 296 EntrySet::iterator current_entry = entries_set_.find(it->first);
264 if (current_entry != entries_set_.end()) { 297 if (current_entry != entries_set_.end()) {
265 // When Merging, existing valid data in the |current_entry| will prevail. 298 // When Merging, existing valid data in the |current_entry| will prevail.
266 current_entry->second.MergeWith(it->second); 299 SimpleIndexFile::EntryMetadata::Merge(
267 cache_size_ += current_entry->second.GetEntrySize(); 300 it->second, &(current_entry->second));
301 cache_size_ += current_entry->second.entry_size;
268 } else { 302 } else {
269 InsertInEntrySet(it->second, &entries_set_); 303 InsertInternal(&entries_set_, it->second);
270 cache_size_ += it->second.GetEntrySize(); 304 cache_size_ += it->second.entry_size;
271 } 305 }
272 } 306 }
273 307
274 initialized_ = true; 308 initialized_ = true;
275 } 309 }
276 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
277 void SimpleIndex::WriteToDisk() { 344 void SimpleIndex::WriteToDisk() {
278 DCHECK(io_thread_checker_.CalledOnValidThread()); 345 DCHECK(io_thread_checker_.CalledOnValidThread());
279 SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), 346 scoped_ptr<std::string> buffer(new std::string());
280 cache_size_); 347 Serialize(buffer.get());
281 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata,
282 entries_set_);
283 cache_thread_->PostTask(FROM_HERE, base::Bind( 348 cache_thread_->PostTask(FROM_HERE, base::Bind(
284 &SimpleIndex::WriteToDiskInternal, 349 &SimpleIndex::UpdateFile,
285 index_filename_, 350 index_filename_,
286 base::Passed(&pickle))); 351 index_filename_.DirName().AppendASCII("index_temp"),
352 base::Passed(&buffer)));
353 }
354
355 // static
356 void SimpleIndex::UpdateFile(const base::FilePath& index_filename,
357 const base::FilePath& temp_filename,
358 scoped_ptr<std::string> buffer) {
359 int bytes_written = file_util::WriteFile(
360 temp_filename, buffer->data(), buffer->size());
361 DCHECK_EQ(bytes_written, implicit_cast<int>(buffer->size()));
362 if (bytes_written != static_cast<int>(buffer->size())) {
363 // TODO(felipeg): Add better error handling.
364 LOG(ERROR) << "Could not write Simple Cache index to temporary file: "
365 << temp_filename.value();
366 file_util::Delete(temp_filename, /* recursive = */ false);
367 return;
368 }
369 // Swap temp and index_file.
370 bool result = file_util::ReplaceFile(temp_filename, index_filename);
371 DCHECK(result);
287 } 372 }
288 373
289 } // namespace disk_cache 374 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | net/disk_cache/simple/simple_index_file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698