Apache Mesos
xattr.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_POSIX_XATTR_HPP__
14 #define __STOUT_OS_POSIX_XATTR_HPP__
15 
16 #ifdef __FreeBSD__
17 #include <sys/types.h>
18 #include <sys/extattr.h>
19 #else
20 #include <sys/xattr.h>
21 #endif
22 
23 #include <string>
24 
25 #include <stout/error.hpp>
26 #include <stout/nothing.hpp>
27 #include <stout/try.hpp>
28 
29 namespace os {
30 
32  const std::string& path,
33  const std::string& name,
34  const std::string& value,
35  int flags)
36 {
37 #ifdef __APPLE__
38  if (::setxattr(
39  path.c_str(),
40  name.c_str(),
41  value.c_str(),
42  value.length(),
43  0,
44  flags) < 0) {
45 #elif __FreeBSD__
46  if (::extattr_set_file(
47  path.c_str(),
48  EXTATTR_NAMESPACE_USER,
49  name.c_str(),
50  value.c_str(),
51  value.length()) < 0) {
52 #else
53  if (::setxattr(
54  path.c_str(),
55  name.c_str(),
56  value.c_str(),
57  value.length(),
58  flags) < 0) {
59 #endif
60  return ErrnoError();
61  }
62 
63  return Nothing();
64 }
65 
66 
68  const std::string& path,
69  const std::string& name)
70 {
71  // Get the current size of the attribute.
72 #ifdef __APPLE__
73  ssize_t size = ::getxattr(path.c_str(), name.c_str(), nullptr, 0, 0, 0);
74 #elif __FreeBSD__
75  ssize_t size = ::extattr_get_file(path.c_str(),
76  EXTATTR_NAMESPACE_USER,
77  name.c_str(),
78  nullptr,
79  0);
80 #else
81  ssize_t size = ::getxattr(path.c_str(), name.c_str(), nullptr, 0);
82 #endif
83  if (size < 0) {
84  return ErrnoError();
85  }
86 
87  char* temp = new char[size + 1];
88  ::memset(temp, 0, (size_t)size + 1);
89 
90 #ifdef __APPLE__
91  if (::getxattr(path.c_str(), name.c_str(), temp, (size_t)size, 0, 0) < 0) {
92 #elif __FreeBSD__
93  if (::extattr_get_file(
94  path.c_str(),
95  EXTATTR_NAMESPACE_USER,
96  name.c_str(),
97  temp,
98  (size_t)size) < 0) {
99 #else
100  if (::getxattr(path.c_str(), name.c_str(), temp, (size_t)size) < 0) {
101 #endif
102  delete[] temp;
103  return ErrnoError();
104  }
105 
106  std::string result(temp);
107  delete[] temp;
108 
109  return result;
110 }
111 
112 
114  const std::string& path,
115  const std::string& name)
116 {
117 #ifdef __APPLE__
118  if (::removexattr(path.c_str(), name.c_str(), 0) < 0) {
119 #elif __FreeBSD__
120  if (::extattr_delete_file(path.c_str(),
121  EXTATTR_NAMESPACE_USER,
122  name.c_str())) {
123 #else
124  if (::removexattr(path.c_str(), name.c_str()) < 0) {
125 #endif
126  return ErrnoError();
127  }
128 
129  return Nothing();
130 }
131 
132 } // namespace os {
133 
134 #endif /* __STOUT_OS_POSIX_XATTR_HPP__ */
SSIZE_T ssize_t
Definition: windows.hpp:186
Definition: path.hpp:29
Definition: nothing.hpp:16
Try< Nothing > removexattr(const std::string &path, const std::string &name)
Definition: xattr.hpp:113
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
Try< Nothing > setxattr(const std::string &path, const std::string &name, const std::string &value, int flags)
Definition: xattr.hpp:31
Definition: check.hpp:33
Definition: errorbase.hpp:50
Definition: posix_signalhandler.hpp:23
#define flags
Definition: decoder.hpp:18
std::string temp()
Definition: temp.hpp:27
Try< std::string > getxattr(const std::string &path, const std::string &name)
Definition: xattr.hpp:67
Definition: parse.hpp:33
constexpr const char * name
Definition: shell.hpp:41