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 19 matching lines...) Expand all Loading... |
30 } | 30 } |
31 return false; | 31 return false; |
32 } | 32 } |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 namespace crashpad { | 36 namespace crashpad { |
37 | 37 |
38 namespace { | 38 namespace { |
39 | 39 |
40 FileHandle LoggingOpenFileForOutput(DWORD access, | 40 FileHandle OpenFileForOutput(DWORD access, |
41 const base::FilePath& path, | 41 const base::FilePath& path, |
42 FileWriteMode mode, | 42 FileWriteMode mode, |
43 FilePermissions permissions) { | 43 FilePermissions permissions) { |
| 44 DCHECK(access & GENERIC_WRITE); |
| 45 DCHECK_EQ(access & ~(GENERIC_READ | GENERIC_WRITE), 0u); |
| 46 |
44 DWORD disposition = 0; | 47 DWORD disposition = 0; |
45 switch (mode) { | 48 switch (mode) { |
46 case FileWriteMode::kReuseOrFail: | 49 case FileWriteMode::kReuseOrFail: |
47 disposition = OPEN_EXISTING; | 50 disposition = OPEN_EXISTING; |
48 break; | 51 break; |
49 case FileWriteMode::kReuseOrCreate: | 52 case FileWriteMode::kReuseOrCreate: |
50 disposition = OPEN_ALWAYS; | 53 disposition = OPEN_ALWAYS; |
51 break; | 54 break; |
52 case FileWriteMode::kTruncateOrCreate: | 55 case FileWriteMode::kTruncateOrCreate: |
53 disposition = CREATE_ALWAYS; | 56 disposition = CREATE_ALWAYS; |
54 break; | 57 break; |
55 case FileWriteMode::kCreateOrFail: | 58 case FileWriteMode::kCreateOrFail: |
56 disposition = CREATE_NEW; | 59 disposition = CREATE_NEW; |
57 break; | 60 break; |
58 } | 61 } |
59 HANDLE file = CreateFile(path.value().c_str(), | 62 return CreateFile(path.value().c_str(), |
60 access, | 63 access, |
61 FILE_SHARE_READ | FILE_SHARE_WRITE, | 64 FILE_SHARE_READ | FILE_SHARE_WRITE, |
62 nullptr, | 65 nullptr, |
63 disposition, | 66 disposition, |
64 FILE_ATTRIBUTE_NORMAL, | 67 FILE_ATTRIBUTE_NORMAL, |
65 nullptr); | 68 nullptr); |
66 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) | |
67 << "CreateFile " << base::UTF16ToUTF8(path.value()); | |
68 return file; | |
69 } | 69 } |
70 | 70 |
71 } // namespace | 71 } // namespace |
72 | 72 |
73 // TODO(scottmg): Handle > DWORD sized writes if necessary. | 73 // TODO(scottmg): Handle > DWORD sized writes if necessary. |
74 | 74 |
75 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { | 75 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { |
76 DCHECK(!IsSocketHandle(file)); | 76 DCHECK(!IsSocketHandle(file)); |
77 DWORD size_dword = base::checked_cast<DWORD>(size); | 77 DWORD size_dword = base::checked_cast<DWORD>(size); |
78 DWORD total_read = 0; | 78 DWORD total_read = 0; |
(...skipping 29 matching lines...) Expand all Loading... |
108 // network in the future. | 108 // network in the future. |
109 DWORD size_dword = base::checked_cast<DWORD>(size); | 109 DWORD size_dword = base::checked_cast<DWORD>(size); |
110 DWORD bytes_written; | 110 DWORD bytes_written; |
111 BOOL rv = ::WriteFile(file, buffer, size_dword, &bytes_written, nullptr); | 111 BOOL rv = ::WriteFile(file, buffer, size_dword, &bytes_written, nullptr); |
112 if (!rv) | 112 if (!rv) |
113 return -1; | 113 return -1; |
114 CHECK_EQ(bytes_written, size_dword); | 114 CHECK_EQ(bytes_written, size_dword); |
115 return bytes_written; | 115 return bytes_written; |
116 } | 116 } |
117 | 117 |
| 118 FileHandle OpenFileForRead(const base::FilePath& path) { |
| 119 return CreateFile(path.value().c_str(), |
| 120 GENERIC_READ, |
| 121 FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 122 nullptr, |
| 123 OPEN_EXISTING, |
| 124 0, |
| 125 nullptr); |
| 126 } |
| 127 |
| 128 FileHandle OpenFileForWrite(const base::FilePath& path, |
| 129 FileWriteMode mode, |
| 130 FilePermissions permissions) { |
| 131 return OpenFileForOutput(GENERIC_WRITE, path, mode, permissions); |
| 132 } |
| 133 |
| 134 FileHandle OpenFileForReadAndWrite(const base::FilePath& path, |
| 135 FileWriteMode mode, |
| 136 FilePermissions permissions) { |
| 137 return OpenFileForOutput( |
| 138 GENERIC_READ | GENERIC_WRITE, path, mode, permissions); |
| 139 } |
| 140 |
118 FileHandle LoggingOpenFileForRead(const base::FilePath& path) { | 141 FileHandle LoggingOpenFileForRead(const base::FilePath& path) { |
119 HANDLE file = CreateFile(path.value().c_str(), | 142 FileHandle file = OpenFileForRead(path); |
120 GENERIC_READ, | |
121 FILE_SHARE_READ | FILE_SHARE_WRITE, | |
122 nullptr, | |
123 OPEN_EXISTING, | |
124 0, | |
125 nullptr); | |
126 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) | 143 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) |
127 << "CreateFile " << base::UTF16ToUTF8(path.value()); | 144 << "CreateFile " << base::UTF16ToUTF8(path.value()); |
128 return file; | 145 return file; |
129 } | 146 } |
130 | 147 |
131 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, | 148 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, |
132 FileWriteMode mode, | 149 FileWriteMode mode, |
133 FilePermissions permissions) { | 150 FilePermissions permissions) { |
134 return LoggingOpenFileForOutput(GENERIC_WRITE, path, mode, permissions); | 151 FileHandle file = OpenFileForWrite(path, mode, permissions); |
| 152 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) |
| 153 << "CreateFile " << base::UTF16ToUTF8(path.value()); |
| 154 return file; |
135 } | 155 } |
136 | 156 |
137 FileHandle LoggingOpenFileForReadAndWrite(const base::FilePath& path, | 157 FileHandle LoggingOpenFileForReadAndWrite(const base::FilePath& path, |
138 FileWriteMode mode, | 158 FileWriteMode mode, |
139 FilePermissions permissions) { | 159 FilePermissions permissions) { |
140 return LoggingOpenFileForOutput( | 160 FileHandle file = OpenFileForReadAndWrite(path, mode, permissions); |
141 GENERIC_READ | GENERIC_WRITE, path, mode, permissions); | 161 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) |
| 162 << "CreateFile " << base::UTF16ToUTF8(path.value()); |
| 163 return file; |
142 } | 164 } |
143 | 165 |
144 bool LoggingLockFile(FileHandle file, FileLocking locking) { | 166 bool LoggingLockFile(FileHandle file, FileLocking locking) { |
145 DWORD flags = | 167 DWORD flags = |
146 (locking == FileLocking::kExclusive) ? LOCKFILE_EXCLUSIVE_LOCK : 0; | 168 (locking == FileLocking::kExclusive) ? LOCKFILE_EXCLUSIVE_LOCK : 0; |
147 | 169 |
148 // Note that the `Offset` fields of overlapped indicate the start location for | 170 // Note that the `Offset` fields of overlapped indicate the start location for |
149 // locking (beginning of file in this case), and `hEvent` must be also be set | 171 // locking (beginning of file in this case), and `hEvent` must be also be set |
150 // to 0. | 172 // to 0. |
151 OVERLAPPED overlapped = {0}; | 173 OVERLAPPED overlapped = {0}; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 return true; | 228 return true; |
207 } | 229 } |
208 | 230 |
209 bool LoggingCloseFile(FileHandle file) { | 231 bool LoggingCloseFile(FileHandle file) { |
210 BOOL rv = CloseHandle(file); | 232 BOOL rv = CloseHandle(file); |
211 PLOG_IF(ERROR, !rv) << "CloseHandle"; | 233 PLOG_IF(ERROR, !rv) << "CloseHandle"; |
212 return rv; | 234 return rv; |
213 } | 235 } |
214 | 236 |
215 } // namespace crashpad | 237 } // namespace crashpad |
OLD | NEW |