drv_maildir.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2010-2013 Oswald Buddenhagen <ossi@users.sf.net>
  5. * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * As a special exception, mbsync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #include "driver.h"
  24. #include <assert.h>
  25. #include <limits.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <dirent.h>
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <sys/stat.h>
  34. #include <sys/file.h>
  35. #include <errno.h>
  36. #include <time.h>
  37. #include <utime.h>
  38. #ifdef __linux__
  39. # define LEGACY_FLOCK 1
  40. #endif
  41. #if !defined(_POSIX_SYNCHRONIZED_IO) || _POSIX_SYNCHRONIZED_IO <= 0
  42. # define fdatasync fsync
  43. #endif
  44. #ifdef USE_DB
  45. #include <db.h>
  46. #endif /* USE_DB */
  47. typedef struct maildir_store_conf {
  48. store_conf_t gen;
  49. char *inbox;
  50. #ifdef USE_DB
  51. int alt_map;
  52. #endif /* USE_DB */
  53. char info_delimiter;
  54. char failed;
  55. char *info_prefix, *info_stop; /* precalculated from info_delimiter */
  56. } maildir_store_conf_t;
  57. typedef struct maildir_message {
  58. message_t gen;
  59. char *base;
  60. } maildir_message_t;
  61. typedef struct maildir_store {
  62. store_t gen;
  63. int uvfd, uvok, nuid, is_inbox, fresh[3];
  64. int minuid, maxuid, newuid, nexcs, *excs;
  65. char *trash;
  66. #ifdef USE_DB
  67. DB *db;
  68. char *usedb;
  69. #endif /* USE_DB */
  70. wakeup_t lcktmr;
  71. } maildir_store_t;
  72. #ifdef USE_DB
  73. static DBT key, value; /* no need to be reentrant, and this saves lots of memset()s */
  74. #endif /* USE_DB */
  75. static struct flock lck;
  76. static int MaildirCount;
  77. static void ATTR_PRINTFLIKE(1, 2)
  78. debug( const char *msg, ... )
  79. {
  80. va_list va;
  81. va_start( va, msg );
  82. vdebug( DEBUG_SYNC, msg, va );
  83. va_end( va );
  84. }
  85. static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
  86. static uchar
  87. maildir_parse_flags( const char *info_prefix, const char *base )
  88. {
  89. const char *s;
  90. uint i;
  91. uchar flags;
  92. flags = 0;
  93. if ((s = strstr( base, info_prefix )))
  94. for (s += 3, i = 0; i < as(Flags); i++)
  95. if (strchr( s, Flags[i] ))
  96. flags |= (1 << i);
  97. return flags;
  98. }
  99. static char *
  100. maildir_join_path( const char *prefix, const char *box )
  101. {
  102. char *out, *p;
  103. int pl, bl, n;
  104. char c;
  105. pl = strlen( prefix );
  106. for (bl = 0, n = 0; (c = box[bl]); bl++)
  107. if (c == '/')
  108. n++;
  109. out = nfmalloc( pl + bl + n + 1 );
  110. memcpy( out, prefix, pl );
  111. p = out + pl;
  112. while ((c = *box++)) {
  113. *p++ = c;
  114. if (c == '/')
  115. *p++ = '.';
  116. }
  117. *p = 0;
  118. return out;
  119. }
  120. static int
  121. maildir_validate_path( maildir_store_conf_t *conf )
  122. {
  123. struct stat st;
  124. if (!conf->gen.path) {
  125. error( "Maildir error: store '%s' has no Path\n", conf->gen.name );
  126. conf->failed = FAIL_FINAL;
  127. return -1;
  128. }
  129. if (stat( conf->gen.path, &st ) || !S_ISDIR(st.st_mode)) {
  130. error( "Maildir error: cannot open store '%s'\n", conf->gen.path );
  131. conf->failed = FAIL_FINAL;
  132. return -1;
  133. }
  134. return 0;
  135. }
  136. static void lcktmr_timeout( void *aux );
  137. static void
  138. maildir_open_store( store_conf_t *gconf, const char *label ATTR_UNUSED,
  139. void (*cb)( store_t *ctx, void *aux ), void *aux )
  140. {
  141. maildir_store_conf_t *conf = (maildir_store_conf_t *)gconf;
  142. maildir_store_t *ctx;
  143. ctx = nfcalloc( sizeof(*ctx) );
  144. ctx->gen.conf = gconf;
  145. ctx->uvfd = -1;
  146. init_wakeup( &ctx->lcktmr, lcktmr_timeout, ctx );
  147. if (gconf->trash) {
  148. if (maildir_validate_path( conf ) < 0) {
  149. free( ctx );
  150. cb( 0, aux );
  151. return;
  152. }
  153. ctx->trash = maildir_join_path( gconf->path, gconf->trash );
  154. }
  155. cb( &ctx->gen, aux );
  156. }
  157. static void
  158. free_maildir_messages( message_t *msg )
  159. {
  160. message_t *tmsg;
  161. for (; (tmsg = msg); msg = tmsg) {
  162. tmsg = msg->next;
  163. free( ((maildir_message_t *)msg)->base );
  164. free( msg );
  165. }
  166. }
  167. static void
  168. maildir_cleanup( store_t *gctx )
  169. {
  170. maildir_store_t *ctx = (maildir_store_t *)gctx;
  171. free_maildir_messages( gctx->msgs );
  172. #ifdef USE_DB
  173. if (ctx->db)
  174. ctx->db->close( ctx->db, 0 );
  175. free( ctx->usedb );
  176. #endif /* USE_DB */
  177. free( gctx->path );
  178. free( ctx->excs );
  179. if (ctx->uvfd >= 0)
  180. close( ctx->uvfd );
  181. conf_wakeup( &ctx->lcktmr, -1 );
  182. }
  183. static void
  184. maildir_disown_store( store_t *gctx )
  185. {
  186. maildir_store_t *ctx = (maildir_store_t *)gctx;
  187. maildir_cleanup( gctx );
  188. wipe_wakeup( &ctx->lcktmr );
  189. free( ctx->trash );
  190. free_string_list( gctx->boxes );
  191. free( gctx );
  192. }
  193. static void
  194. maildir_cleanup_drv( void )
  195. {
  196. }
  197. static void
  198. maildir_invoke_bad_callback( store_t *ctx )
  199. {
  200. ctx->bad_callback( ctx->bad_callback_aux );
  201. }
  202. static int maildir_list_inbox( store_t *gctx, int flags, const char *basePath );
  203. static int maildir_list_path( store_t *gctx, int flags, const char *inbox );
  204. static int
  205. maildir_list_recurse( store_t *gctx, int isBox, int flags,
  206. const char *inbox, int inboxLen, const char *basePath, int basePathLen,
  207. char *path, int pathLen, char *name, int nameLen )
  208. {
  209. DIR *dir;
  210. int pl, nl;
  211. struct dirent *de;
  212. struct stat st;
  213. if (!(dir = opendir( path ))) {
  214. if (isBox && (errno == ENOENT || errno == ENOTDIR))
  215. return 0;
  216. sys_error( "Maildir error: cannot list %s", path );
  217. return -1;
  218. }
  219. while ((de = readdir( dir ))) {
  220. const char *ent = de->d_name;
  221. pl = pathLen + nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "%s", ent );
  222. if (inbox && equals( path, pl, inbox, inboxLen )) {
  223. /* Inbox nested into Path. List now if it won't be listed separately anyway. */
  224. if (!(flags & LIST_INBOX) && maildir_list_inbox( gctx, flags, 0 ) < 0) {
  225. closedir( dir );
  226. return -1;
  227. }
  228. } else if (basePath && equals( path, pl, basePath, basePathLen )) {
  229. /* Path nested into Inbox. List now if it won't be listed separately anyway. */
  230. if (!(flags & LIST_PATH) && maildir_list_path( gctx, flags, 0 ) < 0) {
  231. closedir( dir );
  232. return -1;
  233. }
  234. } else {
  235. if (*ent == '.') {
  236. if (!isBox)
  237. continue;
  238. if (!ent[1] || ent[1] == '.')
  239. continue;
  240. ent++;
  241. } else {
  242. if (isBox)
  243. continue;
  244. if (!nameLen && equals( ent, -1, "INBOX", 5 )) {
  245. path[pathLen] = 0;
  246. warn( "Maildir warning: ignoring INBOX in %s\n", path );
  247. continue;
  248. }
  249. }
  250. nl = nameLen + nfsnprintf( name + nameLen, _POSIX_PATH_MAX - nameLen, "%s", ent );
  251. path[pl++] = '/';
  252. nfsnprintf( path + pl, _POSIX_PATH_MAX - pl, "cur" );
  253. if (!stat( path, &st ) && S_ISDIR(st.st_mode))
  254. add_string_list( &gctx->boxes, name );
  255. path[pl] = 0;
  256. name[nl++] = '/';
  257. if (maildir_list_recurse( gctx, 1, flags, inbox, inboxLen, basePath, basePathLen, path, pl, name, nl ) < 0) {
  258. closedir( dir );
  259. return -1;
  260. }
  261. }
  262. }
  263. closedir (dir);
  264. return 0;
  265. }
  266. static int
  267. maildir_list_inbox( store_t *gctx, int flags, const char *basePath )
  268. {
  269. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  270. add_string_list( &gctx->boxes, "INBOX" );
  271. return maildir_list_recurse(
  272. gctx, 1, flags, 0, 0, basePath, basePath ? strlen( basePath ) - 1 : 0,
  273. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s/", ((maildir_store_conf_t *)gctx->conf)->inbox ),
  274. name, nfsnprintf( name, _POSIX_PATH_MAX, "INBOX/" ) );
  275. }
  276. static int
  277. maildir_list_path( store_t *gctx, int flags, const char *inbox )
  278. {
  279. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  280. if (maildir_validate_path( (maildir_store_conf_t *)gctx->conf ) < 0)
  281. return -1;
  282. return maildir_list_recurse(
  283. gctx, 0, flags, inbox, inbox ? strlen( inbox ) : 0, 0, 0,
  284. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s", gctx->conf->path ),
  285. name, 0 );
  286. }
  287. static void
  288. maildir_list_store( store_t *gctx, int flags,
  289. void (*cb)( int sts, void *aux ), void *aux )
  290. {
  291. if (((flags & LIST_PATH) && maildir_list_path( gctx, flags, ((maildir_store_conf_t *)gctx->conf)->inbox ) < 0) ||
  292. ((flags & LIST_INBOX) && maildir_list_inbox( gctx, flags, gctx->conf->path ) < 0)) {
  293. maildir_invoke_bad_callback( gctx );
  294. cb( DRV_CANCELED, aux );
  295. } else {
  296. cb( DRV_OK, aux );
  297. }
  298. }
  299. static const char *subdirs[] = { "cur", "new", "tmp" };
  300. typedef struct {
  301. char *base;
  302. int size;
  303. uint uid:31, recent:1;
  304. char tuid[TUIDL];
  305. } msg_t;
  306. typedef struct {
  307. msg_t *ents;
  308. int nents, nalloc;
  309. } msglist_t;
  310. static void
  311. maildir_free_scan( msglist_t *msglist )
  312. {
  313. int i;
  314. if (msglist->ents) {
  315. for (i = 0; i < msglist->nents; i++)
  316. free( msglist->ents[i].base );
  317. free( msglist->ents );
  318. }
  319. }
  320. #define _24_HOURS (3600 * 24)
  321. static int
  322. maildir_clear_tmp( char *buf, int bufsz, int bl )
  323. {
  324. DIR *dirp;
  325. struct dirent *entry;
  326. time_t now;
  327. struct stat st;
  328. memcpy( buf + bl, "tmp/", 5 );
  329. bl += 4;
  330. if (!(dirp = opendir( buf ))) {
  331. sys_error( "Maildir error: cannot list %s", buf );
  332. return DRV_BOX_BAD;
  333. }
  334. time( &now );
  335. while ((entry = readdir( dirp ))) {
  336. nfsnprintf( buf + bl, bufsz - bl, "%s", entry->d_name );
  337. if (stat( buf, &st )) {
  338. if (errno != ENOENT)
  339. sys_error( "Maildir error: cannot access %s", buf );
  340. } else if (S_ISREG(st.st_mode) && now - st.st_ctime >= _24_HOURS) {
  341. /* This should happen infrequently enough that it won't be
  342. * bothersome to the user to display when it occurs.
  343. */
  344. notice( "Maildir notice: removing stale file %s\n", buf );
  345. if (unlink( buf ) && errno != ENOENT)
  346. sys_error( "Maildir error: cannot remove %s", buf );
  347. }
  348. }
  349. closedir( dirp );
  350. return DRV_OK;
  351. }
  352. static int
  353. make_box_dir( char *buf, int bl )
  354. {
  355. char *p;
  356. if (!mkdir( buf, 0700 ) || errno == EEXIST)
  357. return 0;
  358. p = memrchr( buf, '/', bl - 1 );
  359. if (*(p + 1) != '.') {
  360. errno = ENOENT;
  361. return -1;
  362. }
  363. *p = 0;
  364. if (make_box_dir( buf, (int)(p - buf) ))
  365. return -1;
  366. *p = '/';
  367. return mkdir( buf, 0700 );
  368. }
  369. static int
  370. maildir_validate( const char *box, int create, maildir_store_t *ctx )
  371. {
  372. int i, bl, ret;
  373. struct stat st;
  374. char buf[_POSIX_PATH_MAX];
  375. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", box );
  376. if (stat( buf, &st )) {
  377. if (errno != ENOENT) {
  378. sys_error( "Maildir error: cannot access mailbox '%s'", box );
  379. return DRV_BOX_BAD;
  380. }
  381. if (!create)
  382. return DRV_BOX_BAD;
  383. if (make_box_dir( buf, bl )) {
  384. sys_error( "Maildir error: cannot create mailbox '%s'", box );
  385. ((maildir_store_conf_t *)ctx->gen.conf)->failed = FAIL_FINAL;
  386. maildir_invoke_bad_callback( &ctx->gen );
  387. return DRV_CANCELED;
  388. }
  389. } else if (!S_ISDIR(st.st_mode)) {
  390. notdir:
  391. error( "Maildir error: '%s' is no valid mailbox\n", box );
  392. return DRV_BOX_BAD;
  393. }
  394. for (i = 0; i < 3; i++) {
  395. memcpy( buf + bl, subdirs[i], 4 );
  396. if (stat( buf, &st )) {
  397. /* We always create new/ and tmp/ if they are missing. cur/ is the presence indicator. */
  398. if (!i && !create)
  399. return DRV_BOX_BAD;
  400. if (mkdir( buf, 0700 )) {
  401. sys_error( "Maildir error: cannot create directory %s", buf );
  402. return DRV_BOX_BAD;
  403. }
  404. ctx->fresh[i] = 1;
  405. } else if (!S_ISDIR(st.st_mode)) {
  406. goto notdir;
  407. } else {
  408. if (i == 2) {
  409. if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) != DRV_OK)
  410. return ret;
  411. }
  412. }
  413. }
  414. return DRV_OK;
  415. }
  416. #ifdef USE_DB
  417. static void
  418. make_key( const char *info_stop, DBT *tkey, char *name )
  419. {
  420. char *u = strpbrk( name, info_stop );
  421. tkey->data = name;
  422. tkey->size = u ? (size_t)(u - name) : strlen( name );
  423. }
  424. #endif /* USE_DB */
  425. static int
  426. maildir_store_uidval( maildir_store_t *ctx )
  427. {
  428. int n;
  429. #ifdef USE_DB
  430. int ret, uv[2];
  431. #endif
  432. char buf[128];
  433. #ifdef USE_DB
  434. if (ctx->db) {
  435. key.data = (void *)"UIDVALIDITY";
  436. key.size = 11;
  437. uv[0] = ctx->gen.uidvalidity;
  438. uv[1] = ctx->nuid;
  439. value.data = uv;
  440. value.size = sizeof(uv);
  441. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  442. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  443. return DRV_BOX_BAD;
  444. }
  445. if ((ret = ctx->db->sync( ctx->db, 0 ))) {
  446. ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" );
  447. return DRV_BOX_BAD;
  448. }
  449. } else
  450. #endif /* USE_DB */
  451. {
  452. n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid );
  453. lseek( ctx->uvfd, 0, SEEK_SET );
  454. if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n ) || (UseFSync && fdatasync( ctx->uvfd ))) {
  455. error( "Maildir error: cannot write UIDVALIDITY.\n" );
  456. return DRV_BOX_BAD;
  457. }
  458. }
  459. conf_wakeup( &ctx->lcktmr, 2 );
  460. return DRV_OK;
  461. }
  462. static int
  463. maildir_init_uidval( maildir_store_t *ctx )
  464. {
  465. ctx->gen.uidvalidity = time( 0 );
  466. ctx->nuid = 0;
  467. ctx->uvok = 0;
  468. #ifdef USE_DB
  469. if (ctx->db) {
  470. u_int32_t count;
  471. ctx->db->truncate( ctx->db, 0, &count, 0 );
  472. }
  473. #endif /* USE_DB */
  474. return maildir_store_uidval( ctx );
  475. }
  476. static int
  477. maildir_init_uidval_new( maildir_store_t *ctx )
  478. {
  479. notice( "Maildir notice: no UIDVALIDITY, creating new.\n" );
  480. return maildir_init_uidval( ctx );
  481. }
  482. static int
  483. maildir_uidval_lock( maildir_store_t *ctx )
  484. {
  485. int n;
  486. #ifdef USE_DB
  487. int ret;
  488. struct stat st;
  489. #endif
  490. char buf[128];
  491. if (pending_wakeup( &ctx->lcktmr )) {
  492. /* The unlock timer is active, so we are obviously already locked. */
  493. return DRV_OK;
  494. }
  495. #ifdef LEGACY_FLOCK
  496. /* This is legacy only */
  497. if (flock( ctx->uvfd, LOCK_EX ) < 0) {
  498. error( "Maildir error: cannot flock UIDVALIDITY.\n" );
  499. return DRV_BOX_BAD;
  500. }
  501. #endif
  502. /* This (theoretically) works over NFS. Let's hope nobody else did
  503. the same in the opposite order, as we'd deadlock then. */
  504. #if SEEK_SET != 0
  505. lck.l_whence = SEEK_SET;
  506. #endif
  507. lck.l_type = F_WRLCK;
  508. if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
  509. error( "Maildir error: cannot fcntl lock UIDVALIDITY.\n" );
  510. return DRV_BOX_BAD;
  511. }
  512. #ifdef USE_DB
  513. if (ctx->usedb) {
  514. if (fstat( ctx->uvfd, &st )) {
  515. sys_error( "Maildir error: cannot fstat UID database" );
  516. return DRV_BOX_BAD;
  517. }
  518. if (db_create( &ctx->db, 0, 0 )) {
  519. fputs( "Maildir error: db_create() failed\n", stderr );
  520. return DRV_BOX_BAD;
  521. }
  522. if ((ret = (ctx->db->open)( ctx->db, 0, ctx->usedb, 0, DB_HASH,
  523. st.st_size ? 0 : DB_CREATE | DB_TRUNCATE, 0 ))) {
  524. ctx->db->err( ctx->db, ret, "Maildir error: db->open(%s)", ctx->usedb );
  525. return DRV_BOX_BAD;
  526. }
  527. key.data = (void *)"UIDVALIDITY";
  528. key.size = 11;
  529. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  530. if (ret != DB_NOTFOUND) {
  531. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  532. return DRV_BOX_BAD;
  533. }
  534. return maildir_init_uidval_new( ctx );
  535. }
  536. ctx->gen.uidvalidity = ((int *)value.data)[0];
  537. ctx->nuid = ((int *)value.data)[1];
  538. } else
  539. #endif
  540. {
  541. lseek( ctx->uvfd, 0, SEEK_SET );
  542. if ((n = read( ctx->uvfd, buf, sizeof(buf) - 1 )) <= 0 ||
  543. (buf[n] = 0, sscanf( buf, "%d\n%d", &ctx->gen.uidvalidity, &ctx->nuid ) != 2)) {
  544. #if 1
  545. /* In a generic driver, resetting the UID validity would be the right thing.
  546. * But this would mess up the sync state completely. So better bail out and
  547. * give the user a chance to fix the mailbox. */
  548. if (n) {
  549. error( "Maildir error: cannot read UIDVALIDITY.\n" );
  550. return DRV_BOX_BAD;
  551. }
  552. #endif
  553. return maildir_init_uidval_new( ctx );
  554. }
  555. }
  556. ctx->uvok = 1;
  557. conf_wakeup( &ctx->lcktmr, 2 );
  558. return DRV_OK;
  559. }
  560. static void
  561. maildir_uidval_unlock( maildir_store_t *ctx )
  562. {
  563. #ifdef USE_DB
  564. if (ctx->db) {
  565. ctx->db->close( ctx->db, 0 );
  566. ctx->db = 0;
  567. }
  568. #endif /* USE_DB */
  569. lck.l_type = F_UNLCK;
  570. fcntl( ctx->uvfd, F_SETLK, &lck );
  571. #ifdef LEGACY_FLOCK
  572. /* This is legacy only */
  573. flock( ctx->uvfd, LOCK_UN );
  574. #endif
  575. }
  576. static void
  577. lcktmr_timeout( void *aux )
  578. {
  579. maildir_uidval_unlock( (maildir_store_t *)aux );
  580. }
  581. static int
  582. maildir_obtain_uid( maildir_store_t *ctx, int *uid )
  583. {
  584. int ret;
  585. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  586. return ret;
  587. *uid = ++ctx->nuid;
  588. return maildir_store_uidval( ctx );
  589. }
  590. #ifdef USE_DB
  591. static int
  592. maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
  593. {
  594. int ret;
  595. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  596. return ret;
  597. *uid = ++ctx->nuid;
  598. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  599. value.data = uid;
  600. value.size = sizeof(*uid);
  601. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  602. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  603. return DRV_BOX_BAD;
  604. }
  605. return maildir_store_uidval( ctx );
  606. }
  607. #endif
  608. static int
  609. maildir_compare( const void *l, const void *r )
  610. {
  611. msg_t *lm = (msg_t *)l, *rm = (msg_t *)r;
  612. char *ldot, *rdot, *ldot2, *rdot2, *lseq, *rseq;
  613. int ret, llen, rlen;
  614. if ((ret = lm->uid - rm->uid))
  615. return ret;
  616. /* No UID, so sort by arrival date. We should not do this, but we rely
  617. on the suggested unique file name scheme - we have no choice. */
  618. /* The first field are always the seconds. Alphabetical sort should be
  619. faster than numeric. */
  620. if (!(ldot = strchr( lm->base, '.' )) || !(rdot = strchr( rm->base, '.' )))
  621. goto stronly; /* Should never happen ... */
  622. llen = ldot - lm->base, rlen = rdot - rm->base;
  623. /* The shorter number is smaller. Really. This won't trigger with any
  624. mail created after Sep 9 2001 anyway. */
  625. if ((ret = llen - rlen))
  626. return ret;
  627. if ((ret = memcmp( lm->base, rm->base, llen )))
  628. return ret;
  629. ldot++, rdot++;
  630. if ((llen = strtol( ldot, &ldot2, 10 ))) {
  631. if (!(rlen = strtol( rdot, &rdot2, 10 )))
  632. goto stronly; /* Comparing apples to oranges ... */
  633. /* Classical PID specs */
  634. if ((ret = llen - rlen)) {
  635. retpid:
  636. /* Handle PID wraparound. This works only on systems
  637. where PIDs are not reused too fast */
  638. if (ret > 20000 || ret < -20000)
  639. ret = -ret;
  640. return ret;
  641. }
  642. return (*ldot2 != '_' ? 0 : atoi( ldot2 + 1 )) -
  643. (*rdot2 != '_' ? 0 : atoi( rdot2 + 1 ));
  644. }
  645. if (!(ldot2 = strchr( ldot, '.' )) || !(rdot2 = strchr( rdot, '.' )))
  646. goto stronly; /* Should never happen ... */
  647. llen = ldot2 - ldot, rlen = rdot2 - rdot;
  648. if (((lseq = memchr( ldot, '#', llen )) && (rseq = memchr( rdot, '#', rlen ))) ||
  649. ((lseq = memchr( ldot, 'M', llen )) && (rseq = memchr( rdot, 'M', rlen ))))
  650. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  651. if ((lseq = memchr( ldot, 'P', llen )) && (rseq = memchr( rdot, 'P', rlen ))) {
  652. if ((ret = atoi( lseq + 1 ) - atoi( rseq + 1 )))
  653. goto retpid;
  654. if ((lseq = memchr( ldot, 'Q', llen )) && (rseq = memchr( rdot, 'Q', rlen )))
  655. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  656. }
  657. stronly:
  658. /* Fall-back, so the sort order is defined at all */
  659. return strcmp( lm->base, rm->base );
  660. }
  661. static int
  662. maildir_scan( maildir_store_t *ctx, msglist_t *msglist )
  663. {
  664. maildir_store_conf_t *conf = (maildir_store_conf_t *)ctx->gen.conf;
  665. DIR *d;
  666. FILE *f;
  667. struct dirent *e;
  668. const char *u, *ru;
  669. #ifdef USE_DB
  670. DB *tdb;
  671. DBC *dbc;
  672. #endif /* USE_DB */
  673. msg_t *entry;
  674. int i, j, uid, bl, fnl, ret;
  675. time_t now, stamps[2];
  676. struct stat st;
  677. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  678. again:
  679. msglist->ents = 0;
  680. msglist->nents = msglist->nalloc = 0;
  681. ctx->gen.count = ctx->gen.recent = 0;
  682. if (ctx->uvok || ctx->maxuid == INT_MAX) {
  683. #ifdef USE_DB
  684. if (ctx->usedb) {
  685. if (db_create( &tdb, 0, 0 )) {
  686. fputs( "Maildir error: db_create() failed\n", stderr );
  687. return DRV_BOX_BAD;
  688. }
  689. if ((tdb->open)( tdb, 0, 0, 0, DB_HASH, DB_CREATE, 0 )) {
  690. fputs( "Maildir error: tdb->open() failed\n", stderr );
  691. bork:
  692. tdb->close( tdb, 0 );
  693. return DRV_BOX_BAD;
  694. }
  695. }
  696. #endif /* USE_DB */
  697. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", ctx->gen.path );
  698. restat:
  699. now = time( 0 );
  700. for (i = 0; i < 2; i++) {
  701. memcpy( buf + bl, subdirs[i], 4 );
  702. if (stat( buf, &st )) {
  703. sys_error( "Maildir error: cannot stat %s", buf );
  704. goto dfail;
  705. }
  706. if (st.st_mtime == now && !(DFlags & ZERODELAY) && !ctx->fresh[i]) {
  707. /* If the modification happened during this second, we wouldn't be able to
  708. * tell if there were further modifications during this second. So wait.
  709. * This has the nice side effect that we wait for "batches" of changes to
  710. * complete. On the downside, it can potentially block indefinitely. */
  711. notice( "Maildir notice: sleeping due to recent directory modification.\n" );
  712. sleep( 1 ); /* FIXME: should make this async */
  713. goto restat;
  714. }
  715. stamps[i] = st.st_mtime;
  716. }
  717. for (i = 0; i < 2; i++) {
  718. memcpy( buf + bl, subdirs[i], 4 );
  719. if (!(d = opendir( buf ))) {
  720. sys_error( "Maildir error: cannot list %s", buf );
  721. rfail:
  722. maildir_free_scan( msglist );
  723. dfail:
  724. #ifdef USE_DB
  725. if (ctx->usedb)
  726. tdb->close( tdb, 0 );
  727. #endif /* USE_DB */
  728. return DRV_BOX_BAD;
  729. }
  730. while ((e = readdir( d ))) {
  731. if (*e->d_name == '.')
  732. continue;
  733. ctx->gen.count++;
  734. ctx->gen.recent += i;
  735. #ifdef USE_DB
  736. if (ctx->usedb) {
  737. if (maildir_uidval_lock( ctx ) != DRV_OK)
  738. goto mbork;
  739. make_key( conf->info_stop, &key, e->d_name );
  740. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  741. if (ret != DB_NOTFOUND) {
  742. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  743. mbork:
  744. maildir_free_scan( msglist );
  745. closedir( d );
  746. goto bork;
  747. }
  748. uid = INT_MAX;
  749. } else {
  750. value.size = 0;
  751. if ((ret = tdb->put( tdb, 0, &key, &value, 0 ))) {
  752. tdb->err( tdb, ret, "Maildir error: tdb->put()" );
  753. goto mbork;
  754. }
  755. uid = *(int *)value.data;
  756. }
  757. } else
  758. #endif /* USE_DB */
  759. {
  760. uid = (ctx->uvok && (u = strstr( e->d_name, ",U=" ))) ? atoi( u + 3 ) : 0;
  761. if (!uid)
  762. uid = INT_MAX;
  763. }
  764. if (uid <= ctx->maxuid) {
  765. if (uid < ctx->minuid) {
  766. for (j = 0; j < ctx->nexcs; j++)
  767. if (ctx->excs[j] == uid)
  768. goto oke;
  769. continue;
  770. oke: ;
  771. }
  772. if (msglist->nalloc == msglist->nents) {
  773. msglist->nalloc = msglist->nalloc * 2 + 100;
  774. msglist->ents = nfrealloc( msglist->ents, msglist->nalloc * sizeof(msg_t) );
  775. }
  776. entry = &msglist->ents[msglist->nents++];
  777. entry->base = nfstrdup( e->d_name );
  778. entry->uid = uid;
  779. entry->recent = i;
  780. entry->size = 0;
  781. entry->tuid[0] = 0;
  782. }
  783. }
  784. closedir( d );
  785. }
  786. for (i = 0; i < 2; i++) {
  787. memcpy( buf + bl, subdirs[i], 4 );
  788. if (stat( buf, &st )) {
  789. sys_error( "Maildir error: cannot re-stat %s", buf );
  790. goto rfail;
  791. }
  792. if (st.st_mtime != stamps[i]) {
  793. /* Somebody messed with the mailbox since we started listing it. */
  794. #ifdef USE_DB
  795. if (ctx->usedb)
  796. tdb->close( tdb, 0 );
  797. #endif /* USE_DB */
  798. maildir_free_scan( msglist );
  799. goto again;
  800. }
  801. }
  802. #ifdef USE_DB
  803. if (ctx->usedb) {
  804. if (maildir_uidval_lock( ctx ) != DRV_OK)
  805. ;
  806. else if ((ret = ctx->db->cursor( ctx->db, 0, &dbc, 0 )))
  807. ctx->db->err( ctx->db, ret, "Maildir error: db->cursor()" );
  808. else {
  809. for (;;) {
  810. if ((ret = dbc->c_get( dbc, &key, &value, DB_NEXT ))) {
  811. if (ret != DB_NOTFOUND)
  812. ctx->db->err( ctx->db, ret, "Maildir error: db->c_get()" );
  813. break;
  814. }
  815. if (!equals( key.data, key.size, "UIDVALIDITY", 11 ) &&
  816. (ret = tdb->get( tdb, 0, &key, &value, 0 ))) {
  817. if (ret != DB_NOTFOUND) {
  818. tdb->err( tdb, ret, "Maildir error: tdb->get()" );
  819. break;
  820. }
  821. if ((ret = dbc->c_del( dbc, 0 ))) {
  822. ctx->db->err( ctx->db, ret, "Maildir error: db->c_del()" );
  823. break;
  824. }
  825. }
  826. }
  827. dbc->c_close( dbc );
  828. }
  829. tdb->close( tdb, 0 );
  830. }
  831. #endif /* USE_DB */
  832. qsort( msglist->ents, msglist->nents, sizeof(msg_t), maildir_compare );
  833. for (uid = i = 0; i < msglist->nents; i++) {
  834. entry = &msglist->ents[i];
  835. if (entry->uid != INT_MAX) {
  836. if (uid == entry->uid) {
  837. #if 1
  838. /* See comment in maildir_uidval_lock() why this is fatal. */
  839. error( "Maildir error: duplicate UID %d.\n", uid );
  840. maildir_free_scan( msglist );
  841. return DRV_BOX_BAD;
  842. #else
  843. notice( "Maildir notice: duplicate UID; changing UIDVALIDITY.\n");
  844. if ((ret = maildir_init_uid( ctx )) != DRV_OK) {
  845. maildir_free_scan( msglist );
  846. return ret;
  847. }
  848. maildir_free_scan( msglist );
  849. goto again;
  850. #endif
  851. }
  852. uid = entry->uid;
  853. if (uid > ctx->nuid) {
  854. /* In principle, we could just warn and top up nuid. However, getting into this
  855. * situation might indicate some serious trouble, so let's not make it worse. */
  856. error( "Maildir error: UID %d is beyond highest assigned UID %d.\n", uid, ctx->nuid );
  857. maildir_free_scan( msglist );
  858. return DRV_BOX_BAD;
  859. }
  860. if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
  861. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  862. #ifdef USE_DB
  863. } else if (ctx->usedb) {
  864. if ((ret = maildir_set_uid( ctx, entry->base, &uid )) != DRV_OK) {
  865. maildir_free_scan( msglist );
  866. return ret;
  867. }
  868. entry->uid = uid;
  869. if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
  870. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  871. #endif /* USE_DB */
  872. } else {
  873. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  874. maildir_free_scan( msglist );
  875. return ret;
  876. }
  877. entry->uid = uid;
  878. if ((u = strstr( entry->base, ",U=" )))
  879. for (ru = u + 3; isdigit( (uchar)*ru ); ru++);
  880. else
  881. u = ru = strchr( entry->base, conf->info_delimiter );
  882. fnl = (u ?
  883. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%.*s,U=%d%s", subdirs[entry->recent], (int)(u - entry->base), entry->base, uid, ru ) :
  884. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s,U=%d", subdirs[entry->recent], entry->base, uid ))
  885. + 1 - 4;
  886. memcpy( nbuf, buf, bl + 4 );
  887. nfsnprintf( nbuf + bl + 4, sizeof(nbuf) - bl - 4, "%s", entry->base );
  888. if (rename( nbuf, buf )) {
  889. if (errno != ENOENT) {
  890. sys_error( "Maildir error: cannot rename %s to %s", nbuf, buf );
  891. fail:
  892. maildir_free_scan( msglist );
  893. return DRV_BOX_BAD;
  894. }
  895. retry:
  896. maildir_free_scan( msglist );
  897. goto again;
  898. }
  899. free( entry->base );
  900. entry->base = nfmalloc( fnl );
  901. memcpy( entry->base, buf + bl + 4, fnl );
  902. }
  903. if (ctx->gen.opts & OPEN_SIZE) {
  904. if (stat( buf, &st )) {
  905. if (errno != ENOENT) {
  906. sys_error( "Maildir error: cannot stat %s", buf );
  907. goto fail;
  908. }
  909. goto retry;
  910. }
  911. entry->size = st.st_size;
  912. }
  913. if ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid) {
  914. if (!(f = fopen( buf, "r" ))) {
  915. if (errno != ENOENT) {
  916. sys_error( "Maildir error: cannot open %s", buf );
  917. goto fail;
  918. }
  919. goto retry;
  920. }
  921. while (fgets( nbuf, sizeof(nbuf), f )) {
  922. if (!nbuf[0] || nbuf[0] == '\n')
  923. break;
  924. if (starts_with( nbuf, -1, "X-TUID: ", 8 ) && nbuf[8 + TUIDL] == '\n') {
  925. memcpy( entry->tuid, nbuf + 8, TUIDL );
  926. break;
  927. }
  928. }
  929. fclose( f );
  930. }
  931. }
  932. ctx->uvok = 1;
  933. }
  934. return DRV_OK;
  935. }
  936. static void
  937. maildir_init_msg( maildir_store_t *ctx, maildir_message_t *msg, msg_t *entry )
  938. {
  939. msg->base = entry->base;
  940. entry->base = 0; /* prevent deletion */
  941. msg->gen.size = entry->size;
  942. msg->gen.srec = 0;
  943. strncpy( msg->gen.tuid, entry->tuid, TUIDL );
  944. if (entry->recent)
  945. msg->gen.status |= M_RECENT;
  946. if (ctx->gen.opts & OPEN_FLAGS) {
  947. msg->gen.status |= M_FLAGS;
  948. msg->gen.flags = maildir_parse_flags( ((maildir_store_conf_t *)ctx->gen.conf)->info_prefix, msg->base );
  949. } else
  950. msg->gen.flags = 0;
  951. }
  952. static void
  953. maildir_app_msg( maildir_store_t *ctx, message_t ***msgapp, msg_t *entry )
  954. {
  955. maildir_message_t *msg = nfmalloc( sizeof(*msg) );
  956. msg->gen.next = **msgapp;
  957. **msgapp = &msg->gen;
  958. *msgapp = &msg->gen.next;
  959. msg->gen.uid = entry->uid;
  960. msg->gen.status = 0;
  961. maildir_init_msg( ctx, msg, entry );
  962. }
  963. static int
  964. maildir_select_box( store_t *gctx, const char *name )
  965. {
  966. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  967. maildir_store_t *ctx = (maildir_store_t *)gctx;
  968. maildir_cleanup( gctx );
  969. gctx->msgs = 0;
  970. ctx->excs = 0;
  971. ctx->uvfd = -1;
  972. #ifdef USE_DB
  973. ctx->db = 0;
  974. ctx->usedb = 0;
  975. #endif /* USE_DB */
  976. ctx->fresh[0] = ctx->fresh[1] = 0;
  977. if (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) {
  978. gctx->path = maildir_join_path( conf->inbox, name + 5 );
  979. ctx->is_inbox = !name[5];
  980. } else {
  981. if (maildir_validate_path( conf ) < 0) {
  982. gctx->path = 0;
  983. return DRV_CANCELED;
  984. }
  985. gctx->path = maildir_join_path( gctx->conf->path, name );
  986. ctx->is_inbox = 0;
  987. }
  988. return DRV_OK;
  989. }
  990. static void
  991. maildir_open_box( store_t *gctx,
  992. void (*cb)( int sts, void *aux ), void *aux )
  993. {
  994. maildir_store_t *ctx = (maildir_store_t *)gctx;
  995. int ret;
  996. char uvpath[_POSIX_PATH_MAX];
  997. if ((ret = maildir_validate( gctx->path, ctx->is_inbox, ctx )) != DRV_OK)
  998. goto bail;
  999. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  1000. #ifndef USE_DB
  1001. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) < 0) {
  1002. sys_error( "Maildir error: cannot write %s", uvpath );
  1003. cb( DRV_BOX_BAD, aux );
  1004. return;
  1005. }
  1006. #else
  1007. ctx->usedb = 0;
  1008. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1009. nfsnprintf( uvpath, sizeof(uvpath), "%s/.isyncuidmap.db", gctx->path );
  1010. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1011. if (((maildir_store_conf_t *)gctx->conf)->alt_map) {
  1012. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1013. goto dbok;
  1014. } else {
  1015. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  1016. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1017. goto fnok;
  1018. }
  1019. sys_error( "Maildir error: cannot write %s", uvpath );
  1020. cb( DRV_BOX_BAD, aux );
  1021. return;
  1022. } else {
  1023. dbok:
  1024. ctx->usedb = nfstrdup( uvpath );
  1025. }
  1026. }
  1027. fnok:
  1028. #endif /* USE_DB */
  1029. ret = maildir_uidval_lock( ctx );
  1030. bail:
  1031. cb( ret, aux );
  1032. }
  1033. static void
  1034. maildir_create_box( store_t *gctx,
  1035. void (*cb)( int sts, void *aux ), void *aux )
  1036. {
  1037. cb( maildir_validate( gctx->path, 1, (maildir_store_t *)gctx ), aux );
  1038. }
  1039. static int
  1040. maildir_confirm_box_empty( store_t *gctx )
  1041. {
  1042. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1043. msglist_t msglist;
  1044. ctx->nexcs = ctx->minuid = ctx->maxuid = ctx->newuid = 0;
  1045. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1046. return DRV_BOX_BAD;
  1047. maildir_free_scan( &msglist );
  1048. return gctx->count ? DRV_BOX_BAD : DRV_OK;
  1049. }
  1050. static void
  1051. maildir_delete_box( store_t *gctx,
  1052. void (*cb)( int sts, void *aux ), void *aux )
  1053. {
  1054. int i, bl, ret = DRV_OK;
  1055. struct stat st;
  1056. char buf[_POSIX_PATH_MAX];
  1057. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", gctx->path );
  1058. if (stat( buf, &st )) {
  1059. if (errno != ENOENT) {
  1060. sys_error( "Maildir error: cannot access mailbox '%s'", gctx->path );
  1061. ret = DRV_BOX_BAD;
  1062. }
  1063. } else if (!S_ISDIR(st.st_mode)) {
  1064. error( "Maildir error: '%s' is no valid mailbox\n", gctx->path );
  1065. ret = DRV_BOX_BAD;
  1066. } else if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) == DRV_OK) {
  1067. nfsnprintf( buf + bl, sizeof(buf) - bl, ".uidvalidity" );
  1068. if (unlink( buf ) && errno != ENOENT)
  1069. goto badrm;
  1070. #ifdef USE_DB
  1071. nfsnprintf( buf + bl, sizeof(buf) - bl, ".isyncuidmap.db" );
  1072. if (unlink( buf ) && errno != ENOENT)
  1073. goto badrm;
  1074. #endif
  1075. /* We delete cur/ last, as it is the indicator for a present mailbox.
  1076. * That way an interrupted operation can be resumed. */
  1077. for (i = 3; --i >= 0; ) {
  1078. memcpy( buf + bl, subdirs[i], 4 );
  1079. if (rmdir( buf ) && errno != ENOENT) {
  1080. badrm:
  1081. sys_error( "Maildir error: cannot remove '%s'", buf );
  1082. ret = DRV_BOX_BAD;
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. cb( ret, aux );
  1088. }
  1089. static int
  1090. maildir_finish_delete_box( store_t *gctx )
  1091. {
  1092. /* Subfolders are not deleted; the deleted folder is only "stripped of its mailboxness".
  1093. * Consequently, the rmdir may legitimately fail. This behavior follows the IMAP spec. */
  1094. if (rmdir( gctx->path ) && errno != ENOENT && errno != ENOTEMPTY) {
  1095. sys_error( "Maildir warning: cannot remove '%s'", gctx->path );
  1096. return DRV_BOX_BAD;
  1097. }
  1098. return DRV_OK;
  1099. }
  1100. static void
  1101. maildir_prepare_load_box( store_t *gctx, int opts )
  1102. {
  1103. if (opts & OPEN_SETFLAGS)
  1104. opts |= OPEN_OLD;
  1105. if (opts & OPEN_EXPUNGE)
  1106. opts |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  1107. gctx->opts = opts;
  1108. }
  1109. static void
  1110. maildir_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int *excs, int nexcs,
  1111. void (*cb)( int sts, void *aux ), void *aux )
  1112. {
  1113. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1114. message_t **msgapp;
  1115. msglist_t msglist;
  1116. int i;
  1117. ctx->minuid = minuid;
  1118. ctx->maxuid = maxuid;
  1119. ctx->newuid = newuid;
  1120. ctx->excs = nfrealloc( excs, nexcs * sizeof(int) );
  1121. ctx->nexcs = nexcs;
  1122. if (maildir_scan( ctx, &msglist ) != DRV_OK) {
  1123. cb( DRV_BOX_BAD, aux );
  1124. return;
  1125. }
  1126. msgapp = &ctx->gen.msgs;
  1127. for (i = 0; i < msglist.nents; i++)
  1128. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1129. maildir_free_scan( &msglist );
  1130. cb( DRV_OK, aux );
  1131. }
  1132. static int
  1133. maildir_rescan( maildir_store_t *ctx )
  1134. {
  1135. message_t **msgapp;
  1136. maildir_message_t *msg;
  1137. msglist_t msglist;
  1138. int i;
  1139. ctx->fresh[0] = ctx->fresh[1] = 0;
  1140. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1141. return DRV_BOX_BAD;
  1142. for (msgapp = &ctx->gen.msgs, i = 0;
  1143. (msg = (maildir_message_t *)*msgapp) || i < msglist.nents; )
  1144. {
  1145. if (!msg) {
  1146. #if 0
  1147. debug( "adding new message %d\n", msglist.ents[i].uid );
  1148. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1149. #else
  1150. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  1151. #endif
  1152. i++;
  1153. } else if (i >= msglist.nents) {
  1154. debug( "purging deleted message %d\n", msg->gen.uid );
  1155. msg->gen.status = M_DEAD;
  1156. msgapp = &msg->gen.next;
  1157. } else if (msglist.ents[i].uid < msg->gen.uid) {
  1158. /* this should not happen, actually */
  1159. #if 0
  1160. debug( "adding new message %d\n", msglist.ents[i].uid );
  1161. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1162. #else
  1163. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  1164. #endif
  1165. i++;
  1166. } else if (msglist.ents[i].uid > msg->gen.uid) {
  1167. debug( "purging deleted message %d\n", msg->gen.uid );
  1168. msg->gen.status = M_DEAD;
  1169. msgapp = &msg->gen.next;
  1170. } else {
  1171. debug( "updating message %d\n", msg->gen.uid );
  1172. msg->gen.status &= ~(M_FLAGS|M_RECENT);
  1173. free( msg->base );
  1174. maildir_init_msg( ctx, msg, msglist.ents + i );
  1175. i++, msgapp = &msg->gen.next;
  1176. }
  1177. }
  1178. maildir_free_scan( &msglist );
  1179. return DRV_OK;
  1180. }
  1181. static int
  1182. maildir_again( maildir_store_t *ctx, maildir_message_t *msg,
  1183. const char *err, const char *fn, const char *fn2 )
  1184. {
  1185. int ret;
  1186. if (errno != ENOENT) {
  1187. sys_error( err, fn, fn2 );
  1188. return DRV_BOX_BAD;
  1189. }
  1190. if ((ret = maildir_rescan( ctx )) != DRV_OK)
  1191. return ret;
  1192. return (msg->gen.status & M_DEAD) ? DRV_MSG_BAD : DRV_OK;
  1193. }
  1194. static void
  1195. maildir_fetch_msg( store_t *gctx, message_t *gmsg, msg_data_t *data,
  1196. void (*cb)( int sts, void *aux ), void *aux )
  1197. {
  1198. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1199. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1200. int fd, ret;
  1201. struct stat st;
  1202. char buf[_POSIX_PATH_MAX];
  1203. for (;;) {
  1204. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1205. if ((fd = open( buf, O_RDONLY )) >= 0)
  1206. break;
  1207. if ((ret = maildir_again( ctx, msg, "Cannot open %s", buf, 0 )) != DRV_OK) {
  1208. cb( ret, aux );
  1209. return;
  1210. }
  1211. }
  1212. fstat( fd, &st );
  1213. data->len = st.st_size;
  1214. if (data->date == -1)
  1215. data->date = st.st_mtime;
  1216. data->data = nfmalloc( data->len );
  1217. if (read( fd, data->data, data->len ) != data->len) {
  1218. sys_error( "Maildir error: cannot read %s", buf );
  1219. close( fd );
  1220. cb( DRV_MSG_BAD, aux );
  1221. return;
  1222. }
  1223. close( fd );
  1224. if (!(gmsg->status & M_FLAGS))
  1225. data->flags = maildir_parse_flags( ((maildir_store_conf_t *)gctx->conf)->info_prefix, msg->base );
  1226. cb( DRV_OK, aux );
  1227. }
  1228. static int
  1229. maildir_make_flags( char info_delimiter, int flags, char *buf )
  1230. {
  1231. uint i, d;
  1232. buf[0] = info_delimiter;
  1233. buf[1] = '2';
  1234. buf[2] = ',';
  1235. for (d = 3, i = 0; i < as(Flags); i++)
  1236. if (flags & (1 << i))
  1237. buf[d++] = Flags[i];
  1238. buf[d] = 0;
  1239. return d;
  1240. }
  1241. static void
  1242. maildir_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1243. void (*cb)( int sts, int uid, void *aux ), void *aux )
  1244. {
  1245. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1246. const char *box;
  1247. int ret, fd, bl, uid;
  1248. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX], fbuf[NUM_FLAGS + 3], base[128];
  1249. bl = nfsnprintf( base, sizeof(base), "%ld.%d_%d.%s", (long)time( 0 ), Pid, ++MaildirCount, Hostname );
  1250. if (!to_trash) {
  1251. #ifdef USE_DB
  1252. if (ctx->usedb) {
  1253. if ((ret = maildir_set_uid( ctx, base, &uid )) != DRV_OK) {
  1254. free( data->data );
  1255. cb( ret, 0, aux );
  1256. return;
  1257. }
  1258. } else
  1259. #endif /* USE_DB */
  1260. {
  1261. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  1262. free( data->data );
  1263. cb( ret, 0, aux );
  1264. return;
  1265. }
  1266. nfsnprintf( base + bl, sizeof(base) - bl, ",U=%d", uid );
  1267. }
  1268. box = gctx->path;
  1269. } else {
  1270. uid = 0;
  1271. box = ctx->trash;
  1272. }
  1273. maildir_make_flags( ((maildir_store_conf_t *)gctx->conf)->info_delimiter, data->flags, fbuf );
  1274. nfsnprintf( buf, sizeof(buf), "%s/tmp/%s%s", box, base, fbuf );
  1275. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1276. if (errno != ENOENT || !to_trash) {
  1277. sys_error( "Maildir error: cannot create %s", buf );
  1278. free( data->data );
  1279. cb( DRV_BOX_BAD, 0, aux );
  1280. return;
  1281. }
  1282. if ((ret = maildir_validate( box, 1, ctx )) != DRV_OK) {
  1283. free( data->data );
  1284. cb( ret, 0, aux );
  1285. return;
  1286. }
  1287. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1288. sys_error( "Maildir error: cannot create %s", buf );
  1289. free( data->data );
  1290. cb( DRV_BOX_BAD, 0, aux );
  1291. return;
  1292. }
  1293. }
  1294. ret = write( fd, data->data, data->len );
  1295. free( data->data );
  1296. if (ret != data->len || (UseFSync && (ret = fsync( fd )))) {
  1297. if (ret < 0)
  1298. sys_error( "Maildir error: cannot write %s", buf );
  1299. else
  1300. error( "Maildir error: cannot write %s. Disk full?\n", buf );
  1301. close( fd );
  1302. cb( DRV_BOX_BAD, 0, aux );
  1303. return;
  1304. }
  1305. if (close( fd ) < 0) {
  1306. /* Quota exceeded may cause this. */
  1307. sys_error( "Maildir error: cannot write %s", buf );
  1308. cb( DRV_BOX_BAD, 0, aux );
  1309. return;
  1310. }
  1311. if (data->date) {
  1312. /* Set atime and mtime according to INTERNALDATE or mtime of source message */
  1313. struct utimbuf utimebuf;
  1314. utimebuf.actime = utimebuf.modtime = data->date;
  1315. if (utime( buf, &utimebuf ) < 0) {
  1316. sys_error( "Maildir error: cannot set times for %s", buf );
  1317. cb( DRV_BOX_BAD, 0, aux );
  1318. return;
  1319. }
  1320. }
  1321. /* Moving seen messages to cur/ is strictly speaking incorrect, but makes mutt happy. */
  1322. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%s%s", box, subdirs[!(data->flags & F_SEEN)], base, fbuf );
  1323. if (rename( buf, nbuf )) {
  1324. sys_error( "Maildir error: cannot rename %s to %s", buf, nbuf );
  1325. cb( DRV_BOX_BAD, 0, aux );
  1326. return;
  1327. }
  1328. cb( DRV_OK, uid, aux );
  1329. }
  1330. static void
  1331. maildir_find_new_msgs( store_t *gctx ATTR_UNUSED, int newuid ATTR_UNUSED,
  1332. void (*cb)( int sts, void *aux ) ATTR_UNUSED, void *aux ATTR_UNUSED )
  1333. {
  1334. assert( !"maildir_find_new_msgs is not supposed to be called" );
  1335. }
  1336. static void
  1337. maildir_set_msg_flags( store_t *gctx, message_t *gmsg, int uid ATTR_UNUSED, int add, int del,
  1338. void (*cb)( int sts, void *aux ), void *aux )
  1339. {
  1340. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  1341. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1342. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1343. char *s, *p;
  1344. uint i;
  1345. int j, ret, ol, fl, bbl, bl, tl;
  1346. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1347. bbl = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1348. memcpy( nbuf, gctx->path, bbl - 1 );
  1349. memcpy( nbuf + bbl - 1, "/cur/", 5 );
  1350. for (;;) {
  1351. bl = bbl + nfsnprintf( buf + bbl, sizeof(buf) - bbl, "%s/", subdirs[gmsg->status & M_RECENT] );
  1352. ol = strlen( msg->base );
  1353. if ((int)sizeof(buf) - bl < ol + 3 + NUM_FLAGS)
  1354. oob();
  1355. memcpy( buf + bl, msg->base, ol + 1 );
  1356. memcpy( nbuf + bl, msg->base, ol + 1 );
  1357. if ((s = strstr( nbuf + bl, conf->info_prefix ))) {
  1358. s += 3;
  1359. fl = ol - (s - (nbuf + bl));
  1360. for (i = 0; i < as(Flags); i++) {
  1361. if ((p = strchr( s, Flags[i] ))) {
  1362. if (del & (1 << i)) {
  1363. memmove( p, p + 1, fl - (p - s) );
  1364. fl--;
  1365. }
  1366. } else if (add & (1 << i)) {
  1367. for (j = 0; j < fl && Flags[i] > s[j]; j++);
  1368. fl++;
  1369. memmove( s + j + 1, s + j, fl - j );
  1370. s[j] = Flags[i];
  1371. }
  1372. }
  1373. tl = ol + 3 + fl;
  1374. } else {
  1375. tl = ol + maildir_make_flags( conf->info_delimiter, msg->gen.flags, nbuf + bl + ol );
  1376. }
  1377. if (!rename( buf, nbuf ))
  1378. break;
  1379. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot rename %s to %s", buf, nbuf )) != DRV_OK) {
  1380. cb( ret, aux );
  1381. return;
  1382. }
  1383. }
  1384. free( msg->base );
  1385. msg->base = nfmalloc( tl + 1 );
  1386. memcpy( msg->base, nbuf + bl, tl + 1 );
  1387. msg->gen.flags |= add;
  1388. msg->gen.flags &= ~del;
  1389. gmsg->status &= ~M_RECENT;
  1390. cb( DRV_OK, aux );
  1391. }
  1392. #ifdef USE_DB
  1393. static int
  1394. maildir_purge_msg( maildir_store_t *ctx, const char *name )
  1395. {
  1396. int ret;
  1397. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  1398. return ret;
  1399. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  1400. if ((ret = ctx->db->del( ctx->db, 0, &key, 0 ))) {
  1401. ctx->db->err( ctx->db, ret, "Maildir error: db->del()" );
  1402. return DRV_BOX_BAD;
  1403. }
  1404. return DRV_OK;
  1405. }
  1406. #endif /* USE_DB */
  1407. static void
  1408. maildir_trash_msg( store_t *gctx, message_t *gmsg,
  1409. void (*cb)( int sts, void *aux ), void *aux )
  1410. {
  1411. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1412. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1413. char *s;
  1414. int ret;
  1415. struct stat st;
  1416. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1417. for (;;) {
  1418. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1419. s = strstr( msg->base, ((maildir_store_conf_t *)gctx->conf)->info_prefix );
  1420. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%ld.%d_%d.%s%s", ctx->trash,
  1421. subdirs[gmsg->status & M_RECENT], (long)time( 0 ), Pid, ++MaildirCount, Hostname, s ? s : "" );
  1422. if (!rename( buf, nbuf ))
  1423. break;
  1424. if (!stat( buf, &st )) {
  1425. if ((ret = maildir_validate( ctx->trash, 1, ctx )) != DRV_OK) {
  1426. cb( ret, aux );
  1427. return;
  1428. }
  1429. if (!rename( buf, nbuf ))
  1430. break;
  1431. if (errno != ENOENT) {
  1432. sys_error( "Maildir error: cannot move %s to %s", buf, nbuf );
  1433. cb( DRV_BOX_BAD, aux );
  1434. return;
  1435. }
  1436. }
  1437. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot move %s to %s", buf, nbuf )) != DRV_OK) {
  1438. cb( ret, aux );
  1439. return;
  1440. }
  1441. }
  1442. gmsg->status |= M_DEAD;
  1443. gctx->count--;
  1444. #ifdef USE_DB
  1445. if (ctx->usedb) {
  1446. cb( maildir_purge_msg( ctx, msg->base ), aux );
  1447. return;
  1448. }
  1449. #endif /* USE_DB */
  1450. cb( DRV_OK, aux );
  1451. }
  1452. static void
  1453. maildir_close_box( store_t *gctx,
  1454. void (*cb)( int sts, void *aux ), void *aux )
  1455. {
  1456. #ifdef USE_DB
  1457. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1458. #endif /* USE_DB */
  1459. message_t *msg;
  1460. int basel, retry, ret;
  1461. char buf[_POSIX_PATH_MAX];
  1462. for (;;) {
  1463. retry = 0;
  1464. basel = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1465. for (msg = gctx->msgs; msg; msg = msg->next)
  1466. if (!(msg->status & M_DEAD) && (msg->flags & F_DELETED)) {
  1467. nfsnprintf( buf + basel, sizeof(buf) - basel, "%s/%s", subdirs[msg->status & M_RECENT], ((maildir_message_t *)msg)->base );
  1468. if (unlink( buf )) {
  1469. if (errno == ENOENT)
  1470. retry = 1;
  1471. else
  1472. sys_error( "Maildir error: cannot remove %s", buf );
  1473. } else {
  1474. msg->status |= M_DEAD;
  1475. gctx->count--;
  1476. #ifdef USE_DB
  1477. if (ctx->db && (ret = maildir_purge_msg( ctx, ((maildir_message_t *)msg)->base )) != DRV_OK) {
  1478. cb( ret, aux );
  1479. return;
  1480. }
  1481. #endif /* USE_DB */
  1482. }
  1483. }
  1484. if (!retry) {
  1485. cb( DRV_OK, aux );
  1486. return;
  1487. }
  1488. if ((ret = maildir_rescan( (maildir_store_t *)gctx )) != DRV_OK) {
  1489. cb( ret, aux );
  1490. return;
  1491. }
  1492. }
  1493. }
  1494. static void
  1495. maildir_cancel_cmds( store_t *gctx ATTR_UNUSED,
  1496. void (*cb)( void *aux ), void *aux )
  1497. {
  1498. cb( aux );
  1499. }
  1500. static void
  1501. maildir_commit_cmds( store_t *gctx )
  1502. {
  1503. (void) gctx;
  1504. }
  1505. static int
  1506. maildir_memory_usage( store_t *gctx ATTR_UNUSED )
  1507. {
  1508. return 0;
  1509. }
  1510. static int
  1511. maildir_fail_state( store_conf_t *gconf )
  1512. {
  1513. return ((maildir_store_conf_t *)gconf)->failed;
  1514. }
  1515. static int
  1516. maildir_parse_store( conffile_t *cfg, store_conf_t **storep )
  1517. {
  1518. maildir_store_conf_t *store;
  1519. if (strcasecmp( "MaildirStore", cfg->cmd ))
  1520. return 0;
  1521. store = nfcalloc( sizeof(*store) );
  1522. store->info_delimiter = FieldDelimiter;
  1523. store->gen.driver = &maildir_driver;
  1524. store->gen.name = nfstrdup( cfg->val );
  1525. while (getcline( cfg ) && cfg->cmd)
  1526. if (!strcasecmp( "Inbox", cfg->cmd ))
  1527. store->inbox = expand_strdup( cfg->val );
  1528. else if (!strcasecmp( "Path", cfg->cmd ))
  1529. store->gen.path = expand_strdup( cfg->val );
  1530. #ifdef USE_DB
  1531. else if (!strcasecmp( "AltMap", cfg->cmd ))
  1532. store->alt_map = parse_bool( cfg );
  1533. #endif /* USE_DB */
  1534. else if (!strcasecmp( "InfoDelimiter", cfg->cmd )) {
  1535. if (strlen( cfg->val ) != 1) {
  1536. error( "%s:%d: Info delimiter must be exactly one character long\n", cfg->file, cfg->line );
  1537. cfg->err = 1;
  1538. continue;
  1539. }
  1540. store->info_delimiter = cfg->val[0];
  1541. if (!ispunct( store->info_delimiter )) {
  1542. error( "%s:%d: Info delimiter must be a punctuation character\n", cfg->file, cfg->line );
  1543. cfg->err = 1;
  1544. continue;
  1545. }
  1546. } else
  1547. parse_generic_store( &store->gen, cfg );
  1548. if (!store->inbox)
  1549. store->inbox = expand_strdup( "~/Maildir" );
  1550. nfasprintf( &store->info_prefix, "%c2,", store->info_delimiter );
  1551. nfasprintf( &store->info_stop, "%c,", store->info_delimiter );
  1552. *storep = &store->gen;
  1553. return 1;
  1554. }
  1555. struct driver maildir_driver = {
  1556. 0, /* XXX DRV_CRLF? */
  1557. maildir_parse_store,
  1558. maildir_cleanup_drv,
  1559. maildir_open_store,
  1560. maildir_disown_store,
  1561. maildir_disown_store, /* _cancel_, but it's the same */
  1562. maildir_list_store,
  1563. maildir_select_box,
  1564. maildir_create_box,
  1565. maildir_open_box,
  1566. maildir_confirm_box_empty,
  1567. maildir_delete_box,
  1568. maildir_finish_delete_box,
  1569. maildir_prepare_load_box,
  1570. maildir_load_box,
  1571. maildir_fetch_msg,
  1572. maildir_store_msg,
  1573. maildir_find_new_msgs,
  1574. maildir_set_msg_flags,
  1575. maildir_trash_msg,
  1576. maildir_close_box,
  1577. maildir_cancel_cmds,
  1578. maildir_commit_cmds,
  1579. maildir_memory_usage,
  1580. maildir_fail_state,
  1581. };