Skip to content

Commit

Permalink
Make sure our std::filesystem users use the std::error_code versions
Browse files Browse the repository at this point in the history
Otherwise these crash out.
  • Loading branch information
Sonicadvance1 committed Aug 3, 2021
1 parent 83bdbc8 commit b586592
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion External/FEXCore/Source/Common/Paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace FEXCore::Paths {

std::error_code ec{};
// Ensure the folder structure is created for our Data
if (!std::filesystem::exists(*EntryCache) &&
if (!std::filesystem::exists(*EntryCache, ec) &&
!std::filesystem::create_directories(*EntryCache, ec)) {
LogMan::Msg::D("Couldn't create EntryCache directory: '%s'", EntryCache->c_str());
}
Expand Down
15 changes: 9 additions & 6 deletions External/FEXCore/Source/Interface/Config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace FEXCore::Config {

// Ensure the folder structure is created for our configuration
std::error_code ec{};
if (!std::filesystem::exists(ConfigDir) &&
if (!std::filesystem::exists(ConfigDir, ec) &&
!std::filesystem::create_directories(ConfigDir, ec)) {
// Let's go local in this case
return "./";
Expand Down Expand Up @@ -88,7 +88,7 @@ namespace FEXCore::Config {

std::error_code ec{};
if (!Global &&
!std::filesystem::exists(ConfigFile) &&
!std::filesystem::exists(ConfigFile, ec) &&
!std::filesystem::create_directories(ConfigFile, ec)) {
LogMan::Msg::D("Couldn't create config directory: '%s'", ConfigFile.c_str());
// Let's go local in this case
Expand All @@ -99,7 +99,7 @@ namespace FEXCore::Config {

// Attempt to create the local folder if it doesn't exist
if (!Global &&
!std::filesystem::exists(ConfigFile) &&
!std::filesystem::exists(ConfigFile, ec) &&
!std::filesystem::create_directories(ConfigFile, ec)) {
// Let's go local in this case
return "./" + Filename + ".json";
Expand Down Expand Up @@ -274,7 +274,8 @@ namespace FEXCore::Config {
Path = std::filesystem::absolute(Path);

// Only return if it exists
if (std::filesystem::exists(Path)) {
std::error_code ec{};
if (std::filesystem::exists(Path, ec)) {
return Path;
}
}
Expand Down Expand Up @@ -310,7 +311,8 @@ namespace FEXCore::Config {
else if (!PathName().empty()) {
// If the filesystem doesn't exist then let's see if it exists in the fex-emu folder
std::string NamedRootFS = GetDataDirectory() + "RootFS/" + PathName();
if (std::filesystem::exists(NamedRootFS)) {
std::error_code ec{};
if (std::filesystem::exists(NamedRootFS, ec)) {
FEXCore::Config::EraseSet(FEXCore::Config::CONFIG_ROOTFS, NamedRootFS);
}
}
Expand All @@ -333,7 +335,8 @@ namespace FEXCore::Config {
else if (!PathName().empty()) {
// If the filesystem doesn't exist then let's see if it exists in the fex-emu folder
std::string NamedConfig = GetDataDirectory() + "ThunkConfigs/" + PathName();
if (std::filesystem::exists(NamedConfig)) {
std::error_code ec{};
if (std::filesystem::exists(NamedConfig, ec)) {
FEXCore::Config::EraseSet(FEXCore::Config::CONFIG_THUNKCONFIG, NamedConfig);
}
}
Expand Down
6 changes: 4 additions & 2 deletions Source/Common/RootFSSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ static std::fstream SquashFSLock{};
bool SanityCheckPath(std::string const &LDPath) {
// Check if we have an directory inside our temp folder
std::string PathUser = LDPath + "/usr";
if (!std::filesystem::exists(PathUser)) {
std::error_code ec{};
if (!std::filesystem::exists(PathUser, ec)) {
LogMan::Msg::D("Child couldn't mount rootfs, /usr doesn't exist");
rmdir(LDPath.c_str());
return false;
Expand All @@ -33,7 +34,8 @@ bool SanityCheckPath(std::string const &LDPath) {
bool CheckLockExists(std::string const LockPath) {
// If the lock file for a squashfs path exists the we can try
// to open it and ref counting will keep it alive
if (std::filesystem::exists(LockPath)) {
std::error_code ec{};
if (std::filesystem::exists(LockPath, ec)) {
SquashFSLock.open(LockPath, std::ios_base::in | std::ios_base::binary);
if (SquashFSLock.is_open()) {
// We managed to open the file. Which means the mount application has now refcounted our interaction with it
Expand Down
5 changes: 3 additions & 2 deletions Source/Linux/Utils/ELFContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ ELFContainer::ELFContainer(std::string const &Filename, std::string const &RootF
RawString = &RawFile.at(InterpreterHeader._64->p_offset);
}
std::string RootFSLink = RootFS + RawString;
while (std::filesystem::is_symlink(RootFSLink)) {
std::error_code ec{};
while (std::filesystem::is_symlink(RootFSLink, ec)) {
// Do some special handling if the RootFS's linker is a symlink
// Ubuntu's rootFS by default provides an absolute location symlink to the linker
// Resolve this around back to the rootfs
auto SymlinkTarget = std::filesystem::read_symlink(RootFSLink);
auto SymlinkTarget = std::filesystem::read_symlink(RootFSLink, ec);
if (SymlinkTarget.is_absolute()) {
RootFSLink = RootFS + SymlinkTarget.string();
}
Expand Down
10 changes: 6 additions & 4 deletions Source/Tests/FEXLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ bool RanAsInterpreter(char *Program) {
bool IsInterpreterInstalled() {
// The interpreter is installed if both the binfmt_misc handlers are available
// Or if we were originally executed with FD. Which means the interpreter is installed

std::error_code ec{};
return ExecutedWithFD ||
(std::filesystem::exists("/proc/sys/fs/binfmt_misc/FEX-x86") &&
std::filesystem::exists("/proc/sys/fs/binfmt_misc/FEX-x86_64"));
(std::filesystem::exists("/proc/sys/fs/binfmt_misc/FEX-x86", ec) &&
std::filesystem::exists("/proc/sys/fs/binfmt_misc/FEX-x86_64", ec));
}

void AOTGenSection(FEXCore::Context::Context *CTX, ELFCodeLoader2::LoadedSection &Section) {
Expand Down Expand Up @@ -409,7 +411,8 @@ int main(int argc, char **argv, char **const envp) {

InterpreterHandler(&Program, LDPath(), &Args);

if (!std::filesystem::exists(Program)) {
std::error_code ec{};
if (!std::filesystem::exists(Program, ec)) {
// Early exit if the program passed in doesn't exist
// Will prevent a crash later
fprintf(stderr, "%s: command not found\n", Program.c_str());
Expand Down Expand Up @@ -548,7 +551,6 @@ int main(int argc, char **argv, char **const envp) {
FEXCore::Context::RunUntilExit(CTX);
}

std::error_code ec{};
if (std::filesystem::create_directories(std::filesystem::path(FEXCore::Config::GetDataDirectory()) / "aotir", ec)) {
FEXCore::Context::WriteFilesWithCode(CTX, [](const std::string& fileid, const std::string& filename) {
auto filepath = std::filesystem::path(FEXCore::Config::GetDataDirectory()) / "aotir" / (fileid + ".path");
Expand Down
8 changes: 5 additions & 3 deletions Source/Tools/FEXConfig/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ namespace {
}

bool OpenFile(std::string Filename, bool LoadDefault = false) {
if (!std::filesystem::exists(Filename)) {
std::error_code ec{};
if (!std::filesystem::exists(Filename, ec)) {
if (LoadDefault) {
LoadDefaultSettings();
ConfigFilename = Filename;
Expand All @@ -98,9 +99,10 @@ namespace {
std::scoped_lock<std::mutex> lk{NamedRootFSUpdator};
NamedRootFS.clear();
std::string RootFS = FEXCore::Config::GetDataDirectory() + "RootFS/";
if (!std::filesystem::exists(RootFS)) {
std::error_code ec{};
if (!std::filesystem::exists(RootFS, ec)) {
// Doesn't exist, create the the folder as a user convenience
if (!std::filesystem::create_directories(RootFS)) {
if (!std::filesystem::create_directories(RootFS, ec)) {
// Well I guess we failed
return;
}
Expand Down

0 comments on commit b586592

Please sign in to comment.