Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(584)

Side by Side Diff: chromecast/crash/linux/dummy_minidump_generator.cc

Issue 1227963002: Replace system IO calls in chromecast/crash with Chrome utilities. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: nit. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
9 #include "base/threading/thread_restrictions.h"
13 10
14 namespace chromecast { 11 namespace chromecast {
15 12
16 namespace {
17
18 const int kBufferSize = 32768;
19
20 } // namespace
21
22 DummyMinidumpGenerator::DummyMinidumpGenerator( 13 DummyMinidumpGenerator::DummyMinidumpGenerator(
23 const std::string& existing_minidump_path) 14 const std::string& existing_minidump_path)
24 : existing_minidump_path_(existing_minidump_path) { 15 : existing_minidump_path_(existing_minidump_path) {
25 } 16 }
26 17
27 bool DummyMinidumpGenerator::Generate(const std::string& minidump_path) { 18 bool DummyMinidumpGenerator::Generate(const std::string& minidump_path) {
28 // Use stdlib calls here to avoid potential IO restrictions on this thread. 19 base::ThreadRestrictions::AssertIOAllowed();
29 20
30 // Return false if the file does not exist. 21 // Return false if the file does not exist.
31 struct stat st; 22 if (!base::PathExists(base::FilePath(existing_minidump_path_))) {
32 if (stat(existing_minidump_path_.c_str(), &st) != 0) { 23 LOG(ERROR) << existing_minidump_path_ << " does not exist.";
33 PLOG(ERROR) << existing_minidump_path_.c_str() << " does not exist: ";
34 return false; 24 return false;
35 } 25 }
36 26
37 LOG(INFO) << "Moving minidump from " << existing_minidump_path_ << " to " 27 LOG(INFO) << "Moving minidump from " << existing_minidump_path_ << " to "
38 << minidump_path << " for further uploading."; 28 << minidump_path << " for further uploading.";
39 29 return base::Move(base::FilePath(existing_minidump_path_),
40 // Attempt to rename(). If this operation fails, the files are on different 30 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 } 31 }
98 32
99 } // namespace chromecast 33 } // namespace chromecast
OLDNEW
« no previous file with comments | « chromecast/crash/linux/dummy_minidump_generator.h ('k') | chromecast/crash/linux/dummy_minidump_generator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698