src/StreamletAttributeFinder.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 19 Jan 2020 15:44:30 +0100
branchv_0
changeset 47 beefddde951e
parent 46 b5ae61996281
child 48 26a8b1a14889
permissions -rw-r--r--
streamlets: find only executable files
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>
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    21
#include <regex>
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    22
#include <memory>
46
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    23
#include <sstream>
47
beefddde951e streamlets: find only executable files
František Kučera <franta-hg@frantovo.cz>
parents: 46
diff changeset
    24
#include <unistd.h>
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
    25
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    26
#include <relpipe/writer/typedefs.h>
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    27
#include <relpipe/writer/AttributeMetadata.h>
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    28
#include <relpipe/writer/RelationalWriter.h>
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
    29
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    30
#include "RequestedField.h"
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    31
#include "SubProcess.h"
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    32
#include "AttributeFinder.h"
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    33
#include "StreamletMsg.h"
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace relpipe {
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace in {
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace filesystem {
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
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
    39
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
    40
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
    41
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    42
class StreamletAttributeFinder : public AttributeFinder {
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    43
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
    44
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    45
	std::map<int, std::shared_ptr<SubProcess>> subProcesses;
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    46
	std::map<int, std::vector<AttributeMetadata>> cachedMetadata;
28
9172bd97ae99 custom scripts for additional attributes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    47
46
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    48
	string_t getStreamletCommand(const RequestedField& field) {
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    49
		const char* streamletPathChars = getenv("RELPIPE_IN_FILESYSTEM_STREAMLET_PATH");
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    50
		if (streamletPathChars) {
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    51
			std::wstringstream current;
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    52
			string_t streamletPath = convertor.from_bytes(streamletPathChars);
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    53
			for (int i = 0, streamletPathSize = streamletPath.size(); i < streamletPathSize; i++) {
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    54
				if (streamletPath[i] == ':' || i == streamletPathSize - 1) { // FIXME: support \: and \\ escaping
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    55
					current << L"/" << field.name;
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    56
					fs::path streamletFile(current.str());
47
beefddde951e streamlets: find only executable files
František Kučera <franta-hg@frantovo.cz>
parents: 46
diff changeset
    57
					if (fs::exists(streamletFile) && ::access(streamletFile.c_str(), X_OK) == 0) { // n.b. must be set executable using e.g. chmod – files executable through only ACL, are actually not executable
46
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    58
						return current.str();
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    59
					} else {
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    60
						current.str(L"");
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    61
						current.clear();
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    62
					}
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    63
				} else {
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    64
					current.put(streamletPath[i]);
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    65
				}
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    66
			}
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    67
			throw RelpipeWriterException(L"Streamlet „" + field.name + L"“ was not found at $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH");
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    68
		} else {
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    69
			throw RelpipeWriterException(L"Missing environment variable RELPIPE_IN_FILESYSTEM_STREAMLET_PATH → unable to find streamlet.");
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    70
		}
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
    71
	}
45
f466b4c7d9b1 streamlets: use $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH variable instead of __relpipe_in_filesystem_script_ prefix
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    72
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
    73
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
    74
35
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    75
	void startFile(const fs::path& file, const string& fileRaw, bool exists) override {
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    76
		AttributeFinder::startFile(file, fileRaw, exists);
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    77
		if (exists) {
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    78
			for (auto subProcess : subProcesses) {
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    79
				subProcess.second->write({StreamletMsg::INPUT_ATTRIBUTE, L"0", convertor.from_bytes(currentFileRaw), L"false"}); // index, value, isNull
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    80
				subProcess.second->write({StreamletMsg::WAITING_FOR_OUTPUT_ATTRIBUTES});
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    81
			}
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    82
		}
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    83
	}
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    84
926eb93c302f streamlets: enable parallel processing
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    85
	virtual void writeFieldOfExistingFile(RelationalWriter* writer, const string_t& relationName, const RequestedField & field) override {
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    86
		// TODO: paralelize also over records → fork multiple processes and distribute records across them; then collect results (with a lock)
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    87
		if (field.group == RequestedField::GROUP_STREAMLET) {
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    88
			for (auto metadata : cachedMetadata[field.id]) {
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    89
				SubProcess::Message m = subProcesses[field.id]->read();
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    90
				if (m.code == StreamletMsg::OUTPUT_ATTRIBUTE) writer->writeAttribute(m.parameters[0]);
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    91
				else throw RelpipeWriterException(L"Protocol violation from exec sub-process while reading: „" + metadata.attributeName + L"“. Expected OUTPUT_ATTRIBUTE but got: " + m.toString());
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    92
			}
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    93
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    94
			SubProcess::Message m = subProcesses[field.id]->read();
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    95
			if (m.code != StreamletMsg::WAITING_FOR_INPUT_ATTRIBUTES) throw RelpipeWriterException(L"Protocol violation from exec sub-process. Expected WAITING_FOR_INPUT_ATTRIBUTES but got: " + m.toString());
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    96
			// TODO: generic protocol violation error messages / method for checking responses
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
    97
		}
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
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
public:
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   102
	static const string_t SCRIPT_PREFIX;
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   103
32
bccda5688d71 propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   104
	virtual vector<AttributeMetadata> toMetadata(RelationalWriter* writer, const string_t& relationName, const RequestedField& field) override {
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   105
		if (field.group == RequestedField::GROUP_STREAMLET) {
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   106
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   107
			if (cachedMetadata.count(field.id)) {
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   108
				return cachedMetadata[field.id];
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   109
			} else {
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   110
46
b5ae61996281 streamlets: fix command name collisions, do not prepend $RELPIPE_IN_FILESYSTEM_STREAMLET_PATH to the $PATH
František Kučera <franta-hg@frantovo.cz>
parents: 45
diff changeset
   111
				std::vector<string_t> commandLine = {getStreamletCommand(field)};
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   112
				std::map<string_t, string_t> environment;
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   113
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   114
				for (auto mn : StreamletMsg::getMessageNames()) {
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   115
					environment[L"EXEC_MSG_" + mn.second] = std::to_wstring(mn.first);
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   116
					environment[L"EXEC_MSG_" + std::to_wstring(mn.first)] = mn.second;
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   117
				}
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   118
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   119
				shared_ptr<SubProcess> subProcess(SubProcess::create(commandLine, environment));
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   120
				subProcesses[field.id] = subProcess;
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   121
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   122
				string_t version = L"1";
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   123
				subProcess->write({StreamletMsg::VERSION_SUPPORTED, version});
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   124
				subProcess->write({StreamletMsg::WAITING_FOR_VERSION});
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   125
				SubProcess::Message versionMessage = subProcess->read();
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   126
				if (versionMessage.code == StreamletMsg::VERSION_ACCEPTED && versionMessage.parameters[0] == version) {
32
bccda5688d71 propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   127
					subProcess->write({StreamletMsg::RELATION_START, relationName});
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   128
					subProcess->write({StreamletMsg::INPUT_ATTRIBUTE_METADATA, L"path", L"string"});
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   129
					for (string_t alias : field.getAliases()) subProcess->write({StreamletMsg::OUTPUT_ATTRIBUTE_ALIAS, alias});
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   130
					for (int i = 0; i < field.options.size();) subProcess->write({StreamletMsg::OPTION, field.options[i++], field.options[i++]});
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   131
					subProcess->write({StreamletMsg::WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA});
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   132
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   133
					vector<AttributeMetadata> metadata;
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   134
					while (true) {
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   135
						SubProcess::Message m = subProcess->read();
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   136
						if (m.code == StreamletMsg::OUTPUT_ATTRIBUTE_METADATA) metadata.push_back({m.parameters[0], writer->toTypeId(m.parameters[1])});
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   137
						else if (m.code == StreamletMsg::WAITING_FOR_INPUT_ATTRIBUTES) break;
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   138
					}
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   139
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   140
					cachedMetadata[field.id] = metadata;
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   141
					return metadata;
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   142
				} else {
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   143
					throw RelpipeWriterException(L"Incompatible exec sub-process version or message: " + versionMessage.toString());
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   144
				}
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   145
			}
5
ec661baf433a support field aliases
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   146
		} else {
ec661baf433a support field aliases
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   147
			return {};
ec661baf433a support field aliases
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   148
		}
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   149
	}
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
   150
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   151
	virtual ~StreamletAttributeFinder() override {
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   152
		for (auto s : subProcesses) {
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   153
			try {
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   154
				s.second->write({StreamletMsg::RELATION_END});
29
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   155
				s.second->wait();
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   156
			} catch (...) {
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   157
				std::wcerr << L"Exception caught during closing sub-process #" + std::to_wstring(s.first) + L" and waiting for its end." << std::endl;
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   158
			}
6f15f18d2abf field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   159
		}
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
	}
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
};
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
31
c64e1588f428 rename --exec to --streamlet
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   163
const relpipe::writer::string_t StreamletAttributeFinder::SCRIPT_PREFIX = L"__relpipe_in_filesystem_script_";
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   164
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
}
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
}
27
532953173cd5 file hash: md5, sha1, sha256, sha512
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   167
}