Back to index

courier  0.68.2
maildirwatch.h
Go to the documentation of this file.
00001 #ifndef maildirwatch_h
00002 #define maildirwatch_h
00003 /*
00004 ** Copyright 2002 Double Precision, Inc.
00005 ** See COPYING for distribution information.
00006 */
00007 
00008 
00009 #ifdef  __cplusplus
00010 extern "C" {
00011 #endif
00012 
00013 #if HAVE_CONFIG_H
00014 #include "config.h"
00015 #endif
00016 
00017 /*
00018 ** These function leverage libfam.a to watch for maildir changes.
00019 **
00020 ** If libfam.a is not available, these functions are compiled to no-ops
00021 */
00022 
00023 #if HAVE_FAM
00024 #include <fam.h>
00025 #endif
00026 
00027 
00028 #if TIME_WITH_SYS_TIME
00029 #include      <sys/time.h>
00030 #include      <time.h>
00031 #else
00032 #if HAVE_SYS_TIME_H
00033 #include      <sys/time.h>
00034 #else
00035 #include      <time.h>
00036 #endif
00037 #endif
00038 
00039 #if HAVE_FAM
00040 struct maildirwatch_fam {
00041        FAMConnection fc;
00042        int broken;
00043        unsigned refcnt;
00044 };
00045 
00046 #endif
00047 
00048 struct maildirwatch {
00049        char *maildir;
00050 
00051 #if HAVE_FAM
00052        struct maildirwatch_fam *fam;
00053 #endif
00054        time_t now;
00055        time_t timeout;
00056 
00057 };
00058 
00059 #define WATCHDOTLOCK "tmp/courier.lock"
00060 
00061 #define KEYWORDDIR "courierimapkeywords"
00062 
00063 struct maildirwatch *maildirwatch_alloc(const char *maildir);
00064 
00065 void maildirwatch_free(struct maildirwatch *w);
00066        /*
00067        ** Wait for WATCHDOTLOCK to go away
00068        */
00069 
00070 void maildirwatch_cleanup();
00071        /* Final cleanup before prog terminates */
00072 
00073 int maildirwatch_unlock(struct maildirwatch *w, int nseconds);
00074 
00075        /*********** Wait for changes to new and cur subdirs ************/
00076 
00077        /* Caller must allocate the follownig structure: */
00078 
00079 struct maildirwatch_contents {
00080        struct maildirwatch *w;
00081 
00082 #if HAVE_FAM
00083        FAMRequest new_req;
00084        FAMRequest cur_req;
00085        FAMRequest courierimapkeywords_req;
00086 
00087        unsigned short endexists_received;
00088        unsigned short ack_received;
00089 
00090        unsigned short cancelled;
00091 
00092 #endif
00093 
00094 };
00095 
00096 /*
00097 ** maildirwatch_start() initiates the process of monitoring the maildir.
00098 ** The monitoring process does not get started right away, since FAM needs
00099 ** to acknowledge th monitoring requests first.
00100 **
00101 ** Returns: 0 - monitoring request sent.
00102 **          1 - FAM is not available, will fall back to 60 second polls.
00103 **         -1 - Fatal error.
00104 */
00105 
00106 int maildirwatch_start(struct maildirwatch *p,
00107                      struct maildirwatch_contents *w);
00108 
00109 /*
00110 ** Check if FAM started monitoring yet.
00111 **
00112 ** Returns: 1 - Monitoring has started, or we're in fallback mode.
00113 **          0 - Not yet, *fdret is initialized to file descriptor to wait on.
00114 **         -1 - A fatal error occured, fall back to polling mode.
00115 **
00116 ** maildirwatch_started() returns right away, without blocking.
00117 */
00118 
00119 int maildirwatch_started(struct maildirwatch_contents *w,
00120                       int *fdret);
00121 
00122 /*
00123 ** Check if maildir's contents have changed.
00124 **
00125 ** Returns: 0 - Monitoring in progress.  *changed set to non-zero if maildir
00126 **              was changed.
00127 **         -1 - Fatal error.
00128 **
00129 ** *fdret and *timeout get initialized to the file descriptor to wait on,
00130 ** and the requested timeout.  *fdret may be negative in polling mode.
00131 */
00132 
00133 int maildirwatch_check(struct maildirwatch_contents *w,
00134                      int *changed,
00135                      int *fdret,
00136                      int *timeout);
00137 
00138        /*
00139        ** Clean everything up.
00140        */
00141 void maildirwatch_end(struct maildirwatch_contents *w);
00142 
00143 
00144        /*
00145        ** Courier-IMAP compatible maildir lock.
00146        **
00147        ** Returns a non-NULL filename on success.  To unlock:
00148        **
00149        ** unlink(filename); free(filename);
00150        **
00151        ** A NULL return with tryAnyway != 0 means that the lock failed
00152        ** probably as a result of misconfigured FAM, or something.
00153        **
00154        */
00155 char *maildir_lock(const char *maildir,
00156                  struct maildirwatch *w, /* If NULL, we sleep() */
00157                  int *tryAnyway);
00158 
00159 #ifdef  __cplusplus
00160 }
00161 #endif
00162 
00163 #endif