From be647d455a76f6a5fbb6bbdbd559a2dee398c99a Mon Sep 17 00:00:00 2001 From: Dave Lane <42013603+ReenigneArcher@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:50:01 -0500 Subject: [PATCH] fix(config): warn when an explicit config path is unusable --- src/config.cpp | 26 ++++++++++----- src/config.h | 3 +- src/config_file.h | 68 ++++++++++++++++++++++++++++++++++++++ src/main.cpp | 8 ++++- tests/unit/test_config.cpp | 39 ++++++++++++++++++++++ 5 files changed, 133 insertions(+), 11 deletions(-) create mode 100644 src/config_file.h diff --git a/src/config.cpp b/src/config.cpp index 77d2d466e..308af3919 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -24,6 +24,7 @@ // local includes #include "config.h" +#include "config_file.h" #include "entry_handler.h" #include "file_handler.h" #include "logging.h" @@ -2024,11 +2025,10 @@ namespace config { } #endif - /** - * @brief Parse serialized text into the corresponding runtime representation. - */ - int parse(int argc, char *argv[]) { + int parse(int argc, char *argv[], std::string &config_warning) { + config_warning.clear(); config_map_t cmd_vars; + bool explicit_config_file = false; #ifdef _WIN32 bool shortcut_launch = false; bool service_admin_launch = false; @@ -2066,6 +2066,7 @@ namespace config { auto pos = std::find(line, line_end, '='); if (pos == line_end) { sunshine.config_file = line; + explicit_config_file = true; } else { TUPLE_EL(var, 1, parse_option(line, line_end)); if (!var) { @@ -2090,13 +2091,20 @@ namespace config { // Create appdata folder if it does not exist file_handler::make_directory(platf::appdata().string()); - // Create empty config file if it does not exist - if (!fs::exists(sunshine.config_file)) { - std::ofstream {sunshine.config_file}; + std::string file_content; + if (explicit_config_file) { + auto result = read_explicit_config_file(sunshine.config_file); + file_content = std::move(result.contents); + config_warning = std::move(result.warning); + } else { + // Preserve the default config creation and loading behavior. + if (!fs::exists(sunshine.config_file)) { + std::ofstream {sunshine.config_file}; + } + file_content = file_handler::read_file(sunshine.config_file.c_str()); } - // Read config file - auto vars = parse_config(file_handler::read_file(sunshine.config_file.c_str())); + auto vars = parse_config(file_content); for (auto &[name, value] : cmd_vars) { vars.insert_or_assign(name, std::move(value)); diff --git a/src/config.h b/src/config.h index dc890e2ee..2d655fea7 100644 --- a/src/config.h +++ b/src/config.h @@ -431,9 +431,10 @@ namespace config { * * @param argc Number of command-line arguments. * @param argv Command-line argument vector. + * @param config_warning Warning to log after initialization if an explicit config path was unusable. * @return 0 on success; nonzero when command-line or configuration parsing fails. */ - int parse(int argc, char *argv[]); + int parse(int argc, char *argv[], std::string &config_warning); /** * @brief Parse Sunshine configuration text into key-value entries. * diff --git a/src/config_file.h b/src/config_file.h new file mode 100644 index 000000000..af76a870a --- /dev/null +++ b/src/config_file.h @@ -0,0 +1,68 @@ +/** + * @file src/config_file.h + * @brief Loading and diagnostics for an explicitly selected configuration file. + */ +#pragma once + +// standard includes +#include +#include +#include +#include +#include + +namespace config { + /** + * @brief Text or a diagnostic from loading an explicit config path. + */ + struct explicit_config_file_t { + std::string contents; ///< Configuration text; may be empty even on success. + std::string warning; ///< Nonempty when the explicit path could not supply a file. + }; + + /** + * @brief Read an explicit config file, creating it if missing. + * @details An empty file is valid. Never truncate an existing file. + * + * @param path Explicit configuration file path. + * @return Configuration text on success, or a warning on failure. + */ + inline explicit_config_file_t read_explicit_config_file(const std::string &path) { + if (path.empty()) { + return {{}, "The configuration file path argument is empty. Starting without a configuration file."}; + } + + const auto unavailable = [&] { + return explicit_config_file_t {{}, "Could not read or create configuration file [" + path + "]. Starting without a configuration file."}; + }; + + const std::filesystem::path config_path {path}; + std::error_code error; + const auto status = std::filesystem::status(config_path, error); + if (status.type() == std::filesystem::file_type::not_found) { + std::ofstream file {config_path, std::ios::app}; + file.close(); + if (!file) { + return unavailable(); + } + } else if (error || !std::filesystem::is_regular_file(status)) { + return unavailable(); + } + + std::ifstream file {config_path}; + if (!file) { + return unavailable(); + } + + explicit_config_file_t result; + std::array buffer; + while (file.read(buffer.data(), buffer.size())) { + result.contents.append(buffer.data(), buffer.size()); + } + if (file.bad() || !file.eof()) { + return unavailable(); + } + result.contents.append(buffer.data(), file.gcount()); + return result; + } +} // namespace config diff --git a/src/main.cpp b/src/main.cpp index 841e5c8c4..7ee67a9af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include // platform includes #ifdef __APPLE__ @@ -247,7 +248,8 @@ int main(int argc, char *argv[]) { mail::man = std::make_shared(); // parse config file - if (config::parse(argc, argv)) { + std::string config_warning; + if (config::parse(argc, argv, config_warning)) { return 0; } @@ -264,6 +266,10 @@ int main(int argc, char *argv[]) { // Log publisher metadata log_publisher_data(); + if (!config_warning.empty()) { + BOOST_LOG(warning) << config_warning; + } + // Log modified_config_settings config::log_config_settings(config::modified_config_settings, false); config::modified_config_settings.clear(); diff --git a/tests/unit/test_config.cpp b/tests/unit/test_config.cpp index f3bb7ceaf..94765d48d 100644 --- a/tests/unit/test_config.cpp +++ b/tests/unit/test_config.cpp @@ -15,6 +15,7 @@ // local includes #include +#include #include #include @@ -159,6 +160,44 @@ namespace { } // namespace +/** + * @brief An explicit empty CLI argument is not a valid config file. + */ +TEST(ConfigFileLoadingTest, RejectsAnExplicitEmptyPath) { + const auto loaded = config::read_explicit_config_file(""); + EXPECT_TRUE(loaded.contents.empty()); + EXPECT_NE(loaded.warning.find("path argument is empty"), std::string::npos); +} + +/** + * @brief A valid new path still creates a config that can subsequently be loaded. + */ +TEST_F(ConfigPersistenceTest, CreatesAndReadsExplicitConfigurationFile) { + const auto created = config::read_explicit_config_file(config_file().string()); + EXPECT_TRUE(created.warning.empty()); + EXPECT_TRUE(created.contents.empty()); + EXPECT_TRUE(std::filesystem::is_regular_file(config_file())); + + ASSERT_EQ(file_handler::write_file(config_file().string().c_str(), "encoder = software"), 0); + const auto loaded = config::read_explicit_config_file(config_file().string()); + EXPECT_TRUE(loaded.warning.empty()); + EXPECT_EQ(loaded.contents, "encoder = software"); +} + +/** + * @brief Paths that cannot provide a config are distinct from valid empty files. + */ +TEST_F(ConfigPersistenceTest, RejectsADirectoryAndAMissingConfigParent) { + const auto directory = config_file().parent_path().string(); + const auto missing_parent = (config_file() / "sunshine.conf").string(); + for (const auto &path : {directory, missing_parent}) { + const auto loaded = config::read_explicit_config_file(path); + EXPECT_TRUE(loaded.contents.empty()); + EXPECT_NE(loaded.warning.find(path), std::string::npos); + } + EXPECT_FALSE(std::filesystem::exists(config_file())); +} + TEST_F(ConfigPersistenceTest, AppendsMissingOptionWithoutReplacingExistingText) { ASSERT_EQ(file_handler::write_file(config_file().string().c_str(), "# retained comment"), 0);