src/FilesystemWorker.h
branchv_0
changeset 61 640ba8948d69
parent 59 7471529c0d11
equal deleted inserted replaced
60:bb7ca5891755 61:640ba8948d69
       
     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 <cstdlib>
       
    20 #include <iostream>
       
    21 #include <sstream>
       
    22 #include <string>
       
    23 #include <vector>
       
    24 #include <map>
       
    25 #include <memory>
       
    26 #include <algorithm>
       
    27 #include <filesystem>
       
    28 
       
    29 #include <pwd.h>
       
    30 #include <grp.h>
       
    31 #include <sys/stat.h>
       
    32 
       
    33 #include <sys/xattr.h>
       
    34 
       
    35 #include <relpipe/writer/typedefs.h>
       
    36 
       
    37 #include "Configuration.h"
       
    38 #include "AttributeFinder.h"
       
    39 #include "FileAttributeFinder.h"
       
    40 #include "XattrAttributeFinder.h"
       
    41 #include "StreamletAttributeFinder.h"
       
    42 
       
    43 namespace relpipe {
       
    44 namespace in {
       
    45 namespace filesystem {
       
    46 
       
    47 namespace fs = std::filesystem;
       
    48 using namespace relpipe::writer;
       
    49 
       
    50 class 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:
       
   110 
       
   111 	virtual ~FilesystemWorker() = default;
       
   112 };
       
   113 
       
   114 }
       
   115 }
       
   116 }