| 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/file_io.h" |
| 16 |
| 17 #include <fcntl.h> |
| 18 #include <sys/file.h> |
| 19 #include <unistd.h> |
| 20 |
| 21 #include "base/files/file_path.h" |
| 22 #include "base/logging.h" |
| 23 #include "base/numerics/safe_conversions.h" |
| 24 #include "base/posix/eintr_wrapper.h" |
| 25 |
| 26 namespace { |
| 27 |
| 28 struct ReadTraits { |
| 29 using VoidBufferType = void*; |
| 30 using CharBufferType = char*; |
| 31 static crashpad::FileOperationResult Operate(int fd, |
| 32 CharBufferType buffer, |
| 33 size_t size) { |
| 34 return read(fd, buffer, size); |
| 35 } |
| 36 }; |
| 37 |
| 38 struct WriteTraits { |
| 39 using VoidBufferType = const void*; |
| 40 using CharBufferType = const char*; |
| 41 static crashpad::FileOperationResult Operate(int fd, |
| 42 CharBufferType buffer, |
| 43 size_t size) { |
| 44 return write(fd, buffer, size); |
| 45 } |
| 46 }; |
| 47 |
| 48 template <typename Traits> |
| 49 crashpad::FileOperationResult |
| 50 ReadOrWrite(int fd, typename Traits::VoidBufferType buffer, size_t size) { |
| 51 typename Traits::CharBufferType buffer_c = |
| 52 reinterpret_cast<typename Traits::CharBufferType>(buffer); |
| 53 |
| 54 crashpad::FileOperationResult total_bytes = 0; |
| 55 while (size > 0) { |
| 56 crashpad::FileOperationResult bytes = |
| 57 HANDLE_EINTR(Traits::Operate(fd, buffer_c, size)); |
| 58 if (bytes < 0) { |
| 59 return bytes; |
| 60 } else if (bytes == 0) { |
| 61 break; |
| 62 } |
| 63 |
| 64 buffer_c += bytes; |
| 65 size -= bytes; |
| 66 total_bytes += bytes; |
| 67 } |
| 68 |
| 69 return total_bytes; |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
| 74 namespace crashpad { |
| 75 |
| 76 namespace { |
| 77 |
| 78 FileHandle OpenFileForOutput(int rdwr_or_wronly, |
| 79 const base::FilePath& path, |
| 80 FileWriteMode mode, |
| 81 FilePermissions permissions) { |
| 82 DCHECK(rdwr_or_wronly & (O_RDWR | O_WRONLY)); |
| 83 DCHECK_EQ(rdwr_or_wronly & ~(O_RDWR | O_WRONLY), 0); |
| 84 |
| 85 int flags = rdwr_or_wronly; |
| 86 |
| 87 switch (mode) { |
| 88 case FileWriteMode::kReuseOrFail: |
| 89 break; |
| 90 case FileWriteMode::kReuseOrCreate: |
| 91 flags |= O_CREAT; |
| 92 break; |
| 93 case FileWriteMode::kTruncateOrCreate: |
| 94 flags |= O_CREAT | O_TRUNC; |
| 95 break; |
| 96 case FileWriteMode::kCreateOrFail: |
| 97 flags |= O_CREAT | O_EXCL; |
| 98 break; |
| 99 } |
| 100 |
| 101 return HANDLE_EINTR( |
| 102 open(path.value().c_str(), |
| 103 flags, |
| 104 permissions == FilePermissions::kWorldReadable ? 0644 : 0600)); |
| 105 } |
| 106 |
| 107 } // namespace |
| 108 |
| 109 FileOperationResult ReadFile(FileHandle file, void* buffer, size_t size) { |
| 110 return ReadOrWrite<ReadTraits>(file, buffer, size); |
| 111 } |
| 112 |
| 113 FileOperationResult WriteFile(FileHandle file, |
| 114 const void* buffer, |
| 115 size_t size) { |
| 116 return ReadOrWrite<WriteTraits>(file, buffer, size); |
| 117 } |
| 118 |
| 119 FileHandle OpenFileForRead(const base::FilePath& path) { |
| 120 return HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)); |
| 121 } |
| 122 |
| 123 FileHandle OpenFileForWrite(const base::FilePath& path, |
| 124 FileWriteMode mode, |
| 125 FilePermissions permissions) { |
| 126 return OpenFileForOutput(O_WRONLY, path, mode, permissions); |
| 127 } |
| 128 |
| 129 FileHandle OpenFileForReadAndWrite(const base::FilePath& path, |
| 130 FileWriteMode mode, |
| 131 FilePermissions permissions) { |
| 132 return OpenFileForOutput(O_RDWR, path, mode, permissions); |
| 133 } |
| 134 |
| 135 FileHandle LoggingOpenFileForRead(const base::FilePath& path) { |
| 136 FileHandle fd = OpenFileForRead(path); |
| 137 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); |
| 138 return fd; |
| 139 } |
| 140 |
| 141 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, |
| 142 FileWriteMode mode, |
| 143 FilePermissions permissions) { |
| 144 FileHandle fd = OpenFileForWrite(path, mode, permissions); |
| 145 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); |
| 146 return fd; |
| 147 } |
| 148 |
| 149 FileHandle LoggingOpenFileForReadAndWrite(const base::FilePath& path, |
| 150 FileWriteMode mode, |
| 151 FilePermissions permissions) { |
| 152 FileHandle fd = OpenFileForReadAndWrite(path, mode, permissions); |
| 153 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); |
| 154 return fd; |
| 155 } |
| 156 |
| 157 bool LoggingLockFile(FileHandle file, FileLocking locking) { |
| 158 int operation = (locking == FileLocking::kShared) ? LOCK_SH : LOCK_EX; |
| 159 int rv = HANDLE_EINTR(flock(file, operation)); |
| 160 PLOG_IF(ERROR, rv != 0) << "flock"; |
| 161 return rv == 0; |
| 162 } |
| 163 |
| 164 bool LoggingUnlockFile(FileHandle file) { |
| 165 int rv = flock(file, LOCK_UN); |
| 166 PLOG_IF(ERROR, rv != 0) << "flock"; |
| 167 return rv == 0; |
| 168 } |
| 169 |
| 170 FileOffset LoggingSeekFile(FileHandle file, FileOffset offset, int whence) { |
| 171 off_t rv = lseek(file, offset, whence); |
| 172 PLOG_IF(ERROR, rv < 0) << "lseek"; |
| 173 return rv; |
| 174 } |
| 175 |
| 176 bool LoggingTruncateFile(FileHandle file) { |
| 177 if (HANDLE_EINTR(ftruncate(file, 0)) != 0) { |
| 178 PLOG(ERROR) << "ftruncate"; |
| 179 return false; |
| 180 } |
| 181 return true; |
| 182 } |
| 183 |
| 184 bool LoggingCloseFile(FileHandle file) { |
| 185 int rv = IGNORE_EINTR(close(file)); |
| 186 PLOG_IF(ERROR, rv != 0) << "close"; |
| 187 return rv == 0; |
| 188 } |
| 189 |
| 190 } // namespace crashpad |
| OLD | NEW |