/*** Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: FileAttrs.Unix.cpp Date: 2023-2-4 Author: Reece ***/ #include #include "FS.hpp" #include "FileAttrs.Unix.hpp" #include #include namespace Aurora::IO::FS { AUKN_SYM AuList FileAttrsList(const AuString &path) { static const auto kLength = 10 * 1024; AuString buffer; auto srcPath = NormalizePathRet(path); if (srcPath.empty()) { return {}; } if (!AuTryResize(buffer, kLength)) { SysPushErrorMemory(); return {}; } auto length = ::listxattr(srcPath.c_str(), buffer.data(), buffer.size()); if (length < 0) { SysPushErrorIO("Error listing attributes"); return {}; } if (!AuTryResize(buffer, length)) { SysPushErrorMemory(); return {}; } return AuSplitString(buffer, "\00"); } AUKN_SYM AuResult FileAttrsGet(const AuString &path, const AuString &attr) { AuByteBuffer buffer; auto srcPath = NormalizePathRet(path); if (srcPath.empty()) { return {}; } auto length = ::getxattr(srcPath.c_str(), attr.c_str(), nullptr, 0); if (length < 0) { SysPushErrorIO("Error reading attribute"); return {}; } if (!length) { return AuMove(buffer); } if (!AuTryResize(buffer, length)) { SysPushErrorMemory(); return {}; } length = ::getxattr(srcPath.c_str(), attr.c_str(), buffer.base, length); if (length < 0) { SysPushErrorIO("Error reading attribute"); return {}; } buffer.writePtr += length; return AuMove(buffer); } AUKN_SYM bool FileAttrsSet(const AuString &path, const AuString &attr, const Memory::MemoryViewRead &view) { auto srcPath = NormalizePathRet(path); if (srcPath.empty()) { return false; } if (::setxattr(srcPath.c_str(), attr.c_str(), view.ptr, view.length, XATTR_CREATE) == -1) { if (errno == EEXIST) { if (::setxattr(srcPath.c_str(), attr.c_str(), view.ptr, view.length, XATTR_REPLACE) == -1) { SysPushErrorIO(); return false; } } } return true; } AUKN_SYM bool FileAttrsDel(const AuString &path, const AuString &attr) { auto srcPath = NormalizePathRet(path); if (srcPath.empty()) { return false; } return (::removexattr(srcPath.c_str(), attr.c_str()) == 0) || (errno == ENODATA); } }