1 #ifndef maildirwatch_h
2 #define maildirwatch_h
3 /*
4 ** Copyright 2002-2021 Double Precision, Inc.
5 ** See COPYING for distribution information.
6 */
7 
8 
9 #ifdef  __cplusplus
10 extern "C" {
11 #endif
12 
13 #if HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16 
17 /*
18 ** These function use inotify to watch for maildir changes.
19 */
20 
21 #if TIME_WITH_SYS_TIME
22 #include	<sys/time.h>
23 #include	<time.h>
24 #else
25 #if HAVE_SYS_TIME_H
26 #include	<sys/time.h>
27 #else
28 #include	<time.h>
29 #endif
30 #endif
31 
32 struct maildirwatch {
33 	char *maildir;
34 
35 #if HAVE_INOTIFY_INIT
36 	int inotify_fd;
37 #endif
38 	time_t now;
39 	time_t timeout;
40 
41 };
42 
43 #define WATCHDOTLOCK	"tmp/courier.lock"
44 
45 #define KEYWORDDIR "courierimapkeywords"
46 
47 struct maildirwatch *maildirwatch_alloc(const char *maildir);
48 
49 void maildirwatch_free(struct maildirwatch *w);
50 	/*
51 	** Wait for WATCHDOTLOCK to go away
52 	*/
53 
54 void maildirwatch_cleanup();
55 	/* Final cleanup before prog terminates */
56 
57 int maildirwatch_unlock(struct maildirwatch *w, int nseconds);
58 
59 	/*********** Wait for changes to new and cur subdirs ************/
60 
61 	/* Caller must allocate the follownig structure: */
62 
63 struct maildirwatch_contents {
64 	struct maildirwatch *w;
65 
66 #if HAVE_INOTIFY_INIT
67 	int handles[3];
68 #endif
69 };
70 
71 /*
72 ** maildirwatch_start() initiates the process of monitoring the maildir.
73 **
74 ** Returns: 0 - monitoring started.
75 **          1 - inotify not available, will fall back to 60 second polls.
76 **         -1 - Fatal error.
77 */
78 
79 int maildirwatch_start(struct maildirwatch *p,
80 		       struct maildirwatch_contents *w);
81 
82 /*
83 ** Check the status of inotify monitoring.
84 **
85 ** Returns: 1 - Monitoring has started, or we're in fallback mode.
86 **          0 - Not yet, *fdret is initialized to file descriptor to wait on.
87 **              (not used at this time).
88 **         -1 - A fatal error occured, fall back to polling mode.
89 **
90 ** maildirwatch_started() returns right away, without blocking.
91 */
92 
93 int maildirwatch_started(struct maildirwatch_contents *w,
94 			 int *fdret);
95 
96 /*
97 ** Check if maildir's contents have changed.
98 **
99 ** Returns: 0 - Monitoring in progress.  *changed set to non-zero if maildir
100 **              was changed.
101 **         -1 - Fatal error.
102 **
103 ** *fdret and *timeout get initialized to the file descriptor to wait on,
104 ** and the requested timeout.  *fdret may be negative in polling mode, this
105 ** should be interpreted as: if *changed is not set, sleep for this period of
106 ** time.
107 */
108 
109 int maildirwatch_check(struct maildirwatch_contents *w,
110 		       int *changed,
111 		       int *fdret,
112 		       int *timeout);
113 
114 	/*
115 	** Clean everything up.
116 	*/
117 void maildirwatch_end(struct maildirwatch_contents *w);
118 
119 
120 	/*
121 	** Courier-IMAP compatible maildir lock.
122 	**
123 	** Returns a non-NULL filename on success.  To unlock:
124 	**
125 	** unlink(filename); free(filename);
126 	**
127 	** A NULL return with tryAnyway != 0 means that the lock failed
128 	** probably as a result of misconfigured FAM, or something.
129 	**
130 	*/
131 char *maildir_lock(const char *maildir,
132 		   struct maildirwatch *w, /* If NULL, we sleep() */
133 		   int *tryAnyway);
134 
135 #ifdef  __cplusplus
136 }
137 #endif
138 
139 #endif
140