[FileAccess] Implement support for reading and writing extended file attributes/alternate data streams.

This commit is contained in:
Pāvels Nadtočajevs
2025-01-16 13:42:24 +02:00
parent 235a32ad11
commit d454e1a1e0
7 changed files with 391 additions and 0 deletions

View File

@ -38,6 +38,9 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(WEB_ENABLED)
#include <sys/xattr.h>
#endif
#include <unistd.h>
#include <cerrno>
@ -506,6 +509,117 @@ Error FileAccessUnix::_set_read_only_attribute(const String &p_file, bool p_ro)
#endif
}
PackedByteArray FileAccessUnix::_get_extended_attribute(const String &p_file, const String &p_attribute_name) {
ERR_FAIL_COND_V(p_attribute_name.is_empty(), PackedByteArray());
String file = fix_path(p_file);
PackedByteArray data;
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(WEB_ENABLED)
// Not supported.
#elif defined(__APPLE__)
CharString attr_name = p_attribute_name.utf8();
ssize_t attr_size = getxattr(file.utf8().get_data(), attr_name.get_data(), nullptr, 0, 0, 0);
if (attr_size <= 0) {
return PackedByteArray();
}
data.resize(attr_size);
attr_size = getxattr(file.utf8().get_data(), attr_name.get_data(), (void *)data.ptrw(), data.size(), 0, 0);
ERR_FAIL_COND_V_MSG(attr_size != data.size(), PackedByteArray(), "Failed to set extended attributes for: " + p_file);
#else
CharString attr_name = ("user." + p_attribute_name).utf8();
ssize_t attr_size = getxattr(file.utf8().get_data(), attr_name.get_data(), nullptr, 0);
if (attr_size <= 0) {
return PackedByteArray();
}
data.resize(attr_size);
attr_size = getxattr(file.utf8().get_data(), attr_name.get_data(), (void *)data.ptrw(), data.size());
ERR_FAIL_COND_V_MSG(attr_size != data.size(), PackedByteArray(), "Failed to set extended attributes for: " + p_file);
#endif
return data;
}
Error FileAccessUnix::_set_extended_attribute(const String &p_file, const String &p_attribute_name, const PackedByteArray &p_data) {
ERR_FAIL_COND_V(p_attribute_name.is_empty(), FAILED);
String file = fix_path(p_file);
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(WEB_ENABLED)
// Not supported.
#elif defined(__APPLE__)
int err = setxattr(file.utf8().get_data(), p_attribute_name.utf8().get_data(), (const void *)p_data.ptr(), p_data.size(), 0, 0);
if (err != 0) {
return FAILED;
}
#else
int err = setxattr(file.utf8().get_data(), ("user." + p_attribute_name).utf8().get_data(), (const void *)p_data.ptr(), p_data.size(), 0);
if (err != 0) {
return FAILED;
}
#endif
return OK;
}
Error FileAccessUnix::_remove_extended_attribute(const String &p_file, const String &p_attribute_name) {
ERR_FAIL_COND_V(p_attribute_name.is_empty(), FAILED);
String file = fix_path(p_file);
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(WEB_ENABLED)
// Not supported.
#elif defined(__APPLE__)
int err = removexattr(file.utf8().get_data(), p_attribute_name.utf8().get_data(), 0);
if (err != 0) {
return FAILED;
}
#else
int err = removexattr(file.utf8().get_data(), ("user." + p_attribute_name).utf8().get_data());
if (err != 0) {
return FAILED;
}
#endif
return OK;
}
PackedStringArray FileAccessUnix::_get_extended_attributes_list(const String &p_file) {
PackedStringArray ret;
String file = fix_path(p_file);
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(WEB_ENABLED)
// Not supported.
#elif defined(__APPLE__)
size_t size = listxattr(file.utf8().get_data(), nullptr, 0, 0);
if (size > 0) {
PackedByteArray data;
data.resize(size);
listxattr(file.utf8().get_data(), (char *)data.ptrw(), data.size(), 0);
int64_t start = 0;
for (int64_t x = 0; x < data.size(); x++) {
if (x != start && data[x] == 0) {
ret.push_back(String::utf8((const char *)(data.ptr() + start), x - start));
start = x + 1;
}
}
}
#else
size_t size = listxattr(file.utf8().get_data(), nullptr, 0);
if (size > 0) {
PackedByteArray data;
data.resize(size);
listxattr(file.utf8().get_data(), (char *)data.ptrw(), data.size());
int64_t start = 0;
for (int64_t x = 0; x < data.size(); x++) {
if (x != start && data[x] == 0) {
String name = String::utf8((const char *)(data.ptr() + start), x - start);
if (name.begins_with("user.")) {
ret.push_back(name.trim_prefix("user."));
}
start = x + 1;
}
}
}
#endif
return ret;
}
void FileAccessUnix::close() {
_close();
}

View File

