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