Fix applying relative working directory multiple times

This commit is contained in:
Martchus 2021-07-31 14:55:49 +02:00
parent 1ce736d66f
commit 95ae827c74
2 changed files with 14 additions and 0 deletions

View File

@ -237,8 +237,16 @@ void ServiceSetup::loadConfigFiles(bool restoreStateAndDiscardDatabases)
} }
} }
// apply working directory // apply working directory
// note: As this function can run multiple times (as live-reconfigurations are supported) we
// must restore the initial working directory here so relative paths are always treated
// relative to the initial working directory.
if (!workingDirectory.empty()) { if (!workingDirectory.empty()) {
try { try {
if (initialWorkingDirectory.empty()) {
initialWorkingDirectory = std::filesystem::current_path();
} else {
std::filesystem::current_path(initialWorkingDirectory);
}
workingDirectory = std::filesystem::absolute(workingDirectory); workingDirectory = std::filesystem::absolute(workingDirectory);
} catch (const std::filesystem::filesystem_error &e) { } catch (const std::filesystem::filesystem_error &e) {
cerr << Phrases::WarningMessage << "Unable to determine absolute path of specified working directory: " << e.what() cerr << Phrases::WarningMessage << "Unable to determine absolute path of specified working directory: " << e.what()
@ -247,6 +255,11 @@ void ServiceSetup::loadConfigFiles(bool restoreStateAndDiscardDatabases)
if (chdir(workingDirectory.c_str()) != 0) { if (chdir(workingDirectory.c_str()) != 0) {
cerr << Phrases::WarningMessage << "Unable to change the working directory to \"" << workingDirectory cerr << Phrases::WarningMessage << "Unable to change the working directory to \"" << workingDirectory
<< "\": " << std::strerror(errno) << Phrases::EndFlush; << "\": " << std::strerror(errno) << Phrases::EndFlush;
try {
workingDirectory = std::filesystem::current_path();
} catch (const std::filesystem::filesystem_error &e) {
cerr << Phrases::WarningMessage << "Unable to determine effective working directory: " << e.what() << Phrases::EndFlush;
}
} }
} }
// restore state/cache and discard databases // restore state/cache and discard databases

View File

@ -40,6 +40,7 @@ struct LIBREPOMGR_EXPORT ServiceSetup : public LibPkg::Lockable {
// -> acquire the setup lock for these // -> acquire the setup lock for these
std::string configFilePath = "server.conf"; std::string configFilePath = "server.conf";
std::string pacmanConfigFilePath = "/etc/pacman.conf"; std::string pacmanConfigFilePath = "/etc/pacman.conf";
std::filesystem::path initialWorkingDirectory;
std::string workingDirectory = "workingdir"; std::string workingDirectory = "workingdir";
// variables relevant for the web server; only changed when (re)loading config // variables relevant for the web server; only changed when (re)loading config