/*** 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 #include "FS.hpp" #include "FSPlatformDevices.hpp" namespace Aurora::IO::FS { void InitPlatformFSCacheAtLoad() { static AuInitOnce gInitOnce; gInitOnce.Call([]() { ResetDeviceCache(); }); } AUKN_SYM AuResult GetDeviceModel(const AuROString &physicalDevicePath) { InitPlatformFSCacheAtLoad(); AU_LOCK_GLOBAL_GUARD(gFSDirMutex); for (const auto &refFSDevice : gCachedDevices) { if (refFSDevice.devicePath == physicalDevicePath) { auto copy = refFSDevice.productModel; return AuMove(copy); } } return {}; } AUKN_SYM AuResult GetFSDeviceByFilePath(const AuROString &path) { AuPair lastDev = AuMakePair(0, nullptr); InitPlatformFSCacheAtLoad(); auto normalized = AuFS::NormalizePathRet(path); if (normalized.empty()) { return {}; } AU_LOCK_GLOBAL_GUARD(gFSDirMutex); for (const auto &refFSDevice : gCachedDevices) { for (const auto &refPart : refFSDevice.partitions) { for (const auto &refMnt : refPart.filesystemMountPoints) { if (AuStartsWith(path, refMnt)) { if (AuGet<0>(lastDev) < refMnt.size()) { lastDev = AuMakePair(refMnt.size(), &refFSDevice); } } } } } if (AuGet<0>(lastDev) == 0) { return {}; } auto copy = *AuGet<1>(lastDev); return AuMove(copy); } AUKN_SYM AuList GetFSDevices() { InitPlatformFSCacheAtLoad(); AU_LOCK_GLOBAL_GUARD(gFSDirMutex); return gCachedDevices; } AUKN_SYM AuResult GetFSDeviceByDevice(const AuROString &physicalDevicePath) { InitPlatformFSCacheAtLoad(); AU_LOCK_GLOBAL_GUARD(gFSDirMutex); for (const auto &refFSDevice : gCachedDevices) { if (refFSDevice.devicePath == physicalDevicePath) { auto copy = refFSDevice; return AuMove(copy); } } return {}; } AUKN_SYM const AuList &GetFSDevicesCachedUnsafe() { InitPlatformFSCacheAtLoad(); return gCachedDevices; } AuList SysGetFSDevices(); AUKN_SYM void ResetDeviceCache() { AU_LOCK_GLOBAL_GUARD(gFSDirMutex); gCachedDevices = AuMove(SysGetFSDevices()); } }