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 <sys/xattr.h> | |
19 | |
20 #include "base/basictypes.h" | |
21 #include "base/logging.h" | |
22 #include "base/numerics/safe_conversions.h" | |
23 #include "base/strings/stringprintf.h" | |
24 #include "base/strings/string_number_conversions.h" | |
25 | |
26 namespace crashpad { | |
27 | |
28 bool ReadXattr(const base::FilePath& file, | |
29 const base::StringPiece& name, | |
30 std::string* value) { | |
31 value->resize(128); | |
32 ssize_t rv = 0; | |
33 do { | |
Mark Mentovai
2014/12/30 19:19:15
Hmm…
The man page says
Robert Sesek
2014/12/30 21:59:04
Oh, I missed that. Yes, that's better.
| |
34 // getxattr() has a |position| argument, but it is only used for resource | |
35 // fork attributes. If reading the attribute filled up to |value|'s | |
36 // capacity, resize the buffer and try to read the entire attribute again. | |
37 rv = getxattr(file.value().c_str(), name.data(), &(*value)[0], | |
38 value->size(), 0, 0); | |
39 if (rv < 0) { | |
40 if (errno == ERANGE) { | |
41 value->resize(value->size() * 2); | |
Mark Mentovai
2014/12/30 19:19:15
…or if you keep this, just make sure there’s no wa
| |
42 } else { | |
43 PLOG(ERROR) << "getxattr " << name << " on file " << file.value(); | |
44 return false; | |
45 } | |
46 } else { | |
47 // Resize the buffer to be the length of the read string. | |
48 value->resize(rv); | |
49 } | |
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) << "setxattr " << 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 LOG(ERROR) << "ReadXattrBool " << name << " on file " << file.value() | |
79 << " could not be interpreted as boolean"; | |
80 return false; | |
81 } | |
82 } | |
83 | |
84 bool WriteXattrBool(const base::FilePath& file, | |
85 const base::StringPiece& name, | |
86 bool value) { | |
87 return WriteXattr(file, name, (value ? "1" : "0")); | |
88 } | |
89 | |
90 bool ReadXattrInt(const base::FilePath& file, | |
91 const base::StringPiece& name, | |
92 int* value) { | |
93 std::string tmp; | |
94 if (!ReadXattr(file, name, &tmp)) | |
95 return false; | |
96 return base::StringToInt(tmp, value); | |
97 } | |
98 | |
99 bool WriteXattrInt(const base::FilePath& file, | |
100 const base::StringPiece& name, | |
101 int value) { | |
102 std::string tmp = base::StringPrintf("%d", value); | |
103 return WriteXattr(file, name, tmp); | |
104 } | |
105 | |
106 bool ReadXattrTimeT(const base::FilePath& file, | |
107 const base::StringPiece& name, | |
108 time_t* value) { | |
109 // time_t on OS X is defined as a long, but it will be read into an | |
110 // int64_t here, since there is no string conversion method for long. | |
111 std::string tmp; | |
112 if (!ReadXattr(file, name, &tmp)) | |
113 return false; | |
114 | |
115 int64_t encoded_value; | |
Mark Mentovai
2014/12/30 19:19:15
#include <stdint.h>.
Robert Sesek
2014/12/30 21:59:04
Done.
| |
116 if (!base::StringToInt64(tmp, &encoded_value)) | |
117 return false; | |
118 | |
119 *value = base::saturated_cast<time_t, int64_t>(encoded_value); | |
120 if (!base::IsValueInRangeForNumericType<time_t, int64_t>(encoded_value)) { | |
Mark Mentovai
2014/12/30 19:19:15
You can leave out the int64_t template parameters
Robert Sesek
2014/12/30 21:59:04
Done.
| |
121 LOG(ERROR) << "ReadXattrTimeT " << name << " on file " << file.value() | |
122 << " read over-sized value and will saturate"; | |
123 return false; | |
124 } | |
125 | |
126 return true; | |
127 } | |
128 | |
129 bool WriteXattrTimeT(const base::FilePath& file, | |
130 const base::StringPiece& name, | |
131 time_t value) { | |
132 std::string tmp = base::StringPrintf("%ld", value); | |
133 return WriteXattr(file, name, tmp); | |
134 } | |
135 | |
136 } // namespace crashpad | |
OLD | NEW |