src/XattrAttributeFinder.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 18 Jan 2019 20:23:48 +0100
branchv_0
changeset 13 b0fabeceb18a
parent 9 b4f29fb16159
child 24 4353cd19a6b5
permissions -rw-r--r--
xattr: detault namespace is: user.

/**
 * Relational pipes
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <vector>
#include <filesystem>

#include <relpipe/writer/typedefs.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/writer/RelationalWriter.h>

#include "RequestedField.h"

namespace relpipe {
namespace in {
namespace filesystem {

namespace fs = std::filesystem;
using namespace relpipe::writer;

class XattrAttributeFinder : public AttributeFinder {
private:
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.

	string_t getXattr(const fs::path& file, const string_t& attributeName) {
		// TODO: review, test on multiple platforms
		// TODO: improve documentation in xattr.h in glibc
		// TODO: check XATTR_NAME_MAX in limits.h and if it is reasonably small on our system, allocate buffer for it instead of reading twice
		// TODO: avoid race condition (another process might change the attribute between reading its length and its value)
		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(), buffer.size());
			return convertor.from_bytes(buffer.data());
		} else {
			return L"";
		}
	}
protected:

	virtual void writeFieldOfExistingFile(RelationalWriter* writer, const RequestedField& field) override {
		for (string_t alias : field.getAliases()) {
			// TODO: support also other namespaces through CLI --option namespace someOtherNS
			string_t xattrName = L"user." + field.name;
			if (field.group == RequestedField::GROUP_XATTR) writer->writeAttribute(getXattr(currentFile, xattrName));
		}
	}

public:

	virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) override {
		if (field.group == RequestedField::GROUP_XATTR) {
			vector<AttributeMetadata> metadata;
			for (string_t alias : field.getAliases()) metadata.push_back(AttributeMetadata{alias, TypeId::STRING});
			return metadata;
		} else {
			return {};
		}
	}

	virtual ~XattrAttributeFinder() override {
	}
};

}
}
}