read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
--- a/src/FilesystemCommand.h Sat Jan 12 19:29:49 2019 +0100
+++ b/src/FilesystemCommand.h Sun Jan 13 03:13:07 2019 +0100
@@ -21,7 +21,15 @@
#include <iostream>
#include <string>
#include <vector>
+#include <array>
#include <algorithm>
+#include <filesystem>
+
+#include <pwd.h>
+#include <grp.h>
+#include <sys/stat.h>
+
+#include <sys/xattr.h>
#include <relpipe/writer/typedefs.h>
@@ -29,25 +37,146 @@
namespace in {
namespace filesystem {
+namespace fs = std::filesystem;
+using namespace relpipe::writer;
+
class FilesystemCommand {
+ std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
+
+ void reset(std::stringstream& stream) {
+ stream.str("");
+ stream.clear();
+ }
+
+ bool readNext(std::istream& input, std::stringstream& originalName) {
+ for (char ch; input.get(ch);) {
+ if (ch == 0) return true;
+ else originalName << ch;
+ }
+ return originalName.tellp();
+ }
+
+ string_t getType(const fs::path& file) {
+ // TODO: Use whole words? (letters are compatible with find -type)
+ if (fs::is_regular_file(file)) return L"f";
+ else if (fs::is_symlink(file)) return L"l"; // symlinks to directories are both symlinks and directories
+ else if (fs::is_directory(file)) return L"d";
+ else if (fs::is_fifo(file)) return L"p";
+ else if (fs::is_socket(file)) return L"s";
+ else if (fs::is_block_file(file)) return L"b";
+ else if (fs::is_character_file(file)) return L"c";
+ else return L"o";
+ }
+
+ void fetchOwner(const fs::path& file, string_t& owner, string_t& group) {
+ // TODO: throw exception on error
+ // TODO: get user and group in C++ way?
+ struct stat info;
+ stat(file.c_str(), &info);
+ /**
+ * The return value may point to a static area, and may be
+ * overwritten by subsequent calls to getpwent(3), getpw‐
+ * nam(), or getpwuid(). (Do not pass the returned pointer
+ * to free(3).)
+ */
+ struct passwd* pw = getpwuid(info.st_uid);
+ struct group* gr = getgrgid(info.st_gid);
+ owner = convertor.from_bytes(pw->pw_name);
+ group = convertor.from_bytes(gr->gr_name);
+ }
+
+ string_t getXattr(const fs::path& file, const string_t& attributeName) {
+ // TODO: review, test on multiple platforms
+ ssize_t length = getxattr(file.c_str(), convertor.to_bytes(attributeName).c_str(), nullptr, 0);
+ if (length > 0) {
+ std::vector<char> buffer(length + 1);
+ getxattr(file.c_str(), convertor.to_bytes(attributeName).c_str(), buffer.data(), length);
+ return convertor.from_bytes(buffer.data());
+ } else {
+ return L"";
+ }
+ }
+
public:
void process(std::istream& input, std::ostream& output) {
- using namespace relpipe::writer;
std::shared_ptr<RelationalWriter> writer(Factory::create(output));
- // TODO: remove demo
- // Various data types passed as strings
- writer->startRelation(L"filesystem",{
- {L"s", TypeId::STRING},
- {L"i", TypeId::INTEGER},
- {L"b", TypeId::BOOLEAN}
- }, true);
+ // TODO: redesign: parametrized from CLI + groups of information fetched together and used for multiple attributes
+ bool enableName = true;
+ bool enableExists = false;
+ bool enableNameAbsolute = false;
+ bool enableNameCanonical = false;
+ bool enableType = true;
+ bool enableSize = true;
+ bool enableOwner = true;
+ bool enableGroup = true;
+ bool enableXattrUrl = true; // just a demo
+
+ std::vector<AttributeMetadata> attributesMetadata;
+ if (enableName) attributesMetadata.push_back({L"name", TypeId::STRING});
+ if (enableExists) attributesMetadata.push_back({L"exists", TypeId::BOOLEAN});
+ if (enableNameAbsolute) attributesMetadata.push_back({L"name_absolute", TypeId::STRING});
+ if (enableNameCanonical) attributesMetadata.push_back({L"name_canonical", TypeId::STRING});
+ if (enableType) attributesMetadata.push_back({L"type", TypeId::STRING});
+ if (enableSize) attributesMetadata.push_back({L"size", TypeId::INTEGER});
+ if (enableOwner) attributesMetadata.push_back({L"owner", TypeId::STRING});
+ if (enableGroup) attributesMetadata.push_back({L"group", TypeId::STRING});
+ if (enableXattrUrl) attributesMetadata.push_back({L"xattr_url", TypeId::STRING});
+
+ writer->startRelation(L"filesystem", attributesMetadata, true);
+
+ for (std::stringstream originalName; readNext(input, originalName); reset(originalName)) {
+ if (enableName) writer->writeAttribute(convertor.from_bytes(originalName.str()));
+ fs::path file(originalName.str());
+ bool exists = false;
+
+ try {
+ exists = fs::exists(file);
+ } catch (const fs::filesystem_error& e) {
+ // we probably do not have permissions to given directory → pretend that the file does not exist
+ }
- writer->writeAttribute(L"a");
- writer->writeAttribute(L"1");
- writer->writeAttribute(L"true");
-
+ if (exists) {
+ if (enableExists) writer->writeAttribute(L"true");
+ if (enableNameAbsolute) writer->writeAttribute(fs::absolute(file).wstring());
+ if (enableNameCanonical) writer->writeAttribute(fs::canonical(file).wstring());
+ if (enableType) writer->writeAttribute(getType(file));
+ if (enableSize) {
+ integer_t size = fs::is_regular_file(file) ? fs::file_size(file) : 0;
+ writer->writeAttribute(&size, typeid (size));
+ }
+
+ if (enableOwner || enableGroup) {
+ string_t owner;
+ string_t group;
+ fetchOwner(file, owner, group);
+
+ if (enableOwner) writer->writeAttribute(owner);
+ if (enableGroup) writer->writeAttribute(group);
+ }
+
+ if (enableXattrUrl) writer->writeAttribute(getXattr(file, L"user.xdg.origin.url"));
+
+ } else {
+ // Only original name was written → write remaining attributes
+ // TODO: null values (requires adding null support to the format specification)
+ for (int i = 1; i < attributesMetadata.size(); i++) {
+ switch (attributesMetadata[i].typeId) {
+ case TypeId::BOOLEAN: writer->writeAttribute(L"false");
+ break;
+ case TypeId::INTEGER: writer->writeAttribute(L"0");
+ break;
+ case TypeId::STRING: writer->writeAttribute(L"");
+ break;
+ default:
+ throw RelpipeWriterException(L"Unsupported data type.");
+
+ }
+ }
+ }
+ }
+
}
};