src/PlainFilesystemCommand.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 24 Jan 2020 16:53:31 +0100
branchv_0
changeset 58 4679f67a8324
parent 57 c40a241d6e0c
child 59 7471529c0d11
permissions -rw-r--r--
parallel processing: put some common code in FilesystemCommand + use POSIX semaphores for STDOUT synchronization across sub-processes

/**
 * 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, version 3 of the License.
 *
 * 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 <ext/stdio_filebuf.h>

#include "FilesystemCommand.h"

namespace relpipe {
namespace in {
namespace filesystem {

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

class PlainFilesystemCommand : public FilesystemCommand {
public:

	void process(int inputFD, int outputFD, Configuration& configuration) {
		__gnu_cxx::stdio_filebuf<char> inputBuffer(inputFD, std::ios::in);
		__gnu_cxx::stdio_filebuf<char> outputBuffer(outputFD, std::ios::out);
		std::istream input(&inputBuffer);
		std::ostream output(&outputBuffer);

		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
		std::map<string_t, std::shared_ptr < AttributeFinder>> attributeFinders = createAttributeFinders();

		string_t relationName = fetchRelationName(&configuration);
		writeHeader(writer.get(), attributeFinders, relationName, &configuration.fields);
		

		for (std::stringstream originalName; readNext(input, originalName); reset(originalName)) {
			fs::path file(originalName.str().empty() ? "." : originalName.str()); // interpret empty string as current directory (e.g. result of: find -printf '%P\0')
			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
			}

			for (auto& finder : attributeFinders) finder.second->startFile(file, originalName.str(), exists);

			for (RequestedField field : configuration.fields) {
				std::shared_ptr<AttributeFinder> finder = attributeFinders[field.group]; // should not be nullptr, because already checked while writing the relation metadata
				finder->writeField(writer.get(), relationName, field);
			}

			for (auto& finder : attributeFinders) finder.second->endFile();
		}
	}
};

}
}
}