diff --git a/src/config/config.cpp b/src/config/config.cpp index eafc1f4..80d28a0 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -46,17 +46,19 @@ namespace { throw std::runtime_error("Failed to initialize inotify:\n" "- " + std::string(strerror(errno))); - const int wd = inotify_add_watch(fd, file.c_str(), + const std::string parent = std::filesystem::path(file).parent_path().string(); + const int wd = inotify_add_watch(fd, parent.c_str(), IN_MODIFY | IN_CLOSE_WRITE | IN_MOVE_SELF); if (wd < 0) { close(fd); - throw std::runtime_error("Failed to add inotify watch for " + file + ":\n" + throw std::runtime_error("Failed to add inotify watch for " + parent + ":\n" "- " + std::string(strerror(errno))); } // watch for changes std::optional discard_until; + const std::string filename = std::filesystem::path(file).filename().string(); std::array buffer{}; while (true) { @@ -87,10 +89,14 @@ namespace { while (std::cmp_less(i, len)) { auto* event = reinterpret_cast(&buffer.at(i)); i += sizeof(inotify_event) + event->len; + if (event->len <= 0 || event->mask & IN_IGNORED) + continue; + + std::string name(reinterpret_cast(event->name)); + if (name != filename) + continue; // stall a bit, then mark as invalid - if (!discard_until.has_value()) - std::cerr << "lsfg-vk: Configuration file changed, invalidating config...\n"; discard_until.emplace(std::chrono::steady_clock::now() + std::chrono::milliseconds(500)); } @@ -131,6 +137,7 @@ bool Config::loadAndWatchConfig(const std::string& file) { } bool Config::updateConfig(const std::string& file) { + globalConf.valid->store(true, std::memory_order_relaxed); if (!std::filesystem::exists(file)) return false; @@ -190,7 +197,6 @@ bool Config::updateConfig(const std::string& file) { } // store configurations - global.valid->store(true, std::memory_order_release); globalConf = global; gameConfs = std::move(games); return true; diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 855d5a9..f58532e 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -229,6 +229,6 @@ std::string Utils::getConfigFile() { return{configFile}; const char* homePath = std::getenv("HOME"); if (homePath && *homePath != '\0') - return std::string(homePath) + "/.config/lsfg-vk.toml"; - return "/etc/lsfg-vk.toml"; + return std::string(homePath) + "/.config/lsfg-vk/conf.toml"; + return "/etc/lsfg-vk/conf.toml"; }