fix(config): warn when an explicit config path is unusable

This commit is contained in:
Dave Lane committed 2026-11-19 15:50:01 -05:00
1 parent 4fe14218bd
commit be647d455a
5 files changed
+133 -11

No files matched your search

+17 -9
View File
@@ -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));
+2 -1
View File
@@ -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.
*
+68
View File
@@ -0,0 +1,68 @@
/**
* @file src/config_file.h
* @brief Loading and diagnostics for an explicitly selected configuration file.
*/
#pragma once
// standard includes
#include <array>
#include <filesystem>
#include <fstream>
#include <string>
#include <system_error>
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<char, 4096> 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
+7 -1
View File
@@ -10,6 +10,7 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
// platform includes
#ifdef __APPLE__
@@ -247,7 +248,8 @@ int main(int argc, char *argv[]) {
mail::man = std::make_shared<safe::mail_raw_t>();
// 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();
+39
View File
@@ -15,6 +15,7 @@
// local includes
#include <src/config.h>
#include <src/config_file.h>
#include <src/file_handler.h>
#include <src/utility.h>
@@ -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);