xattr: XAttrs class that read/write/list extended attributes v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 10 Dec 2023 22:23:32 +0100
branchv_0
changeset 24 98d033d3ef7c
parent 23 42341f66de52
child 25 717653cedc4a
xattr: XAttrs class that read/write/list extended attributes
Makefile
Shark.cpp
XAttrs.cpp
XAttrs.h
nbproject/configurations.xml
--- a/Makefile	Sun Dec 10 22:20:37 2023 +0100
+++ b/Makefile	Sun Dec 10 22:23:32 2023 +0100
@@ -23,9 +23,6 @@
 run: build/shader-shark
 	SHADER_SHARK_DATA_DIR=. $(<)
 
-build:
-	mkdir -p $(@)
-	
 SRC= \
     Shark.cpp \
     shader-shark.cpp \
@@ -33,8 +30,10 @@
     Texture.cpp \
     Shader.cpp \
     Program.cpp \
+    XAttrs.cpp \
     FileMonitor.cpp
 
-build/shader-shark: $(SRC) build *.h
+build/shader-shark: $(SRC) *.h
+	mkdir -p build
 	$(CXX) -std=c++20 -g -o $(@) $(SRC) $$(pkg-config --cflags --libs \
 	    epoxy x11 glu glm Magick++)
--- a/Shark.cpp	Sun Dec 10 22:20:37 2023 +0100
+++ b/Shark.cpp	Sun Dec 10 22:23:32 2023 +0100
@@ -33,6 +33,7 @@
 #include "Shader.h"
 #include "Program.h"
 #include "FileMonitor.h"
+#include "XAttrs.h"
 
 #include "Shark.h"
 
@@ -553,6 +554,8 @@
 				img(imageLoader.loadImage(MappedFile(tex.fileName)));
 		textures.push_back(std::make_shared<Texture>(
 				img->width, img->height, *img, tex.fileName));
