AuroraRuntime/Source/IO/FS/Resources.cpp

163 lines
4.1 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Resources.cpp
Date: 2021-6-16
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "FS.hpp"
#include "Resources.hpp"
#if defined(AURORA_PLATFORM_LINUX)
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#elif defined(AURORA_PLATFORM_WIN32)
#include <ShlObj_core.h>
#endif
namespace Aurora::IO::FS
{
static AuString gHomeDirectory;
static AuString gApplicationData;
AUKN_SYM bool GetSystemDomain(AuString &path)
{
path = gApplicationData;
return true;
}
AUKN_SYM bool GetProfileDomain(AuString &path)
{
path = gHomeDirectory;
return true;
}
AUKN_SYM bool GetSystemResourcePath(const AuString &fileName, AuString &path)
{
path.clear();
if (fileName.find("..") != AuString::npos)
{
LogWarn("Exploit Attempt? System resource path may not contain relative directory move operations: {}", fileName);
return false;
}
{
AuString tempPath;
if (Process::GetWorkingDirectory(tempPath))
{
tempPath += "/" + fileName;
if (FileExists(tempPath))
{
path = tempPath;
return true;
}
}
}
{
AuString tempPath;
if (Process::GetProcPath(tempPath))
{
tempPath += "/" + fileName;
if (FileExists(tempPath))
{
path = tempPath;
return true;
}
}
}
{
auto systemPath = gHomeDirectory + fileName;
if (FileExists(systemPath))
{
path = systemPath;
return true;
}
}
{
auto systemPath = gApplicationData + fileName;
if (FileExists(systemPath))
{
path = systemPath;
return true;
}
}
return false;
}
#if defined(AURORA_PLATFORM_WIN32)
static AuString GetSpecialDir(REFKNOWNFOLDERID rfid)
{
PWSTR directory;
if (SHGetKnownFolderPath(rfid, KF_FLAG_DEFAULT, NULL, &directory) != S_OK)
{
SysPanic("Couldn't get known special directory path of [MS:{}-{}-{}-{}{}{}{}{}{}{}{}] with a NULL access token",
rfid.Data1, rfid.Data2, rfid.Data3, rfid.Data4[0], rfid.Data4[1], rfid.Data4[2], rfid.Data4[3], rfid.Data4[4], rfid.Data4[5], rfid.Data4[6], rfid.Data4[7]);
}
return Locale::ConvertFromWChar(directory);
}
static void SetNamespaceDirectories()
{
gHomeDirectory = GetSpecialDir(FOLDERID_RoamingAppData);
gApplicationData = GetSpecialDir(FOLDERID_ProgramData);
}
#elif defined(AURORA_PLATFORM_LINUX)
static void SetNamespaceDirectories()
{
const char *homedir;
homedir = getenv("HOME");
if (!homedir)
{
homedir = getpwuid(getuid())->pw_dir;
}
gHomeDirectory = homedir;
gApplicationData = gHomeDirectory;
}
#else
static void SetNamespaceDirectories()
{
gHomeDirectory = ".";
gApplicationData = ".";
}
#endif
static void ChangeDir()
{
#if !defined(AU_NO_AU_HOME_BRANDING)
gApplicationData += "/Aurora/System";
gHomeDirectory += "/Aurora/Profile";
#else
gApplicationData += "/.application";
gHomeDirectory += "/.config"; //most unix programs hide their private user data under here
#endif
NormalizePath(gApplicationData);
NormalizePath(gHomeDirectory);
// Noting we append a path splitter to prevent hair pulling over missing path delimiters
// Eg: GetHome() + "myAwesomeApp/Config" = %HOME%/Aurora/ProfilemyAwsomeApp/Config
gApplicationData += kPathSplitter;
gHomeDirectory += kPathSplitter;
}
void InitResources()
{
SetNamespaceDirectories();
ChangeDir();
}
}