From db1ff0cbf8ff6ed412c4339a49f0a4cf53ca6983 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Mon, 11 Mar 2024 21:05:03 +0000 Subject: [PATCH] [*] Refactored FS resources to return AuOptional --- Include/Aurora/IO/FS/Resources.hpp | 16 ++--- Source/Console/ConsoleFIO/ConsoleFIO.cpp | 13 +++- Source/IO/FS/FS.cpp | 22 ++++-- Source/IO/FS/Resources.cpp | 90 ++++++++++-------------- 4 files changed, 75 insertions(+), 66 deletions(-) diff --git a/Include/Aurora/IO/FS/Resources.hpp b/Include/Aurora/IO/FS/Resources.hpp index af84b1ba..9f276413 100644 --- a/Include/Aurora/IO/FS/Resources.hpp +++ b/Include/Aurora/IO/FS/Resources.hpp @@ -12,12 +12,12 @@ namespace Aurora::IO::FS /** * @brief Provides an application specific storage path for sharing data amongst Aurora applications sharing the same branding info (defer to the init structure) */ - AUKN_SYM bool GetSystemDomain(AuString &path); + AUKN_SYM AuOptional GetSystemDomain(); /** * @brief Provides an application specific storage path for user-local application data, isolated with respect to your Aurora application brand info (defer to the init structure) */ - AUKN_SYM bool GetProfileDomain(AuString &path); + AUKN_SYM AuOptional GetProfileDomain(); AUKN_SYM bool GetSystemResourcePath(const AuString &fileName, AuString &path); @@ -32,36 +32,36 @@ namespace Aurora::IO::FS * @param path * @return */ - AUKN_SYM bool GetPackagePath(AuString &path); + AUKN_SYM AuOptional GetPackagePath(); /** * @brief Pulls the application directory as defined by the operating system standard file system hierarchy * Otherwise, XDG_CONFIG_CONFIG or %appdata%. * This directory might be sandboxed to the user or application by the operating system. */ - AUKN_SYM bool GetAppData(AuString &path); + AUKN_SYM AuOptional GetAppData(); /** * @brief Pulls the users home directory, untouched */ - AUKN_SYM bool GetUserHome(AuString &path); + AUKN_SYM AuOptional GetUserHome(); /** * @brief Global application data that requires no special permissions to access. * This could be an SD-Card or user profile. * This could be the root of the user managable aplication directory. */ - AUKN_SYM bool GetWritableAppdata(AuString &path); + AUKN_SYM AuOptional GetWritableAppdata(); /** * @brief Global application data that requires special permissions to access, usually configured by a system account during installation */ - AUKN_SYM bool GetRootAppdata(AuString &path); + AUKN_SYM AuOptional GetRootAppdata(); /** * @brief Get user installable application directory */ - AUKN_SYM bool GetUserProgramsFolder(AuString &path); + AUKN_SYM AuOptional GetUserProgramsFolder(); AUKN_SYM AuOptional NewTempFile(); diff --git a/Source/Console/ConsoleFIO/ConsoleFIO.cpp b/Source/Console/ConsoleFIO/ConsoleFIO.cpp index 60e0683c..bf246301 100644 --- a/Source/Console/ConsoleFIO/ConsoleFIO.cpp +++ b/Source/Console/ConsoleFIO/ConsoleFIO.cpp @@ -21,7 +21,18 @@ namespace Aurora::Console::ConsoleFIO AuString path; AuString procName; - if ((!gLogConfig.bWriteLogsToUserDir) || (!AuIOFS::GetProfileDomain(path))) + if (gLogConfig.bWriteLogsToUserDir) + { + if (auto optProfileDomain = AuIOFS::GetProfileDomain()) + { + path = *optProfileDomain; + } + else + { + path = "."; + } + } + else { path = "."; } diff --git a/Source/IO/FS/FS.cpp b/Source/IO/FS/FS.cpp index 6ae6eabf..906afe0c 100644 --- a/Source/IO/FS/FS.cpp +++ b/Source/IO/FS/FS.cpp @@ -70,7 +70,11 @@ namespace Aurora::IO::FS // Local Aurora profile else if (path[iOffset] == '~') { - if (!FS::GetProfileDomain(buffer)) + if (auto optPath = GetProfileDomain()) + { + buffer = *optPath; + } + else { result.clear(); return; @@ -79,7 +83,11 @@ namespace Aurora::IO::FS // Global Aurora profile else if (path[iOffset] == '!') { - if (!FS::GetSystemDomain(buffer)) + if (auto optPath = GetSystemDomain()) + { + buffer = *optPath; + } + else { result.clear(); return; @@ -246,13 +254,19 @@ namespace Aurora::IO::FS if (str[0] == '~') { - AuFS::GetProfileDomain(str); + if (auto optPath = FS::GetProfileDomain()) + { + str = *optPath; + } return; } if (str[0] == '!') { - AuFS::GetSystemDomain(str); + if (auto optPath = FS::GetSystemDomain()) + { + str = *optPath; + } return; } } diff --git a/Source/IO/FS/Resources.cpp b/Source/IO/FS/Resources.cpp index e9c0876d..12ab04fd 100644 --- a/Source/IO/FS/Resources.cpp +++ b/Source/IO/FS/Resources.cpp @@ -38,23 +38,28 @@ namespace Aurora::IO::FS static AuOptional gUserLibPath2; static AuString gTempDir; // Should the following be /opt? Probably, if it were a direct replacement for Windows' appdata on Linux for global software packages outside of our ecosystem, sure; however, this is strictly a fallback for when there is no home - // We don't support initially-undefined global application configurations across users on Unix targets. We can therefore conclue the application running is a service whose user is without a home, and should be subject to the same rules as an application deployed by a real package manager + // We don't support initially-undefined global application configurations across users on Unix targets. We can therefore conclude the application running is a service whose user is without a home, and should be subject to the same rules as daemon deployed by a real package manager // For internal packages, in our own ecosystem of tools, I think this follows the UNIX spec, not that I care what arcahic C-with-vendor-packages-as-an-OS specification says. // The only way you can break this assumption is if you argue for users who will be outside of our deployment pipeline, wanting global configs, and don't have write permission on a relevant global directory. // They can shove it. Superuser should install software for all users. - // XDG (falling back to home) for non-root installs; for root installs, installing a service package, use /var; for root installs of an application whose system configs should be shared amongst all users, unsupported, idc, it's sandboxed per user + // We should use: + // 2> XDG envvars (with a fallback to a home relative path) for non-root installs; for special installs via package manager, use /var; + // 2> for root installs of an application whose system configs should be shared amongst all users, unsupported, idc, it's sandboxed per user. + // we don't have any good examples of home family computer-esc posix machines static const char * kUnixAppData {"/var"}; - AUKN_SYM bool GetSystemDomain(AuString &path) + AUKN_SYM AuOptional GetSystemDomain() { - path = gApplicationData; - return path.size(); + return gApplicationData.size() ? + gApplicationData : + AuOptional {}; } - AUKN_SYM bool GetProfileDomain(AuString &path) + AUKN_SYM AuOptional GetProfileDomain() { - path = gHomeDirectory; - return path.size(); + return gHomeDirectory.size() ? + gHomeDirectory : + AuOptional {}; } AUKN_SYM bool GetSystemResourcePath(const AuString &fileName, AuString &path) @@ -453,71 +458,50 @@ namespace Aurora::IO::FS gApplicationData.clear(); } - AUKN_SYM bool GetAppData(AuString &path) + AUKN_SYM AuOptional GetAppData() { - path.clear(); - if (gUserWritableAppData.empty()) - { - return false; - } - path = gUserWritableAppData; - return path.size(); + return gUserWritableAppData.size() ? + gUserWritableAppData : + AuOptional {}; } - AUKN_SYM bool GetUserHome(AuString &path) + AUKN_SYM AuOptional GetUserHome() { - path.clear(); - if (gUserHomeDirectory.empty()) - { - return false; - } - path = gUserHomeDirectory; - return path.size(); + return gUserHomeDirectory.size() ? + gUserHomeDirectory : + AuOptional {}; } - AUKN_SYM bool GetPackagePath(AuString &path) + AUKN_SYM AuOptional GetPackagePath() { // TODO: iOS/mac OS -> CFBundleCopyResourcesDirectoryURL - if (auto pProcessDirectory = Process::GetProcessDirectory()) + if (auto optProcessDirectory = Process::GetProcessDirectory()) { - path = *pProcessDirectory; - return true; + return optProcessDirectory; } - return false; + return AuOptional{}; } - AUKN_SYM bool GetWritableAppdata(AuString &path) + AUKN_SYM AuOptional GetWritableAppdata() { - path.clear(); - if (gGlobalWritableAppDirectory.empty()) - { - return GetSystemDomain(path); - } - path = gGlobalWritableAppDirectory; - return path.size(); + return gGlobalWritableAppDirectory.size() ? + gGlobalWritableAppDirectory : + AuOptional {}; } - AUKN_SYM bool GetRootAppdata(AuString &path) + AUKN_SYM AuOptional GetRootAppdata() { - path.clear(); - if (gAdminWritableAppDirectory.empty()) - { - return false; - } - path = gAdminWritableAppDirectory; - return path.size(); + return gAdminWritableAppDirectory.size() ? + gAdminWritableAppDirectory : + AuOptional {}; } - AUKN_SYM bool GetUserProgramsFolder(AuString &path) + AUKN_SYM AuOptional GetUserProgramsFolder() { - path.clear(); - if (gProgramsFolder.empty()) - { - return false; - } - path = gProgramsFolder; - return path.size(); + return gProgramsFolder.size() ? + gProgramsFolder : + AuOptional {}; } AUKN_SYM AuOptional NewTempFile()