XAttrs.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 28 Dec 2023 01:35:17 +0100
branchv_0
changeset 37 fb673fa1cad5
parent 29 dc3c102e1264
child 39 2068a992e97e
permissions -rw-r--r--
documentation: mouse and keyboard controls
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
     2
 * OHP3D
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
262828ae9682 OOP for Texture
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
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <string>
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    19
#include <cstring>
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <stdexcept>
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    21
#include <vector>
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    22
#include <sys/xattr.h>
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    23
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    24
#include "XAttrs.h"
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    26
std::string xattrGet(
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    27
		const std::string fileName,
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    28
		const std::string name,
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    29
		bool* exists = nullptr) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    30
	std::string buffer;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    31
	ssize_t size = getxattr(fileName.c_str(), name.c_str(), nullptr, 0);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    32
	if (size > 0) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    33
		buffer.resize(size);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    34
		getxattr(fileName.c_str(), name.c_str(), buffer.data(), buffer.size());
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    35
		if (exists) *exists = true;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    36
		return buffer;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    37
	} else if (size == 0) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    38
		if (exists) *exists = true;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    39
		return "";
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    40
	} else if (errno == ENODATA) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    41
		if (exists) *exists = false;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    42
		return "";
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    43
	} else if (errno == ERANGE) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    44
		// rare race condition - the value has changed between the two calls
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    45
		return xattrGet(fileName, name, exists);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    46
	} else {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    47
		throw std::logic_error(
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    48
				std::string("Unable to get extended attribute: ")
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    49
				+ strerror(errno));
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    50
	}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    51
}
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    53
bool xattrSet(const std::string fileName,
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    54
		const std::string name,
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    55
		const std::string value,
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    56
		bool exists = true) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    57
	int result;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    58
	if (exists) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    59
		result = setxattr(
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    60
				fileName.c_str(),
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    61
				name.c_str(),
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    62
				value.c_str(),
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    63
				value.size(),
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    64
				0);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    65
	} else {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    66
		result = removexattr(fileName.c_str(), name.c_str());
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    67
		if (result < 0 && errno == ENODATA) return false;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    68
	}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    69
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    70
	if (result < 0) throw std::logic_error(
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    71
			std::string("Unable to set extended attribute: ")
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    72
			+ strerror(errno));
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    73
	else return true;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    74
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    75
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    76
std::vector<std::string> xattrList(const std::string fileName) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    77
	std::string buffer;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    78
	ssize_t size = listxattr(fileName.c_str(), nullptr, 0);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    79
	if (size >= 0) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    80
		buffer.resize(size);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    81
		listxattr(fileName.c_str(), buffer.data(), buffer.size());
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    82
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    83
		std::vector<std::string> result;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    84
		for (const char* k = buffer.c_str(); strlen(k); k += strlen(k) + 1) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    85
			result.push_back(k);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    86
		}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    87
		return result;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    88
	} else if (errno == ERANGE) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    89
		// rare race condition - the list has changed between the two calls
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    90
		return xattrList(fileName);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    91
	} else {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    92
		throw std::logic_error(
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    93
				std::string("Unable to list extended attributes: ")
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    94
				+ strerror(errno));
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    95
	}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    96
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    97
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    98
class XAttrs::Attribute::Impl {
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
public:
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   100
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   101
	Impl() {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   102
	}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   103
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   104
	virtual ~Impl() {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   105
	}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   106
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   107
	std::shared_ptr<XAttrs::Impl> xattrs;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   108
	std::string name;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   109
	std::string value;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   110
	bool exists = true;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   111
	bool loaded = false;
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
};
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   114
class XAttrs::Impl {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   115
public:
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   116
	std::string nameSpace;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   117
	std::string fileName;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   118
	std::vector<Attribute> attributes;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   119
	bool loaded = false;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   120
	const XAttrs::Attribute& save(const XAttrs::Attribute& attribute);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   121
	XAttrs::Attribute& load(XAttrs::Attribute& attribute);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   122
};
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   123
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   124
XAttrs::XAttrs(const std::string& fileName, const std::string& nameSpace) :
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   125
impl(std::make_shared<Impl>()) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   126
	impl->nameSpace = nameSpace;
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
	impl->fileName = fileName;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   130
