OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/download/base_file.h" | 5 #include "content/browser/download/base_file.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 | 508 |
509 // We may be re-opening the file after rename. Always make sure we're | 509 // We may be re-opening the file after rename. Always make sure we're |
510 // writing at the end of the file. | 510 // writing at the end of the file. |
511 int64 seek_result = file_stream_->SeekSync(net::FROM_END, 0); | 511 int64 seek_result = file_stream_->SeekSync(net::FROM_END, 0); |
512 if (seek_result < 0) | 512 if (seek_result < 0) |
513 return ClearStream(LOG_ERROR("Seek", seek_result)); | 513 return ClearStream(LOG_ERROR("Seek", seek_result)); |
514 } else { | 514 } else { |
515 file_stream_->SetBoundNetLogSource(bound_net_log_); | 515 file_stream_->SetBoundNetLogSource(bound_net_log_); |
516 } | 516 } |
517 | 517 |
| 518 int64 file_size = file_stream_->SeekSync(net::FROM_END, 0); |
| 519 if (file_size > bytes_so_far_) { |
| 520 // The file is larger than we expected. |
| 521 // This is OK, as long as we don't use the extra. |
| 522 // Truncate the file. |
| 523 int64 truncate_result = file_stream_->Truncate(bytes_so_far_); |
| 524 DCHECK_EQ(bytes_so_far_, truncate_result); |
| 525 } else if (file_size < bytes_so_far_) { |
| 526 // The file is shorter than we expected. Our hashes won't be valid. |
| 527 // This will map to a generic 'FAIL' interrupt reason. |
| 528 return LOG_ERROR("Seek", 1); // net::ERR_UNEXPECTED. |
| 529 } |
| 530 |
518 return net::OK; | 531 return net::OK; |
519 } | 532 } |
520 | 533 |
521 void BaseFile::Close() { | 534 void BaseFile::Close() { |
522 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 535 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
523 | 536 |
524 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_CLOSED); | 537 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_CLOSED); |
525 | 538 |
526 if (file_stream_.get()) { | 539 if (file_stream_.get()) { |
527 #if defined(OS_CHROMEOS) | 540 #if defined(OS_CHROMEOS) |
(...skipping 28 matching lines...) Expand all Loading... |
556 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const { | 569 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const { |
557 base::TimeDelta diff = current_time - start_tick_; | 570 base::TimeDelta diff = current_time - start_tick_; |
558 int64 diff_ms = diff.InMilliseconds(); | 571 int64 diff_ms = diff.InMilliseconds(); |
559 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; | 572 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; |
560 } | 573 } |
561 | 574 |
562 int64 BaseFile::CurrentSpeed() const { | 575 int64 BaseFile::CurrentSpeed() const { |
563 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 576 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
564 return CurrentSpeedAtTime(base::TimeTicks::Now()); | 577 return CurrentSpeedAtTime(base::TimeTicks::Now()); |
565 } | 578 } |
OLD | NEW |