| 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 <stdio.h> | 7 #include "base/files/file_util.h" |
| 8 #include <sys/stat.h> | |
| 9 | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/logging.h" | 8 #include "base/logging.h" |
| 13 | 9 |
| 14 namespace chromecast { | 10 namespace chromecast { |
| 15 | 11 |
| 16 namespace { | |
| 17 | |
| 18 const int kBufferSize = 32768; | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 22 DummyMinidumpGenerator::DummyMinidumpGenerator( | 12 DummyMinidumpGenerator::DummyMinidumpGenerator( |
| 23 const std::string& existing_minidump_path) | 13 const std::string& existing_minidump_path) |
| 24 : existing_minidump_path_(existing_minidump_path) { | 14 : existing_minidump_path_(existing_minidump_path) { |
| 25 } | 15 } |
| 26 | 16 |
| 27 bool DummyMinidumpGenerator::Generate(const std::string& minidump_path) { | 17 bool DummyMinidumpGenerator::Generate(const std::string& minidump_path) { |
| 28 // Use stdlib calls here to avoid potential IO restrictions on this thread. | |
| 29 | |
| 30 // Return false if the file does not exist. | 18 // Return false if the file does not exist. |
| 31 struct stat st; | 19 if (!base::PathExists(base::FilePath(existing_minidump_path_))) { |
| 32 if (stat(existing_minidump_path_.c_str(), &st) != 0) { | 20 LOG(ERROR) << existing_minidump_path_ << " does not exist."; |
| 33 PLOG(ERROR) << existing_minidump_path_.c_str() << " does not exist: "; | |
| 34 return false; | 21 return false; |
| 35 } | 22 } |
| 36 | 23 |
| 37 LOG(INFO) << "Moving minidump from " << existing_minidump_path_ << " to " | 24 LOG(INFO) << "Moving minidump from " << existing_minidump_path_ << " to " |
| 38 << minidump_path << " for further uploading."; | 25 << minidump_path << " for further uploading."; |
| 39 | 26 return base::Move(base::FilePath(existing_minidump_path_), |
| 40 // Attempt to rename(). If this operation fails, the files are on different | 27 base::FilePath(minidump_path)); |
| 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; | |
| 97 } | 28 } |
| 98 | 29 |
| 99 } // namespace chromecast | 30 } // namespace chromecast |
| OLD | NEW |