emory/tools/stat.cpp

97 lines
2.4 KiB
C++
Raw Normal View History

2019-04-25 14:19:55 +10:00
#include <sys/types.h>
#include <sys/acl.h>
#include "../acl.hpp"
#include <cruft/util/bitwise.hpp>
#include <cruft/util/posix/except.hpp>
#include <cruft/util/posix/util.hpp>
#include <cruft/util/posix/ostream.hpp>
#include <cruft/util/string.hpp>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <unistd.h>
#include <cstdlib>
2019-04-25 09:22:43 +10:00
//-----------------------------------------------------------------------------
void do_xattr (char const *path)
{
std::cout << ", xattr: ";
2019-04-25 09:22:43 +10:00
{
std::cout << "{ ";
std::string attr_list (1024, '\0');
{
2019-04-25 09:22:43 +10:00
auto res = listxattr (path, attr_list.data (), attr_list.size ());
if (res < 0)
cruft::posix::error::throw_code ();
attr_list.resize (res);
}
2019-04-25 09:22:43 +10:00
std::string keybuffer (64, '\0');
std::string val (128, '\0');
cruft::view<char const*> attr_view { attr_list.data (), attr_list.size () };
for (auto const &key: cruft::tokeniser (attr_view, '\0')) {
keybuffer.resize (0);
std::copy (std::begin (key), std::end (key), std::back_inserter (keybuffer));
while (true) {
auto const res = getxattr (path, keybuffer.c_str (), val.data (), val.size ());
if (res < 0 && errno == E2BIG) {
val = "___E2BIG";
break;
}
if (res < 0 && errno == ERANGE) {
2019-04-25 09:22:43 +10:00
val.resize (val.size () * 2);
continue;
}
if (res < 0)
cruft::posix::error::throw_code ();
2019-04-25 09:22:43 +10:00
val.resize (res);
break;
}
2019-04-25 09:22:43 +10:00
std::cout << keybuffer << ": " << val << ", ";
}
2019-04-25 09:22:43 +10:00
std::cout << "}";
}
}
2019-04-25 14:19:55 +10:00
//-----------------------------------------------------------------------------
void do_acl (char const *path)
{
std::cout << cruft::acl (path, ACL_TYPE_ACCESS) << '\n';
}
2019-04-25 09:22:43 +10:00
///////////////////////////////////////////////////////////////////////////////
int main (int argc, char **argv)
{
if (argc < 2) {
std::cerr << argv[0] << " <target> ...\n";
return EXIT_FAILURE;
}
for (int i = 1; i < argc; ++i) {
std::cout << "{ stat: ";
char const *path = argv[i];
std::cout << cruft::posix::stat (path);
2019-04-25 09:22:43 +10:00
do_xattr (path);
2019-04-25 14:19:55 +10:00
do_acl (path);
2019-04-25 09:22:43 +10:00
std::cout << '\n';
}
}