Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chromecast/base/error_codes.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "chromecast/base/path_utils.h" | |
| 15 | |
| 16 namespace chromecast { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kInitialErrorFile[] = "initial_error"; | |
| 21 | |
| 22 base::FilePath GetInitialErrorFilePath() { | |
| 23 return GetHomePathASCII(kInitialErrorFile); | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 ErrorCode GetInitialErrorCode() { | |
| 29 std::string initial_error_code_str; | |
| 30 if (!base::ReadFileToString(GetInitialErrorFilePath(), | |
| 31 &initial_error_code_str)) { | |
| 32 return NO_ERROR; | |
| 33 } | |
| 34 | |
| 35 int initial_error_code = 0; | |
| 36 if (base::StringToInt(initial_error_code_str, &initial_error_code) && | |
| 37 initial_error_code >= NO_ERROR && initial_error_code <= ERROR_UNKNOWN) { | |
| 38 VLOG(1) << "Initial error from " << GetInitialErrorFilePath().value() | |
| 39 << ": " << initial_error_code; | |
| 40 return static_cast<ErrorCode>(initial_error_code); | |
| 41 } | |
| 42 | |
| 43 LOG(ERROR) << "Unknown initial error code: " << initial_error_code_str; | |
| 44 return NO_ERROR; | |
| 45 } | |
| 46 | |
| 47 bool SetInitialErrorCode(ErrorCode initial_error_code) { | |
| 48 // Note: Do not use Chromium IO methods in this function. When cast_shell | |
| 49 // crashes, this function can be called by any thread. | |
| 50 const std::string error_file_path = GetInitialErrorFilePath().value(); | |
|
gunsch
2015/06/12 00:37:09
const std::string&
slan
2015/06/15 17:01:45
Done.
| |
| 51 | |
| 52 if (initial_error_code > NO_ERROR && initial_error_code <= ERROR_UNKNOWN) { | |
| 53 const std::string initial_error_code_str( | |
| 54 base::IntToString(initial_error_code)); | |
| 55 int fd = creat(error_file_path.c_str(), 0640); | |
| 56 if (fd < 0) { | |
| 57 LOG(ERROR) << "Could not open error code file"; | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 int written = | |
| 62 write(fd, initial_error_code_str.data(), initial_error_code_str.size()); | |
| 63 close(fd); | |
| 64 | |
| 65 if (written != static_cast<int>(initial_error_code_str.size())) { | |
| 66 LOG(ERROR) << "Could not write error code to file"; | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 // Remove initial error file if no error. | |
| 74 if (unlink(error_file_path.c_str()) == 0 || errno == ENOENT) | |
| 75 return true; | |
| 76 | |
| 77 LOG(ERROR) << "Failed to remove error file"; | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 } // namespace chromecast | |
| OLD | NEW |