author | František Kučera <franta-hg@frantovo.cz> |
Wed, 16 Jan 2019 17:23:05 +0100 | |
branch | v_0 |
changeset 4 | d44ed75822e7 |
parent 3 | src/FilesystemCommand.h@62eac7ab4cf4 |
child 5 | ec661baf433a |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
4 | 3 |
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
0 | 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> |
|
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
21 |
#include <filesystem> |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
22 |
|
4 | 23 |
#include <relpipe/writer/typedefs.h> |
24 |
#include <relpipe/writer/AttributeMetadata.h> |
|
25 |
#include <relpipe/writer/RelationalWriter.h> |
|
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
26 |
|
4 | 27 |
#include "RequestedField.h" |
0 | 28 |
|
29 |
namespace relpipe { |
|
30 |
namespace in { |
|
31 |
namespace filesystem { |
|
32 |
||
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
33 |
namespace fs = std::filesystem; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
34 |
using namespace relpipe::writer; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
35 |
|
4 | 36 |
class FileAttributeFinder : public AttributeFinder { |
37 |
private: |
|
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
|
4 | 40 |
fs::path currentFile; |
41 |
string_t currentOwner; |
|
42 |
string_t currentGroup; |
|
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
|
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
string_t getType(const fs::path& file) { |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
// TODO: Use whole words? (letters are compatible with find -type) |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
if (fs::is_regular_file(file)) return L"f"; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
else if (fs::is_symlink(file)) return L"l"; // symlinks to directories are both symlinks and directories |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
else if (fs::is_directory(file)) return L"d"; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
else if (fs::is_fifo(file)) return L"p"; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
else if (fs::is_socket(file)) return L"s"; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
else if (fs::is_block_file(file)) return L"b"; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
else if (fs::is_character_file(file)) return L"c"; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
else return L"o"; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
} |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
|
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
void fetchOwner(const fs::path& file, string_t& owner, string_t& group) { |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
// TODO: throw exception on error |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
// TODO: get user and group in C++ way? |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
struct stat info; |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
stat(file.c_str(), &info); |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
/** |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
* The return value may point to a static area, and may be |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
* overwritten by subsequent calls to getpwent(3), getpw‐ |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
* nam(), or getpwuid(). (Do not pass the returned pointer |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
* to free(3).) |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
*/ |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
struct passwd* pw = getpwuid(info.st_uid); |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
struct group* gr = getgrgid(info.st_gid); |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
owner = convertor.from_bytes(pw->pw_name); |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
group = convertor.from_bytes(gr->gr_name); |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
} |
4 | 72 |
public: |
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
|
4 | 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}}; |
|
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
87 |
} else { |
4 | 88 |
return {}; |
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
} |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
} |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
|
4 | 92 |
void startFile(const fs::path& file) override { |
93 |
currentFile = file; |
|
94 |
}; |
|
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
95 |
|
4 | 96 |
void endFile() override { |
97 |
currentOwner.clear(); |
|
98 |
currentGroup.clear(); |
|
99 |
}; |
|
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
100 |
|
4 | 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); |
|
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
122 |
} else { |
4 | 123 |
// TODO: should not happend; check supported attributes in toMetadata()? |
124 |
writer->writeAttribute(L""); |
|
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
125 |
} |
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
126 |
} |
4 | 127 |
} |
2
f07ed604a0ab
read filenames separated by a null-byte; fetch their size, owner, group… and some xattr (extended attribute); a preview version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
128 |
|
4 | 129 |
virtual ~FileAttributeFinder() override { |
0 | 130 |
} |
131 |
}; |
|
132 |
||
4 | 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 |
||
0 | 142 |
} |
143 |
} |
|
144 |
} |