| 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 10 matching lines...) Expand all Loading... |
| 21 #include "base/files/file_path.h" | 21 #include "base/files/file_path.h" |
| 22 #include "base/logging.h" | 22 #include "base/logging.h" |
| 23 #include "base/numerics/safe_conversions.h" | 23 #include "base/numerics/safe_conversions.h" |
| 24 #include "base/posix/eintr_wrapper.h" | 24 #include "base/posix/eintr_wrapper.h" |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 struct ReadTraits { | 28 struct ReadTraits { |
| 29 using VoidBufferType = void*; | 29 using VoidBufferType = void*; |
| 30 using CharBufferType = char*; | 30 using CharBufferType = char*; |
| 31 static ssize_t Operate(int fd, CharBufferType buffer, size_t size) { | 31 static FileOperationResult Operate(int fd, |
| 32 CharBufferType buffer, |
| 33 size_t size) { |
| 32 return read(fd, buffer, size); | 34 return read(fd, buffer, size); |
| 33 } | 35 } |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 struct WriteTraits { | 38 struct WriteTraits { |
| 37 using VoidBufferType = const void*; | 39 using VoidBufferType = const void*; |
| 38 using CharBufferType = const char*; | 40 using CharBufferType = const char*; |
| 39 static ssize_t Operate(int fd, CharBufferType buffer, size_t size) { | 41 static FileOperationResult Operate(int fd, |
| 42 CharBufferType buffer, |
| 43 size_t size) { |
| 40 return write(fd, buffer, size); | 44 return write(fd, buffer, size); |
| 41 } | 45 } |
| 42 }; | 46 }; |
| 43 | 47 |
| 44 template <typename Traits> | 48 template <typename Traits> |
| 45 ssize_t ReadOrWrite(int fd, | 49 FileOperationResult ReadOrWrite(int fd, |
| 46 typename Traits::VoidBufferType buffer, | 50 typename Traits::VoidBufferType buffer, |
| 47 size_t size) { | 51 size_t size) { |
| 48 typename Traits::CharBufferType buffer_c = | 52 typename Traits::CharBufferType buffer_c = |
| 49 reinterpret_cast<typename Traits::CharBufferType>(buffer); | 53 reinterpret_cast<typename Traits::CharBufferType>(buffer); |
| 50 | 54 |
| 51 ssize_t total_bytes = 0; | 55 FileOperationResult total_bytes = 0; |
| 52 while (size > 0) { | 56 while (size > 0) { |
| 53 ssize_t bytes = HANDLE_EINTR(Traits::Operate(fd, buffer_c, size)); | 57 FileOperationResult bytes = |
| 58 HANDLE_EINTR(Traits::Operate(fd, buffer_c, size)); |
| 54 if (bytes < 0) { | 59 if (bytes < 0) { |
| 55 return bytes; | 60 return bytes; |
| 56 } else if (bytes == 0) { | 61 } else if (bytes == 0) { |
| 57 break; | 62 break; |
| 58 } | 63 } |
| 59 | 64 |
| 60 buffer_c += bytes; | 65 buffer_c += bytes; |
| 61 size -= bytes; | 66 size -= bytes; |
| 62 total_bytes += bytes; | 67 total_bytes += bytes; |
| 63 } | 68 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 } | 100 } |
| 96 | 101 |
| 97 return HANDLE_EINTR( | 102 return HANDLE_EINTR( |
| 98 open(path.value().c_str(), | 103 open(path.value().c_str(), |
| 99 flags, | 104 flags, |
| 100 permissions == FilePermissions::kWorldReadable ? 0644 : 0600)); | 105 permissions == FilePermissions::kWorldReadable ? 0644 : 0600)); |
| 101 } | 106 } |
| 102 | 107 |
| 103 } // namespace | 108 } // namespace |
| 104 | 109 |
| 105 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { | 110 FileOperationResult ReadFile(FileHandle file, void* buffer, size_t size) { |
| 106 return ReadOrWrite<ReadTraits>(file, buffer, size); | 111 return ReadOrWrite<ReadTraits>(file, buffer, size); |
| 107 } | 112 } |
| 108 | 113 |
| 109 ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) { | 114 FileOperationResult WriteFile(FileHandle file, |
| 115 const void* buffer, |
| 116 size_t size) { |
| 110 return ReadOrWrite<WriteTraits>(file, buffer, size); | 117 return ReadOrWrite<WriteTraits>(file, buffer, size); |
| 111 } | 118 } |
| 112 | 119 |
| 113 FileHandle OpenFileForRead(const base::FilePath& path) { | 120 FileHandle OpenFileForRead(const base::FilePath& path) { |
| 114 return HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)); | 121 return HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)); |
| 115 } | 122 } |
| 116 | 123 |
| 117 FileHandle OpenFileForWrite(const base::FilePath& path, | 124 FileHandle OpenFileForWrite(const base::FilePath& path, |
| 118 FileWriteMode mode, | 125 FileWriteMode mode, |
| 119 FilePermissions permissions) { | 126 FilePermissions permissions) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 return true; | 182 return true; |
| 176 } | 183 } |
| 177 | 184 |
| 178 bool LoggingCloseFile(FileHandle file) { | 185 bool LoggingCloseFile(FileHandle file) { |
| 179 int rv = IGNORE_EINTR(close(file)); | 186 int rv = IGNORE_EINTR(close(file)); |
| 180 PLOG_IF(ERROR, rv != 0) << "close"; | 187 PLOG_IF(ERROR, rv != 0) << "close"; |
| 181 return rv == 0; | 188 return rv == 0; |
| 182 } | 189 } |
| 183 | 190 |
| 184 } // namespace crashpad | 191 } // namespace crashpad |
| OLD | NEW |