2016-05-26 18:22:22 +03:00
|
|
|
/*
|
2022-01-05 01:58:22 +03:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-05-26 18:22:22 +03:00
|
|
|
*
|
2019-06-20 02:58:25 +03:00
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* GNU General Public License version 2.
|
2016-05-26 18:22:22 +03:00
|
|
|
*/
|
2019-10-11 15:26:59 +03:00
|
|
|
|
2022-07-19 22:37:57 +03:00
|
|
|
#include "eden/fs/utils/XAttr.h"
|
2016-05-26 18:22:22 +03:00
|
|
|
#include <folly/Exception.h>
|
2018-01-31 08:40:53 +03:00
|
|
|
#include <folly/File.h>
|
2016-05-26 18:22:22 +03:00
|
|
|
|
2022-07-19 22:37:57 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2022-05-17 20:12:56 +03:00
|
|
|
namespace facebook::eden {
|
2016-05-26 18:22:22 +03:00
|
|
|
|
2018-01-31 08:40:53 +03:00
|
|
|
std::string getxattr(folly::StringPiece path, folly::StringPiece name) {
|
|
|
|
folly::File file(path, O_RDONLY);
|
|
|
|
return fgetxattr(file.fd(), name);
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:22:22 +03:00
|
|
|
std::string fgetxattr(int fd, folly::StringPiece name) {
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
// Reasonable ballpark for most attributes we might want; this saves
|
|
|
|
// us from an extra syscall to query the size in the common case.
|
|
|
|
result.resize(64, 0);
|
|
|
|
|
|
|
|
auto namestr = name.str();
|
|
|
|
|
|
|
|
// We loop until we either hit a hard error or succeed in extracting
|
|
|
|
// the requested information.
|
|
|
|
while (true) {
|
|
|
|
// First, try to read into the buffer at its existing size.
|
|
|
|
auto size = ::fgetxattr(
|
|
|
|
fd,
|
|
|
|
namestr.c_str(),
|
|
|
|
&result[0],
|
2018-01-31 08:40:53 +03:00
|
|
|
result.size()
|
2016-05-26 18:22:22 +03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
,
|
|
|
|
0, // position
|
|
|
|
0 // options
|
|
|
|
#endif
|
2017-11-04 01:58:04 +03:00
|
|
|
);
|
2016-05-26 18:22:22 +03:00
|
|
|
if (size != -1) {
|
|
|
|
result.resize(size);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ERANGE means that the buffer wasn't large enough. Any other
|
|
|
|
// error terminates our attempt to get the attribute.
|
|
|
|
if (errno != ERANGE) {
|
|
|
|
folly::throwSystemError("fgetxattr");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Got the wrong size, query to find out what we should have used
|
|
|
|
size = ::fgetxattr(
|
|
|
|
fd,
|
|
|
|
namestr.c_str(),
|
|
|
|
nullptr,
|
|
|
|
0
|
|
|
|
#ifdef __APPLE__
|
|
|
|
,
|
|
|
|
0, // position
|
|
|
|
0 // options
|
|
|
|
#endif
|
2017-11-04 01:58:04 +03:00
|
|
|
);
|
2016-05-26 18:22:22 +03:00
|
|
|
if (size < 0) {
|
|
|
|
folly::throwSystemError("fgetxattr to query the size failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we have room for a trailing NUL byte.
|
|
|
|
result.resize(size + 1, 0);
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 18:22:23 +03:00
|
|
|
|
|
|
|
void fsetxattr(int fd, folly::StringPiece name, folly::StringPiece value) {
|
|
|
|
auto namestr = name.str();
|
|
|
|
|
|
|
|
folly::checkUnixError(::fsetxattr(
|
|
|
|
fd,
|
|
|
|
namestr.c_str(),
|
|
|
|
value.data(),
|
|
|
|
value.size()
|
|
|
|
#ifdef __APPLE__
|
|
|
|
,
|
|
|
|
0 // position
|
|
|
|
#endif
|
|
|
|
,
|
|
|
|
0 // allow create and replace
|
|
|
|
));
|
|
|
|
}
|
2018-01-31 08:40:53 +03:00
|
|
|
|
|
|
|
std::vector<std::string> listxattr(folly::StringPiece path) {
|
|
|
|
std::string buf;
|
|
|
|
auto pathStr = path.str();
|
|
|
|
|
|
|
|
buf.resize(128, 0);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
auto size = ::listxattr(
|
|
|
|
pathStr.c_str(),
|
|
|
|
&buf[0],
|
|
|
|
buf.size()
|
|
|
|
#ifdef __APPLE__
|
|
|
|
,
|
|
|
|
XATTR_NOFOLLOW
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
if (size != -1) {
|
|
|
|
// Success; parse the result in a list of names separated by NUL
|
|
|
|
// bytes, terminated by a NUL byte.
|
|
|
|
std::vector<std::string> result;
|
|
|
|
// Don't include the final terminator in the size, as that just causes
|
|
|
|
// the split array to contain a final empty name.
|
|
|
|
folly::split('\0', folly::StringPiece(buf.data(), size - 1), result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno != ERANGE) {
|
|
|
|
folly::throwSystemError("listxattr");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query for the size
|
|
|
|
size = ::listxattr(
|
|
|
|
pathStr.c_str(),
|
|
|
|
nullptr,
|
|
|
|
0
|
|
|
|
#ifdef __APPLE__
|
|
|
|
,
|
|
|
|
XATTR_NOFOLLOW
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
if (size == -1) {
|
|
|
|
folly::throwSystemError("listxattr");
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.resize(size, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 20:12:56 +03:00
|
|
|
} // namespace facebook::eden
|
2020-09-23 22:18:56 +03:00
|
|
|
|
|
|
|
#endif
|