Chromium Code Reviews| 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> | |
|
Mark Mentovai
2014/12/30 22:36:59
Lose this now.
Robert Sesek
2014/12/30 22:40:25
Done.
| |
| 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 bool 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 PLOG(ERROR) << "getxattr size " << name << " on file " << file.value(); | |
| 37 return false; | |
| 38 } | |
| 39 | |
| 40 // Resize the buffer and read into it. | |
| 41 value->resize(buffer_size); | |
| 42 ssize_t bytes_read = getxattr(file.value().c_str(), name.data(), | |
| 43 &(*value)[0], value->size(), | |
| 44 0, 0); | |
| 45 if (bytes_read < 0) { | |
| 46 PLOG(ERROR) << "getxattr " << name << " on file " << file.value(); | |
| 47 return false; | |
| 48 } | |
| 49 DCHECK_EQ(bytes_read, buffer_size); | |
| 50 | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 bool WriteXattr(const base::FilePath& file, | |
| 55 const base::StringPiece& name, | |
| 56 const std::string& value) { | |
| 57 int rv = setxattr(file.value().c_str(), name.data(), value.c_str(), | |
| 58 value.length(), 0, 0); | |
| 59 PLOG_IF(ERROR, rv != 0) << "setxattr " << name << " on file " | |
| 60 << file.value(); | |
| 61 return rv == 0; | |
| 62 } | |
| 63 | |
| 64 bool ReadXattrBool(const base::FilePath& file, | |
| 65 const base::StringPiece& name, | |
| 66 bool* value) { | |
| 67 std::string tmp; | |
| 68 if (!ReadXattr(file, name, &tmp)) | |
| 69 return false; | |
| 70 if (tmp == "1") { | |
| 71 *value = true; | |
| 72 return true; | |
| 73 } else if (tmp == "0") { | |
| 74 *value = false; | |
| 75 return true; | |
| 76 } else { | |
| 77 LOG(ERROR) << "ReadXattrBool " << name << " on file " << file.value() | |
| 78 << " could not be interpreted as boolean"; | |
| 79 return false; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 bool WriteXattrBool(const base::FilePath& file, | |
| 84 const base::StringPiece& name, | |
| 85 bool value) { | |
| 86 return WriteXattr(file, name, (value ? "1" : "0")); | |
| 87 } | |
| 88 | |
| 89 bool ReadXattrInt(const base::FilePath& file, | |
| 90 const base::StringPiece& name, | |
| 91 int* value) { | |
| 92 std::string tmp; | |
| 93 if (!ReadXattr(file, name, &tmp)) | |
| 94 return false; | |
| 95 return base::StringToInt(tmp, value); | |
|
Mark Mentovai
2014/12/30 22:36:59
If this is false, you should LOG something to main
Robert Sesek
2014/12/30 22:40:25
Done.
| |
| 96 } | |
| 97 | |
| 98 bool WriteXattrInt(const base::FilePath& file, | |
| 99 const base::StringPiece& name, | |
| 100 int value) { | |
| 101 std::string tmp = base::StringPrintf("%d", value); | |
| 102 return WriteXattr(file, name, tmp); | |
| 103 } | |
| 104 | |
| 105 bool ReadXattrTimeT(const base::FilePath& file, | |
| 106 const base::StringPiece& name, | |
| 107 time_t* value) { | |
| 108 // time_t on OS X is defined as a long, but it will be read into an | |
| 109 // int64_t here, since there is no string conversion method for long. | |
| 110 std::string tmp; | |
| 111 if (!ReadXattr(file, name, &tmp)) | |
| 112 return false; | |
| 113 | |
| 114 int64_t encoded_value; | |
| 115 if (!base::StringToInt64(tmp, &encoded_value)) | |
| 116 return false; | |
|
Mark Mentovai
2014/12/30 22:36:59
Same here.
Robert Sesek
2014/12/30 22:40:25
Done.
| |
| 117 | |
| 118 *value = base::saturated_cast<time_t>(encoded_value); | |
| 119 if (!base::IsValueInRangeForNumericType<time_t>(encoded_value)) { | |
| 120 LOG(ERROR) << "ReadXattrTimeT " << name << " on file " << file.value() | |
| 121 << " read over-sized value and will saturate"; | |
| 122 return false; | |
| 123 } | |
| 124 | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 128 bool WriteXattrTimeT(const base::FilePath& file, | |
| 129 const base::StringPiece& name, | |
| 130 time_t value) { | |
| 131 std::string tmp = base::StringPrintf("%ld", value); | |
| 132 return WriteXattr(file, name, tmp); | |
| 133 } | |
| 134 | |
| 135 } // namespace crashpad | |
| OLD | NEW |