drv_maildir.c 38 KB

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