Skip to content

Commit

Permalink
fix: Correctly handle default configuration when user dirs do not exi…
Browse files Browse the repository at this point in the history
…st (#222)

We assumed that the user configuration directory hierarchy always
exists, but with a completely fresh installation, it does not.

Consequently, populating the default configuration silently failed
and the environment is broken.

To resolve this issue, we now check to see if the directory the
configuration file lives in exists, and we create it if it does not.

Additionally, we now support populating a full set of default
configuration in case multiple files and folders are required for
initial configuration.
  • Loading branch information
Conan-Kudo committed Aug 28, 2024
1 parent 6b5eb23 commit b4fa88e
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ using namespace miracle;

namespace
{
const char* MIRACLE_USR_SHARE_DIR = "/usr/share/miracle-wm/config/default.yaml";
const char* MIRACLE_DEFAULT_CONFIG_FILE = "/usr/share/miracle-wm/config/default.yaml";
const char* MIRACLE_DEFAULT_CONFIG_DIR = "/usr/share/miracle-wm/default-config";

int program_exists(std::string const& name)
{
Expand Down Expand Up @@ -218,11 +219,23 @@ void FilesystemConfiguration::_init(std::optional<StartupApp> const& startup_app
mir::log_info("Configuration file path is: %s", config_path.c_str());
if (!std::filesystem::exists(config_path))
{
if (std::filesystem::exists(MIRACLE_USR_SHARE_DIR))

if (!std::filesystem::exists(std::filesystem::path(config_path).parent_path()))
{
mir::log_info("Configuration directory path missing, creating it now");
std::filesystem::create_directories(std::filesystem::path(config_path).parent_path());
}
if (std::filesystem::exists(MIRACLE_DEFAULT_CONFIG_DIR))
{
mir::log_info("Configuration hierarchy being copied from %s", MIRACLE_DEFAULT_CONFIG_DIR);
const auto fs_copyopts = std::filesystem::copy_options::recursive;
std::filesystem::copy(MIRACLE_DEFAULT_CONFIG_DIR, std::filesystem::path(config_path).parent_path(), fs_copyopts);
}
else if (std::filesystem::exists(MIRACLE_DEFAULT_CONFIG_FILE))
{
mir::log_info("Configuration being copied from %s", MIRACLE_USR_SHARE_DIR);
mir::log_info("Configuration being copied from %s", MIRACLE_DEFAULT_CONFIG_FILE);
std::filesystem::copy_file(
MIRACLE_USR_SHARE_DIR,
MIRACLE_DEFAULT_CONFIG_FILE,
config_path);
}
else
Expand Down

0 comments on commit b4fa88e

Please sign in to comment.