| 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, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "util/file/fd_io.h" | 15 #include "util/file/file_io.h" |
| 16 | 16 |
| 17 #include <unistd.h> | 17 #include <unistd.h> |
| 18 | 18 |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/numerics/safe_conversions.h" | 20 #include "base/numerics/safe_conversions.h" |
| 21 #include "base/posix/eintr_wrapper.h" | 21 #include "base/posix/eintr_wrapper.h" |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 struct ReadTraits { | 25 struct ReadTraits { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 total_bytes += bytes; | 59 total_bytes += bytes; |
| 60 } | 60 } |
| 61 | 61 |
| 62 return total_bytes; | 62 return total_bytes; |
| 63 } | 63 } |
| 64 | 64 |
| 65 } // namespace | 65 } // namespace |
| 66 | 66 |
| 67 namespace crashpad { | 67 namespace crashpad { |
| 68 | 68 |
| 69 ssize_t ReadFD(int fd, void* buffer, size_t size) { | 69 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { |
| 70 return ReadOrWrite<ReadTraits>(fd, buffer, size); | 70 return ReadOrWrite<ReadTraits>(file, buffer, size); |
| 71 } | 71 } |
| 72 | 72 |
| 73 ssize_t WriteFD(int fd, const void* buffer, size_t size) { | 73 ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) { |
| 74 return ReadOrWrite<WriteTraits>(fd, buffer, size); | 74 return ReadOrWrite<WriteTraits>(file, buffer, size); |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool LoggingReadFD(int fd, void* buffer, size_t size) { | 77 bool LoggingCloseFile(FileHandle file) { |
| 78 ssize_t expect = base::checked_cast<ssize_t>(size); | 78 int rv = IGNORE_EINTR(close(file)); |
| 79 ssize_t rv = ReadFD(fd, buffer, size); | |
| 80 if (rv < 0) { | |
| 81 PLOG(ERROR) << "read"; | |
| 82 return false; | |
| 83 } | |
| 84 if (rv != expect) { | |
| 85 LOG(ERROR) << "read: expected " << expect << ", observed " << rv; | |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 bool LoggingWriteFD(int fd, const void* buffer, size_t size) { | |
| 93 ssize_t expect = base::checked_cast<ssize_t>(size); | |
| 94 ssize_t rv = WriteFD(fd, buffer, size); | |
| 95 if (rv < 0) { | |
| 96 PLOG(ERROR) << "write"; | |
| 97 return false; | |
| 98 } | |
| 99 if (rv != expect) { | |
| 100 LOG(ERROR) << "write: expected " << expect << ", observed " << rv; | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 void CheckedReadFD(int fd, void* buffer, size_t size) { | |
| 108 CHECK(LoggingReadFD(fd, buffer, size)); | |
| 109 } | |
| 110 | |
| 111 void CheckedWriteFD(int fd, const void* buffer, size_t size) { | |
| 112 CHECK(LoggingWriteFD(fd, buffer, size)); | |
| 113 } | |
| 114 | |
| 115 void CheckedReadFDAtEOF(int fd) { | |
| 116 char c; | |
| 117 ssize_t rv = ReadFD(fd, &c, 1); | |
| 118 if (rv < 0) { | |
| 119 PCHECK(rv == 0) << "read"; | |
| 120 } else { | |
| 121 CHECK_EQ(rv, 0) << "read"; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 bool LoggingCloseFD(int fd) { | |
| 126 int rv = IGNORE_EINTR(close(fd)); | |
| 127 PLOG_IF(ERROR, rv != 0) << "close"; | 79 PLOG_IF(ERROR, rv != 0) << "close"; |
| 128 return rv == 0; | 80 return rv == 0; |
| 129 } | 81 } |
| 130 | 82 |
| 131 void CheckedCloseFD(int fd) { | |
| 132 CHECK(LoggingCloseFD(fd)); | |
| 133 } | |
| 134 | |
| 135 } // namespace crashpad | 83 } // namespace crashpad |
| OLD | NEW |