OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/mac/xattr.h" | 15 #include "util/mac/xattr.h" |
16 | 16 |
| 17 #include <errno.h> |
17 #include <stdint.h> | 18 #include <stdint.h> |
18 #include <sys/xattr.h> | 19 #include <sys/xattr.h> |
19 | 20 |
20 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
21 #include "base/logging.h" | 22 #include "base/logging.h" |
22 #include "base/numerics/safe_conversions.h" | 23 #include "base/numerics/safe_conversions.h" |
23 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
24 #include "base/strings/string_number_conversions.h" | 25 #include "base/strings/string_number_conversions.h" |
25 | 26 |
26 namespace crashpad { | 27 namespace crashpad { |
27 | 28 |
28 bool ReadXattr(const base::FilePath& file, | 29 XattrStatus ReadXattr(const base::FilePath& file, |
29 const base::StringPiece& name, | 30 const base::StringPiece& name, |
30 std::string* value) { | 31 std::string* value) { |
31 // First get the size of the attribute value. | 32 // First get the size of the attribute value. |
32 ssize_t buffer_size = getxattr(file.value().c_str(), name.data(), nullptr, | 33 ssize_t buffer_size = getxattr(file.value().c_str(), name.data(), nullptr, |
33 0, 0, 0); | 34 0, 0, 0); |
34 if (buffer_size < 0) { | 35 if (buffer_size < 0) { |
| 36 if (errno == ENOATTR) |
| 37 return XattrStatus::kNoAttribute; |
35 PLOG(ERROR) << "getxattr size " << name << " on file " << file.value(); | 38 PLOG(ERROR) << "getxattr size " << name << " on file " << file.value(); |
36 return false; | 39 return XattrStatus::kOtherError; |
37 } | 40 } |
38 | 41 |
39 // Resize the buffer and read into it. | 42 // Resize the buffer and read into it. |
40 value->resize(buffer_size); | 43 value->resize(buffer_size); |
41 ssize_t bytes_read = getxattr(file.value().c_str(), name.data(), | 44 ssize_t bytes_read = getxattr(file.value().c_str(), name.data(), |
42 &(*value)[0], value->size(), | 45 &(*value)[0], value->size(), |
43 0, 0); | 46 0, 0); |
44 if (bytes_read < 0) { | 47 if (bytes_read < 0) { |
45 PLOG(ERROR) << "getxattr " << name << " on file " << file.value(); | 48 PLOG(ERROR) << "getxattr " << name << " on file " << file.value(); |
46 return false; | 49 return XattrStatus::kOtherError; |
47 } | 50 } |
48 DCHECK_EQ(bytes_read, buffer_size); | 51 DCHECK_EQ(bytes_read, buffer_size); |
49 | 52 |
50 return true; | 53 return XattrStatus::kOK; |
51 } | 54 } |
52 | 55 |
53 bool WriteXattr(const base::FilePath& file, | 56 bool WriteXattr(const base::FilePath& file, |
54 const base::StringPiece& name, | 57 const base::StringPiece& name, |
55 const std::string& value) { | 58 const std::string& value) { |
56 int rv = setxattr(file.value().c_str(), name.data(), value.c_str(), | 59 int rv = setxattr(file.value().c_str(), name.data(), value.c_str(), |
57 value.length(), 0, 0); | 60 value.length(), 0, 0); |
58 PLOG_IF(ERROR, rv != 0) << "setxattr " << name << " on file " | 61 PLOG_IF(ERROR, rv != 0) << "setxattr " << name << " on file " |
59 << file.value(); | 62 << file.value(); |
60 return rv == 0; | 63 return rv == 0; |
61 } | 64 } |
62 | 65 |
63 bool ReadXattrBool(const base::FilePath& file, | 66 XattrStatus ReadXattrBool(const base::FilePath& file, |
64 const base::StringPiece& name, | 67 const base::StringPiece& name, |
65 bool* value) { | 68 bool* value) { |
66 std::string tmp; | 69 std::string tmp; |
67 if (!ReadXattr(file, name, &tmp)) | 70 XattrStatus status; |
68 return false; | 71 if ((status = ReadXattr(file, name, &tmp)) != XattrStatus::kOK) |
| 72 return status; |
69 if (tmp == "1") { | 73 if (tmp == "1") { |
70 *value = true; | 74 *value = true; |
71 return true; | 75 return XattrStatus::kOK; |
72 } else if (tmp == "0") { | 76 } else if (tmp == "0") { |
73 *value = false; | 77 *value = false; |
74 return true; | 78 return XattrStatus::kOK; |
75 } else { | 79 } else { |
76 LOG(ERROR) << "ReadXattrBool " << name << " on file " << file.value() | 80 LOG(ERROR) << "ReadXattrBool " << name << " on file " << file.value() |
77 << " could not be interpreted as boolean"; | 81 << " could not be interpreted as boolean"; |
78 return false; | 82 return XattrStatus::kOtherError; |
79 } | 83 } |
80 } | 84 } |
81 | 85 |
82 bool WriteXattrBool(const base::FilePath& file, | 86 bool WriteXattrBool(const base::FilePath& file, |
83 const base::StringPiece& name, | 87 const base::StringPiece& name, |
84 bool value) { | 88 bool value) { |
85 return WriteXattr(file, name, (value ? "1" : "0")); | 89 return WriteXattr(file, name, (value ? "1" : "0")); |
86 } | 90 } |
87 | 91 |
88 bool ReadXattrInt(const base::FilePath& file, | 92 XattrStatus ReadXattrInt(const base::FilePath& file, |
89 const base::StringPiece& name, | 93 const base::StringPiece& name, |
90 int* value) { | 94 int* value) { |
91 std::string tmp; | 95 std::string tmp; |
92 if (!ReadXattr(file, name, &tmp)) | 96 XattrStatus status; |
93 return false; | 97 if ((status = ReadXattr(file, name, &tmp)) != XattrStatus::kOK) |
| 98 return status; |
94 if (!base::StringToInt(tmp, value)) { | 99 if (!base::StringToInt(tmp, value)) { |
95 LOG(ERROR) << "ReadXattrInt " << name << " on file " << file.value() | 100 LOG(ERROR) << "ReadXattrInt " << name << " on file " << file.value() |
96 << " could not be converted to an int"; | 101 << " could not be converted to an int"; |
97 return false; | 102 return XattrStatus::kOtherError; |
98 } | 103 } |
99 return true; | 104 return XattrStatus::kOK; |
100 } | 105 } |
101 | 106 |
102 bool WriteXattrInt(const base::FilePath& file, | 107 bool WriteXattrInt(const base::FilePath& file, |
103 const base::StringPiece& name, | 108 const base::StringPiece& name, |
104 int value) { | 109 int value) { |
105 std::string tmp = base::StringPrintf("%d", value); | 110 std::string tmp = base::StringPrintf("%d", value); |
106 return WriteXattr(file, name, tmp); | 111 return WriteXattr(file, name, tmp); |
107 } | 112 } |
108 | 113 |
109 bool ReadXattrTimeT(const base::FilePath& file, | 114 XattrStatus ReadXattrTimeT(const base::FilePath& file, |
110 const base::StringPiece& name, | 115 const base::StringPiece& name, |
111 time_t* value) { | 116 time_t* value) { |
112 // time_t on OS X is defined as a long, but it will be read into an | 117 // time_t on OS X is defined as a long, but it will be read into an |
113 // int64_t here, since there is no string conversion method for long. | 118 // int64_t here, since there is no string conversion method for long. |
114 std::string tmp; | 119 std::string tmp; |
115 if (!ReadXattr(file, name, &tmp)) | 120 XattrStatus status; |
116 return false; | 121 if ((status = ReadXattr(file, name, &tmp)) != XattrStatus::kOK) |
| 122 return status; |
117 | 123 |
118 int64_t encoded_value; | 124 int64_t encoded_value; |
119 if (!base::StringToInt64(tmp, &encoded_value)) { | 125 if (!base::StringToInt64(tmp, &encoded_value)) { |
120 LOG(ERROR) << "ReadXattrTimeT " << name << " on file " << file.value() | 126 LOG(ERROR) << "ReadXattrTimeT " << name << " on file " << file.value() |
121 << " could not be converted to an int"; | 127 << " could not be converted to an int"; |
122 return false; | 128 return XattrStatus::kOtherError; |
123 } | 129 } |
124 | 130 |
125 *value = base::saturated_cast<time_t>(encoded_value); | 131 *value = base::saturated_cast<time_t>(encoded_value); |
126 if (!base::IsValueInRangeForNumericType<time_t>(encoded_value)) { | 132 if (!base::IsValueInRangeForNumericType<time_t>(encoded_value)) { |
127 LOG(ERROR) << "ReadXattrTimeT " << name << " on file " << file.value() | 133 LOG(ERROR) << "ReadXattrTimeT " << name << " on file " << file.value() |
128 << " read over-sized value and will saturate"; | 134 << " read over-sized value and will saturate"; |
129 return false; | 135 return XattrStatus::kOtherError; |
130 } | 136 } |
131 | 137 |
132 return true; | 138 return XattrStatus::kOK; |
133 } | 139 } |
134 | 140 |
135 bool WriteXattrTimeT(const base::FilePath& file, | 141 bool WriteXattrTimeT(const base::FilePath& file, |
136 const base::StringPiece& name, | 142 const base::StringPiece& name, |
137 time_t value) { | 143 time_t value) { |
138 std::string tmp = base::StringPrintf("%ld", value); | 144 std::string tmp = base::StringPrintf("%ld", value); |
139 return WriteXattr(file, name, tmp); | 145 return WriteXattr(file, name, tmp); |
140 } | 146 } |
141 | 147 |
142 } // namespace crashpad | 148 } // namespace crashpad |
OLD | NEW |