src/FilesystemCommand.h
branchv_0
changeset 61 640ba8948d69
parent 59 7471529c0d11
equal deleted inserted replaced
60:bb7ca5891755 61:640ba8948d69
    37 #include "Configuration.h"
    37 #include "Configuration.h"
    38 #include "AttributeFinder.h"
    38 #include "AttributeFinder.h"
    39 #include "FileAttributeFinder.h"
    39 #include "FileAttributeFinder.h"
    40 #include "XattrAttributeFinder.h"
    40 #include "XattrAttributeFinder.h"
    41 #include "StreamletAttributeFinder.h"
    41 #include "StreamletAttributeFinder.h"
       
    42 #include "FilesystemWorker.h"
    42 
    43 
    43 namespace relpipe {
    44 namespace relpipe {
    44 namespace in {
    45 namespace in {
    45 namespace filesystem {
    46 namespace filesystem {
    46 
    47 
    47 namespace fs = std::filesystem;
    48 namespace fs = std::filesystem;
    48 using namespace relpipe::writer;
    49 using namespace relpipe::writer;
    49 
    50 
    50 class FilesystemCommand {
    51 class FilesystemCommand : public FilesystemWorker {
    51 protected:
       
    52 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.	
       
    53 
       
    54 	std::map<string_t, std::shared_ptr<AttributeFinder>> createAttributeFinders() {
       
    55 		return {
       
    56 			{RequestedField::GROUP_FILE, std::make_shared<FileAttributeFinder>()},
       
    57 			{RequestedField::GROUP_STREAMLET, std::make_shared<StreamletAttributeFinder>()},
       
    58 			{RequestedField::GROUP_XATTR, std::make_shared<XattrAttributeFinder>()}};
       
    59 	}
       
    60 
       
    61 	void reset(std::stringstream& stream) {
       
    62 		stream.str("");
       
    63 		stream.clear();
       
    64 	}
       
    65 
       
    66 	bool readNext(std::istream& input, std::stringstream& originalName) {
       
    67 		for (char ch; input.get(ch);) {
       
    68 			if (ch == 0) return true;
       
    69 			else originalName << ch;
       
    70 		}
       
    71 		return originalName.tellp();
       
    72 	}
       
    73 
       
    74 	string_t fetchRelationName(Configuration* configuration) {
       
    75 		return configuration->relation.empty() ? L"filesystem" : configuration->relation;
       
    76 	}
       
    77 
       
    78 	void writeHeader(RelationalWriter* writer, std::map<string_t, std::shared_ptr < AttributeFinder>> attributeFinders, string_t relationName, std::vector<RequestedField>* fields, bool writeHeader = true) {
       
    79 		std::vector<AttributeMetadata> attributesMetadata;
       
    80 		for (RequestedField field : *fields) {
       
    81 			std::shared_ptr<AttributeFinder> finder = attributeFinders[field.group];
       
    82 			if (finder) for (AttributeMetadata m : finder->toMetadata(writer, relationName, field)) attributesMetadata.push_back(m);
       
    83 			else throw RelpipeWriterException(L"Unsupported field group: " + field.group);
       
    84 		}
       
    85 
       
    86 		writer->startRelation(relationName, attributesMetadata, writeHeader);
       
    87 	}
       
    88 
       
    89 	void processSingleFile(std::shared_ptr<RelationalWriter> writer, std::stringstream& originalName, std::map<string_t, std::shared_ptr < AttributeFinder>>&attributeFinders, Configuration& configuration, string_t relationName) {
       
    90 		fs::path file(originalName.str().empty() ? "." : originalName.str()); // interpret empty string as current directory (e.g. result of: find -printf '%P\0')
       
    91 		bool exists = false;
       
    92 
       
    93 		try {
       
    94 			exists = fs::exists(file);
       
    95 		} catch (const fs::filesystem_error& e) {
       
    96 			// we probably do not have permissions to given directory → pretend that the file does not exist
       
    97 		}
       
    98 
       
    99 		for (auto& finder : attributeFinders) finder.second->startFile(file, originalName.str(), exists);
       
   100 
       
   101 		for (RequestedField field : configuration.fields) {
       
   102 			std::shared_ptr<AttributeFinder> finder = attributeFinders[field.group]; // should not be nullptr, because already checked while writing the relation metadata
       
   103 			finder->writeField(writer.get(), relationName, field);
       
   104 		}
       
   105 
       
   106 		for (auto& finder : attributeFinders) finder.second->endFile();
       
   107 	}
       
   108 
       
   109 public:
    52 public:
   110 
    53 
   111 	virtual ~FilesystemCommand() = default;
    54 	virtual ~FilesystemCommand() = default;
   112 
    55 
   113 	virtual void process(int inputFD, int outputFD, Configuration& configuration) = 0;
    56 	virtual void process(int inputFD, int outputFD, Configuration& configuration) = 0;