interpret empty string as current directory (e.g. result of: find -printf '%P\0') v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 16 Jan 2019 18:19:50 +0100
branchv_0
changeset 8 eb1ecb37c6b7
parent 7 8d73bff730a7
child 9 b4f29fb16159
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
src/AttributeFinder.h
src/FileAttributeFinder.h
src/FilesystemCommand.h
src/XattrAttributeFinder.h
--- a/src/AttributeFinder.h	Wed Jan 16 18:16:07 2019 +0100
+++ b/src/AttributeFinder.h	Wed Jan 16 18:19:50 2019 +0100
@@ -34,6 +34,9 @@
 using namespace relpipe::writer;
 
 class AttributeFinder {
+protected:
+	fs::path currentFile;
+	string currentFileRaw;
 public:
 
 	/**
@@ -47,14 +50,21 @@
 	/**
 	 * Writing of the record for current file is starting.
 	 * Following writeField() calls are related to this file.
-	 * @param file
+	 * @param file path to the file
+	 * @param fileRaw raw file name as it was on the input
 	 */
-	virtual void startFile(const fs::path& file) = 0;
+	virtual void startFile(const fs::path& file, const string& fileRaw) {
+		currentFile = file;
+		currentFileRaw = fileRaw;
+	}
 
 	/**
 	 * Writing of the record for current file is finished. All resources linked to this file should be released.
 	 */
-	virtual void endFile() = 0;
+	virtual void endFile() {
+		currentFile.clear();
+		currentFileRaw.clear();
+	}
 
 	/**
 	 * Writes field attribute(s). The attribute count must match with count of AttributeMetadata returned in toMetadata().
--- a/src/FileAttributeFinder.h	Wed Jan 16 18:16:07 2019 +0100
+++ b/src/FileAttributeFinder.h	Wed Jan 16 18:19:50 2019 +0100
@@ -25,6 +25,7 @@
 #include <relpipe/writer/RelationalWriter.h>
 
 #include "RequestedField.h"
+#include "AttributeFinder.h"
 
 namespace relpipe {
 namespace in {
@@ -37,7 +38,6 @@
 private:
 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
 
-	fs::path currentFile;
 	string_t currentOwner;
 	string_t currentGroup;
 
@@ -95,11 +95,8 @@
 		}
 	}
 
-	void startFile(const fs::path& file) override {
-		currentFile = file;
-	};
-
 	void endFile() override {
+		AttributeFinder::endFile();
 		currentOwner.clear();
 		currentGroup.clear();
 	};
@@ -110,7 +107,7 @@
 				if (field.name == FIELD_NAME) {
 					writer->writeAttribute(currentFile.filename().wstring());
 				} else if (field.name == FIELD_PATH_ORIGINAL) {
-					writer->writeAttribute(currentFile.wstring());
+					writer->writeAttribute(convertor.from_bytes(currentFileRaw));
 				} else if (field.name == FIELD_PATH_ABSOLUTE) {
 					writer->writeAttribute(fs::absolute(currentFile).wstring());
 				} else if (field.name == FIELD_PATH_CANONICAL) {
--- a/src/FilesystemCommand.h	Wed Jan 16 18:16:07 2019 +0100
+++ b/src/FilesystemCommand.h	Wed Jan 16 18:19:50 2019 +0100
@@ -86,8 +86,7 @@
 
 
 		for (std::stringstream originalName; readNext(input, originalName); reset(originalName)) {
-
-			fs::path file(originalName.str());
+			fs::path file(originalName.str().empty() ? "." : originalName.str()); // interpret empty string as current directory (e.g. result of: find -printf '%P\0')
 			bool exists = false;
 
 			try {
@@ -96,7 +95,7 @@
 				// we probably do not have permissions to given directory → pretend that the file does not exist
 			}
 
-			for (auto& finder : attributeFinders) finder.second->startFile(file);
+			for (auto& finder : attributeFinders) finder.second->startFile(file, originalName.str());
 
 			for (RequestedField field : configuration.fields) {
 				AttributeFinder* finder = attributeFinders[field.group]; // should not be nullptr, because already checked while writing the relation metadata
--- a/src/XattrAttributeFinder.h	Wed Jan 16 18:16:07 2019 +0100
+++ b/src/XattrAttributeFinder.h	Wed Jan 16 18:19:50 2019 +0100
@@ -37,8 +37,6 @@
 private:
 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
 
-	fs::path currentFile;
-
 	string_t getXattr(const fs::path& file, const string_t& attributeName) {
 		// TODO: review, test on multiple platforms
 		// TODO: improve documentation in xattr.h in glibc
@@ -65,13 +63,6 @@
 		}
 	}
 
-	void startFile(const fs::path& file) override {
-		currentFile = file;
-	};
-
-	void endFile() override {
-	};
-
 	virtual void writeField(RelationalWriter* writer, const RequestedField& field) override {
 		for (string_t alias : field.getAliases()) {
 			if (field.group == RequestedField::GROUP_XATTR) writer->writeAttribute(getXattr(currentFile, field.name));