src/ScriptAttributeFinder.h
author František Kučera <franta-hg@frantovo.cz>
Mon, 11 Nov 2019 14:42:13 +0100
branchv_0
changeset 28 9172bd97ae99
parent 27 src/HashAttributeFinder.h@532953173cd5
permissions -rw-r--r--
custom scripts for additional attributes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
24
4353cd19a6b5 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
2
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    20
#include <filesystem>
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    21
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    22
#include <relpipe/writer/typedefs.h>
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    23
#include <relpipe/writer/AttributeMetadata.h>
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    24
#include <relpipe/writer/RelationalWriter.h>
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    25
#include <regex>
2
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    27
#include "RequestedField.h"
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    28
#include "SystemProcess.h"
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    29
#include "AttributeFinder.h"
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace relpipe {
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace in {
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace filesystem {
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
2
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    35
namespace fs = std::filesystem;
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    36
using namespace relpipe::writer;
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    37
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    38
class ScriptAttributeFinder : public AttributeFinder {
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    39
private:
2
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    40
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    42
	std::string getScriptCommand(const RequestedField& field) {
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    43
		return SCRIPT_PREFIX + convertor.to_bytes(field.name);
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    44
	}
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    45
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    46
	std::vector<std::string> toEnvironmentalVariables(const std::vector<string_t>& vector) {
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    47
		std::vector<std::string> result;
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    48
		for (int i = 0; i < vector.size();) {
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    49
			string_t name = vector[i++];
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    50
			string_t value = vector[i++];
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    51
			if (name.rfind(L"env:" == 0)) {
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    52
				result.push_back(convertor.to_bytes(name.substr(4)));
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    53
				result.push_back(convertor.to_bytes(value));
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    54
			}
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    55
		}
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    56
		return result;
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    57
	}
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    58
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    59
	TypeId getAttributeType(const RequestedField& field, const string_t& alias) {
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    60
		// TODO: put latest supported version in the environmental variable
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    61
		// TODO: put alias in the environmental variable
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    62
		SystemProcess process({getScriptCommand(field)}, toEnvironmentalVariables(field.options));
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    63
		std::string output = process.execute();
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    64
		std::regex pattern("(.*)\\n(.*)\\n");
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    65
		std::smatch match;
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    66
		std::regex_match(output, match, pattern);
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    67
		if (match.ready() && match[1] == "1") {
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    68
			// TODO: move to a common library
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    69
			if (match[2] == "boolean") return TypeId::BOOLEAN;
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    70
			if (match[2] == "integer") return TypeId::INTEGER;
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    71
			if (match[2] == "string") return TypeId::STRING;
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    72
			throw RelpipeWriterException(L"Unsupported script data type – field: „" + field.name + L"“ type: „" + convertor.from_bytes(match[2]) + L"“");
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    73
		} else {
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    74
			throw RelpipeWriterException(L"Unsupported script version – field: „" + field.name + L"“ output: „" + convertor.from_bytes(output) + L"“");
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    75
		}
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    76
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    77
	}
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    78
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    79
	string_t getScriptOutput(const fs::path& file, const RequestedField& field, const string_t& alias) {
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    80
		try {
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    81
			// TODO: put alias in the environmental variable
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    82
			SystemProcess process({getScriptCommand(field), currentFileRaw}, toEnvironmentalVariables(field.options));
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    83
			return convertor.from_bytes(process.execute());
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    84
		} catch (relpipe::cli::RelpipeCLIException& e) {
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    85
			// TODO: print warnings?
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    86
			// TODO: do not fork/exec if the file is not readable
2
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
			return L"";
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    88
		}
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    89
	}
9
b4f29fb16159 process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    90
protected:
b4f29fb16159 process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    91
b4f29fb16159 process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    92
	virtual void writeFieldOfExistingFile(RelationalWriter* writer, const RequestedField& field) override {
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    93
		// TODO: paralelization?
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    94
		if (field.group == RequestedField::GROUP_SCRIPT) {
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    95
			for (string_t alias : field.getAliases()) {
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    96
				writer->writeAttribute(getScriptOutput(currentFile, field, alias));
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    97
			}
9
b4f29fb16159 process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    98
		}
b4f29fb16159 process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    99
	}
b4f29fb16159 process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   100
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
public:
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   103
	static const std::string SCRIPT_PREFIX;
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   104
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   105
	virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) override {
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   106
		if (field.group == RequestedField::GROUP_SCRIPT) {
5
ec661baf433a support field aliases
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   107
			vector<AttributeMetadata> metadata;
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   108
			for (string_t alias : field.getAliases()) metadata.push_back(AttributeMetadata{alias, getAttributeType(field, alias)});
5
ec661baf433a support field aliases
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   109
			return metadata;
ec661baf433a support field aliases
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   110
		} else {
ec661baf433a support field aliases
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   111
			return {};
ec661baf433a support field aliases
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   112
		}
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   113
	}
2
f07ed604a0ab read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   114
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   115
	virtual ~ScriptAttributeFinder() override {
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
	}
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
};
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   119
const std::string ScriptAttributeFinder::SCRIPT_PREFIX = "__relpipe_in_filesystem_script_";
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   120
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
}
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
}
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   123
}