Added basic checks to ensure configured directories in lms.conf exist, fixes #828

This commit is contained in:
emeric
2026-03-08 21:30:11 +01:00
parent 3d5e2d3563
commit 4c7ad485bf
2 changed files with 47 additions and 7 deletions
+43 -3
View File
@@ -17,6 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <filesystem>
#include <thread>
#include <Wt/WApplication.h>
@@ -110,19 +111,58 @@ namespace lms
throw core::LmsException{ "Invalid config value for 'jukebox-audio-backend'" };
}
std::vector<std::string> generateWtConfig(std::string execPath)
std::error_code checkDirectoryAccessible(const std::filesystem::path& dir)
{
std::error_code ec;
const std::filesystem::file_status status{ std::filesystem::status(dir, ec) };
if (ec)
return ec;
if (status.type() != std::filesystem::file_type::directory)
return std::make_error_code(std::errc::not_a_directory);
const std::filesystem::directory_iterator it{ dir, ec };
if (ec)
return ec;
if (it != std::filesystem::directory_iterator{})
{
it->status(ec);
if (ec)
return ec;
}
return {};
}
std::vector<std::string> generateWtConfig(const std::string& execPath)
{
core::IConfig& config{ *core::Service<core::IConfig>::get() };
std::vector<std::string> args;
const std::filesystem::path wtConfigPath{ config.getPath("working-dir", "/var/lms") / "wt_config.xml" };
const std::filesystem::path workingDirectoryPath{ config.getPath("working-dir", "/var/lms") };
const std::filesystem::path wtConfigPath{ workingDirectoryPath / "wt_config.xml" };
const std::filesystem::path wtResourcesPath{ config.getPath("wt-resources", "/usr/share/Wt/resources") };
const std::filesystem::path appRootPath{ config.getString("approot", "/usr/share/lms/approot") };
auto checkDirectoryExists{ [](const std::filesystem::path& directory, std::string_view settingName) {
std::string error;
const std::error_code ec{ checkDirectoryAccessible(directory) };
if (ec)
throw core::LmsException{ "Cannot access directory '" + directory.string() + "' specified in setting '" + std::string{ settingName } + "': " + ec.message() };
} };
checkDirectoryExists(workingDirectoryPath, "working-dir");
checkDirectoryExists(wtResourcesPath, "wt-resources");
checkDirectoryExists(appRootPath, "approot");
args.push_back(execPath);
args.push_back("--config=" + wtConfigPath.string());
args.push_back("--docroot=" + std::string{ config.getString("docroot", "/usr/share/lms/docroot/;/resources,/css,/images,/js,/favicon.ico") });
args.push_back("--approot=" + std::string{ config.getString("approot", "/usr/share/lms/approot") });
args.push_back("--approot=" + appRootPath.string());
args.push_back("--deploy-path=" + std::string{ config.getString("deploy-path", "/") });
if (!wtResourcesPath.empty())
args.push_back("--resources-dir=" + wtResourcesPath.string());