drv_maildir.c 33 KB

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