| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 namespace crashpad { | 26 namespace crashpad { |
| 27 | 27 |
| 28 StringHTTPBodyStream::StringHTTPBodyStream(const std::string& string) | 28 StringHTTPBodyStream::StringHTTPBodyStream(const std::string& string) |
| 29 : HTTPBodyStream(), string_(string), bytes_read_() { | 29 : HTTPBodyStream(), string_(string), bytes_read_() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 StringHTTPBodyStream::~StringHTTPBodyStream() { | 32 StringHTTPBodyStream::~StringHTTPBodyStream() { |
| 33 } | 33 } |
| 34 | 34 |
| 35 ssize_t StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { | 35 FileOperationResult StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, |
| 36 size_t max_len) { |
| 36 size_t num_bytes_remaining = string_.length() - bytes_read_; | 37 size_t num_bytes_remaining = string_.length() - bytes_read_; |
| 37 if (num_bytes_remaining == 0) { | 38 if (num_bytes_remaining == 0) { |
| 38 return num_bytes_remaining; | 39 return num_bytes_remaining; |
| 39 } | 40 } |
| 40 | 41 |
| 41 size_t num_bytes_returned = | 42 size_t num_bytes_returned = std::min( |
| 42 std::min(std::min(num_bytes_remaining, max_len), | 43 std::min(num_bytes_remaining, max_len), |
| 43 implicit_cast<size_t>(std::numeric_limits<ssize_t>::max())); | 44 implicit_cast<size_t>(std::numeric_limits<FileOperationResult>::max())); |
| 44 memcpy(buffer, &string_[bytes_read_], num_bytes_returned); | 45 memcpy(buffer, &string_[bytes_read_], num_bytes_returned); |
| 45 bytes_read_ += num_bytes_returned; | 46 bytes_read_ += num_bytes_returned; |
| 46 return num_bytes_returned; | 47 return num_bytes_returned; |
| 47 } | 48 } |
| 48 | 49 |
| 49 FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path) | 50 FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path) |
| 50 : HTTPBodyStream(), path_(path), file_(), file_state_(kUnopenedFile) { | 51 : HTTPBodyStream(), path_(path), file_(), file_state_(kUnopenedFile) { |
| 51 } | 52 } |
| 52 | 53 |
| 53 FileHTTPBodyStream::~FileHTTPBodyStream() { | 54 FileHTTPBodyStream::~FileHTTPBodyStream() { |
| 54 } | 55 } |
| 55 | 56 |
| 56 ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { | 57 FileOperationResult FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, |
| 58 size_t max_len) { |
| 57 switch (file_state_) { | 59 switch (file_state_) { |
| 58 case kUnopenedFile: | 60 case kUnopenedFile: |
| 59 file_.reset(LoggingOpenFileForRead(path_)); | 61 file_.reset(LoggingOpenFileForRead(path_)); |
| 60 if (!file_.is_valid()) { | 62 if (!file_.is_valid()) { |
| 61 file_state_ = kFileOpenError; | 63 file_state_ = kFileOpenError; |
| 62 return -1; | 64 return -1; |
| 63 } | 65 } |
| 64 file_state_ = kReading; | 66 file_state_ = kReading; |
| 65 break; | 67 break; |
| 66 case kFileOpenError: | 68 case kFileOpenError: |
| 67 return -1; | 69 return -1; |
| 68 case kClosedAtEOF: | 70 case kClosedAtEOF: |
| 69 return 0; | 71 return 0; |
| 70 case kReading: | 72 case kReading: |
| 71 break; | 73 break; |
| 72 } | 74 } |
| 73 | 75 |
| 74 ssize_t rv = ReadFile(file_.get(), buffer, max_len); | 76 FileOperationResult rv = ReadFile(file_.get(), buffer, max_len); |
| 75 if (rv == 0) { | 77 if (rv == 0) { |
| 76 file_.reset(); | 78 file_.reset(); |
| 77 file_state_ = kClosedAtEOF; | 79 file_state_ = kClosedAtEOF; |
| 78 } else if (rv < 0) { | 80 } else if (rv < 0) { |
| 79 PLOG(ERROR) << "read"; | 81 PLOG(ERROR) << "read"; |
| 80 } | 82 } |
| 81 return rv; | 83 return rv; |
| 82 } | 84 } |
| 83 | 85 |
| 84 CompositeHTTPBodyStream::CompositeHTTPBodyStream( | 86 CompositeHTTPBodyStream::CompositeHTTPBodyStream( |
| 85 const CompositeHTTPBodyStream::PartsList& parts) | 87 const CompositeHTTPBodyStream::PartsList& parts) |
| 86 : HTTPBodyStream(), parts_(parts), current_part_(parts_.begin()) { | 88 : HTTPBodyStream(), parts_(parts), current_part_(parts_.begin()) { |
| 87 } | 89 } |
| 88 | 90 |
| 89 CompositeHTTPBodyStream::~CompositeHTTPBodyStream() { | 91 CompositeHTTPBodyStream::~CompositeHTTPBodyStream() { |
| 90 STLDeleteContainerPointers(parts_.begin(), parts_.end()); | 92 STLDeleteContainerPointers(parts_.begin(), parts_.end()); |
| 91 } | 93 } |
| 92 | 94 |
| 93 ssize_t CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, | 95 FileOperationResult CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, |
| 94 size_t buffer_len) { | 96 size_t buffer_len) { |
| 95 ssize_t max_len = std::min( | 97 FileOperationResult max_len = std::min( |
| 96 buffer_len, implicit_cast<size_t>(std::numeric_limits<ssize_t>::max())); | 98 buffer_len, |
| 97 ssize_t bytes_copied = 0; | 99 implicit_cast<size_t>(std::numeric_limits<FileOperationResult>::max())); |
| 100 FileOperationResult bytes_copied = 0; |
| 98 while (bytes_copied < max_len && current_part_ != parts_.end()) { | 101 while (bytes_copied < max_len && current_part_ != parts_.end()) { |
| 99 ssize_t this_read = (*current_part_)->GetBytesBuffer( | 102 FileOperationResult this_read = |
| 100 buffer + bytes_copied, max_len - bytes_copied); | 103 (*current_part_) |
| 104 ->GetBytesBuffer(buffer + bytes_copied, max_len - bytes_copied); |
| 101 | 105 |
| 102 if (this_read == 0) { | 106 if (this_read == 0) { |
| 103 // If the current part has returned 0 indicating EOF, advance the current | 107 // If the current part has returned 0 indicating EOF, advance the current |
| 104 // part and try again. | 108 // part and try again. |
| 105 ++current_part_; | 109 ++current_part_; |
| 106 } else if (this_read < 0) { | 110 } else if (this_read < 0) { |
| 107 return this_read; | 111 return this_read; |
| 108 } | 112 } |
| 109 bytes_copied += this_read; | 113 bytes_copied += this_read; |
| 110 } | 114 } |
| 111 | 115 |
| 112 return bytes_copied; | 116 return bytes_copied; |
| 113 } | 117 } |
| 114 | 118 |
| 115 } // namespace crashpad | 119 } // namespace crashpad |
| OLD | NEW |