@@ -36,7 +36,11 @@ void createDirectories(const std::string& path); | |||||
The directory must be empty. Fails silently. | The directory must be empty. Fails silently. | ||||
*/ | */ | ||||
void removeDirectory(const std::string& path); | 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); | 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. */ | /** Returns the number of logical simultaneous multithreading (SMT) (e.g. Intel Hyperthreaded) threads on the CPU. */ | ||||
int getLogicalCoreCount(); | int getLogicalCoreCount(); | ||||
/** Sets a name of the current thread for debuggers and OS-specific process viewers. */ | /** Sets a name of the current thread for debuggers and OS-specific process viewers. */ | ||||
@@ -33,9 +33,7 @@ static void initSystemDir() { | |||||
return; | return; | ||||
if (settings::devMode) { | if (settings::devMode) { | ||||
char buf[4096] = "."; | |||||
getcwd(buf, sizeof(buf)); | |||||
systemDir = buf; | |||||
systemDir = system::getWorkingDirectory(); | |||||
return; | return; | ||||
} | } | ||||
@@ -72,9 +70,7 @@ static void initSystemDir() { | |||||
#endif | #endif | ||||
#if defined ARCH_LIN | #if defined ARCH_LIN | ||||
// Use the current working directory as the default path on Linux. | // Use the current working directory as the default path on Linux. | ||||
char buf[4096] = "."; | |||||
getcwd(buf, sizeof(buf)); | |||||
systemDir = buf; | |||||
systemDir = system::getWorkingDirectory(); | |||||
#endif | #endif | ||||
} | } | ||||
@@ -55,12 +55,11 @@ static void* loadLibrary(std::string libraryPath) { | |||||
} | } | ||||
#else | #else | ||||
// Plugin uses -rpath=. so change working directory so it can find libRack. | // 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 | // And then change it back | ||||
DEFER({ | DEFER({ | ||||
chdir(cwd); | |||||
system::setWorkingDirectory(cwd); | |||||
}); | }); | ||||
// Load library with dlopen | // Load library with dlopen | ||||
void* handle = dlopen(libraryPath.c_str(), RTLD_NOW | RTLD_LOCAL); | void* handle = dlopen(libraryPath.c_str(), RTLD_NOW | RTLD_LOCAL); | ||||
@@ -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() { | int getLogicalCoreCount() { | ||||
return std::thread::hardware_concurrency(); | return std::thread::hardware_concurrency(); | ||||
} | } | ||||