/*** 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 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 GetFSDeviceByFilePath(const AuString &path) { AuList> 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 a, AuPair b) { return AuGet<0>(a) > AuGet<0>(b); }); auto copy = AuGet<1>(devs[0]); return AuMove(copy); } AUKN_SYM AuList GetFSDevices() { InitPlatformFSCacheAtLoad(); AU_LOCK_GUARD(gFSDirMutex); return gCachedDevices; } AUKN_SYM AuResult 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 &GetFSDevicesCachedUnsafe() { InitPlatformFSCacheAtLoad(); return gCachedDevices; } AuList SysGetFSDevices(); AUKN_SYM void ResetDeviceCache() { AU_LOCK_GUARD(gFSDirMutex); gCachedDevices = AuMove(SysGetFSDevices()); } }