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 10 matching lines...) Expand all Loading... | |
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, we'll just upload an empty file. |
hashimoto
2013/10/29 04:08:59
This comment is obsolete.
tzik
2013/10/29 05:48:02
Done.
| |
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) && |
hashimoto
2013/10/29 04:08:59
Shouldn't we return an error when GetFileSize fail
tzik
2013/10/29 05:48:02
Done.
| |
50 file_util::GetFileSize(path, &length) && | |
51 range_offset < static_cast<uint64>(length)) { | 53 range_offset < static_cast<uint64>(length)) { |
52 // Compensate for the offset. | 54 // Compensate for the offset. |
53 length = std::min(length - range_offset, range_length); | 55 length = std::min(length - range_offset, range_length); |
54 } | 56 } |
55 *out_content_length = length; | 57 *out_content_length = length; |
56 out_file_stream->reset(file_stream.release()); | 58 out_file_stream->reset(file_stream.release()); |
57 | 59 |
58 // If the underlying file has been changed and the expected file modification | 60 // 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 | 61 // 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 | 62 // from WebKit is based on time_t precision. So we have to convert both to |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 const int result = ReadInternal(buf, buf_length, BytesRemaining(), | 283 const int result = ReadInternal(buf, buf_length, BytesRemaining(), |
282 file_stream_.get()); | 284 file_stream_.get()); |
283 if (result > 0) { | 285 if (result > 0) { |
284 DCHECK_GE(bytes_remaining_, static_cast<uint64>(result)); | 286 DCHECK_GE(bytes_remaining_, static_cast<uint64>(result)); |
285 bytes_remaining_ -= result; | 287 bytes_remaining_ -= result; |
286 } | 288 } |
287 return result; | 289 return result; |
288 } | 290 } |
289 | 291 |
290 } // namespace net | 292 } // namespace net |
OLD | NEW |