Skip to content

Commit

Permalink
conf: fix reloading on IN_CREATE
Browse files Browse the repository at this point in the history
If the config file is a symlink, wayfire attempts to watch the parent
directory so that, if replaced via a subsequent `ln -sf`, it'll still
be notified. When notified, however, wayfire doesn't immediately
reload the config file. Rather, as this event may be triggered by any
file creation in the directory, we first make sure that the config file
really is the one being created anew.

The filename from inotify is stored as a flexible array member in the
event struct, though, and this was somehow causing problems when
compared with the known std::string basename.

Fixes #2210.
  • Loading branch information
atalii committed Mar 13, 2024
1 parent 3aa8c3f commit 767163d
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/default-config-backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <wayfire/plugin.hpp>
#include <wayfire/core.hpp>

#include <cstring>
#include <sys/inotify.h>
#include <filesystem>
#include <unistd.h>
Expand Down Expand Up @@ -58,18 +59,22 @@ static int handle_config_updated(int fd, uint32_t mask, void *data)
ptr += sizeof(inotify_event) + event->len)
{
event = reinterpret_cast<inotify_event*>(ptr);
// We reload in two main cases:
// We reload in two cases:
//
// - Config file itself was modified
// - Config file was created inside parent directory
should_reload |=
(event->wd == wd_cfg_file) || (cfg_file_basename == event->name);
// 1. The config file itself was modified, or...
should_reload |= event->wd == wd_cfg_file;
// 2. The config file was moved/created inside the parent directory.
if (event->len > 0) {
// This is UB unless event->len > 0.
auto name_matches = strcmp(cfg_file_basename.c_str(), event->name);

if (name_matches) {
inotify_rm_watch(fd, wd_cfg_file);
wd_cfg_file =
inotify_add_watch(fd, (config_dir + "/" + cfg_file_basename).c_str(), IN_CLOSE_WRITE);
}

if ((event->name == cfg_file_basename) && (event->wd == wd_cfg_dir))
{
inotify_rm_watch(fd, wd_cfg_file);
wd_cfg_file =
inotify_add_watch(fd, (config_dir + "/" + cfg_file_basename).c_str(), IN_CLOSE_WRITE);
should_reload |= name_matches;
}
}

Expand Down

0 comments on commit 767163d

Please sign in to comment.