OLD | NEW |
---|---|
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_synchronous_entry.h" | 5 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <functional> | 9 #include <functional> |
10 #include <limits> | 10 #include <limits> |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 | 139 |
140 } // namespace | 140 } // namespace |
141 | 141 |
142 namespace disk_cache { | 142 namespace disk_cache { |
143 | 143 |
144 using simple_util::ConvertEntryHashKeyToHexString; | 144 using simple_util::ConvertEntryHashKeyToHexString; |
145 using simple_util::GetEntryHashKey; | 145 using simple_util::GetEntryHashKey; |
146 using simple_util::GetFilenameFromEntryHashAndIndex; | 146 using simple_util::GetFilenameFromEntryHashAndIndex; |
147 using simple_util::GetDataSizeFromKeyAndFileSize; | 147 using simple_util::GetDataSizeFromKeyAndFileSize; |
148 using simple_util::GetFileSizeFromKeyAndDataSize; | 148 using simple_util::GetFileSizeFromKeyAndDataSize; |
149 using simple_util::GetFileOffsetFromKeyAndDataOffset; | 149 using simple_util::GetFileIndexFromStreamIndex; |
150 using simple_util::GetLastEOFRecordOffset; | |
151 using simple_util::GetFileOffsetFromDataOffset; | |
150 | 152 |
151 SimpleEntryStat::SimpleEntryStat() {} | 153 SimpleEntryStat::SimpleEntryStat() {} |
152 | 154 |
153 SimpleEntryStat::SimpleEntryStat(base::Time last_used_p, | 155 SimpleEntryStat::SimpleEntryStat(base::Time last_used_p, |
154 base::Time last_modified_p, | 156 base::Time last_modified_p, |
155 const int32 data_size_p[]) | 157 const int32 data_size_p[]) |
156 : last_used(last_used_p), | 158 : last_used(last_used_p), |
157 last_modified(last_modified_p) { | 159 last_modified(last_modified_p) { |
158 memcpy(data_size, data_size_p, sizeof(data_size)); | 160 memcpy(data_size, data_size_p, sizeof(data_size)); |
159 } | 161 } |
160 | 162 |
163 int SimpleEntryStat::GetOffsetInFile(const std::string& key, | |
164 int offset, | |
165 int stream_index) const { | |
166 return GetFileOffsetFromDataOffset(key, offset, stream_index, data_size[1]); | |
167 } | |
168 | |
169 int SimpleEntryStat::GetEOFOffsetInFile(const std::string& key, | |
170 int stream_index) const { | |
171 return GetFileOffsetFromDataOffset( | |
172 key, data_size[stream_index], stream_index, data_size[1]); | |
173 } | |
174 | |
175 int SimpleEntryStat::GetLastEOFOffsetInFile(const std::string& key, | |
176 int stream_index) const { | |
177 return GetOffsetInFile( | |
178 key, | |
179 GetLastEOFRecordOffset(GetFileIndexFromStreamIndex(stream_index), | |
180 data_size), | |
181 stream_index); | |
182 } | |
183 | |
161 SimpleEntryCreationResults::SimpleEntryCreationResults( | 184 SimpleEntryCreationResults::SimpleEntryCreationResults( |
162 SimpleEntryStat entry_stat) | 185 SimpleEntryStat entry_stat) |
163 : sync_entry(NULL), | 186 : sync_entry(NULL), |
164 entry_stat(entry_stat), | 187 entry_stat(entry_stat), |
165 result(net::OK) { | 188 result(net::OK) { |
166 } | 189 } |
167 | 190 |
168 SimpleEntryCreationResults::~SimpleEntryCreationResults() { | 191 SimpleEntryCreationResults::~SimpleEntryCreationResults() { |
169 } | 192 } |
170 | 193 |
(...skipping 28 matching lines...) Expand all Loading... | |
199 // static | 222 // static |
200 void SimpleSynchronousEntry::OpenEntry( | 223 void SimpleSynchronousEntry::OpenEntry( |
201 net::CacheType cache_type, | 224 net::CacheType cache_type, |
202 const FilePath& path, | 225 const FilePath& path, |
203 const uint64 entry_hash, | 226 const uint64 entry_hash, |
204 bool had_index, | 227 bool had_index, |
205 SimpleEntryCreationResults *out_results) { | 228 SimpleEntryCreationResults *out_results) { |
206 SimpleSynchronousEntry* sync_entry = | 229 SimpleSynchronousEntry* sync_entry = |
207 new SimpleSynchronousEntry(cache_type, path, "", entry_hash); | 230 new SimpleSynchronousEntry(cache_type, path, "", entry_hash); |
208 out_results->result = sync_entry->InitializeForOpen( | 231 out_results->result = sync_entry->InitializeForOpen( |
209 had_index, &out_results->entry_stat); | 232 had_index, &out_results->entry_stat, &out_results->stream_0_data); |
210 if (out_results->result != net::OK) { | 233 if (out_results->result != net::OK) { |
211 sync_entry->Doom(); | 234 sync_entry->Doom(); |
212 delete sync_entry; | 235 delete sync_entry; |
213 out_results->sync_entry = NULL; | 236 out_results->sync_entry = NULL; |
237 out_results->stream_0_data = NULL; | |
214 return; | 238 return; |
215 } | 239 } |
216 out_results->sync_entry = sync_entry; | 240 out_results->sync_entry = sync_entry; |
217 } | 241 } |
218 | 242 |
219 // static | 243 // static |
220 void SimpleSynchronousEntry::CreateEntry( | 244 void SimpleSynchronousEntry::CreateEntry( |
221 net::CacheType cache_type, | 245 net::CacheType cache_type, |
222 const FilePath& path, | 246 const FilePath& path, |
223 const std::string& key, | 247 const std::string& key, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 const FilePath& path) { | 296 const FilePath& path) { |
273 const size_t did_delete_count = std::count_if( | 297 const size_t did_delete_count = std::count_if( |
274 key_hashes->begin(), key_hashes->end(), std::bind1st( | 298 key_hashes->begin(), key_hashes->end(), std::bind1st( |
275 std::ptr_fun(SimpleSynchronousEntry::DeleteFilesForEntryHash), path)); | 299 std::ptr_fun(SimpleSynchronousEntry::DeleteFilesForEntryHash), path)); |
276 return (did_delete_count == key_hashes->size()) ? net::OK : net::ERR_FAILED; | 300 return (did_delete_count == key_hashes->size()) ? net::OK : net::ERR_FAILED; |
277 } | 301 } |
278 | 302 |
279 void SimpleSynchronousEntry::ReadData(const EntryOperationData& in_entry_op, | 303 void SimpleSynchronousEntry::ReadData(const EntryOperationData& in_entry_op, |
280 net::IOBuffer* out_buf, | 304 net::IOBuffer* out_buf, |
281 uint32* out_crc32, | 305 uint32* out_crc32, |
282 base::Time* out_last_used, | 306 SimpleEntryStat* out_entry_stat, |
283 int* out_result) const { | 307 int* out_result) const { |
284 DCHECK(initialized_); | 308 DCHECK(initialized_); |
285 int64 file_offset = | 309 const int64 file_offset = out_entry_stat->GetOffsetInFile( |
286 GetFileOffsetFromKeyAndDataOffset(key_, in_entry_op.offset); | 310 key_, in_entry_op.offset, in_entry_op.index); |
287 int bytes_read = ReadPlatformFile(files_[in_entry_op.index], | 311 int file_index = GetFileIndexFromStreamIndex(in_entry_op.index); |
288 file_offset, | 312 int bytes_read = ReadPlatformFile( |
289 out_buf->data(), | 313 files_[file_index], file_offset, out_buf->data(), in_entry_op.buf_len); |
290 in_entry_op.buf_len); | |
291 if (bytes_read > 0) { | 314 if (bytes_read > 0) { |
292 *out_last_used = Time::Now(); | 315 out_entry_stat->last_used = Time::Now(); |
293 *out_crc32 = crc32(crc32(0L, Z_NULL, 0), | 316 *out_crc32 = crc32(crc32(0L, Z_NULL, 0), |
294 reinterpret_cast<const Bytef*>(out_buf->data()), | 317 reinterpret_cast<const Bytef*>(out_buf->data()), |
295 bytes_read); | 318 bytes_read); |
296 } | 319 } |
297 if (bytes_read >= 0) { | 320 if (bytes_read >= 0) { |
298 *out_result = bytes_read; | 321 *out_result = bytes_read; |
299 } else { | 322 } else { |
300 *out_result = net::ERR_CACHE_READ_FAILURE; | 323 *out_result = net::ERR_CACHE_READ_FAILURE; |
301 Doom(); | 324 Doom(); |
302 } | 325 } |
303 } | 326 } |
304 | 327 |
305 void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op, | 328 void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op, |
306 net::IOBuffer* in_buf, | 329 net::IOBuffer* in_buf, |
307 SimpleEntryStat* out_entry_stat, | 330 SimpleEntryStat* out_entry_stat, |
308 int* out_result) const { | 331 int* out_result) const { |
309 DCHECK(initialized_); | 332 DCHECK(initialized_); |
310 int index = in_entry_op.index; | 333 int index = in_entry_op.index; |
334 int file_index = GetFileIndexFromStreamIndex(index); | |
311 int offset = in_entry_op.offset; | 335 int offset = in_entry_op.offset; |
312 int buf_len = in_entry_op.buf_len; | 336 int buf_len = in_entry_op.buf_len; |
313 int truncate = in_entry_op.truncate; | 337 int truncate = in_entry_op.truncate; |
314 | 338 const int64 file_offset = out_entry_stat->GetOffsetInFile( |
339 key_, in_entry_op.offset, in_entry_op.index); | |
315 bool extending_by_write = offset + buf_len > out_entry_stat->data_size[index]; | 340 bool extending_by_write = offset + buf_len > out_entry_stat->data_size[index]; |
316 if (extending_by_write) { | 341 if (extending_by_write) { |
317 // We are extending the file, and need to insure the EOF record is zeroed. | 342 // The EOF record and the eventual stream afterward need to be zeroed out. |
318 const int64 file_eof_offset = GetFileOffsetFromKeyAndDataOffset( | 343 const int64 file_eof_offset = |
319 key_, out_entry_stat->data_size[index]); | 344 out_entry_stat->GetEOFOffsetInFile(key_, index); |
320 if (!TruncatePlatformFile(files_[index], file_eof_offset)) { | 345 if (!TruncatePlatformFile(files_[file_index], file_eof_offset)) { |
321 RecordWriteResult(cache_type_, WRITE_RESULT_PRETRUNCATE_FAILURE); | 346 RecordWriteResult(cache_type_, WRITE_RESULT_PRETRUNCATE_FAILURE); |
322 Doom(); | 347 Doom(); |
323 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 348 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
324 return; | 349 return; |
325 } | 350 } |
326 } | 351 } |
327 const int64 file_offset = GetFileOffsetFromKeyAndDataOffset(key_, offset); | |
328 if (buf_len > 0) { | 352 if (buf_len > 0) { |
329 if (WritePlatformFile( | 353 if (WritePlatformFile( |
330 files_[index], file_offset, in_buf->data(), buf_len) != buf_len) { | 354 files_[file_index], file_offset, in_buf->data(), buf_len) != |
355 buf_len) { | |
331 RecordWriteResult(cache_type_, WRITE_RESULT_WRITE_FAILURE); | 356 RecordWriteResult(cache_type_, WRITE_RESULT_WRITE_FAILURE); |
332 Doom(); | 357 Doom(); |
333 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 358 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
334 return; | 359 return; |
335 } | 360 } |
336 } | 361 } |
337 if (!truncate && (buf_len > 0 || !extending_by_write)) { | 362 if (!truncate && (buf_len > 0 || !extending_by_write)) { |
338 out_entry_stat->data_size[index] = | 363 out_entry_stat->data_size[index] = |
339 std::max(out_entry_stat->data_size[index], offset + buf_len); | 364 std::max(out_entry_stat->data_size[index], offset + buf_len); |
340 } else { | 365 } else { |
341 if (!TruncatePlatformFile(files_[index], file_offset + buf_len)) { | 366 out_entry_stat->data_size[index] = offset + buf_len; |
367 int file_eof_offset = out_entry_stat->GetLastEOFOffsetInFile(key_, index); | |
368 if (!TruncatePlatformFile(files_[file_index], file_eof_offset)) { | |
342 RecordWriteResult(cache_type_, WRITE_RESULT_TRUNCATE_FAILURE); | 369 RecordWriteResult(cache_type_, WRITE_RESULT_TRUNCATE_FAILURE); |
343 Doom(); | 370 Doom(); |
344 *out_result = net::ERR_CACHE_WRITE_FAILURE; | 371 *out_result = net::ERR_CACHE_WRITE_FAILURE; |
345 return; | 372 return; |
346 } | 373 } |
347 out_entry_stat->data_size[index] = offset + buf_len; | |
348 } | 374 } |
349 | 375 |
350 RecordWriteResult(cache_type_, WRITE_RESULT_SUCCESS); | 376 RecordWriteResult(cache_type_, WRITE_RESULT_SUCCESS); |
351 out_entry_stat->last_used = out_entry_stat->last_modified = Time::Now(); | 377 out_entry_stat->last_used = out_entry_stat->last_modified = Time::Now(); |
352 *out_result = buf_len; | 378 *out_result = buf_len; |
353 } | 379 } |
354 | 380 |
355 void SimpleSynchronousEntry::CheckEOFRecord(int index, | 381 void SimpleSynchronousEntry::CheckEOFRecord(int index, |
356 int32 data_size, | 382 const SimpleEntryStat& entry_stat, |
357 uint32 expected_crc32, | 383 uint32 expected_crc32, |
358 int* out_result) const { | 384 int* out_result) const { |
359 DCHECK(initialized_); | 385 DCHECK(initialized_); |
360 | 386 uint32 crc32; |
361 SimpleFileEOF eof_record; | 387 bool has_crc32; |
362 int64 file_offset = GetFileOffsetFromKeyAndDataOffset(key_, data_size); | 388 int stream_size; |
363 if (ReadPlatformFile(files_[index], | 389 *out_result = |
364 file_offset, | 390 GetEOFRecordData(index, entry_stat, &has_crc32, &crc32, &stream_size); |
365 reinterpret_cast<char*>(&eof_record), | 391 if (*out_result != net::OK) { |
366 sizeof(eof_record)) != sizeof(eof_record)) { | |
367 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); | |
368 Doom(); | 392 Doom(); |
369 *out_result = net::ERR_CACHE_CHECKSUM_READ_FAILURE; | |
370 return; | 393 return; |
371 } | 394 } |
372 | 395 if (has_crc32 && crc32 != expected_crc32) { |
373 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { | 396 DLOG(INFO) << "Eof record had bad crc."; |
374 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); | 397 *out_result = net::ERR_CACHE_CHECKSUM_MISMATCH; |
375 DLOG(INFO) << "eof record had bad magic number."; | 398 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); |
376 Doom(); | 399 Doom(); |
377 *out_result = net::ERR_CACHE_CHECKSUM_READ_FAILURE; | |
378 return; | 400 return; |
379 } | 401 } |
380 | |
381 const bool has_crc = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) == | |
382 SimpleFileEOF::FLAG_HAS_CRC32; | |
383 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, has_crc); | |
384 if (has_crc && eof_record.data_crc32 != expected_crc32) { | |
385 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | |
386 DLOG(INFO) << "eof record had bad crc."; | |
387 Doom(); | |
388 *out_result = net::ERR_CACHE_CHECKSUM_MISMATCH; | |
389 return; | |
390 } | |
391 | |
392 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | 402 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); |
393 *out_result = net::OK; | |
394 } | 403 } |
395 | 404 |
396 void SimpleSynchronousEntry::Close( | 405 void SimpleSynchronousEntry::Close( |
397 const SimpleEntryStat& entry_stat, | 406 const SimpleEntryStat& entry_stat, |
398 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write) { | 407 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write, |
408 net::GrowableIOBuffer* stream_0_data) { | |
409 if (!stream_0_data) { | |
gavinp
2013/09/17 13:27:11
What's happening in this case?
clamy
2013/09/17 13:59:42
If we were supposed to have data and we don't, it'
gavinp
2013/09/17 14:25:33
In practice we always have data in stream 0, but i
clamy
2013/09/17 14:29:48
If we get non 0 size in data_size[0], then we expe
gavinp
2013/09/17 15:07:43
Aha, yup. So you were right the first time, this s
clamy
2013/09/18 16:17:14
With the other modifications that went in, we shou
| |
410 if (entry_stat.data_size[0]) | |
411 Doom(); | |
412 } else { | |
413 // Write stream 0 data. | |
414 int stream_0_offset = entry_stat.GetOffsetInFile(key_, 0, 0); | |
415 if (WritePlatformFile(files_[0], | |
416 stream_0_offset, | |
417 stream_0_data->data(), | |
418 entry_stat.data_size[0]) != entry_stat.data_size[0]) { | |
419 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); | |
420 DLOG(INFO) << "Could not write stream 0 data."; | |
421 Doom(); | |
422 } | |
423 } | |
424 | |
399 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin(); | 425 for (std::vector<CRCRecord>::const_iterator it = crc32s_to_write->begin(); |
400 it != crc32s_to_write->end(); ++it) { | 426 it != crc32s_to_write->end(); ++it) { |
401 SimpleFileEOF eof_record; | 427 SimpleFileEOF eof_record; |
428 int index = it->index; | |
429 eof_record.stream_size = entry_stat.data_size[index]; | |
402 eof_record.final_magic_number = kSimpleFinalMagicNumber; | 430 eof_record.final_magic_number = kSimpleFinalMagicNumber; |
403 eof_record.flags = 0; | 431 eof_record.flags = 0; |
404 if (it->has_crc32) | 432 if (it->has_crc32) |
405 eof_record.flags |= SimpleFileEOF::FLAG_HAS_CRC32; | 433 eof_record.flags |= SimpleFileEOF::FLAG_HAS_CRC32; |
406 eof_record.data_crc32 = it->data_crc32; | 434 eof_record.data_crc32 = it->data_crc32; |
407 int64 file_offset = GetFileOffsetFromKeyAndDataOffset( | 435 int file_index = GetFileIndexFromStreamIndex(index); |
408 key_, entry_stat.data_size[it->index]); | 436 int eof_offset = entry_stat.GetEOFOffsetInFile(key_, index); |
409 if (WritePlatformFile(files_[it->index], | 437 if (WritePlatformFile(files_[file_index], |
410 file_offset, | 438 eof_offset, |
411 reinterpret_cast<const char*>(&eof_record), | 439 reinterpret_cast<const char*>(&eof_record), |
412 sizeof(eof_record)) != sizeof(eof_record)) { | 440 sizeof(eof_record)) != sizeof(eof_record)) { |
413 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); | 441 RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); |
414 DLOG(INFO) << "Could not write eof record."; | 442 DLOG(INFO) << "Could not write eof record."; |
415 Doom(); | 443 Doom(); |
416 break; | 444 break; |
417 } | 445 } |
418 const int64 file_size = file_offset + sizeof(eof_record); | 446 } |
447 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | |
448 bool did_close_file = ClosePlatformFile(files_[i]); | |
449 CHECK(did_close_file); | |
450 const int64 file_size = GetFileSizeFromKeyAndDataSize( | |
451 key_, GetLastEOFRecordOffset(i, entry_stat.data_size)); | |
419 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, | 452 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, |
420 "LastClusterSize", cache_type_, | 453 "LastClusterSize", cache_type_, |
421 file_size % 4096, 0, 4097, 50); | 454 file_size % 4096, 0, 4097, 50); |
422 const int64 cluster_loss = file_size % 4096 ? 4096 - file_size % 4096 : 0; | 455 const int64 cluster_loss = file_size % 4096 ? 4096 - file_size % 4096 : 0; |
423 SIMPLE_CACHE_UMA(PERCENTAGE, | 456 SIMPLE_CACHE_UMA(PERCENTAGE, |
424 "LastClusterLossPercent", cache_type_, | 457 "LastClusterLossPercent", cache_type_, |
425 cluster_loss * 100 / (cluster_loss + file_size)); | 458 cluster_loss * 100 / (cluster_loss + file_size)); |
426 } | 459 } |
427 | |
428 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | |
429 bool did_close_file = ClosePlatformFile(files_[i]); | |
430 CHECK(did_close_file); | |
431 } | |
432 RecordCloseResult(cache_type_, CLOSE_RESULT_SUCCESS); | 460 RecordCloseResult(cache_type_, CLOSE_RESULT_SUCCESS); |
433 have_open_files_ = false; | 461 have_open_files_ = false; |
434 delete this; | 462 delete this; |
435 } | 463 } |
436 | 464 |
437 SimpleSynchronousEntry::SimpleSynchronousEntry(net::CacheType cache_type, | 465 SimpleSynchronousEntry::SimpleSynchronousEntry(net::CacheType cache_type, |
438 const FilePath& path, | 466 const FilePath& path, |
439 const std::string& key, | 467 const std::string& key, |
440 const uint64 entry_hash) | 468 const uint64 entry_hash) |
441 : cache_type_(cache_type), | 469 : cache_type_(cache_type), |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
510 DLOG_IF(INFO, !did_close) << "Could not close file " | 538 DLOG_IF(INFO, !did_close) << "Could not close file " |
511 << filename.MaybeAsASCII(); | 539 << filename.MaybeAsASCII(); |
512 } | 540 } |
513 return false; | 541 return false; |
514 } | 542 } |
515 } | 543 } |
516 | 544 |
517 have_open_files_ = true; | 545 have_open_files_ = true; |
518 if (create) { | 546 if (create) { |
519 out_entry_stat->last_modified = out_entry_stat->last_used = Time::Now(); | 547 out_entry_stat->last_modified = out_entry_stat->last_used = Time::Now(); |
520 for (int i = 0; i < kSimpleEntryFileCount; ++i) | 548 for (int i = 0; i < kSimpleEntryStreamCount; ++i) |
521 out_entry_stat->data_size[i] = 0; | 549 out_entry_stat->data_size[i] = 0; |
522 } else { | 550 } else { |
523 base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch(); | 551 base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch(); |
524 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 552 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
525 PlatformFileInfo file_info; | 553 PlatformFileInfo file_info; |
526 bool success = GetPlatformFileInfo(files_[i], &file_info); | 554 bool success = GetPlatformFileInfo(files_[i], &file_info); |
527 base::Time file_last_modified; | 555 base::Time file_last_modified; |
528 if (!success) { | 556 if (!success) { |
529 DLOG(WARNING) << "Could not get platform file info."; | 557 DLOG(WARNING) << "Could not get platform file info."; |
530 continue; | 558 continue; |
531 } | 559 } |
532 out_entry_stat->last_used = file_info.last_accessed; | 560 out_entry_stat->last_used = file_info.last_accessed; |
533 if (simple_util::GetMTime(path_, &file_last_modified)) | 561 if (simple_util::GetMTime(path_, &file_last_modified)) |
534 out_entry_stat->last_modified = file_last_modified; | 562 out_entry_stat->last_modified = file_last_modified; |
535 else | 563 else |
536 out_entry_stat->last_modified = file_info.last_modified; | 564 out_entry_stat->last_modified = file_info.last_modified; |
537 | 565 |
538 base::TimeDelta stream_age = | 566 base::TimeDelta stream_age = |
539 base::Time::Now() - out_entry_stat->last_modified; | 567 base::Time::Now() - out_entry_stat->last_modified; |
540 if (stream_age < entry_age) | 568 if (stream_age < entry_age) |
541 entry_age = stream_age; | 569 entry_age = stream_age; |
542 | 570 |
543 // Keep the file size in |data size_| briefly until the key is initialized | 571 // Keep the file size in |data size_| temporarily until the data sizes are |
544 // properly. | 572 // initialized correctly. |
545 out_entry_stat->data_size[i] = file_info.size; | 573 out_entry_stat->data_size[i + 1] = file_info.size; |
546 } | 574 } |
547 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, | 575 SIMPLE_CACHE_UMA(CUSTOM_COUNTS, |
548 "SyncOpenEntryAge", cache_type_, | 576 "SyncOpenEntryAge", cache_type_, |
549 entry_age.InHours(), 1, 1000, 50); | 577 entry_age.InHours(), 1, 1000, 50); |
550 } | 578 } |
551 | 579 |
552 return true; | 580 return true; |
553 } | 581 } |
554 | 582 |
555 void SimpleSynchronousEntry::CloseFiles() { | 583 void SimpleSynchronousEntry::CloseFiles() { |
556 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 584 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
557 DCHECK_NE(kInvalidPlatformFileValue, files_[i]); | 585 DCHECK_NE(kInvalidPlatformFileValue, files_[i]); |
558 bool did_close = ClosePlatformFile(files_[i]); | 586 bool did_close = ClosePlatformFile(files_[i]); |
559 DCHECK(did_close); | 587 DCHECK(did_close); |
560 } | 588 } |
561 } | 589 } |
562 | 590 |
563 int SimpleSynchronousEntry::InitializeForOpen(bool had_index, | 591 int SimpleSynchronousEntry::InitializeForOpen( |
564 SimpleEntryStat* out_entry_stat) { | 592 bool had_index, |
593 SimpleEntryStat* out_entry_stat, | |
594 scoped_refptr<net::GrowableIOBuffer>* stream_0_data) { | |
565 DCHECK(!initialized_); | 595 DCHECK(!initialized_); |
566 if (!OpenOrCreateFiles(false, had_index, out_entry_stat)) | 596 if (!OpenOrCreateFiles(false, had_index, out_entry_stat)) |
567 return net::ERR_FAILED; | 597 return net::ERR_FAILED; |
568 | |
569 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 598 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
570 SimpleFileHeader header; | 599 SimpleFileHeader header; |
571 int header_read_result = | 600 int header_read_result = |
572 ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), | 601 ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), |
573 sizeof(header)); | 602 sizeof(header)); |
574 if (header_read_result != sizeof(header)) { | 603 if (header_read_result != sizeof(header)) { |
575 DLOG(WARNING) << "Cannot read header from entry."; | 604 DLOG(WARNING) << "Cannot read header from entry."; |
576 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_HEADER, had_index); | 605 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_HEADER, had_index); |
577 return net::ERR_FAILED; | 606 return net::ERR_FAILED; |
578 } | 607 } |
(...skipping 16 matching lines...) Expand all Loading... | |
595 scoped_ptr<char[]> key(new char[header.key_length]); | 624 scoped_ptr<char[]> key(new char[header.key_length]); |
596 int key_read_result = ReadPlatformFile(files_[i], sizeof(header), | 625 int key_read_result = ReadPlatformFile(files_[i], sizeof(header), |
597 key.get(), header.key_length); | 626 key.get(), header.key_length); |
598 if (key_read_result != implicit_cast<int>(header.key_length)) { | 627 if (key_read_result != implicit_cast<int>(header.key_length)) { |
599 DLOG(WARNING) << "Cannot read key from entry."; | 628 DLOG(WARNING) << "Cannot read key from entry."; |
600 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_KEY, had_index); | 629 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_KEY, had_index); |
601 return net::ERR_FAILED; | 630 return net::ERR_FAILED; |
602 } | 631 } |
603 | 632 |
604 key_ = std::string(key.get(), header.key_length); | 633 key_ = std::string(key.get(), header.key_length); |
605 out_entry_stat->data_size[i] = | 634 if (i == 0) { |
606 GetDataSizeFromKeyAndFileSize(key_, out_entry_stat->data_size[i]); | 635 // File size for stream 0 has been stored temporarily in data_size[1]. |
607 if (out_entry_stat->data_size[i] < 0) { | 636 int total_data_size = |
608 // This entry can't possibly be valid, as it does not have enough space to | 637 GetDataSizeFromKeyAndFileSize(key_, out_entry_stat->data_size[1]); |
609 // store a valid SimpleFileEOF record. | 638 int ret_value_stream_0 = ReadAndValidateStream0( |
610 return net::ERR_FAILED; | 639 total_data_size, out_entry_stat, stream_0_data); |
640 if (ret_value_stream_0 != net::OK) | |
641 return ret_value_stream_0; | |
642 } else { | |
643 out_entry_stat->data_size[2] = | |
644 GetDataSizeFromKeyAndFileSize(key_, out_entry_stat->data_size[2]); | |
645 if (out_entry_stat->data_size[2] < 0) | |
646 return net::ERR_FAILED; | |
611 } | 647 } |
612 | 648 |
613 if (base::Hash(key.get(), header.key_length) != header.key_hash) { | 649 if (base::Hash(key.get(), header.key_length) != header.key_hash) { |
614 DLOG(WARNING) << "Hash mismatch on key."; | 650 DLOG(WARNING) << "Hash mismatch on key."; |
615 RecordSyncOpenResult( | 651 RecordSyncOpenResult( |
616 cache_type_, OPEN_ENTRY_KEY_HASH_MISMATCH, had_index); | 652 cache_type_, OPEN_ENTRY_KEY_HASH_MISMATCH, had_index); |
617 return net::ERR_FAILED; | 653 return net::ERR_FAILED; |
618 } | 654 } |
619 } | 655 } |
620 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_SUCCESS, had_index); | 656 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_SUCCESS, had_index); |
(...skipping 10 matching lines...) Expand all Loading... | |
631 return net::ERR_FILE_EXISTS; | 667 return net::ERR_FILE_EXISTS; |
632 } | 668 } |
633 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 669 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
634 SimpleFileHeader header; | 670 SimpleFileHeader header; |
635 header.initial_magic_number = kSimpleInitialMagicNumber; | 671 header.initial_magic_number = kSimpleInitialMagicNumber; |
636 header.version = kSimpleVersion; | 672 header.version = kSimpleVersion; |
637 | 673 |
638 header.key_length = key_.size(); | 674 header.key_length = key_.size(); |
639 header.key_hash = base::Hash(key_); | 675 header.key_hash = base::Hash(key_); |
640 | 676 |
641 if (WritePlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), | 677 if (WritePlatformFile( |
642 sizeof(header)) != sizeof(header)) { | 678 files_[i], 0, reinterpret_cast<char*>(&header), sizeof(header)) != |
643 DLOG(WARNING) << "Could not write headers to new cache entry."; | 679 sizeof(header)) { |
680 DLOG(WARNING) << "Could not write cache file header to cache entry."; | |
644 RecordSyncCreateResult( | 681 RecordSyncCreateResult( |
645 cache_type_, CREATE_ENTRY_CANT_WRITE_HEADER, had_index); | 682 cache_type_, CREATE_ENTRY_CANT_WRITE_HEADER, had_index); |
646 return net::ERR_FAILED; | 683 return net::ERR_FAILED; |
647 } | 684 } |
648 | 685 |
649 if (WritePlatformFile(files_[i], sizeof(header), key_.data(), | 686 if (WritePlatformFile( |
650 key_.size()) != implicit_cast<int>(key_.size())) { | 687 files_[i], sizeof(SimpleFileHeader), key_.data(), key_.size()) != |
688 implicit_cast<int>(key_.size())) { | |
651 DLOG(WARNING) << "Could not write keys to new cache entry."; | 689 DLOG(WARNING) << "Could not write keys to new cache entry."; |
652 RecordSyncCreateResult( | 690 RecordSyncCreateResult( |
653 cache_type_, CREATE_ENTRY_CANT_WRITE_KEY, had_index); | 691 cache_type_, CREATE_ENTRY_CANT_WRITE_KEY, had_index); |
654 return net::ERR_FAILED; | 692 return net::ERR_FAILED; |
655 } | 693 } |
656 } | 694 } |
657 RecordSyncCreateResult(cache_type_, CREATE_ENTRY_SUCCESS, had_index); | 695 RecordSyncCreateResult(cache_type_, CREATE_ENTRY_SUCCESS, had_index); |
658 initialized_ = true; | 696 initialized_ = true; |
659 return net::OK; | 697 return net::OK; |
660 } | 698 } |
661 | 699 |
700 int SimpleSynchronousEntry::ReadAndValidateStream0( | |
701 int total_data_size, | |
702 SimpleEntryStat* out_entry_stat, | |
703 scoped_refptr<net::GrowableIOBuffer>* stream_0_data) const { | |
704 // Temporarily assign all the data size to stream 1 in order to read the | |
705 // EOF record for stream 0, which contains the size of stream 0. | |
706 out_entry_stat->data_size[0] = 0; | |
707 out_entry_stat->data_size[1] = total_data_size - sizeof(SimpleFileEOF); | |
708 | |
709 bool has_crc32; | |
710 uint32 read_crc32; | |
711 int stream_0_size; | |
712 int ret_value_crc32 = GetEOFRecordData( | |
713 0, *out_entry_stat, &has_crc32, &read_crc32, &stream_0_size); | |
714 if (ret_value_crc32 != net::OK) | |
715 return ret_value_crc32; | |
716 | |
717 if (stream_0_size > out_entry_stat->data_size[1]) | |
718 return net::ERR_FAILED; | |
719 | |
720 // These are the real values of data size. | |
721 out_entry_stat->data_size[0] = stream_0_size; | |
722 out_entry_stat->data_size[1] -= stream_0_size; | |
723 | |
724 // Put stream 0 data in memory. | |
725 *stream_0_data = NULL; | |
726 if (stream_0_size != 0) { | |
727 *stream_0_data = new net::GrowableIOBuffer(); | |
728 (*stream_0_data)->SetCapacity(stream_0_size); | |
729 int file_offset = out_entry_stat->GetOffsetInFile(key_, 0, 0); | |
730 int bytes_read = ReadPlatformFile( | |
731 files_[0], file_offset, (*stream_0_data)->data(), stream_0_size); | |
732 if (bytes_read != stream_0_size) | |
733 return net::ERR_FAILED; | |
734 } | |
735 | |
736 // Check the CRC32. | |
737 uint32 expected_crc32 = | |
738 stream_0_size == 0 | |
739 ? crc32(0, Z_NULL, 0) | |
740 : crc32(crc32(0, Z_NULL, 0), | |
741 reinterpret_cast<const Bytef*>((*stream_0_data)->data()), | |
742 stream_0_size); | |
743 if (has_crc32 && read_crc32 != expected_crc32) { | |
744 DLOG(INFO) << "Eof record had bad crc."; | |
745 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | |
746 return net::ERR_FAILED; | |
747 } | |
748 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | |
749 return net::OK; | |
750 } | |
751 | |
752 int SimpleSynchronousEntry::GetEOFRecordData(int index, | |
753 const SimpleEntryStat& entry_stat, | |
754 bool* out_has_crc32, | |
755 uint32* out_crc32, | |
756 int* out_data_size) const { | |
757 SimpleFileEOF eof_record; | |
758 int file_offset = entry_stat.GetEOFOffsetInFile(key_, index); | |
759 int file_index = GetFileIndexFromStreamIndex(index); | |
760 if (ReadPlatformFile(files_[file_index], | |
761 file_offset, | |
762 reinterpret_cast<char*>(&eof_record), | |
763 sizeof(eof_record)) != sizeof(eof_record)) { | |
764 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); | |
765 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; | |
766 } | |
767 | |
768 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { | |
769 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); | |
770 DLOG(INFO) << "Eof record had bad magic number."; | |
771 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; | |
772 } | |
773 | |
774 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) == | |
775 SimpleFileEOF::FLAG_HAS_CRC32; | |
776 *out_crc32 = eof_record.data_crc32; | |
777 *out_data_size = eof_record.stream_size; | |
778 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32); | |
779 return net::OK; | |
780 } | |
781 | |
662 void SimpleSynchronousEntry::Doom() const { | 782 void SimpleSynchronousEntry::Doom() const { |
663 // TODO(gavinp): Consider if we should guard against redundant Doom() calls. | 783 // TODO(gavinp): Consider if we should guard against redundant Doom() calls. |
664 DeleteFilesForEntryHash(path_, entry_hash_); | 784 DeleteFilesForEntryHash(path_, entry_hash_); |
665 } | 785 } |
666 | 786 |
667 } // namespace disk_cache | 787 } // namespace disk_cache |
OLD | NEW |