/*** Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: FSTimes.Unix.cpp Date: 2023-2-4 Author: Reece ***/ #include #include "FS.hpp" #include "FSTimes.Unix.hpp" #include #include #include namespace Aurora::IO::FS { void ConvertFileTime(timeval *temp, AuInt64 alternative, AuOptionalEx 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 ×) { 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; } }