monitor directory instead of file

This commit is contained in:
PancakeTAS 2025-07-17 01:53:32 +02:00 committed by Pancake
parent f8bdfd90de
commit 1d9b89122f
2 changed files with 13 additions and 7 deletions

View file

@ -46,17 +46,19 @@ namespace {
throw std::runtime_error("Failed to initialize inotify:\n" throw std::runtime_error("Failed to initialize inotify:\n"
"- " + std::string(strerror(errno))); "- " + 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); IN_MODIFY | IN_CLOSE_WRITE | IN_MOVE_SELF);
if (wd < 0) { if (wd < 0) {
close(fd); 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))); "- " + std::string(strerror(errno)));
} }
// watch for changes // watch for changes
std::optional<std::chrono::steady_clock::time_point> discard_until; std::optional<std::chrono::steady_clock::time_point> discard_until;
const std::string filename = std::filesystem::path(file).filename().string();
std::array<char, (sizeof(inotify_event) + NAME_MAX + 1) * 20> buffer{}; std::array<char, (sizeof(inotify_event) + NAME_MAX + 1) * 20> buffer{};
while (true) { while (true) {
@ -87,10 +89,14 @@ namespace {
while (std::cmp_less(i, len)) { while (std::cmp_less(i, len)) {
auto* event = reinterpret_cast<inotify_event*>(&buffer.at(i)); auto* event = reinterpret_cast<inotify_event*>(&buffer.at(i));
i += sizeof(inotify_event) + event->len; i += sizeof(inotify_event) + event->len;
if (event->len <= 0 || event->mask & IN_IGNORED)
continue;
std::string name(reinterpret_cast<char*>(event->name));
if (name != filename)
continue;
// stall a bit, then mark as invalid // 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() discard_until.emplace(std::chrono::steady_clock::now()
+ std::chrono::milliseconds(500)); + std::chrono::milliseconds(500));
} }
@ -131,6 +137,7 @@ bool Config::loadAndWatchConfig(const std::string& file) {
} }
bool Config::updateConfig(const std::string& file) { bool Config::updateConfig(const std::string& file) {
globalConf.valid->store(true, std::memory_order_relaxed);
if (!std::filesystem::exists(file)) if (!std::filesystem::exists(file))
return false; return false;
@ -190,7 +197,6 @@ bool Config::updateConfig(const std::string& file) {
} }
// store configurations // store configurations
global.valid->store(true, std::memory_order_release);
globalConf = global; globalConf = global;
gameConfs = std::move(games); gameConfs = std::move(games);
return true; return true;

View file

@ -229,6 +229,6 @@ std::string Utils::getConfigFile() {
return{configFile}; return{configFile};
const char* homePath = std::getenv("HOME"); const char* homePath = std::getenv("HOME");
if (homePath && *homePath != '\0') if (homePath && *homePath != '\0')
return std::string(homePath) + "/.config/lsfg-vk.toml"; return std::string(homePath) + "/.config/lsfg-vk/conf.toml";
return "/etc/lsfg-vk.toml"; return "/etc/lsfg-vk/conf.toml";
} }