src/FilesystemCommand.h
branchv_0
changeset 53 170a993745be
parent 52 fea625f0a096
child 54 ef726975c34b
equal deleted inserted replaced
52:fea625f0a096 53:170a993745be
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #pragma once
       
    18 
       
    19 #include "FilesystemCommandBase.h"
       
    20 
       
    21 namespace relpipe {
       
    22 namespace in {
       
    23 namespace filesystem {
       
    24 
       
    25 namespace fs = std::filesystem;
       
    26 using namespace relpipe::writer;
       
    27 
       
    28 class FilesystemCommand : public FilesystemCommandBase {
       
    29 private:
       
    30 	std::map<string_t, std::shared_ptr<AttributeFinder>> attributeFinders = createAttributeFinders();
       
    31 
       
    32 public:
       
    33 
       
    34 	void process(std::istream& input, std::ostream& output, Configuration& configuration) {
       
    35 		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
       
    36 
       
    37 		string_t relationName = configuration.relation.empty() ? L"filesystem" : configuration.relation;
       
    38 
       
    39 		std::vector<AttributeMetadata> attributesMetadata;
       
    40 		for (RequestedField field : configuration.fields) {
       
    41 			std::shared_ptr<AttributeFinder> finder = attributeFinders[field.group];
       
    42 			if (finder) for (AttributeMetadata m : finder->toMetadata(writer.get(), relationName, field)) attributesMetadata.push_back(m);
       
    43 			else throw RelpipeWriterException(L"Unsupported field group: " + field.group);
       
    44 		}
       
    45 
       
    46 		writer->startRelation(relationName, attributesMetadata, true);
       
    47 
       
    48 
       
    49 		for (std::stringstream originalName; readNext(input, originalName); reset(originalName)) {
       
    50 			fs::path file(originalName.str().empty() ? "." : originalName.str()); // interpret empty string as current directory (e.g. result of: find -printf '%P\0')
       
    51 			bool exists = false;
       
    52 
       
    53 			try {
       
    54 				exists = fs::exists(file);
       
    55 			} catch (const fs::filesystem_error& e) {
       
    56 				// we probably do not have permissions to given directory → pretend that the file does not exist
       
    57 			}
       
    58 
       
    59 			for (auto& finder : attributeFinders) finder.second->startFile(file, originalName.str(), exists);
       
    60 
       
    61 			for (RequestedField field : configuration.fields) {
       
    62 				std::shared_ptr<AttributeFinder> finder = attributeFinders[field.group]; // should not be nullptr, because already checked while writing the relation metadata
       
    63 				finder->writeField(writer.get(), relationName, field);
       
    64 			}
       
    65 
       
    66 			for (auto& finder : attributeFinders) finder.second->endFile();
       
    67 		}
       
    68 	}
       
    69 };
       
    70 
       
    71 }
       
    72 }
       
    73 }