NeoMutt  2023-03-22
Teaching an old dog new tricks
DOXYGEN
monitor.h File Reference

Monitor files for changes. More...

#include <stdbool.h>
+ Include dependency graph for monitor.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int mutt_monitor_add (struct Mailbox *m)
 Add a watch for a mailbox. More...
 
int mutt_monitor_remove (struct Mailbox *m)
 Remove a watch for a mailbox. More...
 
int mutt_monitor_poll (void)
 Check for filesystem changes. More...
 

Variables

bool MonitorFilesChanged
 true after a monitored file has changed More...
 
bool MonitorContextChanged
 true after the current mailbox has changed More...
 

Detailed Description

Monitor files for changes.

Authors
  • Gero Treuner

This program is distributed in the hope that it will be useful, 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 along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file monitor.h.

Function Documentation

◆ mutt_monitor_add()

int mutt_monitor_add ( struct Mailbox m)

Add a watch for a mailbox.

Parameters
mMailbox to watch
Return values
0success: new or already existing monitor
-1failed: no mailbox, inaccessible file, create monitor/watcher failed

If m is NULL, try to get the current mailbox from the Index.

Definition at line 480 of file monitor.c.

481{
482 struct MonitorInfo info;
483 monitor_info_init(&info);
484
485 int rc = 0;
486 enum ResolveResult desc = monitor_resolve(&info, m);
487 if (desc != RESOLVE_RES_OK_NOTEXISTING)
488 {
489 if (!m && (desc == RESOLVE_RES_OK_EXISTING))
490 MonitorContextDescriptor = info.monitor->desc;
491 rc = (desc == RESOLVE_RES_OK_EXISTING) ? 0 : -1;
492 goto cleanup;
493 }
494
495 uint32_t mask = info.is_dir ? INOTIFY_MASK_DIR : INOTIFY_MASK_FILE;
496 if (((INotifyFd == -1) && (monitor_init() == -1)) ||
497 ((desc = inotify_add_watch(INotifyFd, info.path, mask)) == -1))
498 {
499 mutt_debug(LL_DEBUG2, "inotify_add_watch failed for '%s', errno=%d %s\n",
500 info.path, errno, strerror(errno));
501 rc = -1;
502 goto cleanup;
503 }
504
505 mutt_debug(LL_DEBUG3, "inotify_add_watch descriptor=%d for '%s'\n", desc, info.path);
506 if (!m)
508
509 monitor_new(&info, desc);
510
511cleanup:
512 monitor_info_free(&info);
513 return rc;
514}
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_DEBUG3
Log at debug level 3.
Definition: logging.h:42
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
#define INOTIFY_MASK_DIR
Definition: monitor.c:60
static int MonitorContextDescriptor
Definition: monitor.c:58
static int INotifyFd
Definition: monitor.c:52
static enum ResolveResult monitor_resolve(struct MonitorInfo *info, struct Mailbox *m)
Get the monitor for a mailbox.
Definition: monitor.c:329
static struct Monitor * monitor_new(struct MonitorInfo *info, int descriptor)
Create a new file monitor.
Definition: monitor.c:205
ResolveResult
Results for the Monitor functions.
Definition: monitor.c:69
@ RESOLVE_RES_OK_NOTEXISTING
File exists, no monitor is attached.
Definition: monitor.c:73
@ RESOLVE_RES_OK_EXISTING
File exists, monitor is already attached.
Definition: monitor.c:74
static int monitor_init(void)
Set up file monitoring.
Definition: monitor.c:156
static void monitor_info_init(struct MonitorInfo *info)
Set up a file monitor.
Definition: monitor.c:225
#define INOTIFY_MASK_FILE
Definition: monitor.c:61
static void monitor_info_free(struct MonitorInfo *info)
Shutdown a file monitor.
Definition: monitor.c:234
Information about a monitored file.
Definition: monitor.c:94
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_monitor_remove()

int mutt_monitor_remove ( struct Mailbox m)

Remove a watch for a mailbox.

Parameters
mMailbox
Return values
0monitor removed (not shared)
1monitor not removed (shared)
2no monitor

If m is NULL, try to get the current mailbox from the Index.

Definition at line 525 of file monitor.c.

