src/XattrAttributeFinder.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 16 Jan 2019 17:48:06 +0100
branchv_0
changeset 5 ec661baf433a
parent 4 d44ed75822e7
child 8 eb1ecb37c6b7
permissions -rw-r--r--
support field aliases - aliases can be used for renames and also for duplication - some specific fields might even fill multiple attributes with different values - example of rename and duplication: c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_TYPE,{L"type_a", L"type_b"})); // CLIParser.h

/**
 * 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.

	fs::path currentFile;

	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"";
		}
	}
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 {};
		}
	}

	void startFile(const fs::path& file) override {
		currentFile = file;
	};

	void endFile() override {
	};

	virtual void writeField(RelationalWriter* writer, const RequestedField& field) override {
		for (string_t alias : field.getAliases()) {
			if (field.group == RequestedField::GROUP_XATTR) writer->writeAttribute(getXattr(currentFile, field.name));
		}
	}

	virtual ~XattrAttributeFinder() override {
	}
};

}
}
}