+		// static const uint32_t watchMask = IN_CLOSE_WRITE | IN_ATTRIB;
+		// watchedFiles.push_back(fileMonitor.watch(tex.fileName, watchMask));
 		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
 		// TODO: review texture loading and binding
 		// works even without this - default texture
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XAttrs.cpp	Sun Dec 10 22:23:32 2023 +0100
@@ -0,0 +1,258 @@
+/**
+ * ShaderShark
+ * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string>
+#include <cstring>
+#include <stdexcept>
+#include <vector>
+#include <sys/xattr.h>
+
+#include "XAttrs.h"
+
+std::string xattrGet(
+		const std::string fileName,
+		const std::string name,
+		bool* exists = nullptr) {
+	std::string buffer;
+	ssize_t size = getxattr(fileName.c_str(), name.c_str(), nullptr, 0);
+	if (size > 0) {
+		buffer.resize(size);
+		getxattr(fileName.c_str(), name.c_str(), buffer.data(), buffer.size());
+		if (exists) *exists = true;
+		return buffer;
+	} else if (size == 0) {
+		if (exists) *exists = true;
+		return "";
+	} else if (errno == ENODATA) {
+		if (exists) *exists = false;
+		return "";
+	} else if (errno == ERANGE) {
+		// rare race condition - the value has changed between the two calls
+		return xattrGet(fileName, name, exists);
+	} else {
+		throw std::logic_error(
+				std::string("Unable to get extended attribute: ")
+				+ strerror(errno));
+	}
+}
+
+bool xattrSet(const std::string fileName,
+		const std::string name,
+		const std::string value,
+		bool exists = true) {
+	int result;
+	if (exists) {
+		result = setxattr(
+				fileName.c_str(),
+				name.c_str(),
+				value.c_str(),
+				value.size(),
+				0);
+	} else {
+		result = removexattr(fileName.c_str(), name.c_str());
+		if (result < 0 && errno == ENODATA) return false;
+	}
+
+	if (result < 0) throw std::logic_error(
+			std::string("Unable to set extended attribute: ")
+			+ strerror(errno));
+	else return true;
+}
+
+std::vector<std::string> xattrList(const std::string fileName) {
+	std::string buffer;
+	ssize_t size = listxattr(fileName.c_str(), nullptr, 0);
+	if (size >= 0) {
+		buffer.resize(size);
+		listxattr(fileName.c_str(), buffer.data(), buffer.size());
+
+		std::vector<std::string> result;
+		for (const char* k = buffer.c_str(); strlen(k); k += strlen(k) + 1) {
+			result.push_back(k);
+		}
+		return result;
+	} else if (errno == ERANGE) {
+		// rare race condition - the list has changed between the two calls
+		return xattrList(fileName);
+	} else {
+		throw std::logic_error(
+				std::string("Unable to list extended attributes: ")
+				+ strerror(errno));
+	}
+}
+
+class XAttrs::Attribute::Impl {
+public:
+
+	Impl() {
+	}
+
+	virtual ~Impl() {
+	}
+
+	std::shared_ptr<XAttrs::Impl> xattrs;
+	std::string name;
+	std::string value;
+	bool exists = true;
+	bool loaded = false;
+};
+
+class XAttrs::Impl {
+public:
+	std::string nameSpace;
+	std::string fileName;
+	std::vector<Attribute> attributes;
+	bool loaded = false;
+	const XAttrs::Attribute& save(const XAttrs::Attribute& attribute);
+	XAttrs::Attribute& load(XAttrs::Attribute& attribute);
+};
+
+XAttrs::XAttrs(const std::string& fileName, const std::string& nameSpace) :
+impl(std::make_shared<Impl>()) {
+	impl->nameSpace = nameSpace;
+	impl->fileName = fileName;
+}
+
+XAttrs::~XAttrs() {
+}
+
+const std::string XAttrs::getFileName() const {
+	return impl->fileName;
+}
+
+size_t XAttrs::size(bool reload) {
+	if (reload || !impl->loaded) {
+		impl->attributes.clear();
+		std::vector<std::string> names = xattrList(impl->fileName);
+		// FIXME: remove nameSpace
+		for (const std::string& name : names) {
+			Attribute a;
+			a.impl->xattrs = impl;
+			a.impl->name = name;
+			a.impl->value = xattrGet(impl->fileName, name, &a.impl->exists);
+			impl->attributes.push_back(a);
+		}
+		impl->loaded = true;
+	}
+	return impl->attributes.size();
+}
+
+XAttrs::Attribute* XAttrs::begin() {
+	size();
+	return &impl->attributes[0];
+}
+
+XAttrs::Attribute* XAttrs::end() {
+	size();
+	return &impl->attributes[impl->attributes.size()];
+}
+
+const XAttrs::Attribute&
+XAttrs::Impl::save(const XAttrs::Attribute& attribute) {
+	xattrSet(
+			fileName,
+			nameSpace + "." + attribute.impl->name,
+			attribute.impl->value,
+			attribute.impl->exists);
+	loaded &= attribute.impl->exists;
+	return attribute;
+}
+
+XAttrs::Attribute&
+XAttrs::Impl::load(XAttrs::Attribute& attribute) {
+	attribute.impl->value = xattrGet(fileName,
+			nameSpace + "." + attribute.impl->name, &attribute.impl->exists);
+	return attribute;
+}
+
+XAttrs::Attribute& XAttrs::operator[](std::string name) {
+	for (XAttrs::Attribute& a : impl->attributes) {
+		if (a.getName() == name) return a; // impl->load(a);
+	}
+
+	XAttrs::Attribute a;
+	a.impl->xattrs = impl;
+	a.impl->name = name;
+	a.impl->exists = false;
+	// impl->load(a);
+
+	impl->attributes.push_back(a);
+	return impl->attributes.back();
+}
+
+XAttrs::Attribute& XAttrs::operator[](std::size_t index) {
+	if (impl->attributes.empty()) size();
+	return impl->attributes[index];
+}
+
+XAttrs::Attribute::Attribute() : impl(std::make_shared<Impl>()) {
+}
+
+XAttrs::Attribute::~Attribute() {
+}
+
+const std::string XAttrs::Attribute::getName() {
+	return impl->name;
+}
+
+const std::string XAttrs::Attribute::getValue(bool reload) {
+	if (reload || !impl->loaded) impl->xattrs->load(*this);
+	return impl->value;
+}
+
+bool XAttrs::Attribute::exists(bool reload) {
+	if (reload || !impl->loaded) impl->xattrs->load(*this);
+	return impl->exists;
+}
+
+bool XAttrs::Attribute::missing(bool reload) {
+	return !exists(reload);
+}
+
+XAttrs::Attribute& XAttrs::Attribute::operator=(const std::string& value) {
+	impl->value = value;
+	impl->exists = true;
+	impl->xattrs->save(*this);
+	return *this;
+}
+
+XAttrs::Attribute& XAttrs::Attribute::
+		operator=(const XAttrs::Attribute& value) {
+	impl->value = value.impl->value;
+	impl->exists = value.impl->exists;
+	impl->xattrs->save(*this);
+	return *this;
+}
+
+XAttrs::Attribute::operator std::string() {
+	impl->xattrs->load(*this);
+	return impl->value;
+}
+
+std::ostream& operator<<(std::ostream& s, XAttrs::Attribute& a) {
+	s << a.getValue().c_str();
+	return s;
+}
+
+std::ostream& operator>>(XAttrs::Attribute& a, std::ostream& s) {
+	s << a.getValue().c_str();
+	return s;
+}
+
+XAttrs::Null::Null() {
+	impl->exists = false;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XAttrs.h	Sun Dec 10 22:23:32 2023 +0100
@@ -0,0 +1,67 @@
+/**
+ * ShaderShark
+ * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include "Buffer.h"
+
+class XAttrs {
+public:
+
+	class Null;
+
+	class Attribute {
+	public:
+		virtual ~Attribute();
+		const std::string getName();
+		const std::string getValue(bool reload = false);
+		bool exists(bool reload = false);
+		bool missing(bool reload = false);
+		Attribute& operator=(const std::string& value);
+		Attribute& operator=(const Attribute& value);
+		operator std::string();
+		friend std::ostream& operator<<(std::ostream& s, Attribute& a);
+		friend std::ostream& operator>>(Attribute& a, std::ostream& s);
+	private:
+		class Impl;
+		std::shared_ptr<Impl> impl;
+		Attribute();
+		friend XAttrs;
+		friend Null;
+	};
+
+	class Null : public Attribute {
+	public:
+		Null();
+	};
+
+	XAttrs(const std::string& fileName, const std::string& nameSpace = "user");
+	virtual ~XAttrs();
+	const std::string getFileName() const;
+	Attribute& operator[](std::string name);
+	Attribute& operator[](std::size_t index);
+	size_t size(bool reload = false);
+	Attribute* begin();
+	Attribute* end();
+private:
+	class Impl;
+	std::shared_ptr<Impl> impl;
+	friend Attribute;
+};
--- a/nbproject/configurations.xml	Sun Dec 10 22:20:37 2023 +0100
+++ b/nbproject/configurations.xml	Sun Dec 10 22:23:32 2023 +0100
@@ -8,6 +8,7 @@
       <in>Shader.cpp</in>
       <in>Shark.cpp</in>
       <in>Texture.cpp</in>
+      <in>XAttrs.cpp</in>
       <in>shader-shark.cpp</in>
     </df>
     <logicalFolder name="ExternalFiles"
@@ -81,6 +82,10 @@
         <ccTool flags="0">
         </ccTool>
       </item>
+      <item path="XAttrs.cpp" ex="false" tool="1" flavor2="0">
+        <ccTool flags="0">
+        </ccTool>
+      </item>
       <item path="shader-shark.cpp" ex="false" tool="1" flavor2="0">
         <ccTool flags="0">
         </ccTool>