OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (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 |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/file/fd_io.h" |
| 16 |
| 17 #include <unistd.h> |
| 18 |
| 19 #include "base/posix/eintr_wrapper.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 struct ReadTraits { |
| 24 typedef void* VoidBufferType; |
| 25 typedef char* CharBufferType; |
| 26 static ssize_t Operate(int fd, CharBufferType buffer, size_t size) { |
| 27 return read(fd, buffer, size); |
| 28 } |
| 29 }; |
| 30 |
| 31 struct WriteTraits { |
| 32 typedef const void* VoidBufferType; |
| 33 typedef const char* CharBufferType; |
| 34 static ssize_t Operate(int fd, CharBufferType buffer, size_t size) { |
| 35 return write(fd, buffer, size); |
| 36 } |
| 37 }; |
| 38 |
| 39 template <typename Traits> |
| 40 ssize_t ReadOrWrite(int fd, |
| 41 typename Traits::VoidBufferType buffer, |
| 42 size_t size) { |
| 43 typename Traits::CharBufferType buffer_c = |
| 44 reinterpret_cast<typename Traits::CharBufferType>(buffer); |
| 45 |
| 46 ssize_t total_bytes = 0; |
| 47 while (size > 0) { |
| 48 ssize_t bytes = HANDLE_EINTR(Traits::Operate(fd, buffer_c, size)); |
| 49 if (bytes < 0) { |
| 50 return bytes; |
| 51 } else if (bytes == 0) { |
| 52 break; |
| 53 } |
| 54 |
| 55 buffer_c += bytes; |
| 56 size -= bytes; |
| 57 total_bytes += bytes; |
| 58 } |
| 59 |
| 60 return total_bytes; |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 namespace crashpad { |
| 66 |
| 67 ssize_t ReadFD(int fd, void* buffer, size_t size) { |
| 68 return ReadOrWrite<ReadTraits>(fd, buffer, size); |
| 69 } |
| 70 |
| 71 ssize_t WriteFD(int fd, const void* buffer, size_t size) { |
| 72 return ReadOrWrite<WriteTraits>(fd, buffer, size); |
| 73 } |
| 74 |
| 75 } // namespace crashpad |
OLD | NEW |