Krolyxon
Commit 94d531f

Branches

Refs →

nsxiv94d531f

autoreload: simplify and cleanup (#342)

the current code is quite hacky and complex as it mixes multiple pointers. all
of this complexity is unnecessary. drop it by introducing an explicit scratch
buffer instead of implicitly abusing `arl->filename` as one. this also reduces
some unnecessary allocation overhead.

additionally, the argument to arl_setup must be the result of `realpath(3)` (as
commented in `nsxiv.h`). instead of commenting it, assert it.

and lastly, rename `arl_setup` to `arl_add` since it's not doing any "setup"
but rather *adding* a file to watch.
Author
NRK
Date
2022-07-30 10:05 +0200
Parent
6d8ec5a
Commit
94d531fd827457cf71fe7f3808c7a2f531dceba5

+27 −20

3 files changed

  1. M autoreload.c +24 −17
  2. M main.c +1 −1
  3. M nsxiv.h +2 −2
M autoreload.c View
@@ -1,4 +1,5 @@
11 /* Copyright 2017 Max Voit, Bert Muennich
2+ * Copyright 2022 nsxiv contributors
23 *
34 * This file is a part of nsxiv.
45 *
@@ -20,16 +21,14 @@
2021
2122 #if HAVE_INOTIFY
2223
24+#include <assert.h>
2325 #include <errno.h>
2426 #include <stdlib.h>
2527 #include <string.h>
2628 #include <sys/inotify.h>
2729 #include <unistd.h>
2830
29−static union {
30− char d[4096]; /* aligned buffer */
31− struct inotify_event e;
32−} buf;
31+static struct { char *buf; size_t len; } scratch;
3332
3433 void arl_init(arl_t *arl)
3534 {
@@ -43,7 +42,7 @@ CLEANUP void arl_cleanup(arl_t *arl)
4342 {
4443 if (arl->fd != -1)
4544 close(arl->fd);
46− free(arl->filename);
45+ free(scratch.buf);
4746 }
4847
4948 static void rm_watch(int fd, int *wd)
@@ -61,26 +60,32 @@ static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
6160 error(0, errno, "inotify: %s", path);
6261 }
6362
64−void arl_setup(arl_t *arl, const char *filepath)
63+static char *arl_scratch_push(const char *filepath, size_t len)
6564 {
66− char *base = strrchr(filepath, '/');
65+ if (scratch.len < len + 1) {
66+ scratch.len = len + 1;
67+ scratch.buf = erealloc(scratch.buf, scratch.len);
68+ }
69+ scratch.buf[len] = '\0';
70+ return memcpy(scratch.buf, filepath, len);
71+}
72+
73+void arl_add(arl_t *arl, const char *filepath)
74+{
75+ char *base, *dir;
6776
6877 if (arl->fd == -1)
6978 return;
7079
7180 rm_watch(arl->fd, &arl->wd_dir);
7281 rm_watch(arl->fd, &arl->wd_file);
73−
7482 add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
7583
76− free(arl->filename);
77− arl->filename = estrdup(filepath);
78−
79− if (base != NULL) {
80− arl->filename[++base - filepath] = '\0';
81− add_watch(arl->fd, &arl->wd_dir, arl->filename, IN_CREATE | IN_MOVED_TO);
82− strcpy(arl->filename, base); /* NOLINT: basename will always be shorter than fullpath */
83− }
84+ base = strrchr(filepath, '/');
85+ assert(base != NULL); /* filepath must be result of `realpath(3)` */
86+ dir = arl_scratch_push(filepath, base - filepath);
87+ add_watch(arl->fd, &arl->wd_dir, dir, IN_CREATE | IN_MOVED_TO);
88+ arl->filename = arl_scratch_push(base + 1, strlen(base + 1));
8489 }
8590
8691 bool arl_handle(arl_t *arl)
@@ -88,6 +93,8 @@ bool arl_handle(arl_t *arl)
8893 bool reload = false;
8994 char *ptr;
9095 const struct inotify_event *e;
96+ /* inotify_event aligned buffer */
97+ static union { char d[4096]; struct inotify_event e; } buf;
9198
9299 while (true) {
93100 ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
@@ -124,7 +131,7 @@ void arl_cleanup(arl_t *arl)
124131 (void) arl;
125132 }
126133
127−void arl_setup(arl_t *arl, const char *filepath)
134+void arl_add(arl_t *arl, const char *filepath)
128135 {
129136 (void) arl;
130137 (void) filepath;
M main.c View
@@ -350,7 +350,7 @@ void load_image(int new)
350350
351351 close_info();
352352 open_info();
353− arl_setup(&arl, files[fileidx].path);
353+ arl_add(&arl, files[fileidx].path);
354354 title_dirty = true;
355355
356356 if (img.multi.cnt > 0 && img.multi.animate)
M nsxiv.h View
@@ -124,12 +124,12 @@ struct arl {
124124 int fd;
125125 int wd_dir;
126126 int wd_file;
127− char *filename;
127+ const char *filename;
128128 };
129129
130130 void arl_init(arl_t*);
131131 void arl_cleanup(arl_t*);
132−void arl_setup(arl_t*, const char* /* result of realpath(3) */);
132+void arl_add(arl_t*, const char* /* result of realpath(3) */);
133133 bool arl_handle(arl_t*);
134134
135135