OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromecast/crash/linux/dummy_minidump_generator.h" | 5 #include "chromecast/crash/linux/dummy_minidump_generator.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include <stdio.h> |
| 8 #include <sys/stat.h> |
| 9 |
| 10 #include <vector> |
| 11 |
8 #include "base/logging.h" | 12 #include "base/logging.h" |
9 #include "base/threading/thread_restrictions.h" | |
10 | 13 |
11 namespace chromecast { | 14 namespace chromecast { |
12 | 15 |
| 16 namespace { |
| 17 |
| 18 const int kBufferSize = 32768; |
| 19 |
| 20 } // namespace |
| 21 |
13 DummyMinidumpGenerator::DummyMinidumpGenerator( | 22 DummyMinidumpGenerator::DummyMinidumpGenerator( |
14 const std::string& existing_minidump_path) | 23 const std::string& existing_minidump_path) |
15 : existing_minidump_path_(existing_minidump_path) { | 24 : existing_minidump_path_(existing_minidump_path) { |
16 } | 25 } |
17 | 26 |
18 bool DummyMinidumpGenerator::Generate(const std::string& minidump_path) { | 27 bool DummyMinidumpGenerator::Generate(const std::string& minidump_path) { |
19 base::ThreadRestrictions::AssertIOAllowed(); | 28 // Use stdlib calls here to avoid potential IO restrictions on this thread. |
20 | 29 |
21 // Return false if the file does not exist. | 30 // Return false if the file does not exist. |
22 if (!base::PathExists(base::FilePath(existing_minidump_path_))) { | 31 struct stat st; |
23 LOG(ERROR) << existing_minidump_path_ << " does not exist."; | 32 if (stat(existing_minidump_path_.c_str(), &st) != 0) { |
| 33 PLOG(ERROR) << existing_minidump_path_.c_str() << " does not exist: "; |
24 return false; | 34 return false; |
25 } | 35 } |
26 | 36 |
27 LOG(INFO) << "Moving minidump from " << existing_minidump_path_ << " to " | 37 LOG(INFO) << "Moving minidump from " << existing_minidump_path_ << " to " |
28 << minidump_path << " for further uploading."; | 38 << minidump_path << " for further uploading."; |
29 return base::Move(base::FilePath(existing_minidump_path_), | 39 |
30 base::FilePath(minidump_path)); | 40 // Attempt to rename(). If this operation fails, the files are on different |
| 41 // volumes. Fall back to a copy and delete. |
| 42 if (rename(existing_minidump_path_.c_str(), minidump_path.c_str()) < 0) { |
| 43 // Any errors will be logged within CopyAndDelete(). |
| 44 return CopyAndDelete(minidump_path); |
| 45 } |
| 46 |
| 47 return true; |
| 48 } |
| 49 |
| 50 bool DummyMinidumpGenerator::CopyAndDelete(const std::string& dest_path) { |
| 51 FILE* src = fopen(existing_minidump_path_.c_str(), "r"); |
| 52 if (!src) { |
| 53 PLOG(ERROR) << existing_minidump_path_ << " failed to open: "; |
| 54 return false; |
| 55 } |
| 56 |
| 57 FILE* dest = fopen(dest_path.c_str(), "w"); |
| 58 if (!dest) { |
| 59 PLOG(ERROR) << dest_path << " failed to open: "; |
| 60 return false; |
| 61 } |
| 62 |
| 63 // Copy all bytes from |src| into |dest|. |
| 64 std::vector<char> buffer(kBufferSize); |
| 65 bool success = false; |
| 66 while (!success) { |
| 67 size_t bytes_read = fread(&buffer[0], 1, buffer.size(), src); |
| 68 if (bytes_read < buffer.size()) { |
| 69 if (feof(src)) { |
| 70 success = true; |
| 71 } else { |
| 72 // An error occurred. |
| 73 PLOG(ERROR) << "Error reading " << existing_minidump_path_ << ": "; |
| 74 break; |
| 75 } |
| 76 } |
| 77 |
| 78 size_t bytes_written = fwrite(&buffer[0], 1, bytes_read, dest); |
| 79 if (bytes_written < bytes_read) { |
| 80 // An error occurred. |
| 81 PLOG(ERROR) << "Error writing to " << dest_path << ": "; |
| 82 success = false; |
| 83 break; |
| 84 } |
| 85 } |
| 86 |
| 87 // Close both files. |
| 88 fclose(src); |
| 89 fclose(dest); |
| 90 |
| 91 // Attempt to delete file at |existing_minidump_path_|. We should log this |
| 92 // error, but the function should not fail if the file is not removed. |
| 93 if (remove(existing_minidump_path_.c_str()) < 0) |
| 94 PLOG(ERROR) << "Could not remove " << existing_minidump_path_ << ": "; |
| 95 |
| 96 return success; |
31 } | 97 } |
32 | 98 |
33 } // namespace chromecast | 99 } // namespace chromecast |
OLD | NEW |