| 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 "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 14 matching lines...) Expand all Loading... |
| 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 scoped_ptr<FileStream> file_stream(new FileStream(NULL)); | 31 scoped_ptr<FileStream> file_stream(new FileStream(NULL)); |
| 32 int64 rv = file_stream->OpenSync( | 32 int64 rv = file_stream->OpenSync( |
| 33 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); | 33 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
| 34 if (rv != OK) { | 34 if (rv != OK) { |
| 35 // If the file can't be opened, we'll just upload an empty file. | 35 // If the file can't be opened, the upload should fail. |
| 36 DLOG(WARNING) << "Failed to open \"" << path.value() | 36 DLOG(WARNING) << "Failed to open \"" << path.value() |
| 37 << "\" for reading: " << rv; | 37 << "\" for reading: " << rv; |
| 38 file_stream.reset(); | 38 return rv; |
| 39 } else if (range_offset) { | 39 } else if (range_offset) { |
| 40 rv = file_stream->SeekSync(FROM_BEGIN, range_offset); | 40 rv = file_stream->SeekSync(FROM_BEGIN, range_offset); |
| 41 if (rv < 0) { | 41 if (rv < 0) { |
| 42 DLOG(WARNING) << "Failed to seek \"" << path.value() | 42 DLOG(WARNING) << "Failed to seek \"" << path.value() |
| 43 << "\" to offset: " << range_offset << " (" << rv << ")"; | 43 << "\" to offset: " << range_offset << " (" << rv << ")"; |
| 44 file_stream.reset(); | 44 return rv; |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 | 47 |
| 48 int64 length = 0; | 48 int64 length = 0; |
| 49 if (file_stream.get() && | 49 if (!file_util::GetFileSize(path, &length)) { |
| 50 file_util::GetFileSize(path, &length) && | 50 DLOG(WARNING) << "Failed to get file size of \"" << path.value() << "\""; |
| 51 range_offset < static_cast<uint64>(length)) { | 51 return ERR_FILE_NOT_FOUND; |
| 52 } |
| 53 |
| 54 if (range_offset < static_cast<uint64>(length)) { |
| 52 // Compensate for the offset. | 55 // Compensate for the offset. |
| 53 length = std::min(length - range_offset, range_length); | 56 length = std::min(length - range_offset, range_length); |
| 54 } | 57 } |
| 55 *out_content_length = length; | |
| 56 out_file_stream->reset(file_stream.release()); | |
| 57 | 58 |
| 58 // If the underlying file has been changed and the expected file modification | 59 // 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 | 60 // 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 | 61 // 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. | 62 // time_t to compare. This check is used for sliced files. |
| 62 if (!expected_modification_time.is_null()) { | 63 if (!expected_modification_time.is_null()) { |
| 63 base::PlatformFileInfo info; | 64 base::PlatformFileInfo info; |
| 64 if (file_util::GetFileInfo(path, &info) && | 65 if (!file_util::GetFileInfo(path, &info)) { |
| 65 expected_modification_time.ToTimeT() != info.last_modified.ToTimeT()) { | 66 DLOG(WARNING) << "Failed to get file info of \"" << path.value() << "\""; |
| 67 return ERR_FILE_NOT_FOUND; |
| 68 } |
| 69 |
| 70 if (expected_modification_time.ToTimeT() != info.last_modified.ToTimeT()) { |
| 66 return ERR_UPLOAD_FILE_CHANGED; | 71 return ERR_UPLOAD_FILE_CHANGED; |
| 67 } | 72 } |
| 68 } | 73 } |
| 69 | 74 |
| 75 *out_content_length = length; |
| 76 out_file_stream->reset(file_stream.release()); |
| 77 |
| 70 return OK; | 78 return OK; |
| 71 } | 79 } |
| 72 | 80 |
| 73 // This function is used to implement Read(). | 81 // This function is used to implement Read(). |
| 74 int ReadInternal(scoped_refptr<IOBuffer> buf, | 82 int ReadInternal(scoped_refptr<IOBuffer> buf, |
| 75 int buf_length, | 83 int buf_length, |
| 76 uint64 bytes_remaining, | 84 uint64 bytes_remaining, |
| 77 FileStream* file_stream) { | 85 FileStream* file_stream) { |
| 78 DCHECK_LT(0, buf_length); | 86 DCHECK_LT(0, buf_length); |
| 79 | 87 |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 const int result = ReadInternal(buf, buf_length, BytesRemaining(), | 289 const int result = ReadInternal(buf, buf_length, BytesRemaining(), |
| 282 file_stream_.get()); | 290 file_stream_.get()); |
| 283 if (result > 0) { | 291 if (result > 0) { |
| 284 DCHECK_GE(bytes_remaining_, static_cast<uint64>(result)); | 292 DCHECK_GE(bytes_remaining_, static_cast<uint64>(result)); |
| 285 bytes_remaining_ -= result; | 293 bytes_remaining_ -= result; |
| 286 } | 294 } |
| 287 return result; | 295 return result; |
| 288 } | 296 } |
| 289 | 297 |
| 290 } // namespace net | 298 } // namespace net |
| OLD | NEW |