OLD | NEW |
---|---|
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 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 "client/crash_report_database.h" | 15 #include "client/crash_report_database.h" |
16 | 16 |
17 #include <rpc.h> | 17 #include <rpc.h> |
18 #include <string.h> | 18 #include <string.h> |
19 #include <time.h> | 19 #include <time.h> |
20 #include <windows.h> | 20 #include <windows.h> |
21 | 21 |
22 #include "base/logging.h" | 22 #include "base/logging.h" |
23 #include "base/numerics/safe_math.h" | 23 #include "base/numerics/safe_math.h" |
24 #include "base/strings/string16.h" | |
24 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
25 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
26 | 27 |
27 namespace crashpad { | 28 namespace crashpad { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 const wchar_t kDatabaseDirectoryName[] = L"Crashpad"; | 32 const wchar_t kDatabaseDirectoryName[] = L"Crashpad"; |
32 | 33 |
33 const wchar_t kReportsDirectory[] = L"reports"; | 34 const wchar_t kReportsDirectory[] = L"reports"; |
34 const wchar_t kMetadataFileName[] = L"metadata"; | 35 const wchar_t kMetadataFileName[] = L"metadata"; |
35 | 36 |
36 const wchar_t kCrashReportFileExtension[] = L"dmp"; | 37 const wchar_t kCrashReportFileExtension[] = L"dmp"; |
37 | 38 |
38 enum class ReportState : int { | 39 const uint32_t kMetadataFileHeaderMagic = 'CPAD'; |
40 const uint32_t kMetadataFileVersion = 1; | |
41 | |
42 using OperationStatus = CrashReportDatabase::OperationStatus; | |
43 | |
44 // The format of the on disk metadata file is a MetadataFileHeader, followed by | |
45 // a number of fixed size records of MetadataFileReportRecord, followed by a | |
46 // string table in UTF8 format, where each string is \0 terminated. | |
47 struct MetadataFileHeader { | |
48 uint32_t magic; | |
49 uint32_t version; | |
50 uint32_t num_records; | |
51 uint32_t padding; | |
52 }; | |
53 | |
54 // Adds a string to the string table and returns the byte index where it was | |
55 // added. | |
56 uint32_t AddStringToTable(std::string* string_table, const std::string& str) { | |
57 uint32_t offset = base::checked_cast<uint32_t>(string_table->size()); | |
58 *string_table += str; | |
59 *string_table += '\0'; | |
60 return offset; | |
61 } | |
62 | |
63 // Converts |str| to UTF8, adds the result to the string table and returns the | |
64 // byte index where it was added. | |
65 uint32_t AddStringToTable(std::string* string_table, | |
66 const base::string16& str) { | |
67 return AddStringToTable(string_table, base::UTF16ToUTF8(str)); | |
68 } | |
69 | |
70 struct ReportDisk; | |
71 | |
72 struct MetadataFileReportRecord { | |
73 UUID uuid; // UUID is a 16 byte, standard layout structure. | |
Peter Kasting
2015/02/14 00:24:24
Nit: Consider lining up most or all of these EOL c
scottmg
2015/02/14 00:42:45
Done.
| |
74 uint32_t file_path_index; // Index into string table. File name is relative | |
75 // to the reports directory when on disk. | |
76 uint32_t id_index; // Index into string table. | |
77 int64_t creation_time; // Holds a time_t. | |
78 int64_t last_upload_attempt_time; // Holds a time_t. | |
79 int32_t upload_attempts; | |
80 int32_t state; // A ReportState. | |
81 uint8_t uploaded; // Boolean, 0 or 1. | |
82 uint8_t padding[7]; | |
83 | |
84 static MetadataFileReportRecord FromReportDisk(const ReportDisk& report, | |
85 std::string* string_table); | |
86 }; | |
87 | |
88 enum class ReportState { | |
39 //! \brief Created and filled out by caller, owned by database. | 89 //! \brief Created and filled out by caller, owned by database. |
40 kPending, | 90 kPending, |
41 //! \brief In the process of uploading, owned by caller. | 91 //! \brief In the process of uploading, owned by caller. |
42 kUploading, | 92 kUploading, |
43 //! \brief Upload completed or skipped, owned by database. | 93 //! \brief Upload completed or skipped, owned by database. |
44 kCompleted, | 94 kCompleted, |
45 }; | 95 }; |
46 | 96 |
47 using OperationStatus = CrashReportDatabase::OperationStatus; | |
48 | |
49 //! \brief Ensures that the node at path is a directory, and creates it if it | |
50 //! does not exist. | |
51 //! | |
52 //! \return If the path points to a file, rather than a directory, or the | |
53 //! directory could not be created, returns `false`. Otherwise, returns | |
54 //! `true`, indicating that path already was or now is a directory. | |
55 bool CreateOrEnsureDirectoryExists(const base::FilePath& path) { | |
56 if (CreateDirectory(path.value().c_str(), nullptr)) { | |
57 return true; | |
58 } else if (GetLastError() == ERROR_ALREADY_EXISTS) { | |
59 DWORD fileattr = GetFileAttributes(path.value().c_str()); | |
60 if (fileattr == INVALID_FILE_ATTRIBUTES) { | |
61 PLOG(ERROR) << "GetFileAttributes"; | |
62 return false; | |
63 } | |
64 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | |
65 return true; | |
66 LOG(ERROR) << "not a directory"; | |
67 return false; | |
68 } else { | |
69 PLOG(ERROR) << "CreateDirectory"; | |
70 return false; | |
71 } | |
72 } | |
73 | |
74 //! \brief A private extension of the Report class that includes additional data | 97 //! \brief A private extension of the Report class that includes additional data |
75 //! that's stored on disk in the metadata file. | 98 //! that's stored on disk in the metadata file. |
76 struct ReportDisk : public CrashReportDatabase::Report { | 99 struct ReportDisk : public CrashReportDatabase::Report { |
77 //! \brief The current state of the report. | 100 //! \brief The current state of the report. |
78 ReportState state; | 101 ReportState state; |
102 | |
103 static ReportDisk FromMetataFileReportRecord( | |
104 const MetadataFileReportRecord& record, | |
105 const base::FilePath& report_dir, | |
106 const std::string& string_table); | |
79 }; | 107 }; |
80 | 108 |
109 // static | |
110 MetadataFileReportRecord MetadataFileReportRecord::FromReportDisk( | |
111 const ReportDisk& report, | |
112 std::string* string_table) { | |
113 MetadataFileReportRecord r; | |
114 memset(&r, 0, sizeof(r)); | |
Peter Kasting
2015/02/14 00:24:23
Nit: This is legal, but the following would be mor
scottmg
2015/02/14 00:42:45
In the most recent ps (the constructor version), I
| |
115 r.uuid = report.uuid; | |
116 r.file_path_index = | |
117 AddStringToTable(string_table, report.file_path.BaseName().value()); | |
118 r.id_index = AddStringToTable(string_table, report.id); | |
119 r.creation_time = report.creation_time; | |
120 r.last_upload_attempt_time = report.last_upload_attempt_time; | |
121 r.uploaded = report.uploaded; | |
122 r.upload_attempts = report.upload_attempts; | |
123 r.state = static_cast<uint32_t>(report.state); | |
124 return r; | |
125 } | |
126 | |
127 // static | |
128 ReportDisk ReportDisk::FromMetataFileReportRecord( | |
129 const MetadataFileReportRecord& record, | |
130 const base::FilePath& report_dir, | |
131 const std::string& string_table) { | |
132 ReportDisk r; | |
133 r.uuid = record.uuid; | |
134 r.file_path = report_dir.Append( | |
135 base::UTF8ToUTF16(&string_table[record.file_path_index])); | |
136 r.id = &string_table[record.id_index]; | |
137 r.creation_time = record.creation_time; | |
138 r.uploaded = record.uploaded; | |
139 r.last_upload_attempt_time = record.last_upload_attempt_time; | |
140 r.upload_attempts = record.upload_attempts; | |
141 r.state = static_cast<ReportState>(record.state); | |
142 return r; | |
143 } | |
144 | |
81 //! \brief A private extension of the NewReport class to hold the UUID during | 145 //! \brief A private extension of the NewReport class to hold the UUID during |
82 //! initial write. We don't store metadata in dump's file attributes, and | 146 //! initial write. We don't store metadata in dump's file attributes, so we |
83 //! use the UUID to identify the dump on write completion. | 147 //! use the UUID to identify the dump on write completion. |
84 struct NewReportDisk : public CrashReportDatabase::NewReport { | 148 struct NewReportDisk : public CrashReportDatabase::NewReport { |
85 //! \brief The UUID for this crash report. | 149 //! \brief The UUID for this crash report. |
86 UUID uuid; | 150 UUID uuid; |
87 }; | 151 }; |
88 | 152 |
89 //! \brief Manages the metadata for the set of reports, handling serialization | 153 //! \brief Manages the metadata for the set of reports, handling serialization |
90 //! to disk, and queries. Instances of this class should be created by using | 154 //! to disk, and queries. |
91 //! CrashReportDatabaseWin::AcquireMetadata(). | |
92 class Metadata { | 155 class Metadata { |
93 public: | 156 public: |
94 //! \brief Writes any changes if necessary, unlocks and closes the file | 157 //! \brief Writes any changes if necessary, unlocks and closes the file |
95 //! handle. | 158 //! handle. |
96 ~Metadata(); | 159 ~Metadata(); |
97 | 160 |
161 static scoped_ptr<Metadata> Create(const base::FilePath& metadata_file, | |
162 const base::FilePath& report_dir); | |
163 | |
98 //! \brief Adds a new report to the set. | 164 //! \brief Adds a new report to the set. |
99 //! | 165 //! |
100 //! \param[in] new_report_disk The record to add. The #state field must be set | 166 //! \param[in] new_report_disk The record to add. The #state field must be set |
101 //! to kPending. | 167 //! to kPending. |
102 void AddNewRecord(const ReportDisk& new_report_disk); | 168 void AddNewRecord(const ReportDisk& new_report_disk); |
103 | 169 |
104 //! \brief Finds all reports in a given state. The \a reports vector is only | 170 //! \brief Finds all reports in a given state. The \a reports vector is only |
105 //! valid when CrashReportDatabase::kNoError is returned. | 171 //! valid when CrashReportDatabase::kNoError is returned. |
106 //! | 172 //! |
107 //! \param[in] desired_state The state to match. | 173 //! \param[in] desired_state The state to match. |
108 //! \param[out] reports Matching reports, must be empty on entry. | 174 //! \param[out] reports Matching reports, must be empty on entry. |
109 OperationStatus FindReports( | 175 OperationStatus FindReports( |
110 ReportState desired_state, | 176 ReportState desired_state, |
111 std::vector<const CrashReportDatabase::Report>* reports); | 177 std::vector<const CrashReportDatabase::Report>* reports) const; |
112 | 178 |
113 //! \brief Finds the report matching the given UUID. | 179 //! \brief Finds the report matching the given UUID. |
114 //! | 180 //! |
115 //! The returned report is only valid if CrashReportDatabase::kNoError is | 181 //! The returned report is only valid if CrashReportDatabase::kNoError is |
116 //! returned. | 182 //! returned. |
117 //! | 183 //! |
118 //! \param[in] uuid The report identifier. | 184 //! \param[in] uuid The report identifier. |
119 //! \param[out] report_disk The found report, valid only if | 185 //! \param[out] report_disk The found report, valid only if |
120 //! CrashReportDatabase::kNoError is returned. Ownership is not | 186 //! CrashReportDatabase::kNoError is returned. Ownership is not |
121 //! transferred to the caller, and the report may not be modified. | 187 //! transferred to the caller, and the report may not be modified. |
122 OperationStatus FindSingleReport(const UUID& uuid, | 188 OperationStatus FindSingleReport(const UUID& uuid, |
123 const ReportDisk** report_disk); | 189 const ReportDisk** report_disk) const; |
124 | 190 |
125 //! \brief Finds a single report matching the given UUID and in the desired | 191 //! \brief Finds a single report matching the given UUID and in the desired |
126 //! state and calls the client-supplied mutator to modify the report. | 192 //! state, and returns a mutable ReportDisk* if found. |
127 //! | 193 //! |
128 //! The mutator object must have an operator()(ReportDisk*) which makes the | 194 //! This marks the metadata as dirty, and on destruction, changes will be |
129 //! desired changes. | 195 //! written to disk via Write(). |
130 //! | 196 //! |
131 //! \return #kNoError on success. #kReportNotFound if there was no report with | 197 //! \return #kNoError on success. #kReportNotFound if there was no report with |
132 //! the specified UUID. #kBusyError if the report was not in the specified | 198 //! the specified UUID. #kBusyError if the report was not in the specified |
133 //! state. | 199 //! state. |
134 template <class T> | 200 OperationStatus FindSingleReportAndMarkDirty(const UUID& uuid, |
135 OperationStatus MutateSingleReport(const UUID& uuid, | 201 ReportState desired_state, |
136 ReportState desired_state, | 202 ReportDisk** report_disk); |
137 const T& mutator); | |
138 | 203 |
139 private: | 204 private: |
140 static scoped_ptr<Metadata> Create(const base::FilePath& metadata_file, | |
141 const base::FilePath& report_dir); | |
142 friend class CrashReportDatabaseWin; | |
143 | |
144 Metadata(FileHandle handle, const base::FilePath& report_dir); | 205 Metadata(FileHandle handle, const base::FilePath& report_dir); |
145 | 206 |
146 bool Rewind(); | 207 bool Rewind(); |
147 | 208 |
148 void Read(); | 209 void Read(); |
149 void Write(); | 210 void Write(); |
150 | 211 |
151 //! \brief Confirms that the corresponding report actually exists on disk | 212 //! \brief Confirms that the corresponding report actually exists on disk |
152 //! (that is, the dump file has not been removed), that the report is in | 213 //! (that is, the dump file has not been removed), and that the report is |
153 //! the given state. | 214 //! in the given state. |
154 static OperationStatus VerifyReport(const ReportDisk& report_disk, | 215 static OperationStatus VerifyReport(const ReportDisk& report_disk, |
155 ReportState desired_state); | 216 ReportState desired_state); |
156 //! \brief Confirms that the corresponding report actually exists on disk | 217 //! \brief Confirms that the corresponding report actually exists on disk |
157 //! (that is, the dump file has not been removed). | 218 //! (that is, the dump file has not been removed). |
158 static OperationStatus VerifyReportAnyState(const ReportDisk& report_disk); | 219 static OperationStatus VerifyReportAnyState(const ReportDisk& report_disk); |
159 | 220 |
160 ScopedFileHandle handle_; | 221 ScopedFileHandle handle_; |
161 const base::FilePath report_dir_; | 222 const base::FilePath report_dir_; |
162 bool dirty_; //! \brief Is a Write() required on destruction? | 223 bool dirty_; //! \brief True when a Write() is required on destruction. |
163 std::vector<ReportDisk> reports_; | 224 std::vector<ReportDisk> reports_; |
164 | 225 |
165 DISALLOW_COPY_AND_ASSIGN(Metadata); | 226 DISALLOW_COPY_AND_ASSIGN(Metadata); |
166 }; | 227 }; |
167 | 228 |
168 Metadata::Metadata(FileHandle handle, const base::FilePath& report_dir) | |
169 : handle_(handle), report_dir_(report_dir), dirty_(false), reports_() { | |
170 } | |
171 | |
172 Metadata::~Metadata() { | 229 Metadata::~Metadata() { |
173 if (dirty_) | 230 if (dirty_) |
174 Write(); | 231 Write(); |
175 // Not actually async, UnlockFileEx requires the Offset fields. | 232 // Not actually async, UnlockFileEx requires the Offset fields. |
176 OVERLAPPED overlapped = {0}; | 233 OVERLAPPED overlapped = {0}; |
177 if (!UnlockFileEx(handle_.get(), 0, MAXDWORD, MAXDWORD, &overlapped)) | 234 if (!UnlockFileEx(handle_.get(), 0, MAXDWORD, MAXDWORD, &overlapped)) |
178 PLOG(ERROR) << "UnlockFileEx"; | 235 PLOG(ERROR) << "UnlockFileEx"; |
179 } | 236 } |
180 | 237 |
181 // The format of the metadata file is a MetadataFileHeader, followed by a | |
182 // number of fixed size records of MetadataFileReportRecord, followed by a | |
183 // string table in UTF8 format, where each string is \0 terminated. | |
184 | |
185 #pragma pack(push, 1) | |
186 | |
187 struct MetadataFileHeader { | |
188 uint32_t magic; | |
189 uint32_t version; | |
190 uint32_t num_records; | |
191 uint32_t padding; | |
192 }; | |
193 | |
194 struct MetadataFileReportRecord { | |
195 UUID uuid; // UUID is a 16 byte, standard layout structure. | |
196 uint32_t file_path_index; // Index into string table. File name is relative | |
197 // to the reports directory when on disk. | |
198 uint32_t id_index; // Index into string table. | |
199 int64_t creation_time; // Holds a time_t. | |
200 int64_t last_upload_attempt_time; // Holds a time_t. | |
201 int32_t upload_attempts; | |
202 int32_t state; // A ReportState. | |
203 uint8_t uploaded; // Boolean, 0 or 1. | |
204 uint8_t padding[7]; | |
205 }; | |
206 | |
207 const uint32_t kMetadataFileHeaderMagic = 'CPAD'; | |
208 const uint32_t kMetadataFileVersion = 1; | |
209 | |
210 #pragma pack(pop) | |
211 | |
212 // Reads from the current file position to EOF and returns as uint8_t[]. | |
213 std::string ReadRestOfFileAsString(FileHandle file) { | |
214 FileOffset read_from = LoggingSeekFile(file, 0, SEEK_CUR); | |
215 FileOffset end = LoggingSeekFile(file, 0, SEEK_END); | |
216 FileOffset original = LoggingSeekFile(file, read_from, SEEK_SET); | |
217 if (read_from == -1 || end == -1 || original == -1) | |
218 return std::string(); | |
219 DCHECK_EQ(read_from, original); | |
220 DCHECK_GE(end, read_from); | |
221 size_t data_length = static_cast<size_t>(end - read_from); | |
222 std::string buffer(data_length, '\0'); | |
223 if (!LoggingReadFile(file, &buffer[0], data_length)) | |
224 return std::string(); | |
225 return buffer; | |
226 } | |
227 | |
228 uint32_t AddStringToTable(std::string* string_table, const std::string& str) { | |
229 uint32_t offset = base::checked_cast<uint32_t>(string_table->size()); | |
230 *string_table += str; | |
231 *string_table += '\0'; | |
232 return offset; | |
233 } | |
234 | |
235 uint32_t AddStringToTable(std::string* string_table, const std::wstring& str) { | |
236 return AddStringToTable(string_table, base::UTF16ToUTF8(str)); | |
237 } | |
238 | |
239 // static | 238 // static |
240 scoped_ptr<Metadata> Metadata::Create(const base::FilePath& metadata_file, | 239 scoped_ptr<Metadata> Metadata::Create(const base::FilePath& metadata_file, |
241 const base::FilePath& report_dir) { | 240 const base::FilePath& report_dir) { |
242 // It is important that dwShareMode be non-zero so that concurrent access to | 241 // It is important that dwShareMode be non-zero so that concurrent access to |
243 // this file results in a successful open. This allows us to get to LockFileEx | 242 // this file results in a successful open. This allows us to get to LockFileEx |
244 // which then blocks to guard access. | 243 // which then blocks to guard access. |
245 FileHandle handle = CreateFile(metadata_file.value().c_str(), | 244 FileHandle handle = CreateFile(metadata_file.value().c_str(), |
246 GENERIC_READ | GENERIC_WRITE, | 245 GENERIC_READ | GENERIC_WRITE, |
247 FILE_SHARE_READ | FILE_SHARE_WRITE, | 246 FILE_SHARE_READ | FILE_SHARE_WRITE, |
248 nullptr, | 247 nullptr, |
(...skipping 16 matching lines...) Expand all Loading... | |
265 | 264 |
266 scoped_ptr<Metadata> metadata(new Metadata(handle, report_dir)); | 265 scoped_ptr<Metadata> metadata(new Metadata(handle, report_dir)); |
267 // If Read() fails, for whatever reason (corruption, etc.) metadata will not | 266 // If Read() fails, for whatever reason (corruption, etc.) metadata will not |
268 // have been modified and will be in a clean empty state. We continue on and | 267 // have been modified and will be in a clean empty state. We continue on and |
269 // return an empty database to hopefully recover. This means that existing | 268 // return an empty database to hopefully recover. This means that existing |
270 // crash reports have been orphaned. | 269 // crash reports have been orphaned. |
271 metadata->Read(); | 270 metadata->Read(); |
272 return metadata; | 271 return metadata; |
273 } | 272 } |
274 | 273 |
274 void Metadata::AddNewRecord(const ReportDisk& new_report_disk) { | |
275 DCHECK(new_report_disk.state == ReportState::kPending); | |
276 reports_.push_back(new_report_disk); | |
277 dirty_ = true; | |
278 } | |
279 | |
280 OperationStatus Metadata::FindReports( | |
281 ReportState desired_state, | |
282 std::vector<const CrashReportDatabase::Report>* reports) const { | |
283 DCHECK(reports->empty()); | |
284 for (const auto& report : reports_) { | |
285 if (report.state == desired_state && | |
286 VerifyReport(report, desired_state) == CrashReportDatabase::kNoError) { | |
287 reports->push_back(report); | |
288 } | |
289 } | |
290 return CrashReportDatabase::kNoError; | |
291 } | |
292 | |
293 OperationStatus Metadata::FindSingleReport( | |
294 const UUID& uuid, | |
295 const ReportDisk** out_report) const { | |
296 auto report_iter = std::find_if( | |
297 reports_.begin(), reports_.end(), [uuid](const ReportDisk& report) { | |
298 return report.uuid == uuid; | |
299 }); | |
300 if (report_iter == reports_.end()) | |
301 return CrashReportDatabase::kReportNotFound; | |
302 OperationStatus os = VerifyReportAnyState(*report_iter); | |
303 if (os == CrashReportDatabase::kNoError) | |
304 *out_report = &*report_iter; | |
305 return os; | |
306 } | |
307 | |
308 OperationStatus Metadata::FindSingleReportAndMarkDirty( | |
309 const UUID& uuid, | |
310 ReportState desired_state, | |
311 ReportDisk** report_disk) { | |
312 auto report_iter = std::find_if( | |
313 reports_.begin(), reports_.end(), [uuid](const ReportDisk& report) { | |
314 return report.uuid == uuid; | |
315 }); | |
316 if (report_iter == reports_.end()) | |
317 return CrashReportDatabase::kReportNotFound; | |
318 OperationStatus os = VerifyReport(*report_iter, desired_state); | |
319 if (os == CrashReportDatabase::kNoError) { | |
320 dirty_ = true; | |
321 *report_disk = &*report_iter; | |
322 } | |
323 return os; | |
324 } | |
325 | |
326 Metadata::Metadata(FileHandle handle, const base::FilePath& report_dir) | |
327 : handle_(handle), report_dir_(report_dir), dirty_(false), reports_() { | |
328 } | |
329 | |
330 // Reads from the current file position to EOF and returns as a string of bytes. | |
331 std::string ReadRestOfFileAsString(FileHandle file) { | |
332 FileOffset read_from = LoggingSeekFile(file, 0, SEEK_CUR); | |
333 FileOffset end = LoggingSeekFile(file, 0, SEEK_END); | |
334 FileOffset original = LoggingSeekFile(file, read_from, SEEK_SET); | |
335 if (read_from == -1 || end == -1 || original == -1 || read_from == end) | |
336 return std::string(); | |
337 DCHECK_EQ(read_from, original); | |
338 DCHECK_GT(end, read_from); | |
339 size_t data_length = static_cast<size_t>(end - read_from); | |
340 std::string buffer(data_length, '\0'); | |
341 return LoggingReadFile(file, &buffer[0], data_length) ? buffer | |
342 : std::string(); | |
343 } | |
344 | |
275 bool Metadata::Rewind() { | 345 bool Metadata::Rewind() { |
276 FileOffset result = LoggingSeekFile(handle_.get(), 0, SEEK_SET); | 346 FileOffset result = LoggingSeekFile(handle_.get(), 0, SEEK_SET); |
277 DCHECK_EQ(result, 0); | 347 DCHECK_EQ(result, 0); |
278 return result == 0; | 348 return result == 0; |
279 } | 349 } |
280 | 350 |
281 void Metadata::Read() { | 351 void Metadata::Read() { |
282 FileOffset length = LoggingSeekFile(handle_.get(), 0, SEEK_END); | 352 FileOffset length = LoggingSeekFile(handle_.get(), 0, SEEK_END); |
283 if (length <= 0) // Failed, or empty: Abort. | 353 if (length <= 0) // Failed, or empty: Abort. |
284 return; | 354 return; |
285 if (!Rewind()) { | 355 if (!Rewind()) { |
286 LOG(ERROR) << "failed to rewind to read"; | 356 LOG(ERROR) << "failed to rewind to read"; |
287 return; | 357 return; |
288 } | 358 } |
289 | 359 |
290 MetadataFileHeader header; | 360 MetadataFileHeader header; |
291 if (!LoggingReadFile(handle_.get(), &header, sizeof(header))) { | 361 if (!LoggingReadFile(handle_.get(), &header, sizeof(header))) { |
292 LOG(ERROR) << "failed to read header"; | 362 LOG(ERROR) << "failed to read header"; |
293 return; | 363 return; |
294 } | 364 } |
295 if (header.magic != kMetadataFileHeaderMagic || | 365 if (header.magic != kMetadataFileHeaderMagic || |
296 header.version != kMetadataFileVersion) { | 366 header.version != kMetadataFileVersion) { |
297 LOG(ERROR) << "unexpected header"; | 367 LOG(ERROR) << "unexpected header"; |
298 return; | 368 return; |
299 } | 369 } |
300 | 370 |
301 auto records_size = base::CheckedNumeric<uint32_t>(header.num_records) * | 371 base::CheckedNumeric<uint32_t> records_size = |
302 sizeof(MetadataFileReportRecord); | 372 base::CheckedNumeric<uint32_t>(header.num_records) * |
373 sizeof(MetadataFileReportRecord); | |
303 if (!records_size.IsValid()) { | 374 if (!records_size.IsValid()) { |
304 LOG(ERROR) << "record size out of range"; | 375 LOG(ERROR) << "record size out of range"; |
305 return; | 376 return; |
306 } | 377 } |
307 | 378 |
308 scoped_ptr<MetadataFileReportRecord[]> records( | 379 std::vector<MetadataFileReportRecord> records(header.num_records); |
309 new MetadataFileReportRecord[header.num_records]); | 380 if (!LoggingReadFile(handle_.get(), &records[0], records_size.ValueOrDie())) { |
310 if (!LoggingReadFile( | |
311 handle_.get(), records.get(), records_size.ValueOrDie())) { | |
312 LOG(ERROR) << "failed to read records"; | 381 LOG(ERROR) << "failed to read records"; |
313 return; | 382 return; |
314 } | 383 } |
315 | 384 |
316 std::string string_table = ReadRestOfFileAsString(handle_.get()); | 385 std::string string_table = ReadRestOfFileAsString(handle_.get()); |
317 if (string_table.empty() || string_table.back() != '\0') { | 386 if (string_table.empty() || string_table.back() != '\0') { |
318 LOG(ERROR) << "bad string table"; | 387 LOG(ERROR) << "bad string table"; |
319 return; | 388 return; |
320 } | 389 } |
321 for (uint32_t i = 0; i < header.num_records; ++i) { | 390 |
322 ReportDisk r; | 391 std::vector<ReportDisk> reports; |
323 const MetadataFileReportRecord* record = &records[i]; | 392 for (const auto& record : records) { |
324 r.uuid = record->uuid; | 393 if (record.file_path_index >= string_table.size() || |
325 if (record->file_path_index >= string_table.size() || | 394 record.id_index >= string_table.size()) { |
326 record->id_index >= string_table.size()) { | |
327 reports_.clear(); | |
328 LOG(ERROR) << "invalid string table index"; | 395 LOG(ERROR) << "invalid string table index"; |
329 return; | 396 return; |
330 } | 397 } |
331 r.file_path = report_dir_.Append( | 398 reports.push_back(ReportDisk::FromMetataFileReportRecord( |
332 base::UTF8ToUTF16(&string_table[record->file_path_index])); | 399 record, report_dir_, string_table)); |
333 r.id = &string_table[record->id_index]; | |
334 r.creation_time = record->creation_time; | |
335 r.uploaded = record->uploaded; | |
336 r.last_upload_attempt_time = record->last_upload_attempt_time; | |
337 r.upload_attempts = record->upload_attempts; | |
338 r.state = static_cast<ReportState>(record->state); | |
339 reports_.push_back(r); | |
340 } | 400 } |
401 reports_.swap(reports); | |
341 } | 402 } |
342 | 403 |
343 void Metadata::Write() { | 404 void Metadata::Write() { |
344 if (!Rewind()) { | 405 if (!Rewind()) { |
345 LOG(ERROR) << "failed to rewind to write"; | 406 LOG(ERROR) << "failed to rewind to write"; |
346 return; | 407 return; |
347 } | 408 } |
348 | 409 |
349 // Truncate to ensure that a partial write doesn't cause a mix of old and new | 410 // Truncate to ensure that a partial write doesn't cause a mix of old and new |
350 // data causing an incorrect interpretation on read. | 411 // data causing an incorrect interpretation on read. |
351 if (!SetEndOfFile(handle_.get())) { | 412 if (!SetEndOfFile(handle_.get())) { |
352 PLOG(ERROR) << "failed to truncate"; | 413 PLOG(ERROR) << "failed to truncate"; |
353 return; | 414 return; |
354 } | 415 } |
355 | 416 |
356 size_t num_records = reports_.size(); | 417 size_t num_records = reports_.size(); |
357 | 418 |
358 // Fill and write out the header. | 419 // Fill and write out the header. |
359 MetadataFileHeader header = {0}; | 420 MetadataFileHeader header = {0}; |
360 header.magic = kMetadataFileHeaderMagic; | 421 header.magic = kMetadataFileHeaderMagic; |
361 header.version = kMetadataFileVersion; | 422 header.version = kMetadataFileVersion; |
362 header.num_records = base::checked_cast<uint32_t>(num_records); | 423 header.num_records = base::checked_cast<uint32_t>(num_records); |
363 if (!LoggingWriteFile(handle_.get(), &header, sizeof(header))) { | 424 if (!LoggingWriteFile(handle_.get(), &header, sizeof(header))) { |
364 LOG(ERROR) << "failed to write header"; | 425 LOG(ERROR) << "failed to write header"; |
365 return; | 426 return; |
366 } | 427 } |
367 | 428 |
368 // Build the records and string table we're going to write. | 429 // Build the records and string table we're going to write. |
369 std::string string_table; | 430 std::string string_table; |
370 scoped_ptr<MetadataFileReportRecord[]> records( | 431 std::vector<MetadataFileReportRecord> records; |
371 new MetadataFileReportRecord[num_records]); | 432 records.reserve(num_records); |
372 memset(records.get(), 0, sizeof(MetadataFileReportRecord) * num_records); | 433 for (const auto& report : reports_) { |
373 for (size_t i = 0; i < num_records; ++i) { | |
374 const ReportDisk& report = reports_[i]; | |
375 MetadataFileReportRecord& record = records[i]; | |
376 record.uuid = report.uuid; | |
377 const base::FilePath& path = report.file_path; | 434 const base::FilePath& path = report.file_path; |
378 if (path.DirName() != report_dir_) { | 435 if (path.DirName() != report_dir_) { |
379 LOG(ERROR) << path.value().c_str() << " expected to start with " | 436 LOG(ERROR) << path.value().c_str() << " expected to start with " |
380 << report_dir_.value().c_str(); | 437 << report_dir_.value().c_str(); |
381 return; | 438 return; |
382 } | 439 } |
383 record.file_path_index = | 440 records.push_back( |
384 AddStringToTable(&string_table, path.BaseName().value().c_str()); | 441 MetadataFileReportRecord::FromReportDisk(report, &string_table)); |
385 record.id_index = AddStringToTable(&string_table, report.id); | |
386 record.creation_time = report.creation_time; | |
387 record.uploaded = report.uploaded; | |
388 record.last_upload_attempt_time = report.last_upload_attempt_time; | |
389 record.upload_attempts = report.upload_attempts; | |
390 record.state = static_cast<uint32_t>(report.state); | |
391 } | 442 } |
392 | 443 |
393 if (!LoggingWriteFile(handle_.get(), | 444 if (!LoggingWriteFile(handle_.get(), |
394 records.get(), | 445 &records[0], |
395 num_records * sizeof(MetadataFileReportRecord))) { | 446 records.size() * sizeof(MetadataFileReportRecord))) { |
396 LOG(ERROR) << "failed to write records"; | 447 LOG(ERROR) << "failed to write records"; |
397 return; | 448 return; |
398 } | 449 } |
399 if (!LoggingWriteFile( | 450 if (!LoggingWriteFile( |
400 handle_.get(), string_table.c_str(), string_table.size())) { | 451 handle_.get(), string_table.c_str(), string_table.size())) { |
401 LOG(ERROR) << "failed to write string table"; | 452 LOG(ERROR) << "failed to write string table"; |
402 return; | 453 return; |
403 } | 454 } |
404 } | 455 } |
405 | 456 |
406 void Metadata::AddNewRecord(const ReportDisk& new_report_disk) { | |
407 DCHECK(new_report_disk.state == ReportState::kPending); | |
408 reports_.push_back(new_report_disk); | |
409 dirty_ = true; | |
410 } | |
411 | |
412 OperationStatus Metadata::FindReports( | |
413 ReportState desired_state, | |
414 std::vector<const CrashReportDatabase::Report>* reports) { | |
415 DCHECK(reports->empty()); | |
416 for (const auto& report : reports_) { | |
417 if (report.state == desired_state) { | |
418 if (VerifyReport(report, desired_state) != CrashReportDatabase::kNoError) | |
419 continue; | |
420 reports->push_back(report); | |
421 } | |
422 } | |
423 return CrashReportDatabase::kNoError; | |
424 } | |
425 | |
426 OperationStatus Metadata::FindSingleReport(const UUID& uuid, | |
427 const ReportDisk** out_report) { | |
428 for (size_t i = 0; i < reports_.size(); ++i) { | |
429 if (reports_[i].uuid == uuid) { | |
430 OperationStatus os = VerifyReportAnyState(reports_[i]); | |
431 if (os != CrashReportDatabase::kNoError) | |
432 return os; | |
433 *out_report = &reports_[i]; | |
434 return CrashReportDatabase::kNoError; | |
435 } | |
436 } | |
437 return CrashReportDatabase::kReportNotFound; | |
438 } | |
439 | |
440 template <class T> | |
441 OperationStatus Metadata::MutateSingleReport( | |
442 const UUID& uuid, | |
443 ReportState desired_state, | |
444 const T& mutator) { | |
445 for (size_t i = 0; i < reports_.size(); ++i) { | |
446 if (reports_[i].uuid == uuid) { | |
447 OperationStatus os = VerifyReport(reports_[i], desired_state); | |
448 if (os != CrashReportDatabase::kNoError) | |
449 return os; | |
450 mutator(&reports_[i]); | |
451 dirty_ = true; | |
452 return CrashReportDatabase::kNoError; | |
453 } | |
454 } | |
455 return CrashReportDatabase::kReportNotFound; | |
456 } | |
457 | |
458 // static | 457 // static |
459 OperationStatus Metadata::VerifyReportAnyState(const ReportDisk& report_disk) { | 458 OperationStatus Metadata::VerifyReportAnyState(const ReportDisk& report_disk) { |
460 DWORD fileattr = GetFileAttributes(report_disk.file_path.value().c_str()); | 459 DWORD fileattr = GetFileAttributes(report_disk.file_path.value().c_str()); |
461 if (fileattr == INVALID_FILE_ATTRIBUTES) | 460 if (fileattr == INVALID_FILE_ATTRIBUTES) |
462 return CrashReportDatabase::kReportNotFound; | 461 return CrashReportDatabase::kReportNotFound; |
463 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | 462 return (fileattr & FILE_ATTRIBUTE_DIRECTORY) |
464 return CrashReportDatabase::kFileSystemError; | 463 ? CrashReportDatabase::kFileSystemError |
465 return CrashReportDatabase::kNoError; | 464 : CrashReportDatabase::kNoError; |
466 } | 465 } |
467 | 466 |
468 // static | 467 // static |
469 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, | 468 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, |
470 ReportState desired_state) { | 469 ReportState desired_state) { |
471 if (report_disk.state != desired_state) | 470 return (report_disk.state == desired_state) |
472 return CrashReportDatabase::kBusyError; | 471 ? VerifyReportAnyState(report_disk) |
473 return VerifyReportAnyState(report_disk); | 472 : CrashReportDatabase::kBusyError; |
473 } | |
474 | |
475 //! \brief Ensures that the node at path is a directory, and creates it if it | |
476 //! does not exist. | |
477 //! | |
478 //! \return If the path points to a file, rather than a directory, or the | |
479 //! directory could not be created, returns `false`. Otherwise, returns | |
480 //! `true`, indicating that path already was or now is a directory. | |
481 bool CreateDirectoryIfNecessary(const base::FilePath& path) { | |
482 if (CreateDirectory(path.value().c_str(), nullptr)) | |
483 return true; | |
484 if (GetLastError() != ERROR_ALREADY_EXISTS) { | |
485 PLOG(ERROR) << "CreateDirectory"; | |
486 return false; | |
487 } | |
488 DWORD fileattr = GetFileAttributes(path.value().c_str()); | |
489 if (fileattr == INVALID_FILE_ATTRIBUTES) { | |
490 PLOG(ERROR) << "GetFileAttributes"; | |
491 return false; | |
492 } | |
493 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | |
494 return true; | |
495 LOG(ERROR) << "not a directory"; | |
496 return false; | |
474 } | 497 } |
475 | 498 |
476 class CrashReportDatabaseWin : public CrashReportDatabase { | 499 class CrashReportDatabaseWin : public CrashReportDatabase { |
477 public: | 500 public: |
478 explicit CrashReportDatabaseWin(const base::FilePath& path); | 501 explicit CrashReportDatabaseWin(const base::FilePath& path); |
479 ~CrashReportDatabaseWin() override; | 502 ~CrashReportDatabaseWin() override; |
480 | 503 |
481 bool Initialize(); | 504 bool Initialize(); |
482 | 505 |
483 // CrashReportDatabase: | 506 // CrashReportDatabase: |
(...skipping 22 matching lines...) Expand all Loading... | |
506 }; | 529 }; |
507 | 530 |
508 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) | 531 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) |
509 : CrashReportDatabase(), base_dir_(path) { | 532 : CrashReportDatabase(), base_dir_(path) { |
510 } | 533 } |
511 | 534 |
512 CrashReportDatabaseWin::~CrashReportDatabaseWin() { | 535 CrashReportDatabaseWin::~CrashReportDatabaseWin() { |
513 } | 536 } |
514 | 537 |
515 bool CrashReportDatabaseWin::Initialize() { | 538 bool CrashReportDatabaseWin::Initialize() { |
516 // Check if the database already exists. | 539 // Ensure the database and report subdirectories exist. |
517 if (!CreateOrEnsureDirectoryExists(base_dir_)) | 540 if (!CreateDirectoryIfNecessary(base_dir_) || |
518 return false; | 541 !CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory))) |
519 | |
520 // Create our reports subdirectory. | |
521 if (!CreateOrEnsureDirectoryExists(base_dir_.Append(kReportsDirectory))) | |
522 return false; | 542 return false; |
523 | 543 |
524 // TODO(scottmg): When are completed reports pruned from disk? Delete here or | 544 // TODO(scottmg): When are completed reports pruned from disk? Delete here or |
525 // maybe on AcquireMetadata(). | 545 // maybe on AcquireMetadata(). |
526 | 546 |
527 return true; | 547 return true; |
528 } | 548 } |
529 | 549 |
530 OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( | 550 OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( |
531 NewReport** out_report) { | 551 NewReport** report) { |
532 scoped_ptr<NewReportDisk> report(new NewReportDisk()); | |
533 | |
534 ::UUID system_uuid; | 552 ::UUID system_uuid; |
535 if (UuidCreate(&system_uuid) != RPC_S_OK) { | 553 if (UuidCreate(&system_uuid) != RPC_S_OK) |
536 return kFileSystemError; | 554 return kFileSystemError; |
537 } | |
538 static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); | 555 static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); |
539 static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); | 556 static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); |
540 UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); | 557 UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); |
541 | 558 |
542 report->uuid = uuid; | 559 scoped_ptr<NewReportDisk> new_report(new NewReportDisk()); |
543 report->path = | 560 new_report->uuid = uuid; |
561 new_report->path = | |
544 base_dir_.Append(kReportsDirectory) | 562 base_dir_.Append(kReportsDirectory) |
545 .Append(uuid.ToWideString() + L"." + kCrashReportFileExtension); | 563 .Append(uuid.ToString16() + L"." + kCrashReportFileExtension); |
546 report->handle = LoggingOpenFileForWrite( | 564 new_report->handle = LoggingOpenFileForWrite(new_report->path, |
547 report->path, FileWriteMode::kCreateOrFail, FilePermissions::kOwnerOnly); | 565 FileWriteMode::kCreateOrFail, |
548 if (report->handle == INVALID_HANDLE_VALUE) | 566 FilePermissions::kOwnerOnly); |
567 if (new_report->handle == INVALID_HANDLE_VALUE) | |
549 return kFileSystemError; | 568 return kFileSystemError; |
550 | 569 |
551 *out_report = report.release(); | 570 *report = new_report.release(); |
552 return kNoError; | 571 return kNoError; |
553 } | 572 } |
554 | 573 |
555 OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( | 574 OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( |
556 NewReport* report, | 575 NewReport* report, |
557 UUID* uuid) { | 576 UUID* uuid) { |
558 // Take ownership of the report, and cast to our private version with UUID. | 577 // Take ownership of the report, and cast to our private version with UUID. |
559 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | 578 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); |
560 // Take ownership of the file handle. | 579 // Take ownership of the file handle. |
561 ScopedFileHandle handle(report->handle); | 580 ScopedFileHandle handle(report->handle); |
(...skipping 30 matching lines...) Expand all Loading... | |
592 } | 611 } |
593 | 612 |
594 OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, | 613 OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, |
595 Report* report) { | 614 Report* report) { |
596 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 615 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
597 if (!metadata) | 616 if (!metadata) |
598 return kDatabaseError; | 617 return kDatabaseError; |
599 // Find and return a copy of the matching report. | 618 // Find and return a copy of the matching report. |
600 const ReportDisk* report_disk; | 619 const ReportDisk* report_disk; |
601 OperationStatus os = metadata->FindSingleReport(uuid, &report_disk); | 620 OperationStatus os = metadata->FindSingleReport(uuid, &report_disk); |
602 if (os != kNoError) | 621 if (os == kNoError) |
603 return os; | 622 *report = *report_disk; |
604 *report = *report_disk; | |
605 return kNoError; | 623 return kNoError; |
606 } | 624 } |
607 | 625 |
608 OperationStatus CrashReportDatabaseWin::GetPendingReports( | 626 OperationStatus CrashReportDatabaseWin::GetPendingReports( |
609 std::vector<const Report>* reports) { | 627 std::vector<const Report>* reports) { |
610 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 628 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
611 if (!metadata) | 629 return metadata ? metadata->FindReports(ReportState::kPending, reports) |
612 return kDatabaseError; | 630 : kDatabaseError; |
613 return metadata->FindReports(ReportState::kPending, reports); | |
614 } | 631 } |
615 | 632 |
616 OperationStatus CrashReportDatabaseWin::GetCompletedReports( | 633 OperationStatus CrashReportDatabaseWin::GetCompletedReports( |
617 std::vector<const Report>* reports) { | 634 std::vector<const Report>* reports) { |
618 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 635 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
619 if (!metadata) | 636 return metadata ? metadata->FindReports(ReportState::kCompleted, reports) |
620 return kDatabaseError; | 637 : kDatabaseError; |
621 return metadata->FindReports(ReportState::kCompleted, reports); | |
622 } | 638 } |
623 | 639 |
624 OperationStatus CrashReportDatabaseWin::GetReportForUploading( | 640 OperationStatus CrashReportDatabaseWin::GetReportForUploading( |
625 const UUID& uuid, | 641 const UUID& uuid, |
626 const Report** report) { | 642 const Report** report) { |
627 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 643 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
628 if (!metadata) | 644 if (!metadata) |
629 return kDatabaseError; | 645 return kDatabaseError; |
630 // TODO(scottmg): After returning this report to the client, there is no way | 646 // TODO(scottmg): After returning this report to the client, there is no way |
631 // to reap this report if the uploader fails to call RecordUploadAttempt() or | 647 // to reap this report if the uploader fails to call RecordUploadAttempt() or |
632 // SkipReportUpload() (if it crashed or was otherwise buggy). To resolve this, | 648 // SkipReportUpload() (if it crashed or was otherwise buggy). To resolve this, |
633 // one possibility would be to change the interface to be FileHandle based, so | 649 // one possibility would be to change the interface to be FileHandle based, so |
634 // that instead of giving the file_path back to the client and changing state | 650 // that instead of giving the file_path back to the client and changing state |
635 // to kUploading, we return an exclusive access handle, and use that as the | 651 // to kUploading, we return an exclusive access handle, and use that as the |
636 // signal that the upload is pending, rather than an update to state in the | 652 // signal that the upload is pending, rather than an update to state in the |
637 // metadata. Alternatively, there could be a "garbage collection" at startup | 653 // metadata. Alternatively, there could be a "garbage collection" at startup |
638 // where any reports that are orphaned in the kUploading state are either | 654 // where any reports that are orphaned in the kUploading state are either |
639 // reset to kPending to retry, or discarded. | 655 // reset to kPending to retry, or discarded. |
640 return metadata->MutateSingleReport( | 656 ReportDisk* report_disk; |
641 uuid, ReportState::kPending, [report](ReportDisk* report_disk) { | 657 OperationStatus os = metadata->FindSingleReportAndMarkDirty( |
642 report_disk->state = ReportState::kUploading; | 658 uuid, ReportState::kPending, &report_disk); |
643 // Create a copy for passing back to client. This will be freed in | 659 if (os != CrashReportDatabase::kNoError) |
644 // RecordUploadAttempt. | 660 return os; |
645 *report = new Report(*report_disk); | 661 report_disk->state = ReportState::kUploading; |
646 }); | 662 // Create a copy for passing back to client. This will be freed in |
663 // RecordUploadAttempt. | |
664 *report = new Report(*report_disk); | |
665 return CrashReportDatabase::kNoError; | |
647 } | 666 } |
648 | 667 |
649 OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( | 668 OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( |
650 const Report* report, | 669 const Report* report, |
651 bool successful, | 670 bool successful, |
652 const std::string& id) { | 671 const std::string& id) { |
653 // Take ownership, allocated in GetReportForUploading. | 672 // Take ownership, allocated in GetReportForUploading. |
654 scoped_ptr<const Report> upload_report(report); | 673 scoped_ptr<const Report> upload_report(report); |
655 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 674 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
656 if (!metadata) | 675 if (!metadata) |
657 return kDatabaseError; | 676 return kDatabaseError; |
658 return metadata->MutateSingleReport( | 677 ReportDisk* report_disk; |
659 report->uuid, | 678 OperationStatus os = metadata->FindSingleReportAndMarkDirty( |
660 ReportState::kUploading, | 679 report->uuid, ReportState::kUploading, &report_disk); |
661 [successful, id](ReportDisk* report_disk) { | 680 if (os != CrashReportDatabaseWin::kNoError) |
662 report_disk->uploaded = successful; | 681 return os; |
663 report_disk->id = id; | 682 report_disk->uploaded = successful; |
664 report_disk->last_upload_attempt_time = time(nullptr); | 683 report_disk->id = id; |
665 report_disk->upload_attempts++; | 684 report_disk->last_upload_attempt_time = time(nullptr); |
666 report_disk->state = | 685 report_disk->upload_attempts++; |
667 successful ? ReportState::kCompleted : ReportState::kPending; | 686 report_disk->state = |
668 }); | 687 successful ? ReportState::kCompleted : ReportState::kPending; |
688 return CrashReportDatabase::kNoError; | |
669 } | 689 } |
670 | 690 |
671 OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { | 691 OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { |
672 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 692 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
673 if (!metadata) | 693 if (!metadata) |
674 return kDatabaseError; | 694 return kDatabaseError; |
675 return metadata->MutateSingleReport( | 695 ReportDisk* report_disk; |
676 uuid, ReportState::kPending, [](ReportDisk* report_disk) { | 696 OperationStatus os = metadata->FindSingleReportAndMarkDirty( |
677 report_disk->state = ReportState::kCompleted; | 697 uuid, ReportState::kPending, &report_disk); |
678 }); | 698 if (os != CrashReportDatabase::kNoError) |
699 return os; | |
700 report_disk->state = ReportState::kCompleted; | |
701 return CrashReportDatabase::kNoError; | |
Peter Kasting
2015/02/14 00:24:23
Nit: Shorter, follows the same pattern as earlier:
scottmg
2015/02/14 00:42:45
Done.
| |
679 } | 702 } |
680 | 703 |
681 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { | 704 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { |
682 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); | 705 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); |
683 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); | 706 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); |
684 } | 707 } |
685 | 708 |
686 } // namespace | 709 } // namespace |
687 | 710 |
688 // static | 711 // static |
689 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( | 712 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( |
690 const base::FilePath& path) { | 713 const base::FilePath& path) { |
691 scoped_ptr<CrashReportDatabaseWin> database_win( | 714 scoped_ptr<CrashReportDatabaseWin> database_win( |
692 new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); | 715 new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); |
693 if (!database_win->Initialize()) | 716 return database_win->Initialize() ? database_win.Pass() |
694 database_win.reset(); | 717 : scoped_ptr<CrashReportDatabaseWin>(); |
695 | |
696 return scoped_ptr<CrashReportDatabase>(database_win.release()); | |
697 } | 718 } |
698 | 719 |
699 } // namespace crashpad | 720 } // namespace crashpad |
OLD | NEW |