drv_maildir.c 36 KB

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