author | František Kučera <franta-hg@frantovo.cz> |
Mon, 20 Jan 2020 13:19:23 +0100 | |
branch | v_0 |
changeset 54 | ef726975c34b |
parent 53 | 170a993745be |
child 57 | c40a241d6e0c |
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 |
|
24
4353cd19a6b5
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
0 | 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 |
||
54
ef726975c34b
parallel processing: rename FilesystemCommandBase to FilesystemCommand
František Kučera <franta-hg@frantovo.cz>
parents:
53
diff
changeset
|
19 |
#include "FilesystemCommand.h" |
4 | 20 |
|
0 | 21 |
namespace relpipe { |
22 |
namespace in { |
|
23 |
namespace filesystem { |
|
24 |
||
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
|
25 |
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
|
26 |
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
|
27 |
|
54
ef726975c34b
parallel processing: rename FilesystemCommandBase to FilesystemCommand
František Kučera <franta-hg@frantovo.cz>
parents:
53
diff
changeset
|
28 |
class PlainFilesystemCommand : public FilesystemCommand { |
4 | 29 |
private: |
52
fea625f0a096
parallel processing: prepare infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
30 |
std::map<string_t, std::shared_ptr<AttributeFinder>> attributeFinders = createAttributeFinders(); |
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
|
31 |
|
0 | 32 |
public: |
33 |
||
4 | 34 |
void process(std::istream& input, std::ostream& output, Configuration& configuration) { |
0 | 35 |
std::shared_ptr<RelationalWriter> writer(Factory::create(output)); |
36 |
||
32
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
37 |
string_t relationName = configuration.relation.empty() ? L"filesystem" : configuration.relation; |
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
38 |
|
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
|
39 |
std::vector<AttributeMetadata> attributesMetadata; |
4 | 40 |
for (RequestedField field : configuration.fields) { |
52
fea625f0a096
parallel processing: prepare infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
41 |
std::shared_ptr<AttributeFinder> finder = attributeFinders[field.group]; |
32
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
42 |
if (finder) for (AttributeMetadata m : finder->toMetadata(writer.get(), relationName, field)) attributesMetadata.push_back(m); |
4 | 43 |
else throw RelpipeWriterException(L"Unsupported field group: " + field.group); |
44 |
} |
|
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
|
45 |
|
16
6180161335be
support custom relation name through --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
46 |
writer->startRelation(relationName, attributesMetadata, true); |
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
|
47 |
|
4 | 48 |
|
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
|
49 |
for (std::stringstream originalName; readNext(input, originalName); reset(originalName)) { |
8
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
50 |
fs::path file(originalName.str().empty() ? "." : originalName.str()); // interpret empty string as current directory (e.g. result of: find -printf '%P\0') |
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
|
51 |
bool exists = false; |
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 |
|
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 |
try { |
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 |
exists = fs::exists(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
|
55 |
} catch (const fs::filesystem_error& e) { |
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 |
// we probably do not have permissions to given directory → pretend that the file does not exist |
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 |
} |
0 | 58 |
|
9
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
59 |
for (auto& finder : attributeFinders) finder.second->startFile(file, originalName.str(), exists); |
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
|
60 |
|
4 | 61 |
for (RequestedField field : configuration.fields) { |
52
fea625f0a096
parallel processing: prepare infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
62 |
std::shared_ptr<AttributeFinder> finder = attributeFinders[field.group]; // should not be nullptr, because already checked while writing the relation metadata |
32
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
63 |
finder->writeField(writer.get(), relationName, field); |
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
|
64 |
} |
7
8d73bff730a7
add symlink link target field
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
65 |
|
4 | 66 |
for (auto& finder : attributeFinders) finder.second->endFile(); |
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
|
67 |
} |
0 | 68 |
} |
69 |
}; |
|
70 |
||
71 |
} |
|
72 |
} |
|
73 |
} |