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 <sys/xattr.h> | |
| 18 | |
| 19 #include "base/basictypes.h" | |
| 20 #include "base/logging.h" | |
| 21 #include "base/numerics/safe_conversions.h" | |
| 22 #include "base/strings/stringprintf.h" | |
| 23 #include "base/strings/string_number_conversions.h" | |
| 24 | |
| 25 namespace crashpad { | |
| 26 | |
| 27 bool ReadXattr(const base::FilePath& file, | |
| 28 const base::StringPiece& name, | |
| 29 std::string* value) { | |
| 30 value->resize(128); | |
| 31 ssize_t rv = 0; | |
| 32 do { | |
| 33 // getxattr() has a |position| argument, but it is only used for resource | |
| 34 // fork attributes. If reading the attribute filled up to |value|'s | |
| 35 // capacity, resize the buffer and try to read the entire attribute again. | |
| 36 rv = getxattr(file.value().c_str(), name.data(), &(*value)[0], | |
|
Mark Mentovai
2014/12/19 23:16:32
I don’t like the new way clang-format wants us to
Robert Sesek
2014/12/30 17:02:21
What's wrong with this? (I didn't clang-format but
Mark Mentovai
2014/12/30 19:19:15
Three parameters on one line, three on the other,
| |
| 37 value->capacity(), 0, 0); | |
|
Mark Mentovai
2014/12/19 23:16:32
You need to use value->size() and not value->capac
Robert Sesek
2014/12/30 17:02:20
Done.
| |
| 38 if (rv < 0) { | |
| 39 if (errno == ERANGE) { | |
|
Mark Mentovai
2014/12/19 23:16:32
#include <errno.h>
Robert Sesek
2014/12/30 17:02:20
Done.
| |
| 40 value->resize(value->capacity() * 2); | |
|
Mark Mentovai
2014/12/19 23:16:32
Where’s the upper-bound protection?
Robert Sesek
2014/12/30 17:02:21
I initially wrote upper-bound protection but later
| |
| 41 continue; | |
|
Mark Mentovai
2014/12/19 23:16:32
Get rid of this continue and put the PLOG/return t
Robert Sesek
2014/12/30 17:02:20
Done.
| |
| 42 } | |
| 43 | |
| 44 PLOG(ERROR) << "ReadXattr " << name << " on file " << file.value(); | |
|
Mark Mentovai
2014/12/19 23:16:32
PLOGs normally show the name of the syscall, not t
Robert Sesek
2014/12/30 17:02:21
Done.
| |
| 45 return false; | |
| 46 } | |
| 47 | |
| 48 // Resize the buffer to be the length of the read string. | |
| 49 value->resize(rv); | |
| 50 } while (rv < 0 && errno == ERANGE); | |
| 51 | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 bool WriteXattr(const base::FilePath& file, | |
| 56 const base::StringPiece& name, | |
| 57 const std::string& value) { | |
| 58 int rv = setxattr(file.value().c_str(), name.data(), value.c_str(), | |
| 59 value.length(), 0, 0); | |
| 60 PLOG_IF(ERROR, rv != 0) << "WriteXattr " << name << " on file " | |
| 61 << file.value(); | |
| 62 return rv == 0; | |
| 63 } | |
| 64 | |
| 65 bool ReadXattrBool(const base::FilePath& file, | |
| 66 const base::StringPiece& name, | |
| 67 bool* value) { | |
| 68 std::string tmp; | |
| 69 if (!ReadXattr(file, name, &tmp)) | |
| 70 return false; | |
| 71 if (tmp == "1") { | |
| 72 *value = true; | |
| 73 return true; | |
| 74 } else if (tmp == "0") { | |
| 75 *value = false; | |
| 76 return true; | |
| 77 } else { | |
| 78 return false; | |
|
Mark Mentovai
2014/12/19 23:16:31
You need to log a message here to maintain the doc
Robert Sesek
2014/12/30 17:02:21
Done.
| |
| 79 } | |
| 80 } | |
| 81 | |
| 82 bool WriteXattrBool(const base::FilePath& file, | |
| 83 const base::StringPiece& name, | |
| 84 bool value) { | |
| 85 return WriteXattr(file, name, (value ? "1" : "0")); | |
|
Mark Mentovai
2014/12/19 23:16:32
The inner () aren’t necessary.
Robert Sesek
2014/12/30 17:02:20
Indeed, but I think they help readability here.
| |
| 86 } | |
| 87 | |
| 88 bool ReadXattrInt(const base::FilePath& file, | |
| 89 const base::StringPiece& name, | |
| 90 int* value) { | |
| 91 std::string tmp; | |
| 92 if (!ReadXattr(file, name, &tmp)) | |
| 93 return false; | |
| 94 return base::StringToInt(tmp, value); | |
| 95 } | |
| 96 | |
| 97 bool WriteXattrInt(const base::FilePath& file, | |
| 98 const base::StringPiece& name, | |
| 99 int value) { | |
| 100 std::string tmp = base::StringPrintf("%d", value); | |
| 101 return WriteXattr(file, name, tmp); | |
| 102 } | |
| 103 | |
| 104 bool ReadXattrTimeT(const base::FilePath& file, | |
| 105 const base::StringPiece& name, | |
| 106 time_t* value) { | |
| 107 // time_t on OS X is defined as a long, but it will be read into an | |
| 108 // int64_t here, since there is no string conversion method for long. | |
| 109 std::string tmp; | |
| 110 if (!ReadXattr(file, name, &tmp)) | |
| 111 return false; | |
| 112 | |
| 113 int64_t encoded_value; | |
| 114 if (!base::StringToInt64(tmp, &encoded_value)) | |
| 115 return false; | |
| 116 | |
| 117 *value = base::saturated_cast<time_t, int64_t>(encoded_value); | |
|
Mark Mentovai
2014/12/19 23:16:32
I think this should log a warning if it saturates.
Robert Sesek
2014/12/30 17:02:21
Done.
| |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 bool WriteXattrTimeT(const base::FilePath& file, | |
| 122 const base::StringPiece& name, | |
| 123 time_t value) { | |
| 124 std::string tmp = base::StringPrintf("%ld", value); | |
| 125 return WriteXattr(file, name, tmp); | |
| 126 } | |
| 127 | |
| 128 } // namespace crashpad | |
| OLD | NEW |