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

Side by Side Diff: net/base/upload_data_stream.cc

Issue 541022: Fix the case where the browser livelocks if we cannot open a file. (Closed)
Patch Set: Created 10 years, 11 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
« net/base/upload_data.cc ('K') | « net/base/upload_data_stream.h ('k') | no next file » | 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/base/upload_data_stream.h" 5 #include "net/base/upload_data_stream.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "net/base/io_buffer.h" 8 #include "net/base/io_buffer.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 10
11 namespace net { 11 namespace net {
12 12
13 UploadDataStream::UploadDataStream(const UploadData* data) 13 UploadDataStream::UploadDataStream(const UploadData* data)
14 : data_(data), 14 : data_(data),
15 buf_(new IOBuffer(kBufSize)), 15 buf_(new IOBuffer(kBufSize)),
16 buf_len_(0), 16 buf_len_(0),
17 next_element_(data->elements().begin()), 17 next_element_(data->elements().begin()),
18 next_element_offset_(0), 18 next_element_offset_(0),
19 next_element_remaining_(0), 19 next_element_remaining_(0),
20 total_size_(data->GetContentLength()), 20 total_size_(data->GetContentLength()),
21 current_position_(0) { 21 current_position_(0),
22 file_failed_to_open_(false) {
22 FillBuf(); 23 FillBuf();
23 } 24 }
24 25
25 UploadDataStream::~UploadDataStream() { 26 UploadDataStream::~UploadDataStream() {
26 } 27 }
27 28
28 void UploadDataStream::DidConsume(size_t num_bytes) { 29 void UploadDataStream::DidConsume(size_t num_bytes) {
29 // TODO(vandebo): Change back to a DCHECK when issue 27870 is resolved. 30 // TODO(vandebo): Change back to a DCHECK when issue 27870 is resolved.
30 CHECK(num_bytes <= buf_len_); 31 CHECK(num_bytes <= buf_len_);
31 32
(...skipping 26 matching lines...) Expand all
58 buf_len_ += bytes_copied; 59 buf_len_ += bytes_copied;
59 60
60 if (bytes_copied == count) { 61 if (bytes_copied == count) {
61 advance_to_next_element = true; 62 advance_to_next_element = true;
62 } else { 63 } else {
63 next_element_offset_ += bytes_copied; 64 next_element_offset_ += bytes_copied;
64 } 65 }
65 } else { 66 } else {
66 DCHECK(element.type() == UploadData::TYPE_FILE); 67 DCHECK(element.type() == UploadData::TYPE_FILE);
67 68
68 if (!next_element_stream_.IsOpen()) { 69 if (!file_failed_to_open_ && !next_element_stream_.IsOpen()) {
69 int flags = base::PLATFORM_FILE_OPEN | 70 int flags = base::PLATFORM_FILE_OPEN |
70 base::PLATFORM_FILE_READ; 71 base::PLATFORM_FILE_READ;
71 int rv = next_element_stream_.Open(element.file_path(), flags); 72 int rv = next_element_stream_.Open(element.file_path(), flags);
72 // If the file does not exist, that's technically okay.. we'll just 73 // If the file does not exist, that's technically okay.. we'll just
73 // upload an empty file. This is for consistency with Mozilla. 74 // upload an empty file. This is for consistency with Mozilla.
74 DLOG_IF(WARNING, rv != OK) << "Failed to open \"" 75 DLOG_IF(WARNING, rv != OK) << "Failed to open \""
75 << element.file_path().value() 76 << element.file_path().value()
76 << "\" for reading: " << rv; 77 << "\" for reading: " << rv;
77 78
78 next_element_remaining_ = 0; // Default to reading nothing. 79 next_element_remaining_ = 0; // Default to reading nothing.
80
81 // If a file is visible to us but not readable then, at this point,
wtc 2010/01/12 19:51:02 What does "visible but not readable" mean?
agl 2010/01/25 14:14:09 For example, /etc/shadow is visible (you can ls -l
wtc 2010/01/25 20:07:11 Got it. Thanks for the explanation. How about "v
82 // we'll have assumed a non-zero length, but we'll have just failed to
83 // open the file. In this case we just fill in NULs.
84 if (rv != OK && element.expected_length() > 0) {
85 file_failed_to_open_ = true;
86 next_element_remaining_ = element.expected_length();
87 }
88
79 if (rv == OK) { 89 if (rv == OK) {
80 uint64 offset = element.file_range_offset(); 90 uint64 offset = element.file_range_offset();
81 if (offset && next_element_stream_.Seek(FROM_BEGIN, offset) < 0) { 91 if (offset && next_element_stream_.Seek(FROM_BEGIN, offset) < 0) {
82 DLOG(WARNING) << "Failed to seek \"" << element.file_path().value() 92 DLOG(WARNING) << "Failed to seek \"" << element.file_path().value()
83 << "\" to offset: " << offset; 93 << "\" to offset: " << offset;
84 } else { 94 } else {
85 next_element_remaining_ = element.file_range_length(); 95 next_element_remaining_ = element.file_range_length();
86 } 96 }
87 } 97 }
88 } 98 }
89 99
90 int rv = 0; 100 int rv = 0;
91 int count = static_cast<int>(std::min( 101 int count = static_cast<int>(std::min(
92 static_cast<uint64>(size_remaining), next_element_remaining_)); 102 static_cast<uint64>(size_remaining), next_element_remaining_));
93 if (count > 0 && 103 if (count > 0 && file_failed_to_open_) {
94 (rv = next_element_stream_.Read(buf_->data() + buf_len_, 104 memset(buf_->data() + buf_len_, 0, count);
wtc 2010/01/12 19:51:02 Please add a comment here to note that we're filli
agl 2010/01/25 14:14:09 Done.
95 count, NULL)) > 0) { 105 buf_len_ += count;
106 next_element_remaining_ -= count;
107 } else if (count > 0 &&
108 (rv = next_element_stream_.Read(buf_->data() + buf_len_,
109 count, NULL)) > 0) {
110 // TODO(darin): The file might have been truncated between when we
111 // asked for its size and now.
96 buf_len_ += rv; 112 buf_len_ += rv;
97 next_element_remaining_ -= rv; 113 next_element_remaining_ -= rv;
98 } else { 114 } else {
99 advance_to_next_element = true; 115 advance_to_next_element = true;
100 } 116 }
101 } 117 }
102 118
103 if (advance_to_next_element) { 119 if (advance_to_next_element) {
104 ++next_element_; 120 ++next_element_;
121 file_failed_to_open_ = false;
105 next_element_offset_ = 0; 122 next_element_offset_ = 0;
106 next_element_stream_.Close(); 123 next_element_stream_.Close();
107 } 124 }
108 } 125 }
109 } 126 }
110 127
111 } // namespace net 128 } // namespace net
OLDNEW
« net/base/upload_data.cc ('K') | « net/base/upload_data_stream.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698