2023-12-05 18:44:38 +00:00
|
|
|
/***
|
|
|
|
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();
|
|
|
|
|
2024-09-09 01:47:38 +00:00
|
|
|
AU_LOCK_GLOBAL_GUARD(gFSDirMutex);
|
2023-12-05 18:44:38 +00:00
|
|
|
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)
|
|
|
|
{
|
2024-07-12 15:21:39 +00:00
|
|
|
AuPair<AuUInt, const FSDevice *> lastDev =
|
|
|
|
AuMakePair<AuUInt, const FSDevice *>(0, nullptr);
|
2023-12-05 18:44:38 +00:00
|
|
|
|
|
|
|
InitPlatformFSCacheAtLoad();
|
|
|
|
|
|
|
|
auto normalized = AuFS::NormalizePathRet(path);
|
|
|
|
if (normalized.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-09-09 01:47:38 +00:00
|
|
|
AU_LOCK_GLOBAL_GUARD(gFSDirMutex);
|
2023-12-05 18:44:38 +00:00
|
|
|
|
2024-07-12 15:21:39 +00:00
|
|
|
for (const auto &refFSDevice : gCachedDevices)
|
|
|
|
{
|
|
|
|
for (const auto &refPart : refFSDevice.partitions)
|
2023-12-05 18:44:38 +00:00
|
|
|
{
|
2024-07-12 15:21:39 +00:00
|
|
|
for (const auto &refMnt : refPart.filesystemMountPoints)
|
2023-12-05 18:44:38 +00:00
|
|
|
{
|
2024-07-12 15:21:39 +00:00
|
|
|
if (AuStartsWith(path, refMnt))
|
2023-12-05 18:44:38 +00:00
|
|
|
{
|
2024-07-12 15:21:39 +00:00
|
|
|
if (AuGet<0>(lastDev) < refMnt.size())
|
2023-12-05 18:44:38 +00:00
|
|
|
{
|
2024-07-12 15:21:39 +00:00
|
|
|
lastDev = AuMakePair(refMnt.size(), &refFSDevice);
|
2023-12-05 18:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 15:21:39 +00:00
|
|
|
if (AuGet<0>(lastDev) == 0)
|
2023-12-05 18:44:38 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-07-12 15:21:39 +00:00
|
|
|
auto copy = *AuGet<1>(lastDev);
|
2023-12-05 18:44:38 +00:00
|
|
|
return AuMove(copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuList<FSDevice> GetFSDevices()
|
|
|
|
{
|
|
|
|
InitPlatformFSCacheAtLoad();
|
2024-09-09 01:47:38 +00:00
|
|
|
AU_LOCK_GLOBAL_GUARD(gFSDirMutex);
|
2023-12-05 18:44:38 +00:00
|
|
|
return gCachedDevices;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuResult<FSDevice> GetFSDeviceByDevice(const AuString &physicalDevicePath)
|
|
|
|
{
|
|
|
|
InitPlatformFSCacheAtLoad();
|
|
|
|
|
2024-09-09 01:47:38 +00:00
|
|
|
AU_LOCK_GLOBAL_GUARD(gFSDirMutex);
|
2023-12-05 18:44:38 +00:00
|
|
|
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()
|
|
|
|
{
|
2024-09-09 01:47:38 +00:00
|
|
|
AU_LOCK_GLOBAL_GUARD(gFSDirMutex);
|
2023-12-05 18:44:38 +00:00
|
|
|
gCachedDevices = AuMove(SysGetFSDevices());
|
|
|
|
}
|
|
|
|
}
|