Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(802)

Side by Side Diff: util/mac/xattr.cc

Issue 791493009: Add wrappers around getxattr() and setxattr(). (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: For landing Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/mac/xattr.h ('k') | util/mac/xattr_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <stdint.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 // First get the size of the attribute value.
32 ssize_t buffer_size = getxattr(file.value().c_str(), name.data(), nullptr,
33 0, 0, 0);
34 if (buffer_size < 0) {
35 PLOG(ERROR) << "getxattr size " << name << " on file " << file.value();
36 return false;
37 }
38
39 // Resize the buffer and read into it.
40 value->resize(buffer_size);
41 ssize_t bytes_read = getxattr(file.value().c_str(), name.data(),
42 &(*value)[0], value->size(),
43 0, 0);
44 if (bytes_read < 0) {
45 PLOG(ERROR) << "getxattr " << name << " on file " << file.value();
46 return false;
47 }
48 DCHECK_EQ(bytes_read, buffer_size);
49
50 return true;
51 }
52
53 bool WriteXattr(const base::FilePath& file,
54 const base::StringPiece& name,
55 const std::string& value) {
56 int rv = setxattr(file.value().c_str(), name.data(), value.c_str(),
57 value.length(), 0, 0);
58 PLOG_IF(ERROR, rv != 0) << "setxattr " << name << " on file "
59 << file.value();
60 return rv == 0;
61 }
62
63 bool ReadXattrBool(const base::FilePath& file,
64 const base::StringPiece& name,
65 bool* value) {
66 std::string tmp;
67 if (!ReadXattr(file, name, &tmp))
68 return false;
69 if (tmp == "1") {
70 *value = true;
71 return true;
72 } else if (tmp == "0") {
73 *value = false;
74 return true;
75 } else {
76 LOG(ERROR) << "ReadXattrBool " << name << " on file " << file.value()
77 << " could not be interpreted as boolean";
78 return false;
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"));
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 if (!base::StringToInt(tmp, value)) {
95 LOG(ERROR) << "ReadXattrInt " << name << " on file " << file.value()
96 << " could not be converted to an int";
97 return false;
98 }
99 return true;
100 }
101
102 bool WriteXattrInt(const base::FilePath& file,
103 const base::StringPiece& name,
104 int value) {
105 std::string tmp = base::StringPrintf("%d", value);
106 return WriteXattr(file, name, tmp);
107 }
108
109 bool ReadXattrTimeT(const base::FilePath& file,
110 const base::StringPiece& name,
111 time_t* value) {
112 // time_t on OS X is defined as a long, but it will be read into an
113 // int64_t here, since there is no string conversion method for long.
114 std::string tmp;
115 if (!ReadXattr(file, name, &tmp))
116 return false;
117
118 int64_t encoded_value;
119 if (!base::StringToInt64(tmp, &encoded_value)) {
120 LOG(ERROR) << "ReadXattrTimeT " << name << " on file " << file.value()
121 << " could not be converted to an int";
122 return false;
123 }
124
125 *value = base::saturated_cast<time_t>(encoded_value);
126 if (!base::IsValueInRangeForNumericType<time_t>(encoded_value)) {
127 LOG(ERROR) << "ReadXattrTimeT " << name << " on file " << file.value()
128 << " read over-sized value and will saturate";
129 return false;
130 }
131
132 return true;
133 }
134
135 bool WriteXattrTimeT(const base::FilePath& file,
136 const base::StringPiece& name,
137 time_t value) {
138 std::string tmp = base::StringPrintf("%ld", value);
139 return WriteXattr(file, name, tmp);
140 }
141
142 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/mac/xattr.h ('k') | util/mac/xattr_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698