drv_maildir.c 38 KB

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