src/FileAttributeFinder.h
branchv_0
changeset 4 d44ed75822e7
parent 3 62eac7ab4cf4
child 5 ec661baf433a
equal deleted inserted replaced
3:62eac7ab4cf4 4:d44ed75822e7
       
     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, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #pragma once
       
    19 
       
    20 #include <vector>
       
    21 #include <filesystem>
       
    22 
       
    23 #include <relpipe/writer/typedefs.h>
       
    24 #include <relpipe/writer/AttributeMetadata.h>
       
    25 #include <relpipe/writer/RelationalWriter.h>
       
    26 
       
    27 #include "RequestedField.h"
       
    28 
       
    29 namespace relpipe {
       
    30 namespace in {
       
    31 namespace filesystem {
       
    32 
       
    33 namespace fs = std::filesystem;
       
    34 using namespace relpipe::writer;
       
    35 
       
    36 class FileAttributeFinder : public AttributeFinder {
       
    37 private:
       
    38 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
       
    39 
       
    40 	fs::path currentFile;
       
    41 	string_t currentOwner;
       
    42 	string_t currentGroup;
       
    43 
       
    44 	string_t getType(const fs::path& file) {
       
    45 		// TODO: Use whole words? (letters are compatible with find -type)
       
    46 		if (fs::is_regular_file(file)) return L"f";
       
    47 		else if (fs::is_symlink(file)) return L"l"; // symlinks to directories are both symlinks and directories
       
    48 		else if (fs::is_directory(file)) return L"d";
       
    49 		else if (fs::is_fifo(file)) return L"p";
       
    50 		else if (fs::is_socket(file)) return L"s";
       
    51 		else if (fs::is_block_file(file)) return L"b";
       
    52 		else if (fs::is_character_file(file)) return L"c";
       
    53 		else return L"o";
       
    54 	}
       
    55 
       
    56 	void fetchOwner(const fs::path& file, string_t& owner, string_t& group) {
       
    57 		// TODO: throw exception on error
       
    58 		// TODO: get user and group in C++ way?
       
    59 		struct stat info;
       
    60 		stat(file.c_str(), &info);
       
    61 		/**
       
    62 		 * The return value may point to a static area, and may  be
       
    63 		 * overwritten  by  subsequent calls to getpwent(3), getpw‐
       
    64 		 * nam(), or getpwuid().  (Do not pass the returned pointer
       
    65 		 * to free(3).)
       
    66 		 */
       
    67 		struct passwd* pw = getpwuid(info.st_uid);
       
    68 		struct group* gr = getgrgid(info.st_gid);
       
    69 		owner = convertor.from_bytes(pw->pw_name);
       
    70 		group = convertor.from_bytes(gr->gr_name);
       
    71 	}
       
    72 public:
       
    73 
       
    74 	static const string_t FIELD_PATH_ORIGINAL;
       
    75 	static const string_t FIELD_NAME;
       
    76 	static const string_t FIELD_SIZE;
       
    77 	static const string_t FIELD_PATH_ABSOLUTE;
       
    78 	static const string_t FIELD_PATH_CANONICAL;
       
    79 	static const string_t FIELD_TYPE;
       
    80 	static const string_t FIELD_OWNER;
       
    81 	static const string_t FIELD_GROUP;
       
    82 
       
    83 	virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) override {
       
    84 		if (field.group == RequestedField::GROUP_FILE) {
       
    85 			if (field.name == FIELD_SIZE) return { AttributeMetadata{field.name, TypeId::INTEGER}};
       
    86 			else return { AttributeMetadata{field.name, TypeId::STRING}};
       
    87 		} else {
       
    88 			return {};
       
    89 		}
       
    90 	}
       
    91 
       
    92 	void startFile(const fs::path& file) override {
       
    93 		currentFile = file;
       
    94 	};
       
    95 
       
    96 	void endFile() override {
       
    97 		currentOwner.clear();
       
    98 		currentGroup.clear();
       
    99 	};
       
   100 
       
   101 	virtual void writeField(RelationalWriter* writer, const RequestedField& field) override {
       
   102 		if (field.group == RequestedField::GROUP_FILE) {
       
   103 			if (field.name == FIELD_NAME) {
       
   104 				writer->writeAttribute(currentFile.filename().wstring());
       
   105 			} else if (field.name == FIELD_PATH_ORIGINAL) {
       
   106 				writer->writeAttribute(currentFile.wstring());
       
   107 			} else if (field.name == FIELD_PATH_ABSOLUTE) {
       
   108 				writer->writeAttribute(fs::absolute(currentFile).wstring());
       
   109 			} else if (field.name == FIELD_PATH_CANONICAL) {
       
   110 				writer->writeAttribute(fs::canonical(currentFile).wstring());
       
   111 			} else if (field.name == FIELD_TYPE) {
       
   112 				writer->writeAttribute(getType(currentFile));
       
   113 			} else if (field.name == FIELD_SIZE) {
       
   114 				integer_t size = fs::is_regular_file(currentFile) ? fs::file_size(currentFile) : 0;
       
   115 				writer->writeAttribute(&size, typeid (size));
       
   116 			} else if (field.name == FIELD_OWNER) {
       
   117 				if (currentOwner.empty()) fetchOwner(currentFile, currentOwner, currentGroup);
       
   118 				writer->writeAttribute(currentOwner);
       
   119 			} else if (field.name == FIELD_GROUP) {
       
   120 				if (currentOwner.empty()) fetchOwner(currentFile, currentOwner, currentGroup);
       
   121 				writer->writeAttribute(currentGroup);
       
   122 			} else {
       
   123 				// TODO: should not happend; check supported attributes in toMetadata()?
       
   124 				writer->writeAttribute(L"");
       
   125 			}
       
   126 		}
       
   127 	}
       
   128 
       
   129 	virtual ~FileAttributeFinder() override {
       
   130 	}
       
   131 };
       
   132 
       
   133 const string_t FileAttributeFinder::FIELD_PATH_ORIGINAL = L"path";
       
   134 const string_t FileAttributeFinder::FIELD_NAME = L"name";
       
   135 const string_t FileAttributeFinder::FIELD_SIZE = L"size";
       
   136 const string_t FileAttributeFinder::FIELD_PATH_ABSOLUTE = L"path_absolute";
       
   137 const string_t FileAttributeFinder::FIELD_PATH_CANONICAL = L"path_canonical";
       
   138 const string_t FileAttributeFinder::FIELD_TYPE = L"type";
       
   139 const string_t FileAttributeFinder::FIELD_OWNER = L"owner";
       
   140 const string_t FileAttributeFinder::FIELD_GROUP = L"group";
       
   141 
       
   142 }
       
   143 }
       
   144 }