src/HashAttributeFinder.h
branchv_0
changeset 27 532953173cd5
parent 24 4353cd19a6b5
equal deleted inserted replaced
26:1b14ef641c7b 27:532953173cd5
       
     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 <vector>
       
    20 #include <filesystem>
       
    21 
       
    22 #include <relpipe/writer/typedefs.h>
       
    23 #include <relpipe/writer/AttributeMetadata.h>
       
    24 #include <relpipe/writer/RelationalWriter.h>
       
    25 #include <regex>
       
    26 
       
    27 #include "RequestedField.h"
       
    28 #include "SystemProcess.h"
       
    29 #include "AttributeFinder.h"
       
    30 
       
    31 namespace relpipe {
       
    32 namespace in {
       
    33 namespace filesystem {
       
    34 
       
    35 namespace fs = std::filesystem;
       
    36 using namespace relpipe::writer;
       
    37 
       
    38 class HashAttributeFinder : public AttributeFinder {
       
    39 private:
       
    40 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
       
    41 
       
    42 	std::wregex standardHashPattern = std::wregex(L"^([a-f0-9]+) .*");
       
    43 
       
    44 	string_t getStandardHash(const fs::path& file, const std::string& hashCommand) {
       
    45 		try {
       
    46 			SystemProcess process({hashCommand, currentFileRaw});
       
    47 			string_t output = convertor.from_bytes(process.execute());
       
    48 
       
    49 			std::wsmatch match;
       
    50 			if (regex_search(output, match, standardHashPattern)) return match[1];
       
    51 			else throw RelpipeWriterException(L"Hash command returned wrong output: " + output);
       
    52 		} catch (relpipe::cli::RelpipeCLIException& e) {
       
    53 			// TODO: print warnings?
       
    54 			// TODO: do not fork/exec if the file is not readable
       
    55 			return L"";
       
    56 		}
       
    57 	}
       
    58 protected:
       
    59 
       
    60 	virtual void writeFieldOfExistingFile(RelationalWriter* writer, const RequestedField& field) override {
       
    61 		// TODO: paralelization?
       
    62 		// TODO: other formats, not only hex, but also base64 or binary
       
    63 		if (field.group == RequestedField::GROUP_HASH) {
       
    64 			for (string_t alias : field.getAliases()) {
       
    65 				if (field.name == FIELD_MD5) writer->writeAttribute(getStandardHash(currentFile, "md5sum"));
       
    66 				else if (field.name == FIELD_SHA1) writer->writeAttribute(getStandardHash(currentFile, "sha1sum"));
       
    67 				else if (field.name == FIELD_SHA256) writer->writeAttribute(getStandardHash(currentFile, "sha256sum"));
       
    68 				else if (field.name == FIELD_SHA512) writer->writeAttribute(getStandardHash(currentFile, "sha512sum"));
       
    69 				else throw RelpipeWriterException(L"Unsupported field name in HashAttributeFinder: " + field.name);
       
    70 			}
       
    71 		}
       
    72 	}
       
    73 
       
    74 public:
       
    75 
       
    76 	static const string_t FIELD_MD5;
       
    77 	static const string_t FIELD_SHA1;
       
    78 	static const string_t FIELD_SHA256;
       
    79 	static const string_t FIELD_SHA512;
       
    80 
       
    81 	virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) override {
       
    82 		if (field.group == RequestedField::GROUP_HASH) {
       
    83 			vector<AttributeMetadata> metadata;
       
    84 			for (string_t alias : field.getAliases()) metadata.push_back(AttributeMetadata{alias, TypeId::STRING});
       
    85 			return metadata;
       
    86 		} else {
       
    87 			return {};
       
    88 		}
       
    89 	}
       
    90 
       
    91 	virtual ~HashAttributeFinder() override {
       
    92 	}
       
    93 };
       
    94 
       
    95 const string_t HashAttributeFinder::FIELD_MD5 = L"md5";
       
    96 const string_t HashAttributeFinder::FIELD_SHA1 = L"sha1";
       
    97 const string_t HashAttributeFinder::FIELD_SHA256 = L"sha256";
       
    98 const string_t HashAttributeFinder::FIELD_SHA512 = L"sha512";
       
    99 
       
   100 }
       
   101 }
       
   102 }