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.
5 #include "chromecast/base/error_codes.h"
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"
16 namespace chromecast
{
20 const char kInitialErrorFile
[] = "initial_error";
22 base::FilePath
GetInitialErrorFilePath() {
23 return GetHomePathASCII(kInitialErrorFile
);
28 ErrorCode
GetInitialErrorCode() {
29 std::string initial_error_code_str
;
30 if (!base::ReadFileToString(GetInitialErrorFilePath(),
31 &initial_error_code_str
)) {
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
);
43 LOG(ERROR
) << "Unknown initial error code: " << initial_error_code_str
;
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();
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);
57 PLOG(ERROR
) << "Could not open error code file";
62 write(fd
, initial_error_code_str
.data(), initial_error_code_str
.size());
64 if (written
!= static_cast<int>(initial_error_code_str
.size())) {
65 PLOG(ERROR
) << "Could not write error code to file: written=" << written
66 << ", expected=" << initial_error_code_str
.size();
75 // Remove initial error file if no error.
76 if (unlink(error_file_path
.c_str()) == 0 || errno
== ENOENT
)
79 PLOG(ERROR
) << "Failed to remove error file";
83 } // namespace chromecast