2017-05-17 20:38:34 +02:00
|
|
|
/* Copyright 2017 Max Voit, Bert Muennich
|
2023-01-15 10:26:46 +01:00
|
|
|
* Copyright 2022-2023 nsxiv contributors
|
2017-01-26 22:18:32 +01:00
|
|
|
*
|
2021-09-16 11:32:59 +02:00
|
|
|
* This file is a part of nsxiv.
|
2017-01-26 22:18:32 +01:00
|
|
|
*
|
2021-09-16 11:32:59 +02:00
|
|
|
* nsxiv is free software; you can redistribute it and/or modify
|
2017-01-26 22:18:32 +01:00
|
|
|
* it under the terms of the GNU General Public License as published
|
|
|
|
* by the Free Software Foundation; either version 2 of the License,
|
|
|
|
* or (at your option) any later version.
|
|
|
|
*
|
2021-09-16 11:32:59 +02:00
|
|
|
* nsxiv is distributed in the hope that it will be useful,
|
2017-01-26 22:18:32 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-09-16 11:32:59 +02:00
|
|
|
* along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-26 22:18:32 +01:00
|
|
|
*/
|
|
|
|
|
2021-09-16 11:32:59 +02:00
|
|
|
#include "nsxiv.h"
|
2017-10-16 21:10:35 +02:00
|
|
|
|
2022-04-27 03:43:27 +02:00
|
|
|
#if HAVE_INOTIFY
|
|
|
|
|
2022-07-30 10:05:10 +02:00
|
|
|
#include <assert.h>
|
2017-05-17 20:14:36 +02:00
|
|
|
#include <errno.h>
|
2017-01-26 22:18:32 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/inotify.h>
|
2022-06-16 09:39:03 +02:00
|
|
|
#include <unistd.h>
|
2017-01-26 22:18:32 +01:00
|
|
|
|
2023-02-10 06:35:53 +01:00
|
|
|
static struct {
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
|
|
|
} scratch;
|
2021-10-11 05:07:18 +02:00
|
|
|
|
2017-05-17 20:16:16 +02:00
|
|
|
void arl_init(arl_t *arl)
|
|
|
|
{
|
|
|
|
arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
|
|
|
|
arl->wd_dir = arl->wd_file = -1;
|
|
|
|
if (arl->fd == -1)
|
|
|
|
error(0, 0, "Could not initialize inotify, no automatic image reloading");
|
|
|
|
}
|
|
|
|
|
2017-05-17 20:07:32 +02:00
|
|
|
CLEANUP void arl_cleanup(arl_t *arl)
|
2017-01-26 22:18:32 +01:00
|
|
|
{
|
2017-05-17 20:14:20 +02:00
|
|
|
if (arl->fd != -1)
|
|
|
|
close(arl->fd);
|
2022-07-30 10:05:10 +02:00
|
|
|
free(scratch.buf);
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 20:16:16 +02:00
|
|
|
static void rm_watch(int fd, int *wd)
|
2017-05-17 20:07:32 +02:00
|
|
|
{
|
2017-05-17 20:16:16 +02:00
|
|
|
if (*wd != -1) {
|
|
|
|
inotify_rm_watch(fd, *wd);
|
|
|
|
*wd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
|
|
|
|
{
|
|
|
|
*wd = inotify_add_watch(fd, path, mask);
|
|
|
|
if (*wd == -1)
|
|
|
|
error(0, errno, "inotify: %s", path);
|
|
|
|
}
|
|
|
|
|
2022-07-30 10:05:10 +02:00
|
|
|
static char *arl_scratch_push(const char *filepath, size_t len)
|
2017-05-17 20:16:16 +02:00
|
|
|
{
|
2022-07-30 10:05:10 +02:00
|
|
|
if (scratch.len < len + 1) {
|
|
|
|
scratch.len = len + 1;
|
|
|
|
scratch.buf = erealloc(scratch.buf, scratch.len);
|
|
|
|
}
|
|
|
|
scratch.buf[len] = '\0';
|
|
|
|
return memcpy(scratch.buf, filepath, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void arl_add(arl_t *arl, const char *filepath)
|
|
|
|
{
|
|
|
|
char *base, *dir;
|
2017-05-17 20:07:32 +02:00
|
|
|
|
2017-05-17 20:13:32 +02:00
|
|
|
if (arl->fd == -1)
|
2017-05-17 20:07:32 +02:00
|
|
|
return;
|
|
|
|
|
2017-05-17 20:16:16 +02:00
|
|
|
rm_watch(arl->fd, &arl->wd_dir);
|
|
|
|
rm_watch(arl->fd, &arl->wd_file);
|
|
|
|
add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
|
|
|
|
|
2022-07-30 10:05:10 +02:00
|
|
|
base = strrchr(filepath, '/');
|
2024-06-12 23:34:11 +02:00
|
|
|
assert(base != NULL && "filepath must be result of realpath(3)");
|
2024-06-16 23:07:33 +02:00
|
|
|
dir = arl_scratch_push(filepath, MAX(base - filepath, 1));
|
2022-07-30 10:05:10 +02:00
|
|
|
add_watch(arl->fd, &arl->wd_dir, dir, IN_CREATE | IN_MOVED_TO);
|
|
|
|
arl->filename = arl_scratch_push(base + 1, strlen(base + 1));
|
2017-05-17 20:07:32 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 20:16:16 +02:00
|
|
|
bool arl_handle(arl_t *arl)
|
2017-01-26 22:18:32 +01:00
|
|
|
{
|
2017-05-17 20:07:32 +02:00
|
|
|
bool reload = false;
|
2017-01-26 22:18:32 +01:00
|
|
|
char *ptr;
|
2018-02-18 13:13:11 +01:00
|
|
|
const struct inotify_event *e;
|
2022-07-30 10:05:10 +02:00
|
|
|
/* inotify_event aligned buffer */
|
2023-02-10 06:35:53 +01:00
|
|
|
static union {
|
|
|
|
char d[4096];
|
|
|
|
struct inotify_event e;
|
|
|
|
} buf;
|
2017-05-17 20:12:22 +02:00
|
|
|
|
2021-10-11 05:07:18 +02:00
|
|
|
while (true) {
|
2017-05-17 20:15:35 +02:00
|
|
|
ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
|
2017-01-26 22:18:32 +01:00
|
|
|
|
2017-05-17 20:14:36 +02:00
|
|
|
if (len == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
2018-02-18 13:13:11 +01:00
|
|
|
for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*e) + e->len) {
|
2023-02-10 06:35:53 +01:00
|
|
|
e = (const struct inotify_event *)ptr;
|
2018-02-18 13:13:11 +01:00
|
|
|
if (e->wd == arl->wd_file && (e->mask & IN_CLOSE_WRITE)) {
|
2017-05-17 20:07:32 +02:00
|
|
|
reload = true;
|
2018-02-18 13:13:11 +01:00
|
|
|
} else if (e->wd == arl->wd_file && (e->mask & IN_DELETE_SELF)) {
|
2017-05-17 20:16:16 +02:00
|
|
|
rm_watch(arl->fd, &arl->wd_file);
|
2018-02-18 13:13:11 +01:00
|
|
|
} else if (e->wd == arl->wd_dir && (e->mask & (IN_CREATE | IN_MOVED_TO))) {
|
|
|
|
if (STREQ(e->name, arl->filename))
|
2017-05-17 20:14:36 +02:00
|
|
|
reload = true;
|
|
|
|
}
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 20:07:32 +02:00
|
|
|
return reload;
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
2022-04-27 03:43:27 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
void arl_init(arl_t *arl)
|
|
|
|
{
|
|
|
|
arl->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void arl_cleanup(arl_t *arl)
|
|
|
|
{
|
2023-02-10 06:35:53 +01:00
|
|
|
(void)arl;
|
2022-04-27 03:43:27 +02:00
|
|
|
}
|
|
|
|
|
2022-07-30 10:05:10 +02:00
|
|
|
void arl_add(arl_t *arl, const char *filepath)
|
2022-04-27 03:43:27 +02:00
|
|
|
{
|
2023-02-10 06:35:53 +01:00
|
|
|
(void)arl;
|
|
|
|
(void)filepath;
|
2022-04-27 03:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool arl_handle(arl_t *arl)
|
|
|
|
{
|
2023-02-10 06:35:53 +01:00
|
|
|
(void)arl;
|
2022-04-27 03:43:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_INOTIFY */
|