526{
527 struct MonitorInfo info, info2;
528 int rc = 0;
529
530 monitor_info_init(&info);
531 monitor_info_init(&info2);
532
533 if (!m)
534 {
536 MonitorContextChanged = false;
537 }
538
540 {
541 rc = 2;
542 goto cleanup;
543 }
544
545 struct Mailbox *m_cur = get_current_mailbox();
546 if (m_cur)
547 {
548 if (m)
549 {
550 if ((monitor_resolve(&info2, NULL) == RESOLVE_RES_OK_EXISTING) &&
551 (info.st_ino == info2.st_ino) && (info.st_dev == info2.st_dev))
552 {
553 rc = 1;
554 goto cleanup;
555 }
556 }
557 else
558 {
559 if (mailbox_find(m_cur->realpath))
560 {
561 rc = 1;
562 goto cleanup;
563 }
564 }
565 }
566
567 inotify_rm_watch(info.monitor->desc, INotifyFd);
568 mutt_debug(LL_DEBUG3, "inotify_rm_watch for '%s' descriptor=%d\n", info.path,
569 info.monitor->desc);
570
571 monitor_delete(info.monitor);
573
574cleanup:
575 monitor_info_free(&info);
576 monitor_info_free(&info2);
577 return rc;
578}
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:618
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:139
static void monitor_delete(struct Monitor *monitor)
Free a file monitor.
Definition: monitor.c:243
static void monitor_check_free(void)
Close down file monitoring.
Definition: monitor.c:188
bool MonitorContextChanged
true after the current mailbox has changed
Definition: monitor.c:50
A mailbox.
Definition: mailbox.h:79
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_monitor_poll()

int mutt_monitor_poll ( void  )

Check for filesystem changes.

Return values
-3unknown/unexpected events: poll timeout / fds not handled by us
-2monitor detected changes, no STDIN input
-1error (see errno)
0(1) input ready from STDIN, or (2) monitoring inactive -> no poll()

Wait for I/O ready file descriptors or signals.

MonitorFilesChanged also reflects changes to monitored files.

Only STDIN and INotify file handles currently expected/supported. More would ask for common infrastructure (sockets?).

Definition at line 398 of file monitor.c.

399{
400 int rc = 0;
401 char buf[EVENT_BUFLEN] __attribute__((aligned(__alignof__(struct inotify_event))));
402
403 MonitorFilesChanged = false;
404
405 if (INotifyFd != -1)
406 {
407 int fds = poll(PollFds, PollFdsCount, MuttGetchTimeout);
408
409 if (fds == -1)
410 {
411 rc = -1;
412 if (errno != EINTR)
413 {
414 mutt_debug(LL_DEBUG2, "poll() failed, errno=%d %s\n", errno, strerror(errno));
415 }
416 }
417 else
418 {
419 bool input_ready = false;
420 for (int i = 0; fds && (i < PollFdsCount); i++)
421 {
422 if (PollFds[i].revents)
423 {
424 fds--;
425 if (PollFds[i].fd == 0)
426 {
427 input_ready = true;
428 }
429 else if (PollFds[i].fd == INotifyFd)
430 {
431 MonitorFilesChanged = true;
432 mutt_debug(LL_DEBUG3, "file change(s) detected\n");
433 char *ptr = buf;
434 const struct inotify_event *event = NULL;
435
436 while (true)
437 {
438 int len = read(INotifyFd, buf, sizeof(buf));
439 if (len == -1)
440 {
441 if (errno != EAGAIN)
442 {
443 mutt_debug(LL_DEBUG2, "read inotify events failed, errno=%d %s\n",
444 errno, strerror(errno));
445 }
446 break;
447 }
448
449 while (ptr < (buf + len))
450 {
451 event = (const struct inotify_event *) ptr;
452 mutt_debug(LL_DEBUG3, "+ detail: descriptor=%d mask=0x%x\n",
453 event->wd, event->mask);
454 if (event->mask & IN_IGNORED)
455 monitor_handle_ignore(event->wd);
456 else if (event->wd == MonitorContextDescriptor)
458 ptr += sizeof(struct inotify_event) + event->len;
459 }
460 }
461 }
462 }
463 }
464 if (!input_ready)
465 rc = MonitorFilesChanged ? -2 : -3;
466 }
467 }
468
469 return rc;
470}
int MuttGetchTimeout
Timeout in ms for mutt_getch()
Definition: curs_lib.c:123
static int monitor_handle_ignore(int desc)
Listen for when a backup file is closed.
Definition: monitor.c:271
static size_t PollFdsCount
Definition: monitor.c:54
bool MonitorFilesChanged
true after a monitored file has changed
Definition: monitor.c:49
#define EVENT_BUFLEN
Definition: monitor.c:63
static struct pollfd * PollFds
Definition: monitor.c:56
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ MonitorFilesChanged

bool MonitorFilesChanged
extern

true after a monitored file has changed

Definition at line 49 of file monitor.c.

◆ MonitorContextChanged

bool MonitorContextChanged
extern

true after the current mailbox has changed

Definition at line 50 of file monitor.c.