drv_maildir.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006 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, write to the Free Software Foundation,
  19. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. *
  21. * As a special exception, mbsync may be linked with the OpenSSL library,
  22. * despite that library's more restrictive license.
  23. */
  24. #include "isync.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. #define USE_DB 1
  38. #ifdef __linux__
  39. # define LEGACY_FLOCK 1
  40. #endif
  41. #ifdef USE_DB
  42. #include <db.h>
  43. #endif /* USE_DB */
  44. typedef struct maildir_store_conf {
  45. store_conf_t gen;
  46. char *inbox;
  47. #ifdef USE_DB
  48. int alt_map;
  49. #endif /* USE_DB */
  50. } maildir_store_conf_t;
  51. typedef struct maildir_message {
  52. message_t gen;
  53. char *base;
  54. char tuid[TUIDL];
  55. } maildir_message_t;
  56. typedef struct maildir_store {
  57. store_t gen;
  58. int uvfd, uvok, nuid;
  59. int minuid, maxuid, nexcs, *excs;
  60. #ifdef USE_DB
  61. DB *db;
  62. #endif /* USE_DB */
  63. } maildir_store_t;
  64. #ifdef USE_DB
  65. static DBT key, value; /* no need to be reentrant, and this saves lots of memset()s */
  66. #endif /* USE_DB */
  67. static struct flock lck;
  68. static int MaildirCount;
  69. static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
  70. static unsigned char
  71. maildir_parse_flags( const char *base )
  72. {
  73. const char *s;
  74. unsigned i;
  75. unsigned char flags;
  76. flags = 0;
  77. if ((s = strstr( base, ":2," )))
  78. for (s += 3, i = 0; i < as(Flags); i++)
  79. if (strchr( s, Flags[i] ))
  80. flags |= (1 << i);
  81. return flags;
  82. }
  83. static void
  84. maildir_open_store( store_conf_t *conf,
  85. void (*cb)( store_t *ctx, void *aux ), void *aux )
  86. {
  87. maildir_store_t *ctx;
  88. struct stat st;
  89. if (stat( conf->path, &st ) || !S_ISDIR(st.st_mode)) {
  90. error( "Maildir error: cannot open store %s\n", conf->path );
  91. cb( 0, aux );
  92. return;
  93. }
  94. ctx = nfcalloc( sizeof(*ctx) );
  95. ctx->gen.conf = conf;
  96. ctx->uvfd = -1;
  97. cb( &ctx->gen, aux );
  98. }
  99. static void
  100. free_maildir_messages( message_t *msg )
  101. {
  102. message_t *tmsg;
  103. for (; (tmsg = msg); msg = tmsg) {
  104. tmsg = msg->next;
  105. free( ((maildir_message_t *)msg)->base );
  106. free( msg );
  107. }
  108. }
  109. static void
  110. maildir_cleanup( store_t *gctx )
  111. {
  112. maildir_store_t *ctx = (maildir_store_t *)gctx;
  113. free_maildir_messages( gctx->msgs );
  114. #ifdef USE_DB
  115. if (ctx->db)
  116. ctx->db->close( ctx->db, 0 );
  117. #endif /* USE_DB */
  118. free( gctx->path );
  119. free( ctx->excs );
  120. if (ctx->uvfd >= 0)
  121. close( ctx->uvfd );
  122. }
  123. static void
  124. maildir_disown_store( store_t *gctx )
  125. {
  126. maildir_cleanup( gctx );
  127. free_string_list( gctx->boxes );
  128. free( gctx );
  129. }
  130. static store_t *
  131. maildir_own_store( store_conf_t *conf )
  132. {
  133. (void)conf;
  134. return 0;
  135. }
  136. static void
  137. maildir_cleanup_drv( void )
  138. {
  139. }
  140. static void
  141. maildir_list( store_t *gctx,
  142. void (*cb)( int sts, void *aux ), void *aux )
  143. {
  144. DIR *dir;
  145. struct dirent *de;
  146. if (!(dir = opendir( gctx->conf->path ))) {
  147. error( "%s: %s\n", gctx->conf->path, strerror(errno) );
  148. cb( DRV_STORE_BAD, aux );
  149. return;
  150. }
  151. while ((de = readdir( dir ))) {
  152. const char *inbox = ((maildir_store_conf_t *)gctx->conf)->inbox;
  153. int bl;
  154. struct stat st;
  155. char buf[PATH_MAX];
  156. if (*de->d_name == '.')
  157. continue;
  158. bl = nfsnprintf( buf, sizeof(buf), "%s%s/cur", gctx->conf->path, de->d_name );
  159. if (stat( buf, &st ) || !S_ISDIR(st.st_mode))
  160. continue;
  161. add_string_list( &gctx->boxes,
  162. !memcmp( buf, inbox, bl - 4 ) && !inbox[bl - 4] ? "INBOX" : de->d_name );
  163. }
  164. closedir (dir);
  165. cb( DRV_OK, aux );
  166. }
  167. static const char *subdirs[] = { "cur", "new", "tmp" };
  168. typedef struct {
  169. char *base;
  170. int size;
  171. unsigned uid:31, recent:1;
  172. char tuid[TUIDL];
  173. } msg_t;
  174. typedef struct {
  175. msg_t *ents;
  176. int nents, nalloc;
  177. } msglist_t;
  178. static void
  179. maildir_free_scan( msglist_t *msglist )
  180. {
  181. int i;
  182. if (msglist->ents) {
  183. for (i = 0; i < msglist->nents; i++)
  184. free( msglist->ents[i].base );
  185. free( msglist->ents );
  186. }
  187. }
  188. #define _24_HOURS (3600 * 24)
  189. static int
  190. maildir_validate( const char *prefix, const char *box, int create )
  191. {
  192. DIR *dirp;
  193. struct dirent *entry;
  194. time_t now;
  195. int i, j, bl;
  196. struct stat st;
  197. char buf[_POSIX_PATH_MAX];
  198. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s%s/", prefix, box );
  199. if (stat( buf, &st )) {
  200. if (errno == ENOENT) {
  201. if (create) {
  202. if (mkdir( buf, 0700 )) {
  203. error( "Maildir error: mkdir %s: %s (errno %d)\n",
  204. buf, strerror(errno), errno );
  205. return DRV_STORE_BAD;
  206. }
  207. mkdirs:
  208. for (i = 0; i < 3; i++) {
  209. memcpy( buf + bl, subdirs[i], 4 );
  210. if (mkdir( buf, 0700 )) {
  211. error( "Maildir error: mkdir %s: %s (errno %d)\n",
  212. buf, strerror(errno), errno );
  213. return DRV_BOX_BAD;
  214. }
  215. }
  216. } else {
  217. error( "Maildir error: mailbox '%s' does not exist\n", buf );
  218. return DRV_BOX_BAD;
  219. }
  220. } else {
  221. error( "Maildir error: stat %s: %s (errno %d)\n",
  222. buf, strerror(errno), errno );
  223. return DRV_BOX_BAD;
  224. }
  225. } else {
  226. for (i = j = 0; i < 3; i++) {
  227. memcpy( buf + bl, subdirs[i], 4 );
  228. if (!stat( buf, &st ) && S_ISDIR(st.st_mode))
  229. j++;
  230. }
  231. if (!j)
  232. goto mkdirs;
  233. if (j != 3) {
  234. error( "Maildir error: '%.*s' is no valid mailbox\n", bl, buf );
  235. return DRV_BOX_BAD;
  236. }
  237. memcpy( buf + bl, "tmp/", 5 );
  238. bl += 4;
  239. if (!(dirp = opendir( buf ))) {
  240. error( "Maildir error: opendir: %s: %s (errno %d)\n",
  241. buf, strerror(errno), errno );
  242. return DRV_BOX_BAD;
  243. }
  244. time( &now );
  245. while ((entry = readdir( dirp ))) {
  246. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s", entry->d_name );
  247. if (stat( buf, &st ))
  248. error( "Maildir error: stat: %s: %s (errno %d)\n",
  249. buf, strerror(errno), errno );
  250. else if (S_ISREG(st.st_mode) && now - st.st_ctime >= _24_HOURS) {
  251. /* this should happen infrequently enough that it won't be
  252. * bothersome to the user to display when it occurs.
  253. */
  254. info( "Maildir notice: removing stale file %s\n", buf );
  255. if (unlink( buf ))
  256. error( "Maildir error: unlink: %s: %s (errno %d)\n",
  257. buf, strerror(errno), errno );
  258. }
  259. }
  260. closedir( dirp );
  261. }
  262. return DRV_OK;
  263. }
  264. #ifdef USE_DB
  265. static void
  266. make_key( DBT *tkey, char *name )
  267. {
  268. char *u = strpbrk( name, ":," );
  269. tkey->data = name;
  270. tkey->size = u ? (size_t)(u - name) : strlen( name );
  271. }
  272. static int
  273. maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
  274. {
  275. int ret, uv[2];
  276. if (uid)
  277. *uid = ++ctx->nuid;
  278. key.data = (void *)"UIDVALIDITY";
  279. key.size = 11;
  280. uv[0] = ctx->gen.uidvalidity;
  281. uv[1] = ctx->nuid;
  282. value.data = uv;
  283. value.size = sizeof(uv);
  284. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  285. tbork:
  286. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  287. bork:
  288. ctx->db->close( ctx->db, 0 );
  289. ctx->db = 0;
  290. return DRV_BOX_BAD;
  291. }
  292. if (uid) {
  293. make_key( &key, (char *)name );
  294. value.data = uid;
  295. value.size = sizeof(*uid);
  296. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 )))
  297. goto tbork;
  298. }
  299. if ((ret = ctx->db->sync( ctx->db, 0 ))) {
  300. ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" );
  301. goto bork;
  302. }
  303. return DRV_OK;
  304. }
  305. #endif /* USE_DB */
  306. static int
  307. maildir_store_uid( maildir_store_t *ctx )
  308. {
  309. int n;
  310. char buf[128];
  311. n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid );
  312. lseek( ctx->uvfd, 0, SEEK_SET );
  313. if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n )) {
  314. error( "Maildir error: cannot write UIDVALIDITY.\n" );
  315. return DRV_BOX_BAD;
  316. }
  317. return DRV_OK;
  318. }
  319. static int
  320. maildir_init_uid( maildir_store_t *ctx, const char *msg )
  321. {
  322. info( "Maildir notice: %s.\n", msg ? msg : "cannot read UIDVALIDITY, creating new" );
  323. ctx->gen.uidvalidity = time( 0 );
  324. ctx->nuid = 0;
  325. ctx->uvok = 0;
  326. #ifdef USE_DB
  327. if (ctx->db) {
  328. u_int32_t count;
  329. ctx->db->truncate( ctx->db, 0, &count, 0 );
  330. return maildir_set_uid( ctx, 0, 0 );
  331. }
  332. #endif /* USE_DB */
  333. return maildir_store_uid( ctx );
  334. }
  335. static int
  336. maildir_uidval_lock( maildir_store_t *ctx )
  337. {
  338. int n;
  339. char buf[128];
  340. #ifdef LEGACY_FLOCK
  341. /* This is legacy only */
  342. if (flock( ctx->uvfd, LOCK_EX ) < 0) {
  343. error( "Maildir error: cannot flock UIDVALIDITY.\n" );
  344. return DRV_BOX_BAD;
  345. }
  346. #endif
  347. /* This (theoretically) works over NFS. Let's hope nobody else did
  348. the same in the opposite order, as we'd deadlock then. */
  349. #if SEEK_SET != 0
  350. lck.l_whence = SEEK_SET;
  351. #endif
  352. lck.l_type = F_WRLCK;
  353. if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
  354. error( "Maildir error: cannot fcntl lock UIDVALIDITY.\n" );
  355. return DRV_BOX_BAD;
  356. }
  357. lseek( ctx->uvfd, 0, SEEK_SET );
  358. if ((n = read( ctx->uvfd, buf, sizeof(buf) )) <= 0 ||
  359. (buf[n] = 0, sscanf( buf, "%d\n%d", &ctx->gen.uidvalidity, &ctx->nuid ) != 2)) {
  360. return maildir_init_uid( ctx, 0 );
  361. } else
  362. ctx->uvok = 1;
  363. return DRV_OK;
  364. }
  365. static void
  366. maildir_uidval_unlock( maildir_store_t *ctx )
  367. {
  368. lck.l_type = F_UNLCK;
  369. fcntl( ctx->uvfd, F_SETLK, &lck );
  370. #ifdef LEGACY_FLOCK
  371. /* This is legacy only */
  372. flock( ctx->uvfd, LOCK_UN );
  373. #endif
  374. }
  375. static int
  376. maildir_obtain_uid( maildir_store_t *ctx, int *uid )
  377. {
  378. *uid = ++ctx->nuid;
  379. return maildir_store_uid( ctx );
  380. }
  381. static int
  382. maildir_compare( const void *l, const void *r )
  383. {
  384. msg_t *lm = (msg_t *)l, *rm = (msg_t *)r;
  385. char *ldot, *rdot, *ldot2, *rdot2, *lseq, *rseq;
  386. int ret, llen, rlen;
  387. if ((ret = lm->uid - rm->uid))
  388. return ret;
  389. /* No UID, so sort by arrival date. We should not do this, but we rely
  390. on the suggested unique file name scheme - we have no choice. */
  391. /* The first field are always the seconds. Alphabetical sort should be
  392. faster than numeric. */
  393. if (!(ldot = strchr( lm->base, '.' )) || !(rdot = strchr( rm->base, '.' )))
  394. goto stronly; /* Should never happen ... */
  395. llen = ldot - lm->base, rlen = rdot - rm->base;
  396. /* The shorter number is smaller. Really. This won't trigger with any
  397. mail created after Sep 9 2001 anyway. */
  398. if ((ret = llen - rlen))
  399. return ret;
  400. if ((ret = memcmp( lm->base, rm->base, llen )))
  401. return ret;
  402. ldot++, rdot++;
  403. if ((llen = strtol( ldot, &ldot2, 10 ))) {
  404. if (!(rlen = strtol( rdot, &rdot2, 10 )))
  405. goto stronly; /* Comparing apples to oranges ... */
  406. /* Classical PID specs */
  407. if ((ret = llen - rlen)) {
  408. retpid:
  409. /* Handle PID wraparound. This works only on systems
  410. where PIDs are not reused too fast */
  411. if (ret > 20000 || ret < -20000)
  412. ret = -ret;
  413. return ret;
  414. }
  415. return (*ldot2 != '_' ? 0 : atoi( ldot2 + 1 )) -
  416. (*rdot2 != '_' ? 0 : atoi( rdot2 + 1 ));
  417. }
  418. if (!(ldot2 = strchr( ldot, '.' )) || !(rdot2 = strchr( rdot, '.' )))
  419. goto stronly; /* Should never happen ... */
  420. llen = ldot2 - ldot, rlen = rdot2 - rdot;
  421. if (((lseq = memchr( ldot, '#', llen )) && (rseq = memchr( rdot, '#', rlen ))) ||
  422. ((lseq = memchr( ldot, 'M', llen )) && (rseq = memchr( rdot, 'M', rlen ))))
  423. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  424. if ((lseq = memchr( ldot, 'P', llen )) && (rseq = memchr( rdot, 'P', rlen ))) {
  425. if ((ret = atoi( lseq + 1 ) - atoi( rseq + 1 )))
  426. goto retpid;
  427. if ((lseq = memchr( ldot, 'Q', llen )) && (rseq = memchr( rdot, 'Q', rlen )))
  428. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  429. }
  430. stronly:
  431. /* Fall-back, so the sort order is defined at all */
  432. return strcmp( lm->base, rm->base );
  433. }
  434. static int
  435. maildir_scan( maildir_store_t *ctx, msglist_t *msglist )
  436. {
  437. DIR *d;
  438. FILE *f;
  439. struct dirent *e;
  440. const char *u, *ru;
  441. #ifdef USE_DB
  442. DB *tdb;
  443. DBC *dbc;
  444. #endif /* USE_DB */
  445. msg_t *entry;
  446. int i, j, uid, bl, fnl, ret;
  447. struct stat st;
  448. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  449. #ifdef USE_DB
  450. if (!ctx->db)
  451. #endif /* USE_DB */
  452. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  453. return ret;
  454. again:
  455. msglist->ents = 0;
  456. msglist->nents = msglist->nalloc = 0;
  457. ctx->gen.count = ctx->gen.recent = 0;
  458. if (ctx->uvok || ctx->maxuid == INT_MAX) {
  459. #ifdef USE_DB
  460. if (ctx->db) {
  461. if (db_create( &tdb, 0, 0 )) {
  462. fputs( "Maildir error: db_create() failed\n", stderr );
  463. return DRV_BOX_BAD;
  464. }
  465. if ((tdb->open)( tdb, 0, 0, 0, DB_HASH, DB_CREATE, 0 )) {
  466. fputs( "Maildir error: tdb->open() failed\n", stderr );
  467. tdb->close( tdb, 0 );
  468. return DRV_BOX_BAD;
  469. }
  470. }
  471. #endif /* USE_DB */
  472. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", ctx->gen.path );
  473. for (i = 0; i < 2; i++) {
  474. memcpy( buf + bl, subdirs[i], 4 );
  475. if (!(d = opendir( buf ))) {
  476. perror( buf );
  477. #ifdef USE_DB
  478. if (!ctx->db)
  479. #endif /* USE_DB */
  480. maildir_uidval_unlock( ctx );
  481. #ifdef USE_DB
  482. bork:
  483. #endif /* USE_DB */
  484. maildir_free_scan( msglist );
  485. #ifdef USE_DB
  486. if (ctx->db)
  487. tdb->close( tdb, 0 );
  488. #endif /* USE_DB */
  489. return DRV_BOX_BAD;
  490. }
  491. while ((e = readdir( d ))) {
  492. if (*e->d_name == '.')
  493. continue;
  494. ctx->gen.count++;
  495. ctx->gen.recent += i;
  496. #ifdef USE_DB
  497. if (ctx->db) {
  498. make_key( &key, e->d_name );
  499. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  500. if (ret != DB_NOTFOUND) {
  501. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  502. ctx->db->close( ctx->db, 0 );
  503. ctx->db = 0;
  504. goto bork;
  505. }
  506. uid = INT_MAX;
  507. } else {
  508. value.size = 0;
  509. if ((ret = tdb->put( tdb, 0, &key, &value, 0 ))) {
  510. tdb->err( tdb, ret, "Maildir error: tdb->put()" );
  511. goto bork;
  512. }
  513. uid = *(int *)value.data;
  514. }
  515. } else
  516. #endif /* USE_DB */
  517. {
  518. uid = (ctx->uvok && (u = strstr( e->d_name, ",U=" ))) ? atoi( u + 3 ) : 0;
  519. if (!uid)
  520. uid = INT_MAX;
  521. }
  522. if (uid <= ctx->maxuid) {
  523. if (uid < ctx->minuid) {
  524. for (j = 0; j < ctx->nexcs; j++)
  525. if (ctx->excs[j] == uid)
  526. goto oke;
  527. continue;
  528. oke: ;
  529. }
  530. if (msglist->nalloc == msglist->nents) {
  531. msglist->nalloc = msglist->nalloc * 2 + 100;
  532. msglist->ents = nfrealloc( msglist->ents, msglist->nalloc * sizeof(msg_t) );
  533. }
  534. entry = &msglist->ents[msglist->nents++];
  535. entry->base = nfstrdup( e->d_name );
  536. entry->uid = uid;
  537. entry->recent = i;
  538. entry->size = 0;
  539. entry->tuid[0] = 0;
  540. }
  541. }
  542. closedir( d );
  543. }
  544. #ifdef USE_DB
  545. if (ctx->db) {
  546. if ((ret = ctx->db->cursor( ctx->db, 0, &dbc, 0 )))
  547. ctx->db->err( ctx->db, ret, "Maildir error: db->cursor()" );
  548. else {
  549. for (;;) {
  550. if ((ret = dbc->c_get( dbc, &key, &value, DB_NEXT ))) {
  551. if (ret != DB_NOTFOUND)
  552. ctx->db->err( ctx->db, ret, "Maildir error: db->c_get()" );
  553. break;
  554. }
  555. if ((key.size != 11 || memcmp( key.data, "UIDVALIDITY", 11 )) &&
  556. (ret = tdb->get( tdb, 0, &key, &value, 0 ))) {
  557. if (ret != DB_NOTFOUND) {
  558. tdb->err( tdb, ret, "Maildir error: tdb->get()" );
  559. break;
  560. }
  561. if ((ret = dbc->c_del( dbc, 0 ))) {
  562. ctx->db->err( ctx->db, ret, "Maildir error: db->c_del()" );
  563. break;
  564. }
  565. }
  566. }
  567. dbc->c_close( dbc );
  568. }
  569. tdb->close( tdb, 0 );
  570. }
  571. #endif /* USE_DB */
  572. qsort( msglist->ents, msglist->nents, sizeof(msg_t), maildir_compare );
  573. for (uid = i = 0; i < msglist->nents; i++) {
  574. entry = &msglist->ents[i];
  575. if (entry->uid != INT_MAX) {
  576. if (uid == entry->uid) {
  577. if ((ret = maildir_init_uid( ctx, "duplicate UID; changing UIDVALIDITY" )) != DRV_OK) {
  578. maildir_free_scan( msglist );
  579. return ret;
  580. }
  581. maildir_free_scan( msglist );
  582. goto again;
  583. }
  584. uid = entry->uid;
  585. if (ctx->gen.opts & (OPEN_SIZE|OPEN_FIND))
  586. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  587. #ifdef USE_DB
  588. } else if (ctx->db) {
  589. if ((ret = maildir_set_uid( ctx, entry->base, &uid )) != DRV_OK) {
  590. maildir_free_scan( msglist );
  591. return ret;
  592. }
  593. entry->uid = uid;
  594. if (ctx->gen.opts & (OPEN_SIZE|OPEN_FIND))
  595. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  596. #endif /* USE_DB */
  597. } else {
  598. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  599. maildir_free_scan( msglist );
  600. return ret;
  601. }
  602. entry->uid = uid;
  603. if ((u = strstr( entry->base, ",U=" )))
  604. for (ru = u + 3; isdigit( (unsigned char)*ru ); ru++);
  605. else
  606. u = ru = strchr( entry->base, ':' );
  607. fnl = (u ?
  608. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%.*s,U=%d%s", subdirs[entry->recent], (int)(u - entry->base), entry->base, uid, ru ) :
  609. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s,U=%d", subdirs[entry->recent], entry->base, uid ))
  610. + 1 - 4;
  611. memcpy( nbuf, buf, bl + 4 );
  612. nfsnprintf( nbuf + bl + 4, sizeof(nbuf) - bl - 4, "%s", entry->base );
  613. if (rename( nbuf, buf )) {
  614. notok:
  615. if (errno != ENOENT) {
  616. perror( buf );
  617. maildir_uidval_unlock( ctx );
  618. maildir_free_scan( msglist );
  619. return DRV_BOX_BAD;
  620. }
  621. maildir_free_scan( msglist );
  622. goto again;
  623. }
  624. free( entry->base );
  625. entry->base = nfmalloc( fnl );
  626. memcpy( entry->base, buf + bl + 4, fnl );
  627. }
  628. if (ctx->gen.opts & OPEN_SIZE) {
  629. if (stat( buf, &st ))
  630. goto notok;
  631. entry->size = st.st_size;
  632. }
  633. if (ctx->gen.opts & OPEN_FIND) {
  634. if (!(f = fopen( buf, "r" )))
  635. goto notok;
  636. while (fgets( nbuf, sizeof(nbuf), f )) {
  637. if (!nbuf[0] || nbuf[0] == '\n')
  638. break;
  639. if (!memcmp( nbuf, "X-TUID: ", 8 ) && nbuf[8 + TUIDL] == '\n') {
  640. memcpy( entry->tuid, nbuf + 8, TUIDL );
  641. break;
  642. }
  643. }
  644. fclose( f );
  645. }
  646. }
  647. ctx->uvok = 1;
  648. }
  649. #ifdef USE_DB
  650. if (!ctx->db)
  651. #endif /* ! USE_DB */
  652. maildir_uidval_unlock( ctx );
  653. return DRV_OK;
  654. }
  655. static void
  656. maildir_init_msg( maildir_store_t *ctx, maildir_message_t *msg, msg_t *entry )
  657. {
  658. msg->base = entry->base;
  659. entry->base = 0; /* prevent deletion */
  660. msg->gen.size = entry->size;
  661. strncpy( msg->tuid, entry->tuid, TUIDL );
  662. if (entry->recent)
  663. msg->gen.status |= M_RECENT;
  664. if (ctx->gen.opts & OPEN_FLAGS) {
  665. msg->gen.status |= M_FLAGS;
  666. msg->gen.flags = maildir_parse_flags( msg->base );
  667. } else
  668. msg->gen.flags = 0;
  669. }
  670. static void
  671. maildir_app_msg( maildir_store_t *ctx, message_t ***msgapp, msg_t *entry )
  672. {
  673. maildir_message_t *msg = nfmalloc( sizeof(*msg) );
  674. msg->gen.next = **msgapp;
  675. **msgapp = &msg->gen;
  676. *msgapp = &msg->gen.next;
  677. msg->gen.uid = entry->uid;
  678. msg->gen.status = 0;
  679. maildir_init_msg( ctx, msg, entry );
  680. }
  681. static void
  682. maildir_prepare_paths( store_t *gctx )
  683. {
  684. maildir_store_t *ctx = (maildir_store_t *)gctx;
  685. maildir_cleanup( gctx );
  686. gctx->msgs = 0;
  687. ctx->uvfd = -1;
  688. #ifdef USE_DB
  689. ctx->db = 0;
  690. #endif /* USE_DB */
  691. if (!strcmp( gctx->name, "INBOX" ))
  692. gctx->path = nfstrdup( ((maildir_store_conf_t *)gctx->conf)->inbox );
  693. else
  694. nfasprintf( &gctx->path, "%s%s", gctx->conf->path, gctx->name );
  695. }
  696. static void
  697. maildir_prepare_opts( store_t *gctx, int opts )
  698. {
  699. if (opts & OPEN_SETFLAGS)
  700. opts |= OPEN_OLD;
  701. if (opts & OPEN_EXPUNGE)
  702. opts |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  703. gctx->opts = opts;
  704. }
  705. static int
  706. maildir_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs,
  707. int (*cb)( int sts, void *aux ), void *aux )
  708. {
  709. maildir_store_t *ctx = (maildir_store_t *)gctx;
  710. message_t **msgapp;
  711. msglist_t msglist;
  712. int i;
  713. #ifdef USE_DB
  714. int ret;
  715. #endif /* USE_DB */
  716. char uvpath[_POSIX_PATH_MAX];
  717. ctx->minuid = minuid;
  718. ctx->maxuid = maxuid;
  719. ctx->excs = nfrealloc( excs, nexcs * sizeof(int) );
  720. ctx->nexcs = nexcs;
  721. if (maildir_validate( gctx->path, "", ctx->gen.opts & OPEN_CREATE ) != DRV_OK)
  722. return cb( DRV_BOX_BAD, aux );
  723. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  724. #ifndef USE_DB
  725. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) < 0) {
  726. perror( uvpath );
  727. return cb( DRV_BOX_BAD, aux );
  728. }
  729. #else
  730. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  731. nfsnprintf( uvpath, sizeof(uvpath), "%s/.isyncuidmap.db", gctx->path );
  732. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  733. if (((maildir_store_conf_t *)gctx->conf)->alt_map) {
  734. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  735. goto dbok;
  736. } else {
  737. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  738. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  739. goto fnok;
  740. }
  741. perror( uvpath );
  742. return cb( DRV_BOX_BAD, aux );
  743. }
  744. dbok:
  745. #if SEEK_SET != 0
  746. lck.l_whence = SEEK_SET;
  747. #endif
  748. lck.l_type = F_WRLCK;
  749. if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
  750. perror( uvpath );
  751. bork:
  752. close( ctx->uvfd );
  753. ctx->uvfd = -1;
  754. return cb( DRV_BOX_BAD, aux );
  755. }
  756. if (db_create( &ctx->db, 0, 0 )) {
  757. fputs( "Maildir error: db_create() failed\n", stderr );
  758. goto bork;
  759. }
  760. if ((ret = (ctx->db->open)( ctx->db, 0, uvpath, 0, DB_HASH, DB_CREATE, 0 ))) {
  761. ctx->db->err( ctx->db, ret, "Maildir error: db->open(%s)", uvpath );
  762. dbork:
  763. ctx->db->close( ctx->db, 0 );
  764. goto bork;
  765. }
  766. key.data = (void *)"UIDVALIDITY";
  767. key.size = 11;
  768. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  769. if (ret != DB_NOTFOUND) {
  770. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  771. goto dbork;
  772. }
  773. if (maildir_init_uid( ctx, 0 ) != DRV_OK)
  774. goto dbork;
  775. } else {
  776. ctx->gen.uidvalidity = ((int *)value.data)[0];
  777. ctx->nuid = ((int *)value.data)[1];
  778. ctx->uvok = 1;
  779. }
  780. }
  781. fnok:
  782. #endif /* USE_DB */
  783. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  784. return cb( DRV_BOX_BAD, aux );
  785. msgapp = &ctx->gen.msgs;
  786. for (i = 0; i < msglist.nents; i++)
  787. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  788. maildir_free_scan( &msglist );
  789. return cb( DRV_OK, aux );
  790. }
  791. static int
  792. maildir_rescan( maildir_store_t *ctx )
  793. {
  794. message_t **msgapp;
  795. maildir_message_t *msg;
  796. msglist_t msglist;
  797. int i;
  798. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  799. return DRV_BOX_BAD;
  800. ctx->gen.recent = 0;
  801. for (msgapp = &ctx->gen.msgs, i = 0;
  802. (msg = (maildir_message_t *)*msgapp) || i < msglist.nents; )
  803. {
  804. if (!msg) {
  805. #if 0
  806. debug( "adding new message %d\n", msglist.ents[i].uid );
  807. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  808. #else
  809. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  810. #endif
  811. i++;
  812. } else if (i >= msglist.nents) {
  813. debug( "purging deleted message %d\n", msg->gen.uid );
  814. msg->gen.status = M_DEAD;
  815. msgapp = &msg->gen.next;
  816. } else if (msglist.ents[i].uid < msg->gen.uid) {
  817. /* this should not happen, actually */
  818. #if 0
  819. debug( "adding new message %d\n", msglist.ents[i].uid );
  820. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  821. #else
  822. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  823. #endif
  824. i++;
  825. } else if (msglist.ents[i].uid > msg->gen.uid) {
  826. debug( "purging deleted message %d\n", msg->gen.uid );
  827. msg->gen.status = M_DEAD;
  828. msgapp = &msg->gen.next;
  829. } else {
  830. debug( "updating message %d\n", msg->gen.uid );
  831. msg->gen.status &= ~(M_FLAGS|M_RECENT);
  832. free( msg->base );
  833. maildir_init_msg( ctx, msg, msglist.ents + i );
  834. i++, msgapp = &msg->gen.next;
  835. }
  836. }
  837. maildir_free_scan( &msglist );
  838. return DRV_OK;
  839. }
  840. static int
  841. maildir_again( maildir_store_t *ctx, maildir_message_t *msg, const char *fn )
  842. {
  843. int ret;
  844. if (errno != ENOENT) {
  845. perror( fn );
  846. return DRV_BOX_BAD;
  847. }
  848. if ((ret = maildir_rescan( ctx )) != DRV_OK)
  849. return ret;
  850. return (msg->gen.status & M_DEAD) ? DRV_MSG_BAD : DRV_OK;
  851. }
  852. static int
  853. maildir_fetch_msg( store_t *gctx, message_t *gmsg, msg_data_t *data,
  854. int (*cb)( int sts, void *aux ), void *aux )
  855. {
  856. maildir_store_t *ctx = (maildir_store_t *)gctx;
  857. maildir_message_t *msg = (maildir_message_t *)gmsg;
  858. int fd, ret;
  859. struct stat st;
  860. char buf[_POSIX_PATH_MAX];
  861. for (;;) {
  862. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  863. if ((fd = open( buf, O_RDONLY )) >= 0)
  864. break;
  865. if ((ret = maildir_again( ctx, msg, buf )) != DRV_OK)
  866. return cb( ret, aux );
  867. }
  868. fstat( fd, &st );
  869. data->len = st.st_size;
  870. data->data = nfmalloc( data->len );
  871. if (read( fd, data->data, data->len ) != data->len) {
  872. perror( buf );
  873. close( fd );
  874. return cb( DRV_MSG_BAD, aux );
  875. }
  876. close( fd );
  877. if (!(gmsg->status & M_FLAGS))
  878. data->flags = maildir_parse_flags( msg->base );
  879. return cb( DRV_OK, aux );
  880. }
  881. static int
  882. maildir_make_flags( int flags, char *buf )
  883. {
  884. unsigned i, d;
  885. buf[0] = ':';
  886. buf[1] = '2';
  887. buf[2] = ',';
  888. for (d = 3, i = 0; i < as(Flags); i++)
  889. if (flags & (1 << i))
  890. buf[d++] = Flags[i];
  891. buf[d] = 0;
  892. return d;
  893. }
  894. static int
  895. maildir_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  896. int (*cb)( int sts, int uid, void *aux ), void *aux )
  897. {
  898. maildir_store_t *ctx = (maildir_store_t *)gctx;
  899. const char *prefix, *box;
  900. int ret, fd, bl, uid;
  901. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX], fbuf[NUM_FLAGS + 3], base[128];
  902. bl = nfsnprintf( base, sizeof(base), "%ld.%d_%d.%s", time( 0 ), Pid, ++MaildirCount, Hostname );
  903. if (!to_trash) {
  904. #ifdef USE_DB
  905. if (ctx->db) {
  906. if ((ret = maildir_set_uid( ctx, base, &uid )) != DRV_OK) {
  907. free( data->data );
  908. return cb( ret, 0, aux );
  909. }
  910. } else
  911. #endif /* USE_DB */
  912. {
  913. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK ||
  914. (ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK)
  915. return cb( ret, 0, aux );
  916. maildir_uidval_unlock( ctx );
  917. nfsnprintf( base + bl, sizeof(base) - bl, ",U=%d", uid );
  918. }
  919. prefix = gctx->path;
  920. box = "";
  921. } else {
  922. prefix = gctx->conf->path;
  923. box = gctx->conf->trash;
  924. }
  925. maildir_make_flags( data->flags, fbuf );
  926. nfsnprintf( buf, sizeof(buf), "%s%s/tmp/%s%s", prefix, box, base, fbuf );
  927. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  928. if (errno != ENOENT) {
  929. perror( buf );
  930. free( data->data );
  931. return cb( DRV_BOX_BAD, 0, aux );
  932. }
  933. if ((ret = maildir_validate( gctx->conf->path, gctx->conf->trash, gctx->opts & OPEN_CREATE )) != DRV_OK) {
  934. free( data->data );
  935. return cb( ret, 0, aux );
  936. }
  937. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  938. perror( buf );
  939. free( data->data );
  940. return cb( DRV_BOX_BAD, 0, aux );
  941. }
  942. }
  943. ret = write( fd, data->data, data->len );
  944. free( data->data );
  945. if (ret != data->len) {
  946. if (ret < 0)
  947. perror( buf );
  948. else
  949. error( "Maildir error: %s: partial write\n", buf );
  950. close( fd );
  951. return cb( DRV_BOX_BAD, 0, aux );
  952. }
  953. if (close( fd ) < 0) {
  954. /* Quota exceeded may cause this. */
  955. perror( buf );
  956. return cb( DRV_BOX_BAD, 0, aux );
  957. }
  958. /* Moving seen messages to cur/ is strictly speaking incorrect, but makes mutt happy. */
  959. nfsnprintf( nbuf, sizeof(nbuf), "%s%s/%s/%s%s", prefix, box, subdirs[!(data->flags & F_SEEN)], base, fbuf );
  960. if (rename( buf, nbuf )) {
  961. perror( nbuf );
  962. return cb( DRV_BOX_BAD, 0, aux );
  963. }
  964. return cb( DRV_OK, uid, aux );
  965. }
  966. static int
  967. maildir_find_msg( store_t *gctx, const char *tuid,
  968. int (*cb)( int sts, int uid, void *aux ), void *aux )
  969. {
  970. message_t *msg;
  971. /* using a hash table might turn out to be more appropriate ... */
  972. for (msg = gctx->msgs; msg; msg = msg->next)
  973. if (!(msg->status & M_DEAD) && !memcmp( ((maildir_message_t *)msg)->tuid, tuid, TUIDL ))
  974. return cb( DRV_OK, msg->uid, aux );
  975. return cb( DRV_MSG_BAD, -1, aux );
  976. }
  977. static int
  978. maildir_set_flags( store_t *gctx, message_t *gmsg, int uid, int add, int del,
  979. int (*cb)( int sts, void *aux ), void *aux )
  980. {
  981. maildir_store_t *ctx = (maildir_store_t *)gctx;
  982. maildir_message_t *msg = (maildir_message_t *)gmsg;
  983. char *s, *p;
  984. unsigned i;
  985. int j, ret, ol, fl, bbl, bl, tl;
  986. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  987. (void) uid;
  988. bbl = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  989. memcpy( nbuf, gctx->path, bbl - 1 );
  990. memcpy( nbuf + bbl - 1, "/cur/", 5 );
  991. for (;;) {
  992. bl = bbl + nfsnprintf( buf + bbl, sizeof(buf) - bbl, "%s/", subdirs[gmsg->status & M_RECENT] );
  993. ol = strlen( msg->base );
  994. if ((int)sizeof(buf) - bl < ol + 3 + NUM_FLAGS)
  995. oob();
  996. memcpy( buf + bl, msg->base, ol + 1 );
  997. memcpy( nbuf + bl, msg->base, ol + 1 );
  998. if ((s = strstr( nbuf + bl, ":2," ))) {
  999. s += 3;
  1000. fl = ol - (s - (nbuf + bl));
  1001. for (i = 0; i < as(Flags); i++) {
  1002. if ((p = strchr( s, Flags[i] ))) {
  1003. if (del & (1 << i)) {
  1004. memcpy( p, p + 1, fl - (p - s) );
  1005. fl--;
  1006. }
  1007. } else if (add & (1 << i)) {
  1008. for (j = 0; j < fl && Flags[i] > s[j]; j++);
  1009. fl++;
  1010. memmove( s + j + 1, s + j, fl - j );
  1011. s[j] = Flags[i];
  1012. }
  1013. }
  1014. tl = ol + 3 + fl;
  1015. } else {
  1016. tl = ol + maildir_make_flags( msg->gen.flags, nbuf + bl + ol );
  1017. }
  1018. if (!rename( buf, nbuf ))
  1019. break;
  1020. if ((ret = maildir_again( ctx, msg, buf )) != DRV_OK)
  1021. return cb( ret, aux );
  1022. }
  1023. free( msg->base );
  1024. msg->base = nfmalloc( tl + 1 );
  1025. memcpy( msg->base, nbuf + bl, tl + 1 );
  1026. msg->gen.flags |= add;
  1027. msg->gen.flags &= ~del;
  1028. gmsg->status &= ~M_RECENT;
  1029. return cb( DRV_OK, aux );
  1030. }
  1031. #ifdef USE_DB
  1032. static int
  1033. maildir_purge_msg( maildir_store_t *ctx, const char *name )
  1034. {
  1035. int ret;
  1036. make_key( &key, (char *)name );
  1037. if ((ret = ctx->db->del( ctx->db, 0, &key, 0 ))) {
  1038. ctx->db->err( ctx->db, ret, "Maildir error: db->del()" );
  1039. ctx->db->close( ctx->db, 0 );
  1040. ctx->db = 0;
  1041. return DRV_BOX_BAD;
  1042. }
  1043. return DRV_OK;
  1044. }
  1045. #endif /* USE_DB */
  1046. static int
  1047. maildir_trash_msg( store_t *gctx, message_t *gmsg,
  1048. int (*cb)( int sts, void *aux ), void *aux )
  1049. {
  1050. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1051. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1052. char *s;
  1053. int ret;
  1054. struct stat st;
  1055. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1056. for (;;) {
  1057. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1058. s = strstr( msg->base, ":2," );
  1059. nfsnprintf( nbuf, sizeof(nbuf), "%s%s/%s/%ld.%d_%d.%s%s", gctx->conf->path, gctx->conf->trash,
  1060. subdirs[gmsg->status & M_RECENT], time( 0 ), Pid, ++MaildirCount, Hostname, s ? s : "" );
  1061. if (!rename( buf, nbuf ))
  1062. break;
  1063. if (!stat( buf, &st )) {
  1064. if ((ret = maildir_validate( gctx->conf->path, gctx->conf->trash, 1 )) != DRV_OK)
  1065. return cb( ret, aux );
  1066. if (!rename( buf, nbuf ))
  1067. break;
  1068. if (errno != ENOENT) {
  1069. perror( nbuf );
  1070. return cb( DRV_BOX_BAD, aux );
  1071. }
  1072. }
  1073. if ((ret = maildir_again( ctx, msg, buf )) != DRV_OK)
  1074. return cb( ret, aux );
  1075. }
  1076. gmsg->status |= M_DEAD;
  1077. gctx->count--;
  1078. #ifdef USE_DB
  1079. if (ctx->db)
  1080. return cb( maildir_purge_msg( ctx, msg->base ), aux );
  1081. #endif /* USE_DB */
  1082. return cb( DRV_OK, aux );
  1083. }
  1084. static int
  1085. maildir_close( store_t *gctx,
  1086. int (*cb)( int sts, void *aux ), void *aux )
  1087. {
  1088. #ifdef USE_DB
  1089. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1090. #endif /* USE_DB */
  1091. message_t *msg;
  1092. int basel, retry, ret;
  1093. char buf[_POSIX_PATH_MAX];
  1094. for (;;) {
  1095. retry = 0;
  1096. basel = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1097. for (msg = gctx->msgs; msg; msg = msg->next)
  1098. if (!(msg->status & M_DEAD) && (msg->flags & F_DELETED)) {
  1099. nfsnprintf( buf + basel, sizeof(buf) - basel, "%s/%s", subdirs[msg->status & M_RECENT], ((maildir_message_t *)msg)->base );
  1100. if (unlink( buf )) {
  1101. if (errno == ENOENT)
  1102. retry = 1;
  1103. else
  1104. perror( buf );
  1105. } else {
  1106. msg->status |= M_DEAD;
  1107. gctx->count--;
  1108. #ifdef USE_DB
  1109. if (ctx->db && (ret = maildir_purge_msg( ctx, ((maildir_message_t *)msg)->base )) != DRV_OK)
  1110. return cb( ret, aux );
  1111. #endif /* USE_DB */
  1112. }
  1113. }
  1114. if (!retry)
  1115. return cb( DRV_OK, aux );
  1116. if ((ret = maildir_rescan( (maildir_store_t *)gctx )) != DRV_OK)
  1117. return cb( ret, aux );
  1118. }
  1119. }
  1120. static void
  1121. maildir_cancel( store_t *gctx,
  1122. void (*cb)( void *aux ), void *aux )
  1123. {
  1124. (void)gctx;
  1125. cb( aux );
  1126. }
  1127. static void
  1128. maildir_commit( store_t *gctx )
  1129. {
  1130. (void) gctx;
  1131. }
  1132. static int
  1133. maildir_parse_store( conffile_t *cfg, store_conf_t **storep, int *err )
  1134. {
  1135. maildir_store_conf_t *store;
  1136. if (strcasecmp( "MaildirStore", cfg->cmd ))
  1137. return 0;
  1138. store = nfcalloc( sizeof(*store) );
  1139. store->gen.driver = &maildir_driver;
  1140. store->gen.name = nfstrdup( cfg->val );
  1141. while (getcline( cfg ) && cfg->cmd)
  1142. if (!strcasecmp( "Inbox", cfg->cmd ))
  1143. store->inbox = expand_strdup( cfg->val );
  1144. else if (!strcasecmp( "Path", cfg->cmd ))
  1145. store->gen.path = expand_strdup( cfg->val );
  1146. #ifdef USE_DB
  1147. else if (!strcasecmp( "AltMap", cfg->cmd ))
  1148. store->alt_map = parse_bool( cfg );
  1149. #endif /* USE_DB */
  1150. else
  1151. parse_generic_store( &store->gen, cfg, err );
  1152. if (!store->inbox)
  1153. store->inbox = expand_strdup( "~/Maildir" );
  1154. *storep = &store->gen;
  1155. return 1;
  1156. }
  1157. struct driver maildir_driver = {
  1158. 0, /* XXX DRV_CRLF? */
  1159. maildir_parse_store,
  1160. maildir_cleanup_drv,
  1161. maildir_open_store,
  1162. maildir_disown_store,
  1163. maildir_own_store,
  1164. maildir_disown_store, /* _cancel_, but it's the same */
  1165. maildir_list,
  1166. maildir_prepare_paths,
  1167. maildir_prepare_opts,
  1168. maildir_select,
  1169. maildir_fetch_msg,
  1170. maildir_store_msg,
  1171. maildir_find_msg,
  1172. maildir_set_flags,
  1173. maildir_trash_msg,
  1174. maildir_close,
  1175. maildir_cancel,
  1176. maildir_commit,
  1177. };