src/PlainFilesystemCommand.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 24 Jan 2020 16:53:31 +0100
branchv_0
changeset 58 4679f67a8324
parent 57 c40a241d6e0c
child 59 7471529c0d11
permissions -rw-r--r--
parallel processing: put some common code in FilesystemCommand + use POSIX semaphores for STDOUT synchronization across sub-processes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     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
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
57
c40a241d6e0c parallel processing: use directly file descriptors (FD) instead of STDIO streams
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
    19
#include <ext/stdio_filebuf.h>
c40a241d6e0c parallel processing: use directly file descriptors (FD) instead of STDIO streams
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
    20
54
ef726975c34b parallel processing: rename FilesystemCommandBase to FilesystemCommand
František Kučera <franta-hg@frantovo.cz>
parents: 53
diff changeset
    21
#include "FilesystemCommand.h"
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    22
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
namespace relpipe {
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
namespace in {
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
namespace filesystem {
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
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
    27
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
    28
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
    29
54
ef726975c34b parallel processing: rename FilesystemCommandBase to FilesystemCommand
František Kučera <franta-hg@frantovo.cz>
parents: 53
diff changeset
    30
class PlainFilesystemCommand : public FilesystemCommand {
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
public:
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
57
c40a241d6e0c parallel processing: use directly file descriptors (FD) instead of STDIO streams
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
    33
	void process(int inputFD, int outputFD, Configuration& configuration) {
c40a241d6e0c parallel processing: use directly file descriptors (FD) instead of STDIO streams
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
    34
		__gnu_cxx::stdio_filebuf<char> inputBuffer(inputFD, std::ios::in);
c40a241d6e0c parallel processing: use directly file descriptors (FD) instead of STDIO streams
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
    35
		__gnu_cxx::stdio_filebuf<char> outputBuffer(outputFD, std::ios::out);
c40a241d6e0c parallel processing: use directly file descriptors (FD) instead of STDIO streams
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
    36
		std::istream input(&inputBuffer);
c40a241d6e0c parallel processing: use directly file descriptors (FD) instead of STDIO streams
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
    37
		std::ostream output(&outputBuffer);
c40a241d6e0c parallel processing: use directly file descriptors (FD) instead of STDIO streams
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
    38
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
58
4679f67a8324 parallel processing: put some common code in FilesystemCommand + use POSIX semaphores for STDOUT synchronization across sub-processes
František Kučera <franta-hg@frantovo.cz>
parents: 57
diff changeset
    40
		std::map<string_t, std::shared_ptr < AttributeFinder>> attributeFinders = createAttributeFinders();
32
bccda5688d71 propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    41
58
4679f67a8324 parallel processing: put some common code in FilesystemCommand + use POSIX semaphores for STDOUT synchronization across sub-processes
František Kučera <franta-hg@frantovo.cz>
parents: 57
diff changeset
    42
		string_t relationName = fetchRelationName(&configuration);
4679f67a8324 parallel processing: put some common code in FilesystemCommand + use POSIX semaphores for STDOUT synchronization across sub-processes
František Kučera <franta-hg@frantovo.cz>
parents: 57
diff changeset
    43
		writeHeader(writer.get(), attributeFinders, relationName, &configuration.fields);
4679f67a8324 parallel processing: put some common code in FilesystemCommand + use POSIX semaphores for STDOUT synchronization across sub-processes
František Kučera <franta-hg@frantovo.cz>
parents: 57
diff changeset
    44
		
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
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
    46
		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
    47
			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
    48
			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
    49
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
			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
    51
				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
    52
			} 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
    53
				// 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
    54
			}
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
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
    56
			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
    57
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
			for (RequestedField field : configuration.fields) {
52
fea625f0a096 parallel processing: prepare infrastructure
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    59
				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
    60
				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
    61
			}
7
8d73bff730a7 add symlink link target field
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    62
4
d44ed75822e7 modular design
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
			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
    64
		}
0
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	}
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
};
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
}
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
}
467d09b62a12 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
}