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

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

Issue 39203004: [Net] Fix error handling on wrong file in UploadData (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month 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) 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 "net/base/upload_file_element_reader.h" 5 #include "net/base/upload_file_element_reader.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/location.h" 9 #include "base/location.h"
10 #include "base/task_runner_util.h" 10 #include "base/task_runner_util.h"
(...skipping 10 matching lines...) Expand all
21 uint64 overriding_content_length = 0; 21 uint64 overriding_content_length = 0;
22 22
23 // This function is used to implement Init(). 23 // This function is used to implement Init().
24 template<typename FileStreamDeleter> 24 template<typename FileStreamDeleter>
25 int InitInternal(const base::FilePath& path, 25 int InitInternal(const base::FilePath& path,
26 uint64 range_offset, 26 uint64 range_offset,
27 uint64 range_length, 27 uint64 range_length,
28 const base::Time& expected_modification_time, 28 const base::Time& expected_modification_time,
29 scoped_ptr<FileStream, FileStreamDeleter>* out_file_stream, 29 scoped_ptr<FileStream, FileStreamDeleter>* out_file_stream,
30 uint64* out_content_length) { 30 uint64* out_content_length) {
31 out_file_stream->reset();
32 *out_content_length = 0;
33
31 scoped_ptr<FileStream> file_stream(new FileStream(NULL)); 34 scoped_ptr<FileStream> file_stream(new FileStream(NULL));
32 int64 rv = file_stream->OpenSync( 35 int64 rv = file_stream->OpenSync(
33 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); 36 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ);
34 if (rv != OK) { 37 if (rv != OK) {
35 // If the file can't be opened, we'll just upload an empty file. 38 // If the file can't be opened, the upload should fail.
36 DLOG(WARNING) << "Failed to open \"" << path.value() 39 DLOG(WARNING) << "Failed to open \"" << path.value()
37 << "\" for reading: " << rv; 40 << "\" for reading: " << rv;
38 file_stream.reset(); 41 return rv;
39 } else if (range_offset) { 42 } else if (range_offset) {
40 rv = file_stream->SeekSync(FROM_BEGIN, range_offset); 43 rv = file_stream->SeekSync(FROM_BEGIN, range_offset);
41 if (rv < 0) { 44 if (rv < 0) {
42 DLOG(WARNING) << "Failed to seek \"" << path.value() 45 DLOG(WARNING) << "Failed to seek \"" << path.value()
43 << "\" to offset: " << range_offset << " (" << rv << ")"; 46 << "\" to offset: " << range_offset << " (" << rv << ")";
44 file_stream.reset(); 47 return rv;
45 } 48 }
46 } 49 }
47 50
48 int64 length = 0; 51 int64 length = 0;
49 if (file_stream.get() && 52 if (!file_util::GetFileSize(path, &length)) {
50 file_util::GetFileSize(path, &length) && 53 DLOG(WARNING) << "Failed to get file size of \"" << path.value() << "\"";
51 range_offset < static_cast<uint64>(length)) { 54 return ERR_FILE_NOT_FOUND;
55 }
56
57 if (range_offset < static_cast<uint64>(length)) {
52 // Compensate for the offset. 58 // Compensate for the offset.
53 length = std::min(length - range_offset, range_length); 59 length = std::min(length - range_offset, range_length);
54 } 60 }
55 *out_content_length = length; 61 *out_content_length = length;
56 out_file_stream->reset(file_stream.release()); 62 out_file_stream->reset(file_stream.release());
57 63
58 // If the underlying file has been changed and the expected file modification 64 // If the underlying file has been changed and the expected file modification
59 // time is set, treat it as error. Note that the expected modification time 65 // time is set, treat it as error. Note that the expected modification time
60 // from WebKit is based on time_t precision. So we have to convert both to 66 // from WebKit is based on time_t precision. So we have to convert both to
61 // time_t to compare. This check is used for sliced files. 67 // time_t to compare. This check is used for sliced files.
62 if (!expected_modification_time.is_null()) { 68 if (!expected_modification_time.is_null()) {
63 base::PlatformFileInfo info; 69 base::PlatformFileInfo info;
64 if (file_util::GetFileInfo(path, &info) && 70 if (file_util::GetFileInfo(path, &info)) {
65 expected_modification_time.ToTimeT() != info.last_modified.ToTimeT()) { 71 DLOG(WARNING) << "Failed to get file info of \"" << path.value() << "\"";
72 return ERR_FILE_NOT_FOUND;
mmenke 2013/10/29 15:29:31 I suggest not setting out_content_length or out_fi
tzik 2013/11/07 04:42:55 Done.
73 }
74
75 if (expected_modification_time.ToTimeT() != info.last_modified.ToTimeT()) {
66 return ERR_UPLOAD_FILE_CHANGED; 76 return ERR_UPLOAD_FILE_CHANGED;
67 } 77 }
68 } 78 }
69 79
70 return OK; 80 return OK;
71 } 81 }
72 82
73 // This function is used to implement Read(). 83 // This function is used to implement Read().
74 int ReadInternal(scoped_refptr<IOBuffer> buf, 84 int ReadInternal(scoped_refptr<IOBuffer> buf,
75 int buf_length, 85 int buf_length,
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 const int result = ReadInternal(buf, buf_length, BytesRemaining(), 291 const int result = ReadInternal(buf, buf_length, BytesRemaining(),
282 file_stream_.get()); 292 file_stream_.get());
283 if (result > 0) { 293 if (result > 0) {
284 DCHECK_GE(bytes_remaining_, static_cast<uint64>(result)); 294 DCHECK_GE(bytes_remaining_, static_cast<uint64>(result));
285 bytes_remaining_ -= result; 295 bytes_remaining_ -= result;
286 } 296 }
287 return result; 297 return result;
288 } 298 }
289 299
290 } // namespace net 300 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/base/upload_file_element_reader_unittest.cc » ('j') | net/http/http_network_transaction_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698