XAttrs::~XAttrs() {
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   133
const std::string XAttrs::getFileName() const {
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
	return impl->fileName;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   137
size_t XAttrs::size(bool reload) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   138
	if (reload || !impl->loaded) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   139
		impl->attributes.clear();
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   140
		std::vector<std::string> names = xattrList(impl->fileName);
27
2a156cb51479 xattr: list attribute names without the namespace prefix 'user.'
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   141
		const std::string prefix = impl->nameSpace + ".";
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   142
		for (const std::string& name : names) {
27
2a156cb51479 xattr: list attribute names without the namespace prefix 'user.'
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   143
			if (name.starts_with(prefix)) {
2a156cb51479 xattr: list attribute names without the namespace prefix 'user.'
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   144
				Attribute a;
2a156cb51479 xattr: list attribute names without the namespace prefix 'user.'
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   145
				a.impl->xattrs = impl;
2a156cb51479 xattr: list attribute names without the namespace prefix 'user.'
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   146
				a.impl->name = name.substr(prefix.size());
2a156cb51479 xattr: list attribute names without the namespace prefix 'user.'
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   147
				a.impl->value = xattrGet(impl->fileName, name, &a.impl->exists);
2a156cb51479 xattr: list attribute names without the namespace prefix 'user.'
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   148
				impl->attributes.push_back(a);
2a156cb51479 xattr: list attribute names without the namespace prefix 'user.'
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   149
			}
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   150
		}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   151
		impl->loaded = true;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   152
	}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   153
	return impl->attributes.size();
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   154
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   155
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   156
XAttrs::Attribute* XAttrs::begin() {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   157
	size();
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   158
	return &impl->attributes[0];
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   159
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   160
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   161
XAttrs::Attribute* XAttrs::end() {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   162
	size();
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   163
	return &impl->attributes[impl->attributes.size()];
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   164
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   166
const XAttrs::Attribute&
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   167
XAttrs::Impl::save(const XAttrs::Attribute& attribute) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   168
	xattrSet(
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   169
			fileName,
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   170
			nameSpace + "." + attribute.impl->name,
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   171
			attribute.impl->value,
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   172
			attribute.impl->exists);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   173
	loaded &= attribute.impl->exists;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   174
	return attribute;
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   177
XAttrs::Attribute&
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   178
XAttrs::Impl::load(XAttrs::Attribute& attribute) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   179
	attribute.impl->value = xattrGet(fileName,
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   180
			nameSpace + "." + attribute.impl->name, &attribute.impl->exists);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   181
	return attribute;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   182
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   183
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   184
XAttrs::Attribute& XAttrs::operator[](std::string name) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   185
	for (XAttrs::Attribute& a : impl->attributes) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   186
		if (a.getName() == name) return a; // impl->load(a);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   187
	}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   188
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   189
	XAttrs::Attribute a;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   190
	a.impl->xattrs = impl;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   191
	a.impl->name = name;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   192
	a.impl->exists = false;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   193
	// impl->load(a);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   194
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   195
	impl->attributes.push_back(a);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   196
	return impl->attributes.back();
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   197
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   198
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   199
XAttrs::Attribute& XAttrs::operator[](std::size_t index) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   200
	if (impl->attributes.empty()) size();
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   201
	return impl->attributes[index];
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   202
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   203
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   204
XAttrs::Attribute::Attribute() : impl(std::make_shared<Impl>()) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   205
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   206
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   207
XAttrs::Attribute::~Attribute() {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   208
}
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   209
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   210
const std::string XAttrs::Attribute::getName() {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   211
	return impl->name;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   212
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   213
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   214
const std::string XAttrs::Attribute::getValue(bool reload) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   215
	if (reload || !impl->loaded) impl->xattrs->load(*this);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   216
	return impl->value;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   217
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   218
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   219
bool XAttrs::Attribute::exists(bool reload) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   220
	if (reload || !impl->loaded) impl->xattrs->load(*this);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   221
	return impl->exists;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   222
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   223
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   224
bool XAttrs::Attribute::missing(bool reload) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   225
	return !exists(reload);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   226
}
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   228
XAttrs::Attribute& XAttrs::Attribute::operator=(const std::string& value) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   229
	impl->value = value;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   230
	impl->exists = true;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   231
	impl->xattrs->save(*this);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   232
	return *this;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   233
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   234
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   235
XAttrs::Attribute& XAttrs::Attribute::
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   236
		operator=(const XAttrs::Attribute& value) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   237
	impl->value = value.impl->value;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   238
	impl->exists = value.impl->exists;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   239
	impl->xattrs->save(*this);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   240
	return *this;
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   241
}
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   242
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   243
XAttrs::Attribute::operator std::string() {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   244
	impl->xattrs->load(*this);
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   245
	return impl->value;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   246
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   247
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   248
std::ostream& operator<<(std::ostream& s, XAttrs::Attribute& a) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   249
	s << a.getValue().c_str();
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   250
	return s;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   251
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   252
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   253
std::ostream& operator>>(XAttrs::Attribute& a, std::ostream& s) {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   254
	s << a.getValue().c_str();
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   255
	return s;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   256
}
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   257
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   258
XAttrs::Null::Null() {
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   259
	impl->exists = false;
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   260
}