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

80 lines
1.9 KiB
C++
Raw Normal View History

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FSTimes.Unix.cpp
Date: 2023-2-4
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FS.hpp"
#include "FSTimes.Unix.hpp"
#include <Source/Time/Time.hpp>
#include <sys/time.h>
#include <utime.h>
namespace Aurora::IO::FS
{
void ConvertFileTime(timeval *temp,
AuInt64 alternative,
AuOptionalEx<AuInt64> time)
{
timespec ts;
if (!time)
{
time = alternative;
}
AuTime::auabsns2ts(&ts, time.value());
temp->tv_sec = ts.tv_sec;
temp->tv_usec = ts.tv_nsec / 1'000ull;
}
AUKN_SYM bool UpdateFileTimes(const AuString &path, const UpdateTimes &times)
{
if (times.createdNs)
{
SysPushErrorArg("Cannot modify a files' created timestamp under UNIX");
return false;
}
auto pathex = NormalizePathRet(path);
if (pathex.empty())
{
return false;
}
Stat auStat;
if (!times.accessedNs ||
!times.modifiedNs)
{
if (!AuFS::StatFile(pathex, auStat))
{
SysPushErrorArg("Cannot stat file of a attr-poke suspect");
return false;
}
}
else
{
if (!AuFS::FileExists(pathex))
{
SysPushErrorIO("Missing file: {}", path);
return false;
}
}
timeval timeVals[2];
ConvertFileTime(&timeVals[0], auStat.accessedNs, times.accessedNs);
ConvertFileTime(&timeVals[1], auStat.modifiedNs, times.modifiedNs);
bool bRet = ::utimes(pathex.c_str(), timeVals) == 0;
if (!bRet)
{
SysPushErrorIO();
}
return bRet;
}
}