| 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/file_io.h" | 15 #include "util/file/file_io.h" |
| 16 | 16 |
| 17 #include <fcntl.h> | 17 #include <fcntl.h> |
| 18 #include <sys/file.h> | 18 #include <sys/file.h> |
| 19 #include <sys/stat.h> | 19 #include <sys/stat.h> |
| 20 #include <unistd.h> | 20 #include <unistd.h> |
| 21 | 21 |
| 22 #include <algorithm> |
| 23 #include <limits> |
| 24 |
| 22 #include "base/files/file_path.h" | 25 #include "base/files/file_path.h" |
| 23 #include "base/logging.h" | 26 #include "base/logging.h" |
| 24 #include "base/numerics/safe_conversions.h" | |
| 25 #include "base/posix/eintr_wrapper.h" | 27 #include "base/posix/eintr_wrapper.h" |
| 26 | 28 |
| 29 namespace crashpad { |
| 30 |
| 27 namespace { | 31 namespace { |
| 28 | 32 |
| 29 struct ReadTraits { | 33 struct ReadTraits { |
| 30 using VoidBufferType = void*; | 34 using BufferType = void*; |
| 31 using CharBufferType = char*; | 35 static FileOperationResult Operate(int fd, BufferType buffer, size_t size) { |
| 32 static crashpad::FileOperationResult Operate(int fd, | |
| 33 CharBufferType buffer, | |
| 34 size_t size) { | |
| 35 return read(fd, buffer, size); | 36 return read(fd, buffer, size); |
| 36 } | 37 } |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 struct WriteTraits { | 40 struct WriteTraits { |
| 40 using VoidBufferType = const void*; | 41 using BufferType = const void*; |
| 41 using CharBufferType = const char*; | 42 static FileOperationResult Operate(int fd, BufferType buffer, size_t size) { |
| 42 static crashpad::FileOperationResult Operate(int fd, | |
| 43 CharBufferType buffer, | |
| 44 size_t size) { | |
| 45 return write(fd, buffer, size); | 43 return write(fd, buffer, size); |
| 46 } | 44 } |
| 47 }; | 45 }; |
| 48 | 46 |
| 49 template <typename Traits> | 47 template <typename Traits> |
| 50 crashpad::FileOperationResult | 48 FileOperationResult ReadOrWrite(int fd, |
| 51 ReadOrWrite(int fd, typename Traits::VoidBufferType buffer, size_t size) { | 49 typename Traits::BufferType buffer, |
| 52 typename Traits::CharBufferType buffer_c = | 50 size_t size) { |
| 53 reinterpret_cast<typename Traits::CharBufferType>(buffer); | 51 constexpr size_t kMaxReadWriteSize = |
| 52 static_cast<size_t>(std::numeric_limits<ssize_t>::max()); |
| 53 const size_t requested_bytes = std::min(size, kMaxReadWriteSize); |
| 54 | 54 |
| 55 crashpad::FileOperationResult total_bytes = 0; | 55 FileOperationResult transacted_bytes = |
| 56 while (size > 0) { | 56 HANDLE_EINTR(Traits::Operate(fd, buffer, requested_bytes)); |
| 57 crashpad::FileOperationResult bytes = | 57 if (transacted_bytes < 0) { |
| 58 HANDLE_EINTR(Traits::Operate(fd, buffer_c, size)); | 58 return -1; |
| 59 if (bytes < 0) { | |
| 60 return bytes; | |
| 61 } else if (bytes == 0) { | |
| 62 break; | |
| 63 } | |
| 64 | |
| 65 buffer_c += bytes; | |
| 66 size -= bytes; | |
| 67 total_bytes += bytes; | |
| 68 } | 59 } |
| 69 | 60 |
| 70 return total_bytes; | 61 DCHECK_LE(static_cast<size_t>(transacted_bytes), requested_bytes); |
| 62 return transacted_bytes; |
| 71 } | 63 } |
| 72 | 64 |
| 73 } // namespace | |
| 74 | |
| 75 namespace crashpad { | |
| 76 | |
| 77 namespace { | |
| 78 | |
| 79 FileHandle OpenFileForOutput(int rdwr_or_wronly, | 65 FileHandle OpenFileForOutput(int rdwr_or_wronly, |
| 80 const base::FilePath& path, | 66 const base::FilePath& path, |
| 81 FileWriteMode mode, | 67 FileWriteMode mode, |
| 82 FilePermissions permissions) { | 68 FilePermissions permissions) { |
| 83 int flags = O_NOCTTY | O_CLOEXEC; | 69 int flags = O_NOCTTY | O_CLOEXEC; |
| 84 | 70 |
| 85 DCHECK(rdwr_or_wronly & (O_RDWR | O_WRONLY)); | 71 DCHECK(rdwr_or_wronly & (O_RDWR | O_WRONLY)); |
| 86 DCHECK_EQ(rdwr_or_wronly & ~(O_RDWR | O_WRONLY), 0); | 72 DCHECK_EQ(rdwr_or_wronly & ~(O_RDWR | O_WRONLY), 0); |
| 87 flags |= rdwr_or_wronly; | 73 flags |= rdwr_or_wronly; |
| 88 | 74 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 101 } | 87 } |
| 102 | 88 |
| 103 return HANDLE_EINTR( | 89 return HANDLE_EINTR( |
| 104 open(path.value().c_str(), | 90 open(path.value().c_str(), |
| 105 flags, | 91 flags, |
| 106 permissions == FilePermissions::kWorldReadable ? 0644 : 0600)); | 92 permissions == FilePermissions::kWorldReadable ? 0644 : 0600)); |
| 107 } | 93 } |
| 108 | 94 |
| 109 } // namespace | 95 } // namespace |
| 110 | 96 |
| 97 namespace internal { |
| 98 |
| 99 const char kNativeReadFunctionName[] = "read"; |
| 100 const char kNativeWriteFunctionName[] = "write"; |
| 101 |
| 102 FileOperationResult NativeWriteFile(FileHandle file, |
| 103 const void* buffer, |
| 104 size_t size) { |
| 105 return ReadOrWrite<WriteTraits>(file, buffer, size); |
| 106 } |
| 107 |
| 108 } // namespace internal |
| 109 |
| 111 FileOperationResult ReadFile(FileHandle file, void* buffer, size_t size) { | 110 FileOperationResult ReadFile(FileHandle file, void* buffer, size_t size) { |
| 112 return ReadOrWrite<ReadTraits>(file, buffer, size); | 111 return ReadOrWrite<ReadTraits>(file, buffer, size); |
| 113 } | 112 } |
| 114 | 113 |
| 115 FileOperationResult WriteFile(FileHandle file, | |
| 116 const void* buffer, | |
| 117 size_t size) { | |
| 118 return ReadOrWrite<WriteTraits>(file, buffer, size); | |
| 119 } | |
| 120 | |
| 121 FileHandle OpenFileForRead(const base::FilePath& path) { | 114 FileHandle OpenFileForRead(const base::FilePath& path) { |
| 122 return HANDLE_EINTR( | 115 return HANDLE_EINTR( |
| 123 open(path.value().c_str(), O_RDONLY | O_NOCTTY | O_CLOEXEC)); | 116 open(path.value().c_str(), O_RDONLY | O_NOCTTY | O_CLOEXEC)); |
| 124 } | 117 } |
| 125 | 118 |
| 126 FileHandle OpenFileForWrite(const base::FilePath& path, | 119 FileHandle OpenFileForWrite(const base::FilePath& path, |
| 127 FileWriteMode mode, | 120 FileWriteMode mode, |
| 128 FilePermissions permissions) { | 121 FilePermissions permissions) { |
| 129 return OpenFileForOutput(O_WRONLY, path, mode, permissions); | 122 return OpenFileForOutput(O_WRONLY, path, mode, permissions); |
| 130 } | 123 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 185 |
| 193 FileOffset LoggingFileSizeByHandle(FileHandle file) { | 186 FileOffset LoggingFileSizeByHandle(FileHandle file) { |
| 194 struct stat st; | 187 struct stat st; |
| 195 if (fstat(file, &st) != 0) { | 188 if (fstat(file, &st) != 0) { |
| 196 PLOG(ERROR) << "fstat"; | 189 PLOG(ERROR) << "fstat"; |
| 197 return -1; | 190 return -1; |
| 198 } | 191 } |
| 199 return st.st_size; | 192 return st.st_size; |
| 200 } | 193 } |
| 201 | 194 |
| 195 FileHandle StdioFileHandle(StdioStream stdio_stream) { |
| 196 switch (stdio_stream) { |
| 197 case StdioStream::kStandardInput: |
| 198 return STDIN_FILENO; |
| 199 case StdioStream::kStandardOutput: |
| 200 return STDOUT_FILENO; |
| 201 case StdioStream::kStandardError: |
| 202 return STDERR_FILENO; |
| 203 } |
| 204 |
| 205 NOTREACHED(); |
| 206 return kInvalidFileHandle; |
| 207 } |
| 208 |
| 202 } // namespace crashpad | 209 } // namespace crashpad |
| OLD | NEW |