AuroraRuntime/Source/IO/FS/FSPlatformDevices.cpp

125 lines
3.1 KiB
C++
Raw Normal View History

/***
Copyright (C) 2023 Jamie Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FSPlatformDevices.cpp
Date: 2023-08-11
Date: 2023-12-05
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FS.hpp"
#include "FSPlatformDevices.hpp"
namespace Aurora::IO::FS
{
void InitPlatformFSCacheAtLoad()
{
static AuInitOnce gInitOnce;
gInitOnce.Call([]()
{
ResetDeviceCache();
});
}
AUKN_SYM AuResult<AuString> GetDeviceModel(const AuString &physicalDevicePath)
{
InitPlatformFSCacheAtLoad();
AU_LOCK_GUARD(gFSDirMutex);
for (const auto &refFSDevice : gCachedDevices)
{
if (refFSDevice.devicePath == physicalDevicePath)
{
auto copy = refFSDevice.productModel;
return AuMove(copy);
}
}
return {};
}
AUKN_SYM AuResult<FSDevice> GetFSDeviceByFilePath(const AuString &path)
{
AuList<AuPair<AuUInt, const FSDevice *>> devs;
InitPlatformFSCacheAtLoad();
auto normalized = AuFS::NormalizePathRet(path);
if (normalized.empty())
{
return {};
}
{
AU_LOCK_GUARD(gFSDirMutex);
for (const auto &refFSDevice : gCachedDevices)
{
for (const auto &refPart : refFSDevice.partitions) // todo sort
{
for (const auto &refMnt : refPart.filesystemMountPoints) // todo sort
{
if (AuStartsWith(path, refMnt))
{
devs.push_back(AuMakePair(refMnt.size(), &refFSDevice));
}
}
}
}
}
if (devs.empty())
{
return {};
}
std::sort(devs.begin(),
devs.end(),
[](AuPair<AuUInt, const FSDevice *> a, AuPair<AuUInt, const FSDevice *> b)
{
return AuGet<0>(a) > AuGet<0>(b);
});
auto copy = AuGet<1>(devs[0]);
return AuMove(copy);
}
AUKN_SYM AuList<FSDevice> GetFSDevices()
{
InitPlatformFSCacheAtLoad();
AU_LOCK_GUARD(gFSDirMutex);
return gCachedDevices;
}
AUKN_SYM AuResult<FSDevice> GetFSDeviceByDevice(const AuString &physicalDevicePath)
{
InitPlatformFSCacheAtLoad();
AU_LOCK_GUARD(gFSDirMutex);
for (const auto &refFSDevice : gCachedDevices)
{
if (refFSDevice.devicePath == physicalDevicePath)
{
auto copy = refFSDevice;
return AuMove(copy);
}
}
return {};
}
AUKN_SYM const AuList<FSDevice> &GetFSDevicesCachedUnsafe()
{
InitPlatformFSCacheAtLoad();
return gCachedDevices;
}
AuList<FSDevice> SysGetFSDevices();
AUKN_SYM void ResetDeviceCache()
{
AU_LOCK_GUARD(gFSDirMutex);
gCachedDevices = AuMove(SysGetFSDevices());
}
}