Chromium Code Reviews| Index: util/mac/xattr.cc |
| diff --git a/util/mac/xattr.cc b/util/mac/xattr.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..338b65d65f2c81e554095a0a9895e3379c4cc473 |
| --- /dev/null |
| +++ b/util/mac/xattr.cc |
| @@ -0,0 +1,135 @@ |
| +// Copyright 2014 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#include "util/mac/xattr.h" |
| + |
| +#include <errno.h> |
|
Mark Mentovai
2014/12/30 22:36:59
Lose this now.
Robert Sesek
2014/12/30 22:40:25
Done.
|
| +#include <stdint.h> |
| +#include <sys/xattr.h> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/logging.h" |
| +#include "base/numerics/safe_conversions.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/strings/string_number_conversions.h" |
| + |
| +namespace crashpad { |
| + |
| +bool ReadXattr(const base::FilePath& file, |
| + const base::StringPiece& name, |
| + std::string* value) { |
| + // First get the size of the attribute value. |
| + ssize_t buffer_size = getxattr(file.value().c_str(), name.data(), nullptr, |
| + 0, 0, 0); |
| + if (buffer_size < 0) { |
| + PLOG(ERROR) << "getxattr size " << name << " on file " << file.value(); |
| + return false; |
| + } |
| + |
| + // Resize the buffer and read into it. |
| + value->resize(buffer_size); |
| + ssize_t bytes_read = getxattr(file.value().c_str(), name.data(), |
| + &(*value)[0], value->size(), |
| + 0, 0); |
| + if (bytes_read < 0) { |
| + PLOG(ERROR) << "getxattr " << name << " on file " << file.value(); |
| + return false; |
| + } |
| + DCHECK_EQ(bytes_read, buffer_size); |
| + |
| + return true; |
| +} |
| + |
| +bool WriteXattr(const base::FilePath& file, |
| + const base::StringPiece& name, |
| + const std::string& value) { |
| + int rv = setxattr(file.value().c_str(), name.data(), value.c_str(), |
| + value.length(), 0, 0); |
| + PLOG_IF(ERROR, rv != 0) << "setxattr " << name << " on file " |
| + << file.value(); |
| + return rv == 0; |
| +} |
| + |
| +bool ReadXattrBool(const base::FilePath& file, |
| + const base::StringPiece& name, |
| + bool* value) { |
| + std::string tmp; |
| + if (!ReadXattr(file, name, &tmp)) |
| + return false; |
| + if (tmp == "1") { |
| + *value = true; |
| + return true; |
| + } else if (tmp == "0") { |
| + *value = false; |
| + return true; |
| + } else { |
| + LOG(ERROR) << "ReadXattrBool " << name << " on file " << file.value() |
| + << " could not be interpreted as boolean"; |
| + return false; |
| + } |
| +} |
| + |
| +bool WriteXattrBool(const base::FilePath& file, |
| + const base::StringPiece& name, |
| + bool value) { |
| + return WriteXattr(file, name, (value ? "1" : "0")); |
| +} |
| + |
| +bool ReadXattrInt(const base::FilePath& file, |
| + const base::StringPiece& name, |
| + int* value) { |
| + std::string tmp; |
| + if (!ReadXattr(file, name, &tmp)) |
| + return false; |
| + 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.
|
| +} |
| + |
| +bool WriteXattrInt(const base::FilePath& file, |
| + const base::StringPiece& name, |
| + int value) { |
| + std::string tmp = base::StringPrintf("%d", value); |
| + return WriteXattr(file, name, tmp); |
| +} |
| + |
| +bool ReadXattrTimeT(const base::FilePath& file, |
| + const base::StringPiece& name, |
| + time_t* value) { |
| + // time_t on OS X is defined as a long, but it will be read into an |
| + // int64_t here, since there is no string conversion method for long. |
| + std::string tmp; |
| + if (!ReadXattr(file, name, &tmp)) |
| + return false; |
| + |
| + int64_t encoded_value; |
| + if (!base::StringToInt64(tmp, &encoded_value)) |
| + return false; |
|
Mark Mentovai
2014/12/30 22:36:59
Same here.
Robert Sesek
2014/12/30 22:40:25
Done.
|
| + |
| + *value = base::saturated_cast<time_t>(encoded_value); |
| + if (!base::IsValueInRangeForNumericType<time_t>(encoded_value)) { |
| + LOG(ERROR) << "ReadXattrTimeT " << name << " on file " << file.value() |
| + << " read over-sized value and will saturate"; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool WriteXattrTimeT(const base::FilePath& file, |
| + const base::StringPiece& name, |
| + time_t value) { |
| + std::string tmp = base::StringPrintf("%ld", value); |
| + return WriteXattr(file, name, tmp); |
| +} |
| + |
| +} // namespace crashpad |