AuroraRuntime/Source/IO/FS/FileAttrs.Unix.cpp
Jamie Reece Wilson 83f34b0c47 [*] I was right. String views are [mostly] pointless (*)
03:28:55:638  17>2 of 53388 functions (<0.1%) were compiled, the rest were copied from previous compilation.
03:28:55:638  17>  0 functions were new in current compilation
03:28:55:638  17>  65 functions had inline decision re-evaluated but remain unchanged
03:28:56:749  17>Finished generating code

the header of const AuString & is the same as std::string_view therefore nothing changes. in fact, we still need to alloc strings a bunch of times for a zero terminated string. worse, <c++20 always allocs each time we want to access a hashmap with o(1) lookup, making small hashmaps kinda pointless when we always have to alloc+copy (thx std)

perhaps this will help some language binders
2024-04-19 05:58:08 +01:00

128 lines
3.0 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 AuROString &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 AuSplitStringLegacy(buffer, "\00");
}
AUKN_SYM AuResult<AuByteBuffer> FileAttrsGet(const AuROString &path, const AuROString &attr)
{
AuString copy(attr);
AuByteBuffer buffer;
auto srcPath = NormalizePathRet(path);
if (srcPath.empty())
{
return {};
}
auto length = ::getxattr(srcPath.c_str(), copy.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(), copy.c_str(), buffer.base, length);
if (length < 0)
{
SysPushErrorIO("Error reading attribute");
return {};
}
buffer.writePtr += length;
return AuMove(buffer);
}
AUKN_SYM bool FileAttrsSet(const AuROString &path, const AuROString &attr, const Memory::MemoryViewRead &view)
{
AuString copy(attr);
auto srcPath = NormalizePathRet(path);
if (srcPath.empty())
{
return false;
}
if (::setxattr(srcPath.c_str(), copy.c_str(), view.ptr, view.length, XATTR_CREATE) == -1)
{
if (errno == EEXIST)
{
if (::setxattr(srcPath.c_str(), copy.c_str(), view.ptr, view.length, XATTR_REPLACE) == -1)
{
SysPushErrorIO();
return false;
}
}
}
return true;
}
AUKN_SYM bool FileAttrsDel(const AuROString &path, const AuROString &attr)
{
AuString copy(attr);
auto srcPath = NormalizePathRet(path);
if (srcPath.empty())
{
return false;
}
return (::removexattr(srcPath.c_str(), copy.c_str()) == 0) ||
(errno == ENODATA);
}
}