@ -91,6 +91,11 @@ public:
virtual bool _get_read_only_attribute(const String &p_file) override;
virtual Error _set_read_only_attribute(const String &p_file, bool p_ro) override;
virtual PackedByteArray _get_extended_attribute(const String &p_file, const String &p_attribute_name) override;
virtual Error _set_extended_attribute(const String &p_file, const String &p_attribute_name, const PackedByteArray &p_data) override;
virtual Error _remove_extended_attribute(const String &p_file, const String &p_attribute_name) override;
virtual PackedStringArray _get_extended_attributes_list(const String &p_file) override;
virtual void close() override;
FileAccessUnix() {}

View File

@ -583,6 +583,104 @@ Error FileAccessWindows::_set_read_only_attribute(const String &p_file, bool p_r
return OK;
}
PackedByteArray FileAccessWindows::_get_extended_attribute(const String &p_file, const String &p_attribute_name) {
ERR_FAIL_COND_V(p_attribute_name.is_empty(), PackedByteArray());
String file = fix_path(p_file);
file += ":" + p_attribute_name;
const Char16String &file_utf16 = file.utf16();
PackedByteArray data;
HANDLE h = CreateFileW((LPCWSTR)file_utf16.get_data(), GENERIC_READ, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (h != INVALID_HANDLE_VALUE) {
size_t bytes_in_buffer = 0;
const int CHUNK_SIZE = 4096;
DWORD read = 0;
for (;;) {
data.resize(bytes_in_buffer + CHUNK_SIZE);
bool success = ReadFile(h, data.ptrw() + bytes_in_buffer, CHUNK_SIZE, &read, nullptr);
if (!success || read == 0) {
break;
}
bytes_in_buffer += read;
}
data.resize(bytes_in_buffer);
CloseHandle(h);
}
return data;
}
Error FileAccessWindows::_set_extended_attribute(const String &p_file, const String &p_attribute_name, const PackedByteArray &p_data) {
ERR_FAIL_COND_V(p_attribute_name.is_empty(), FAILED);
String file = fix_path(p_file);
file += ":" + p_attribute_name;
const Char16String &file_utf16 = file.utf16();
PackedByteArray data;
HANDLE h = CreateFileW((LPCWSTR)file_utf16.get_data(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (h == INVALID_HANDLE_VALUE) {
return FAILED;
}
DWORD written = 0;
bool ok = true;
if (p_data.size() > 0) {
ok = WriteFile(h, p_data.ptr(), p_data.size(), &written, nullptr);
}
CloseHandle(h);
ERR_FAIL_COND_V_MSG(!ok || written != p_data.size(), FAILED, "Failed to set extended attributes for: " + p_file);
return OK;
}
Error FileAccessWindows::_remove_extended_attribute(const String &p_file, const String &p_attribute_name) {
ERR_FAIL_COND_V(p_attribute_name.is_empty(), FAILED);
String file = fix_path(p_file);
file += ":" + p_attribute_name;
const Char16String &file_utf16 = file.utf16();
return DeleteFileW((LPCWSTR)(file_utf16.get_data())) != 0 ? OK : FAILED;
}
PackedStringArray FileAccessWindows::_get_extended_attributes_list(const String &p_file) {
PackedStringArray ret;
String file = fix_path(p_file);
char info_block[65536] = {};
PFILE_STREAM_INFO stream_info = (PFILE_STREAM_INFO)info_block;
HANDLE h = CreateFileW((LPCWSTR)file.utf16().get_data(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
if (h == INVALID_HANDLE_VALUE) {
return ret;
}
BOOL out = GetFileInformationByHandleEx(h, FileStreamInfo, info_block, sizeof(info_block));
CloseHandle(h);
if (!out) {
return ret;
}
while (true) {
if (stream_info->StreamNameLength != 0) {
String name = String::utf16((const char16_t *)stream_info->StreamName, stream_info->StreamNameLength / sizeof(WCHAR));
if (name.ends_with(":$DATA")) {
name = name.trim_prefix(":").trim_suffix(":$DATA");
if (!name.is_empty()) {
ret.push_back(name);
}
}
}
if (stream_info->NextEntryOffset == 0) {
break;
}
stream_info = (PFILE_STREAM_INFO)((LPBYTE)stream_info + stream_info->NextEntryOffset);
}
return ret;
}
void FileAccessWindows::close() {
_close();
}

View File

@ -90,6 +90,11 @@ public:
virtual bool _get_read_only_attribute(const String &p_file) override;
virtual Error _set_read_only_attribute(const String &p_file, bool p_ro) override;
virtual PackedByteArray _get_extended_attribute(const String &p_file, const String &p_attribute_name) override;
virtual Error _set_extended_attribute(const String &p_file, const String &p_attribute_name, const PackedByteArray &p_data) override;
virtual Error _remove_extended_attribute(const String &p_file, const String &p_attribute_name) override;
virtual PackedStringArray _get_extended_attributes_list(const String &p_file) override;
virtual void close() override;
static void initialize();