From b3a3b1cdbdc727fe9c522982e5ebe076656e9294 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 13 Apr 2020 08:18:23 -0700 Subject: [PATCH] Add system::get/setWorkingDirectory() which fixes the Windows build. --- include/system.hpp | 4 ++++ src/asset.cpp | 8 ++------ src/plugin.cpp | 7 +++---- src/system.cpp | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/include/system.hpp b/include/system.hpp index 155a4018..036d16db 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -36,7 +36,11 @@ void createDirectories(const std::string& path); The directory must be empty. Fails silently. */ void removeDirectory(const std::string& path); +/** Deletes a directory if empty and all parent directories that are then empty. +*/ void removeDirectories(const std::string& path); +std::string getWorkingDirectory(); +void setWorkingDirectory(const std::string& path); /** Returns the number of logical simultaneous multithreading (SMT) (e.g. Intel Hyperthreaded) threads on the CPU. */ int getLogicalCoreCount(); /** Sets a name of the current thread for debuggers and OS-specific process viewers. */ diff --git a/src/asset.cpp b/src/asset.cpp index 57eb3be3..396140ae 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -33,9 +33,7 @@ static void initSystemDir() { return; if (settings::devMode) { - char buf[4096] = "."; - getcwd(buf, sizeof(buf)); - systemDir = buf; + systemDir = system::getWorkingDirectory(); return; } @@ -72,9 +70,7 @@ static void initSystemDir() { #endif #if defined ARCH_LIN // Use the current working directory as the default path on Linux. - char buf[4096] = "."; - getcwd(buf, sizeof(buf)); - systemDir = buf; + systemDir = system::getWorkingDirectory(); #endif } diff --git a/src/plugin.cpp b/src/plugin.cpp index f1504310..d30fc9d8 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -55,12 +55,11 @@ static void* loadLibrary(std::string libraryPath) { } #else // Plugin uses -rpath=. so change working directory so it can find libRack. - char cwd[PATH_MAX] = ""; - getcwd(cwd, sizeof(cwd)); - chdir(asset::systemDir.c_str()); + std::string cwd = system::getWorkingDirectory(); + system::setWorkingDirectory(asset::systemDir); // And then change it back DEFER({ - chdir(cwd); + system::setWorkingDirectory(cwd); }); // Load library with dlopen void* handle = dlopen(libraryPath.c_str(), RTLD_NOW | RTLD_LOCAL); diff --git a/src/system.cpp b/src/system.cpp index 82a8d2ba..f9e6d8cc 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -168,6 +168,28 @@ void removeDirectories(const std::string& path) { } +std::string getWorkingDirectory() { +#if defined ARCH_WIN + wchar_t buf[4096] = L""; + GetCurrentDirectory(sizeof(buf), buf); + return string::fromWstring(buf); +#else + char buf[4096] = ""; + getcwd(buf, sizeof(buf)); + return buf; +#endif +} + + +void setWorkingDirectory(const std::string& path) { +#if defined ARCH_WIN + std::wstring pathW = string::toWstring(path); + SetCurrentDirectory(pathW.c_str()); +#else + chdir(path.c_str()); +#endif +} + int getLogicalCoreCount() { return std::thread::hardware_concurrency(); }