AuroraRuntime/Source/IO/FS/FileAttrs.Unix.cpp

123 lines
2.9 KiB
C++

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FileAttrs.Unix.cpp
Date: 2023-2-4
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FS.hpp"
#include "FileAttrs.Unix.hpp"
#include <sys/types.h>
#include <sys/xattr.h>
namespace Aurora::IO::FS
{
AUKN_SYM AuList<AuString> 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<AuByteBuffer> 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);
}
}