| 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 16 matching lines...) Expand all Loading... |
| 27 // socket. | 27 // socket. |
| 28 return !GetNamedPipeInfo(file, NULL, NULL, NULL, NULL); | 28 return !GetNamedPipeInfo(file, NULL, NULL, NULL, NULL); |
| 29 } | 29 } |
| 30 return false; | 30 return false; |
| 31 } | 31 } |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 namespace crashpad { | 35 namespace crashpad { |
| 36 | 36 |
| 37 namespace { |
| 38 |
| 39 FileHandle LoggingOpenFileForOutput(DWORD access, |
| 40 const base::FilePath& path, |
| 41 FileWriteMode mode, |
| 42 FilePermissions permissions) { |
| 43 DWORD disposition = 0; |
| 44 switch (mode) { |
| 45 case FileWriteMode::kReuseOrCreate: |
| 46 disposition = OPEN_ALWAYS; |
| 47 break; |
| 48 case FileWriteMode::kTruncateOrCreate: |
| 49 disposition = CREATE_ALWAYS; |
| 50 break; |
| 51 case FileWriteMode::kCreateOrFail: |
| 52 disposition = CREATE_NEW; |
| 53 break; |
| 54 } |
| 55 HANDLE file = CreateFile(path.value().c_str(), |
| 56 access, |
| 57 FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 58 nullptr, |
| 59 disposition, |
| 60 FILE_ATTRIBUTE_NORMAL, |
| 61 nullptr); |
| 62 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile " |
| 63 << path.value().c_str(); |
| 64 return file; |
| 65 } |
| 66 |
| 67 } // namespace |
| 68 |
| 37 // TODO(scottmg): Handle > DWORD sized writes if necessary. | 69 // TODO(scottmg): Handle > DWORD sized writes if necessary. |
| 38 | 70 |
| 39 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { | 71 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { |
| 40 DCHECK(!IsSocketHandle(file)); | 72 DCHECK(!IsSocketHandle(file)); |
| 41 DWORD size_dword = base::checked_cast<DWORD>(size); | 73 DWORD size_dword = base::checked_cast<DWORD>(size); |
| 42 DWORD total_read = 0; | 74 DWORD total_read = 0; |
| 43 char* buffer_c = reinterpret_cast<char*>(buffer); | 75 char* buffer_c = reinterpret_cast<char*>(buffer); |
| 44 while (size_dword > 0) { | 76 while (size_dword > 0) { |
| 45 DWORD bytes_read; | 77 DWORD bytes_read; |
| 46 BOOL success = ::ReadFile(file, buffer_c, size_dword, &bytes_read, nullptr); | 78 BOOL success = ::ReadFile(file, buffer_c, size_dword, &bytes_read, nullptr); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 0, | 120 0, |
| 89 nullptr); | 121 nullptr); |
| 90 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile " | 122 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile " |
| 91 << path.value().c_str(); | 123 << path.value().c_str(); |
| 92 return file; | 124 return file; |
| 93 } | 125 } |
| 94 | 126 |
| 95 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, | 127 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, |
| 96 FileWriteMode mode, | 128 FileWriteMode mode, |
| 97 FilePermissions permissions) { | 129 FilePermissions permissions) { |
| 98 DWORD disposition = 0; | 130 return LoggingOpenFileForOutput(GENERIC_WRITE, path, mode, permissions); |
| 99 switch (mode) { | 131 } |
| 100 case FileWriteMode::kReuseOrCreate: | 132 |
| 101 disposition = OPEN_ALWAYS; | 133 FileHandle LoggingOpenFileForReadAndWrite(const base::FilePath& path, |
| 102 break; | 134 FileWriteMode mode, |
| 103 case FileWriteMode::kTruncateOrCreate: | 135 FilePermissions permissions) { |
| 104 disposition = CREATE_ALWAYS; | 136 return LoggingOpenFileForOutput( |
| 105 break; | 137 GENERIC_READ | GENERIC_WRITE, path, mode, permissions); |
| 106 case FileWriteMode::kCreateOrFail: | |
| 107 disposition = CREATE_NEW; | |
| 108 break; | |
| 109 } | |
| 110 HANDLE file = CreateFile(path.value().c_str(), | |
| 111 GENERIC_WRITE, | |
| 112 FILE_SHARE_READ | FILE_SHARE_WRITE, | |
| 113 nullptr, | |
| 114 disposition, | |
| 115 FILE_ATTRIBUTE_NORMAL, | |
| 116 nullptr); | |
| 117 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile " | |
| 118 << path.value().c_str(); | |
| 119 return file; | |
| 120 } | 138 } |
| 121 | 139 |
| 122 bool LoggingLockFile(FileHandle file, FileLocking locking) { | 140 bool LoggingLockFile(FileHandle file, FileLocking locking) { |
| 123 DWORD flags = | 141 DWORD flags = |
| 124 (locking == FileLocking::kExclusive) ? LOCKFILE_EXCLUSIVE_LOCK : 0; | 142 (locking == FileLocking::kExclusive) ? LOCKFILE_EXCLUSIVE_LOCK : 0; |
| 125 | 143 |
| 126 // Note that the `Offset` fields of overlapped indicate the start location for | 144 // Note that the `Offset` fields of overlapped indicate the start location for |
| 127 // locking (beginning of file in this case), and `hEvent` must be also be set | 145 // locking (beginning of file in this case), and `hEvent` must be also be set |
| 128 // to 0. | 146 // to 0. |
| 129 OVERLAPPED overlapped = {0}; | 147 OVERLAPPED overlapped = {0}; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 distance_to_move.QuadPart = offset; | 185 distance_to_move.QuadPart = offset; |
| 168 LARGE_INTEGER new_offset; | 186 LARGE_INTEGER new_offset; |
| 169 BOOL result = SetFilePointerEx(file, distance_to_move, &new_offset, method); | 187 BOOL result = SetFilePointerEx(file, distance_to_move, &new_offset, method); |
| 170 if (!result) { | 188 if (!result) { |
| 171 PLOG(ERROR) << "SetFilePointerEx"; | 189 PLOG(ERROR) << "SetFilePointerEx"; |
| 172 return -1; | 190 return -1; |
| 173 } | 191 } |
| 174 return new_offset.QuadPart; | 192 return new_offset.QuadPart; |
| 175 } | 193 } |
| 176 | 194 |
| 195 bool LoggingTruncateFile(FileHandle file) { |
| 196 if (LoggingSeekFile(file, 0, SEEK_SET) != 0) |
| 197 return false; |
| 198 if (!SetEndOfFile(file)) { |
| 199 PLOG(ERROR) << "SetEndOfFile"; |
| 200 return false; |
| 201 } |
| 202 return true; |
| 203 } |
| 204 |
| 177 bool LoggingCloseFile(FileHandle file) { | 205 bool LoggingCloseFile(FileHandle file) { |
| 178 BOOL rv = CloseHandle(file); | 206 BOOL rv = CloseHandle(file); |
| 179 PLOG_IF(ERROR, !rv) << "CloseHandle"; | 207 PLOG_IF(ERROR, !rv) << "CloseHandle"; |
| 180 return rv; | 208 return rv; |
| 181 } | 209 } |
| 182 | 210 |
| 183 } // namespace crashpad | 211 } // namespace crashpad |
| OLD | NEW |