drv_maildir.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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 <errno.h>
  35. #include <time.h>
  36. #include <utime.h>
  37. #if !defined(_POSIX_SYNCHRONIZED_IO) || _POSIX_SYNCHRONIZED_IO <= 0
  38. # define fdatasync fsync
  39. #endif
  40. #ifdef USE_DB
  41. #include <db.h>
  42. #endif /* USE_DB */
  43. #define SUB_UNSET 0
  44. #define SUB_VERBATIM 1
  45. #define SUB_MAILDIRPP 2
  46. #define SUB_LEGACY 3
  47. typedef struct {
  48. store_conf_t gen;
  49. char *inbox;
  50. #ifdef USE_DB
  51. int alt_map;
  52. #endif /* USE_DB */
  53. char info_delimiter;
  54. char sub_style;
  55. char failed;
  56. char *info_prefix, *info_stop; /* precalculated from info_delimiter */
  57. } maildir_store_conf_t;
  58. typedef struct {
  59. message_t gen;
  60. char *base;
  61. } maildir_message_t;
  62. typedef struct {
  63. store_t gen;
  64. int uvfd, uvok, nuid, is_inbox, fresh[3];
  65. int minuid, maxuid, newuid, seenuid;
  66. int_array_t excs;
  67. char *trash;
  68. #ifdef USE_DB
  69. DB *db;
  70. char *usedb;
  71. #endif /* USE_DB */
  72. wakeup_t lcktmr;
  73. } maildir_store_t;
  74. #ifdef USE_DB
  75. static DBT key, value; /* no need to be reentrant, and this saves lots of memset()s */
  76. #endif /* USE_DB */
  77. static struct flock lck;
  78. static int MaildirCount;
  79. static void ATTR_PRINTFLIKE(1, 2)
  80. debug( const char *msg, ... )
  81. {
  82. va_list va;
  83. va_start( va, msg );
  84. vdebug( DEBUG_SYNC, msg, va );
  85. va_end( va );
  86. }
  87. static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
  88. static uchar
  89. maildir_parse_flags( const char *info_prefix, const char *base )
  90. {
  91. const char *s;
  92. uint i;
  93. uchar flags;
  94. flags = 0;
  95. if ((s = strstr( base, info_prefix )))
  96. for (s += 3, i = 0; i < as(Flags); i++)
  97. if (strchr( s, Flags[i] ))
  98. flags |= (1 << i);
  99. return flags;
  100. }
  101. static int
  102. maildir_ensure_path( maildir_store_conf_t *conf )
  103. {
  104. if (!conf->gen.path) {
  105. error( "Maildir error: store '%s' has no Path\n", conf->gen.name );
  106. conf->failed = FAIL_FINAL;
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. /* Subdirs of INBOX include a leading slash. */
  112. /* Path includes a trailing slash, Inbox does not. */
  113. static char *
  114. maildir_join_path( maildir_store_conf_t *conf, int in_inbox, const char *box )
  115. {
  116. char *out, *p;
  117. const char *prefix;
  118. int pl, bl, n;
  119. char c;
  120. if (in_inbox || conf->sub_style == SUB_MAILDIRPP) {
  121. prefix = conf->inbox;
  122. } else {
  123. if (maildir_ensure_path( conf ) < 0)
  124. return 0;
  125. prefix = conf->gen.path;
  126. }
  127. pl = strlen( prefix );
  128. for (bl = 0, n = 0; (c = box[bl]); bl++)
  129. if (c == '/') {
  130. if (conf->sub_style == SUB_UNSET) {
  131. error( "Maildir error: accessing subfolder '%s', but store '%s' does not specify SubFolders style\n",
  132. box, conf->gen.name );
  133. return 0;
  134. }
  135. n++;
  136. } else if (c == '.' && conf->sub_style == SUB_MAILDIRPP) {
  137. error( "Maildir error: store '%s', folder '%s': SubFolders style Maildir++ does not support dots in mailbox names\n",
  138. conf->gen.name, box );
  139. return 0;
  140. }
  141. switch (conf->sub_style) {
  142. case SUB_VERBATIM:
  143. n = 0;
  144. break;
  145. case SUB_MAILDIRPP:
  146. n = 2;
  147. break;
  148. default: /* SUB_LEGACY and SUB_UNSET */
  149. break;
  150. }
  151. out = nfmalloc( pl + bl + n + 1 );
  152. memcpy( out, prefix, pl );
  153. p = out + pl;
  154. if (conf->sub_style == SUB_MAILDIRPP) {
  155. *p++ = '/';
  156. *p++ = '.';
  157. }
  158. while ((c = *box++)) {
  159. if (c == '/') {
  160. switch (conf->sub_style) {
  161. case SUB_VERBATIM:
  162. *p++ = c;
  163. break;
  164. case SUB_LEGACY:
  165. *p++ = c;
  166. /* fallthrough */
  167. default: /* SUB_MAILDIRPP */
  168. *p++ = '.';
  169. break;
  170. }
  171. } else {
  172. *p++ = c;
  173. }
  174. }
  175. *p = 0;
  176. return out;
  177. }
  178. static int
  179. maildir_validate_path( maildir_store_conf_t *conf )
  180. {
  181. struct stat st;
  182. if (stat( conf->gen.path, &st ) || !S_ISDIR(st.st_mode)) {
  183. error( "Maildir error: cannot open store '%s'\n", conf->gen.path );
  184. conf->failed = FAIL_FINAL;
  185. return -1;
  186. }
  187. return 0;
  188. }
  189. static void lcktmr_timeout( void *aux );
  190. static store_t *
  191. maildir_alloc_store( store_conf_t *gconf, const char *label ATTR_UNUSED )
  192. {
  193. maildir_store_t *ctx;
  194. ctx = nfcalloc( sizeof(*ctx) );
  195. ctx->gen.conf = gconf;
  196. ctx->uvfd = -1;
  197. init_wakeup( &ctx->lcktmr, lcktmr_timeout, ctx );
  198. return &ctx->gen;
  199. }
  200. static void
  201. maildir_connect_store( store_t *gctx,
  202. void (*cb)( int sts, void *aux ), void *aux )
  203. {
  204. maildir_store_t *ctx = (maildir_store_t *)gctx;
  205. maildir_store_conf_t *conf = (maildir_store_conf_t *)ctx->gen.conf;
  206. if (conf->gen.path && maildir_validate_path( conf ) < 0) {
  207. cb( DRV_STORE_BAD, aux );
  208. return;
  209. }
  210. if (conf->gen.trash && !(ctx->trash = maildir_join_path( conf, 0, conf->gen.trash ))) {
  211. cb( DRV_STORE_BAD, aux );
  212. return;
  213. }
  214. cb( DRV_OK, aux );
  215. }
  216. static void
  217. free_maildir_messages( message_t *msg )
  218. {
  219. message_t *tmsg;
  220. for (; (tmsg = msg); msg = tmsg) {
  221. tmsg = msg->next;
  222. free( ((maildir_message_t *)msg)->base );
  223. free( msg );
  224. }
  225. }
  226. static void
  227. maildir_cleanup( store_t *gctx )
  228. {
  229. maildir_store_t *ctx = (maildir_store_t *)gctx;
  230. free_maildir_messages( gctx->msgs );
  231. #ifdef USE_DB
  232. if (ctx->db)
  233. ctx->db->close( ctx->db, 0 );
  234. free( ctx->usedb );
  235. #endif /* USE_DB */
  236. free( gctx->path );
  237. free( ctx->excs.data );
  238. if (ctx->uvfd >= 0)
  239. close( ctx->uvfd );
  240. conf_wakeup( &ctx->lcktmr, -1 );
  241. }
  242. static void
  243. maildir_free_store( store_t *gctx )
  244. {
  245. maildir_store_t *ctx = (maildir_store_t *)gctx;
  246. maildir_cleanup( gctx );
  247. wipe_wakeup( &ctx->lcktmr );
  248. free( ctx->trash );
  249. free_string_list( gctx->boxes );
  250. free( gctx );
  251. }
  252. static void
  253. maildir_cleanup_drv( void )
  254. {
  255. }
  256. static void
  257. maildir_invoke_bad_callback( store_t *ctx )
  258. {
  259. ctx->bad_callback( ctx->bad_callback_aux );
  260. }
  261. static int maildir_list_inbox( store_t *gctx, int flags, const char *basePath );
  262. static int maildir_list_path( store_t *gctx, int flags, const char *inbox );
  263. static int
  264. maildir_list_recurse( store_t *gctx, int isBox, int flags,
  265. const char *inbox, int inboxLen, const char *basePath, int basePathLen,
  266. char *path, int pathLen, char *name, int nameLen )
  267. {
  268. DIR *dir;
  269. int style = ((maildir_store_conf_t *)gctx->conf)->sub_style;
  270. int pl, nl, i;
  271. int warned = 0;
  272. struct dirent *de;
  273. struct stat st;
  274. if (!(dir = opendir( path ))) {
  275. if (isBox && (errno == ENOENT || errno == ENOTDIR))
  276. return 0;
  277. sys_error( "Maildir error: cannot list %s", path );
  278. return -1;
  279. }
  280. if (isBox > 1 && style == SUB_UNSET) {
  281. error( "Maildir error: found subfolder '%.*s', but store '%s' does not specify SubFolders style\n",
  282. nameLen - 1, name, gctx->conf->name );
  283. closedir( dir );
  284. return -1;
  285. }
  286. while ((de = readdir( dir ))) {
  287. const char *ent = de->d_name;
  288. if (ent[0] == '.' && (!ent[1] || (ent[1] == '.' && !ent[2])))
  289. continue;
  290. pl = nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "%s", ent );
  291. if (pl == 3 && (!memcmp( ent, "cur", 3 ) || !memcmp( ent, "new", 3 ) || !memcmp( ent, "tmp", 3 )))
  292. continue;
  293. pl += pathLen;
  294. if (inbox && equals( path, pl, inbox, inboxLen )) {
  295. /* Inbox nested into Path. List now if it won't be listed separately anyway. */
  296. if (!(flags & LIST_INBOX) && maildir_list_inbox( gctx, flags, 0 ) < 0) {
  297. closedir( dir );
  298. return -1;
  299. }
  300. } else if (basePath && equals( path, pl, basePath, basePathLen )) {
  301. /* Path nested into Inbox. List now if it won't be listed separately anyway. */
  302. if (!(flags & (LIST_PATH | LIST_PATH_MAYBE)) && maildir_list_path( gctx, flags, 0 ) < 0) {
  303. closedir( dir );
  304. return -1;
  305. }
  306. } else {
  307. char *effName = name;
  308. int nameOff = 0;
  309. if (style == SUB_MAILDIRPP || style == SUB_LEGACY) {
  310. if (*ent == '.') {
  311. if (!isBox)
  312. continue;
  313. ent++;
  314. } else {
  315. if (isBox)
  316. continue;
  317. }
  318. if (style == SUB_MAILDIRPP) {
  319. if (*ent == '.') {
  320. if (!(flags & LIST_INBOX))
  321. continue;
  322. ent++;
  323. } else {
  324. if (!(flags & (LIST_PATH | LIST_PATH_MAYBE)))
  325. continue;
  326. effName = name + 6;
  327. nameOff = 6;
  328. }
  329. }
  330. }
  331. nl = nameLen + nfsnprintf( name + nameLen, _POSIX_PATH_MAX - nameLen, "%s", ent );
  332. if (style == SUB_MAILDIRPP) {
  333. for (i = nameLen; i < nl; i++) {
  334. if (name[i] == '.')
  335. name[i] = '/';
  336. }
  337. }
  338. if (nameLen == nameOff && starts_with( effName, nl - nameOff, "INBOX", 5 ) && (!effName[5] || effName[5] == '/')) {
  339. if (!warned) {
  340. warned = 1;
  341. path[pathLen] = 0;
  342. warn( "Maildir warning: ignoring INBOX in %s\n", path );
  343. }
  344. continue;
  345. }
  346. path[pl++] = '/';
  347. nfsnprintf( path + pl, _POSIX_PATH_MAX - pl, "cur" );
  348. if (!stat( path, &st ) && S_ISDIR(st.st_mode))
  349. add_string_list( &gctx->boxes, effName );
  350. if (style == SUB_MAILDIRPP) {
  351. /* Maildir++ folder - don't recurse further. */
  352. continue;
  353. }
  354. path[pl] = 0;
  355. name[nl++] = '/';
  356. if (maildir_list_recurse( gctx, isBox + 1, flags, inbox, inboxLen, basePath, basePathLen, path, pl, name, nl ) < 0) {
  357. closedir( dir );
  358. return -1;
  359. }
  360. }
  361. }
  362. closedir (dir);
  363. return 0;
  364. }
  365. static int
  366. maildir_list_inbox( store_t *gctx, int flags, const char *basePath )
  367. {
  368. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  369. add_string_list( &gctx->boxes, "INBOX" );
  370. return maildir_list_recurse(
  371. gctx, 1, flags, 0, 0, basePath, basePath ? strlen( basePath ) - 1 : 0,
  372. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s/", ((maildir_store_conf_t *)gctx->conf)->inbox ),
  373. name, nfsnprintf( name, _POSIX_PATH_MAX, "INBOX/" ) );
  374. }
  375. static int
  376. maildir_list_path( store_t *gctx, int flags, const char *inbox )
  377. {
  378. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  379. if (maildir_ensure_path( (maildir_store_conf_t *)gctx->conf ) < 0)
  380. return -1;
  381. return maildir_list_recurse(
  382. gctx, 0, flags, inbox, inbox ? strlen( inbox ) : 0, 0, 0,
  383. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s", gctx->conf->path ),
  384. name, 0 );
  385. }
  386. static void
  387. maildir_list_store( store_t *gctx, int flags,
  388. void (*cb)( int sts, void *aux ), void *aux )
  389. {
  390. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  391. // The Maildir++ Inbox is technically not under Path, so "*" would
  392. // never match INBOX*, which is rather unintuitive. Matching INBOX*
  393. // implicitly instead makes it consistent with an IMAP Store with
  394. // an empty Path.
  395. if (conf->sub_style == SUB_MAILDIRPP)
  396. flags |= LIST_INBOX;
  397. if ((conf->sub_style != SUB_MAILDIRPP
  398. && ((flags & LIST_PATH) || ((flags & LIST_PATH_MAYBE) && gctx->conf->path))
  399. && maildir_list_path( gctx, flags, conf->inbox ) < 0) ||
  400. ((flags & LIST_INBOX)
  401. && maildir_list_inbox( gctx, flags, gctx->conf->path ) < 0)) {
  402. maildir_invoke_bad_callback( gctx );
  403. cb( DRV_CANCELED, aux );
  404. } else {
  405. cb( DRV_OK, aux );
  406. }
  407. }
  408. static const char *subdirs[] = { "cur", "new", "tmp" };
  409. typedef struct {
  410. char *base;
  411. char *msgid;
  412. int size;
  413. uint uid:31, recent:1;
  414. char tuid[TUIDL];
  415. } msg_t;
  416. DEFINE_ARRAY_TYPE(msg_t)
  417. static void
  418. maildir_free_scan( msg_t_array_alloc_t *msglist )
  419. {
  420. int i;
  421. if (msglist->array.data) {
  422. for (i = 0; i < msglist->array.size; i++)
  423. free( msglist->array.data[i].base );
  424. free( msglist->array.data );
  425. }
  426. }
  427. #define _24_HOURS (3600 * 24)
  428. static int
  429. maildir_clear_tmp( char *buf, int bufsz, int bl )
  430. {
  431. DIR *dirp;
  432. struct dirent *entry;
  433. time_t now;
  434. struct stat st;
  435. memcpy( buf + bl, "tmp/", 5 );
  436. bl += 4;
  437. if (!(dirp = opendir( buf ))) {
  438. sys_error( "Maildir error: cannot list %s", buf );
  439. return DRV_BOX_BAD;
  440. }
  441. time( &now );
  442. while ((entry = readdir( dirp ))) {
  443. nfsnprintf( buf + bl, bufsz - bl, "%s", entry->d_name );
  444. if (stat( buf, &st )) {
  445. if (errno != ENOENT)
  446. sys_error( "Maildir error: cannot access %s", buf );
  447. } else if (S_ISREG(st.st_mode) && now - st.st_ctime >= _24_HOURS) {
  448. /* This should happen infrequently enough that it won't be
  449. * bothersome to the user to display when it occurs.
  450. */
  451. notice( "Maildir notice: removing stale file %s\n", buf );
  452. if (unlink( buf ) && errno != ENOENT)
  453. sys_error( "Maildir error: cannot remove %s", buf );
  454. }
  455. }
  456. closedir( dirp );
  457. return DRV_OK;
  458. }
  459. static int
  460. make_box_dir( char *buf, int bl )
  461. {
  462. char *p;
  463. if (!mkdir( buf, 0700 ) || errno == EEXIST)
  464. return 0;
  465. p = memrchr( buf, '/', bl - 1 );
  466. *p = 0;
  467. if (make_box_dir( buf, (int)(p - buf) ))
  468. return -1;
  469. *p = '/';
  470. return mkdir( buf, 0700 );
  471. }
  472. static int
  473. maildir_validate( const char *box, int create, maildir_store_t *ctx )
  474. {
  475. int i, bl, ret;
  476. struct stat st;
  477. char buf[_POSIX_PATH_MAX];
  478. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", box );
  479. if (stat( buf, &st )) {
  480. if (errno != ENOENT) {
  481. sys_error( "Maildir error: cannot access mailbox '%s'", box );
  482. return DRV_BOX_BAD;
  483. }
  484. if (!create)
  485. return DRV_BOX_BAD;
  486. if (make_box_dir( buf, bl )) {
  487. sys_error( "Maildir error: cannot create mailbox '%s'", box );
  488. ((maildir_store_conf_t *)ctx->gen.conf)->failed = FAIL_FINAL;
  489. maildir_invoke_bad_callback( &ctx->gen );
  490. return DRV_CANCELED;
  491. }
  492. } else if (!S_ISDIR(st.st_mode)) {
  493. notdir:
  494. error( "Maildir error: '%s' is no valid mailbox\n", box );
  495. return DRV_BOX_BAD;
  496. }
  497. for (i = 0; i < 3; i++) {
  498. memcpy( buf + bl, subdirs[i], 4 );
  499. if (stat( buf, &st )) {
  500. /* We always create new/ and tmp/ if they are missing. cur/ is the presence indicator. */
  501. if (!i && !create)
  502. return DRV_BOX_BAD;
  503. if (mkdir( buf, 0700 )) {
  504. sys_error( "Maildir error: cannot create directory %s", buf );
  505. return DRV_BOX_BAD;
  506. }
  507. ctx->fresh[i] = 1;
  508. } else if (!S_ISDIR(st.st_mode)) {
  509. goto notdir;
  510. } else {
  511. if (i == 2) {
  512. if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) != DRV_OK)
  513. return ret;
  514. }
  515. }
  516. }
  517. return DRV_OK;
  518. }
  519. #ifdef USE_DB
  520. static void
  521. make_key( const char *info_stop, DBT *tkey, char *name )
  522. {
  523. char *u = strpbrk( name, info_stop );
  524. tkey->data = name;
  525. tkey->size = u ? (size_t)(u - name) : strlen( name );
  526. }
  527. #endif /* USE_DB */
  528. static int
  529. maildir_store_uidval( maildir_store_t *ctx )
  530. {
  531. int n;
  532. #ifdef USE_DB
  533. int ret, uv[2];
  534. #endif
  535. char buf[128];
  536. #ifdef USE_DB
  537. if (ctx->db) {
  538. key.data = (void *)"UIDVALIDITY";
  539. key.size = 11;
  540. uv[0] = ctx->gen.uidvalidity;
  541. uv[1] = ctx->nuid;
  542. value.data = uv;
  543. value.size = sizeof(uv);
  544. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  545. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  546. return DRV_BOX_BAD;
  547. }
  548. if ((ret = ctx->db->sync( ctx->db, 0 ))) {
  549. ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" );
  550. return DRV_BOX_BAD;
  551. }
  552. } else
  553. #endif /* USE_DB */
  554. {
  555. n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid );
  556. lseek( ctx->uvfd, 0, SEEK_SET );
  557. if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n ) || (UseFSync && fdatasync( ctx->uvfd ))) {
  558. error( "Maildir error: cannot write UIDVALIDITY.\n" );
  559. return DRV_BOX_BAD;
  560. }
  561. }
  562. conf_wakeup( &ctx->lcktmr, 2 );
  563. return DRV_OK;
  564. }
  565. static int
  566. maildir_init_uidval( maildir_store_t *ctx )
  567. {
  568. ctx->gen.uidvalidity = time( 0 );
  569. ctx->nuid = 0;
  570. ctx->uvok = 0;
  571. #ifdef USE_DB
  572. if (ctx->db) {
  573. u_int32_t count;
  574. ctx->db->truncate( ctx->db, 0, &count, 0 );
  575. }
  576. #endif /* USE_DB */
  577. return maildir_store_uidval( ctx );
  578. }
  579. static int
  580. maildir_init_uidval_new( maildir_store_t *ctx )
  581. {
  582. notice( "Maildir notice: no UIDVALIDITY, creating new.\n" );
  583. return maildir_init_uidval( ctx );
  584. }
  585. static int
  586. maildir_uidval_lock( maildir_store_t *ctx )
  587. {
  588. int n;
  589. #ifdef USE_DB
  590. int ret;
  591. struct stat st;
  592. #endif
  593. char buf[128];
  594. if (pending_wakeup( &ctx->lcktmr )) {
  595. /* The unlock timer is active, so we are obviously already locked. */
  596. return DRV_OK;
  597. }
  598. /* This (theoretically) works over NFS. Let's hope nobody else did
  599. the same in the opposite order, as we'd deadlock then. */
  600. #if SEEK_SET != 0
  601. lck.l_whence = SEEK_SET;
  602. #endif
  603. lck.l_type = F_WRLCK;
  604. if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
  605. error( "Maildir error: cannot fcntl lock UIDVALIDITY.\n" );
  606. return DRV_BOX_BAD;
  607. }
  608. #ifdef USE_DB
  609. if (ctx->usedb) {
  610. if (fstat( ctx->uvfd, &st )) {
  611. sys_error( "Maildir error: cannot fstat UID database" );
  612. return DRV_BOX_BAD;
  613. }
  614. if (db_create( &ctx->db, 0, 0 )) {
  615. fputs( "Maildir error: db_create() failed\n", stderr );
  616. return DRV_BOX_BAD;
  617. }
  618. if ((ret = (ctx->db->open)( ctx->db, 0, ctx->usedb, 0, DB_HASH,
  619. st.st_size ? 0 : DB_CREATE | DB_TRUNCATE, 0 ))) {
  620. ctx->db->err( ctx->db, ret, "Maildir error: db->open(%s)", ctx->usedb );
  621. return DRV_BOX_BAD;
  622. }
  623. key.data = (void *)"UIDVALIDITY";
  624. key.size = 11;
  625. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  626. if (ret != DB_NOTFOUND) {
  627. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  628. return DRV_BOX_BAD;
  629. }
  630. return maildir_init_uidval_new( ctx );
  631. }
  632. ctx->gen.uidvalidity = ((int *)value.data)[0];
  633. ctx->nuid = ((int *)value.data)[1];
  634. } else
  635. #endif
  636. {
  637. lseek( ctx->uvfd, 0, SEEK_SET );
  638. if ((n = read( ctx->uvfd, buf, sizeof(buf) - 1 )) <= 0 ||
  639. (buf[n] = 0, sscanf( buf, "%d\n%d", &ctx->gen.uidvalidity, &ctx->nuid ) != 2)) {
  640. #if 1
  641. /* In a generic driver, resetting the UID validity would be the right thing.
  642. * But this would mess up the sync state completely. So better bail out and
  643. * give the user a chance to fix the mailbox. */
  644. if (n) {
  645. error( "Maildir error: cannot read UIDVALIDITY.\n" );
  646. return DRV_BOX_BAD;
  647. }
  648. #endif
  649. return maildir_init_uidval_new( ctx );
  650. }
  651. }
  652. ctx->uvok = 1;
  653. conf_wakeup( &ctx->lcktmr, 2 );
  654. return DRV_OK;
  655. }
  656. static void
  657. maildir_uidval_unlock( maildir_store_t *ctx )
  658. {
  659. #ifdef USE_DB
  660. if (ctx->db) {
  661. ctx->db->close( ctx->db, 0 );
  662. ctx->db = 0;
  663. }
  664. #endif /* USE_DB */
  665. lck.l_type = F_UNLCK;
  666. fcntl( ctx->uvfd, F_SETLK, &lck );
  667. }
  668. static void
  669. lcktmr_timeout( void *aux )
  670. {
  671. maildir_uidval_unlock( (maildir_store_t *)aux );
  672. }
  673. static int
  674. maildir_obtain_uid( maildir_store_t *ctx, int *uid )
  675. {
  676. int ret;
  677. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  678. return ret;
  679. *uid = ++ctx->nuid;
  680. return maildir_store_uidval( ctx );
  681. }
  682. #ifdef USE_DB
  683. static int
  684. maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
  685. {
  686. int ret;
  687. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  688. return ret;
  689. *uid = ++ctx->nuid;
  690. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  691. value.data = uid;
  692. value.size = sizeof(*uid);
  693. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  694. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  695. return DRV_BOX_BAD;
  696. }
  697. return maildir_store_uidval( ctx );
  698. }
  699. #endif
  700. static int
  701. maildir_compare( const void *l, const void *r )
  702. {
  703. msg_t *lm = (msg_t *)l, *rm = (msg_t *)r;
  704. char *ldot, *rdot, *ldot2, *rdot2, *lseq, *rseq;
  705. int ret, llen, rlen;
  706. if ((ret = lm->uid - rm->uid))
  707. return ret;
  708. /* No UID, so sort by arrival date. We should not do this, but we rely
  709. on the suggested unique file name scheme - we have no choice. */
  710. /* The first field are always the seconds. Alphabetical sort should be
  711. faster than numeric. */
  712. if (!(ldot = strchr( lm->base, '.' )) || !(rdot = strchr( rm->base, '.' )))
  713. goto stronly; /* Should never happen ... */
  714. llen = ldot - lm->base, rlen = rdot - rm->base;
  715. /* The shorter number is smaller. Really. This won't trigger with any
  716. mail created after Sep 9 2001 anyway. */
  717. if ((ret = llen - rlen))
  718. return ret;
  719. if ((ret = memcmp( lm->base, rm->base, llen )))
  720. return ret;
  721. ldot++, rdot++;
  722. if ((llen = strtol( ldot, &ldot2, 10 ))) {
  723. if (!(rlen = strtol( rdot, &rdot2, 10 )))
  724. goto stronly; /* Comparing apples to oranges ... */
  725. /* Classical PID specs */
  726. if ((ret = llen - rlen)) {
  727. retpid:
  728. /* Handle PID wraparound. This works only on systems
  729. where PIDs are not reused too fast */
  730. if (ret > 20000 || ret < -20000)
  731. ret = -ret;
  732. return ret;
  733. }
  734. return (*ldot2 != '_' ? 0 : atoi( ldot2 + 1 )) -
  735. (*rdot2 != '_' ? 0 : atoi( rdot2 + 1 ));
  736. }
  737. if (!(ldot2 = strchr( ldot, '.' )) || !(rdot2 = strchr( rdot, '.' )))
  738. goto stronly; /* Should never happen ... */
  739. llen = ldot2 - ldot, rlen = rdot2 - rdot;
  740. if (((lseq = memchr( ldot, '#', llen )) && (rseq = memchr( rdot, '#', rlen ))) ||
  741. ((lseq = memchr( ldot, 'M', llen )) && (rseq = memchr( rdot, 'M', rlen ))))
  742. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  743. if ((lseq = memchr( ldot, 'P', llen )) && (rseq = memchr( rdot, 'P', rlen ))) {
  744. if ((ret = atoi( lseq + 1 ) - atoi( rseq + 1 )))
  745. goto retpid;
  746. if ((lseq = memchr( ldot, 'Q', llen )) && (rseq = memchr( rdot, 'Q', rlen )))
  747. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  748. }
  749. stronly:
  750. /* Fall-back, so the sort order is defined at all */
  751. return strcmp( lm->base, rm->base );
  752. }
  753. static int
  754. maildir_scan( maildir_store_t *ctx, msg_t_array_alloc_t *msglist )
  755. {
  756. maildir_store_conf_t *conf = (maildir_store_conf_t *)ctx->gen.conf;
  757. DIR *d;
  758. FILE *f;
  759. struct dirent *e;
  760. const char *u, *ru;
  761. #ifdef USE_DB
  762. DB *tdb;
  763. DBC *dbc;
  764. #endif /* USE_DB */
  765. msg_t *entry;
  766. int i, uid, bl, fnl, ret;
  767. time_t now, stamps[2];
  768. struct stat st;
  769. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  770. again:
  771. ARRAY_INIT( msglist );
  772. ctx->gen.count = ctx->gen.recent = 0;
  773. if (ctx->uvok || ctx->maxuid == INT_MAX) {
  774. #ifdef USE_DB
  775. if (ctx->usedb) {
  776. if (db_create( &tdb, 0, 0 )) {
  777. fputs( "Maildir error: db_create() failed\n", stderr );
  778. return DRV_BOX_BAD;
  779. }
  780. if ((tdb->open)( tdb, 0, 0, 0, DB_HASH, DB_CREATE, 0 )) {
  781. fputs( "Maildir error: tdb->open() failed\n", stderr );
  782. bork:
  783. tdb->close( tdb, 0 );
  784. return DRV_BOX_BAD;
  785. }
  786. }
  787. #endif /* USE_DB */
  788. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", ctx->gen.path );
  789. restat:
  790. now = time( 0 );
  791. for (i = 0; i < 2; i++) {
  792. memcpy( buf + bl, subdirs[i], 4 );
  793. if (stat( buf, &st )) {
  794. sys_error( "Maildir error: cannot stat %s", buf );
  795. goto dfail;
  796. }
  797. if (st.st_mtime == now && !(DFlags & ZERODELAY) && !ctx->fresh[i]) {
  798. /* If the modification happened during this second, we wouldn't be able to
  799. * tell if there were further modifications during this second. So wait.
  800. * This has the nice side effect that we wait for "batches" of changes to
  801. * complete. On the downside, it can potentially block indefinitely. */
  802. notice( "Maildir notice: sleeping due to recent directory modification.\n" );
  803. sleep( 1 ); /* FIXME: should make this async */
  804. goto restat;
  805. }
  806. stamps[i] = st.st_mtime;
  807. }
  808. for (i = 0; i < 2; i++) {
  809. memcpy( buf + bl, subdirs[i], 4 );
  810. if (!(d = opendir( buf ))) {
  811. sys_error( "Maildir error: cannot list %s", buf );
  812. rfail:
  813. maildir_free_scan( msglist );
  814. dfail:
  815. #ifdef USE_DB
  816. if (ctx->usedb)
  817. tdb->close( tdb, 0 );
  818. #endif /* USE_DB */
  819. return DRV_BOX_BAD;
  820. }
  821. while ((e = readdir( d ))) {
  822. if (*e->d_name == '.')
  823. continue;
  824. ctx->gen.count++;
  825. ctx->gen.recent += i;
  826. #ifdef USE_DB
  827. if (ctx->usedb) {
  828. if (maildir_uidval_lock( ctx ) != DRV_OK)
  829. goto mbork;
  830. make_key( conf->info_stop, &key, e->d_name );
  831. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  832. if (ret != DB_NOTFOUND) {
  833. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  834. mbork:
  835. maildir_free_scan( msglist );
  836. closedir( d );
  837. goto bork;
  838. }
  839. uid = INT_MAX;
  840. } else {
  841. value.size = 0;
  842. if ((ret = tdb->put( tdb, 0, &key, &value, 0 ))) {
  843. tdb->err( tdb, ret, "Maildir error: tdb->put()" );
  844. goto mbork;
  845. }
  846. uid = *(int *)value.data;
  847. }
  848. } else
  849. #endif /* USE_DB */
  850. {
  851. uid = (ctx->uvok && (u = strstr( e->d_name, ",U=" ))) ? atoi( u + 3 ) : 0;
  852. if (!uid)
  853. uid = INT_MAX;
  854. }
  855. if (uid <= ctx->maxuid) {
  856. if (uid < ctx->minuid && !find_int_array( ctx->excs, uid ))
  857. continue;
  858. entry = msg_t_array_append( msglist );
  859. entry->base = nfstrdup( e->d_name );
  860. entry->msgid = 0;
  861. entry->uid = uid;
  862. entry->recent = i;
  863. entry->size = 0;
  864. entry->tuid[0] = 0;
  865. }
  866. }
  867. closedir( d );
  868. }
  869. for (i = 0; i < 2; i++) {
  870. memcpy( buf + bl, subdirs[i], 4 );
  871. if (stat( buf, &st )) {
  872. sys_error( "Maildir error: cannot re-stat %s", buf );
  873. goto rfail;
  874. }
  875. if (st.st_mtime != stamps[i]) {
  876. /* Somebody messed with the mailbox since we started listing it. */
  877. #ifdef USE_DB
  878. if (ctx->usedb)
  879. tdb->close( tdb, 0 );
  880. #endif /* USE_DB */
  881. maildir_free_scan( msglist );
  882. goto again;
  883. }
  884. }
  885. #ifdef USE_DB
  886. if (ctx->usedb) {
  887. if (maildir_uidval_lock( ctx ) != DRV_OK)
  888. ;
  889. else if ((ret = ctx->db->cursor( ctx->db, 0, &dbc, 0 )))
  890. ctx->db->err( ctx->db, ret, "Maildir error: db->cursor()" );
  891. else {
  892. for (;;) {
  893. if ((ret = dbc->c_get( dbc, &key, &value, DB_NEXT ))) {
  894. if (ret != DB_NOTFOUND)
  895. ctx->db->err( ctx->db, ret, "Maildir error: db->c_get()" );
  896. break;
  897. }
  898. if (!equals( key.data, key.size, "UIDVALIDITY", 11 ) &&
  899. (ret = tdb->get( tdb, 0, &key, &value, 0 ))) {
  900. if (ret != DB_NOTFOUND) {
  901. tdb->err( tdb, ret, "Maildir error: tdb->get()" );
  902. break;
  903. }
  904. if ((ret = dbc->c_del( dbc, 0 ))) {
  905. ctx->db->err( ctx->db, ret, "Maildir error: db->c_del()" );
  906. break;
  907. }
  908. }
  909. }
  910. dbc->c_close( dbc );
  911. }
  912. tdb->close( tdb, 0 );
  913. }
  914. #endif /* USE_DB */
  915. qsort( msglist->array.data, msglist->array.size, sizeof(msg_t), maildir_compare );
  916. for (uid = i = 0; i < msglist->array.size; i++) {
  917. entry = &msglist->array.data[i];
  918. if (entry->uid != INT_MAX) {
  919. if (uid == entry->uid) {
  920. #if 1
  921. /* See comment in maildir_uidval_lock() why this is fatal. */
  922. error( "Maildir error: duplicate UID %d.\n", uid );
  923. maildir_free_scan( msglist );
  924. return DRV_BOX_BAD;
  925. #else
  926. notice( "Maildir notice: duplicate UID; changing UIDVALIDITY.\n");
  927. if ((ret = maildir_init_uid( ctx )) != DRV_OK) {
  928. maildir_free_scan( msglist );
  929. return ret;
  930. }
  931. maildir_free_scan( msglist );
  932. goto again;
  933. #endif
  934. }
  935. uid = entry->uid;
  936. if (uid > ctx->nuid) {
  937. /* In principle, we could just warn and top up nuid. However, getting into this
  938. * situation might indicate some serious trouble, so let's not make it worse. */
  939. error( "Maildir error: UID %d is beyond highest assigned UID %d.\n", uid, ctx->nuid );
  940. maildir_free_scan( msglist );
  941. return DRV_BOX_BAD;
  942. }
  943. fnl = 0;
  944. #ifdef USE_DB
  945. } else if (ctx->usedb) {
  946. if ((ret = maildir_set_uid( ctx, entry->base, &uid )) != DRV_OK) {
  947. maildir_free_scan( msglist );
  948. return ret;
  949. }
  950. entry->uid = uid;
  951. fnl = 0;
  952. #endif /* USE_DB */
  953. } else {
  954. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  955. maildir_free_scan( msglist );
  956. return ret;
  957. }
  958. entry->uid = uid;
  959. if ((u = strstr( entry->base, ",U=" )))
  960. for (ru = u + 3; isdigit( (uchar)*ru ); ru++);
  961. else
  962. u = ru = strchr( entry->base, conf->info_delimiter );
  963. fnl = (u ?
  964. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%.*s,U=%d%s", subdirs[entry->recent], (int)(u - entry->base), entry->base, uid, ru ) :
  965. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s,U=%d", subdirs[entry->recent], entry->base, uid ))
  966. - 4;
  967. memcpy( nbuf, buf, bl + 4 );
  968. nfsnprintf( nbuf + bl + 4, sizeof(nbuf) - bl - 4, "%s", entry->base );
  969. if (rename( nbuf, buf )) {
  970. if (errno != ENOENT) {
  971. sys_error( "Maildir error: cannot rename %s to %s", nbuf, buf );
  972. fail:
  973. maildir_free_scan( msglist );
  974. return DRV_BOX_BAD;
  975. }
  976. retry:
  977. maildir_free_scan( msglist );
  978. goto again;
  979. }
  980. free( entry->base );
  981. entry->base = nfstrndup( buf + bl + 4, fnl );
  982. }
  983. int want_size = (uid > ctx->seenuid) ? (ctx->gen.opts & OPEN_NEW_SIZE) : (ctx->gen.opts & OPEN_OLD_SIZE);
  984. int want_tuid = ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid);
  985. int want_msgid = ((ctx->gen.opts & OPEN_OLD_IDS) && uid <= ctx->seenuid);
  986. if (!want_size && !want_tuid && !want_msgid)
  987. continue;
  988. if (!fnl)
  989. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  990. if (want_size) {
  991. if (stat( buf, &st )) {
  992. if (errno != ENOENT) {
  993. sys_error( "Maildir error: cannot stat %s", buf );
  994. goto fail;
  995. }
  996. goto retry;
  997. }
  998. entry->size = st.st_size;
  999. }
  1000. if (want_tuid || want_msgid) {
  1001. if (!(f = fopen( buf, "r" ))) {
  1002. if (errno != ENOENT) {
  1003. sys_error( "Maildir error: cannot open %s", buf );
  1004. goto fail;
  1005. }
  1006. goto retry;
  1007. }
  1008. int off, in_msgid = 0;
  1009. while ((want_tuid || want_msgid) && fgets( nbuf, sizeof(nbuf), f )) {
  1010. int bufl = strlen( nbuf );
  1011. if (bufl && nbuf[bufl - 1] == '\n')
  1012. --bufl;
  1013. if (bufl && nbuf[bufl - 1] == '\r')
  1014. --bufl;
  1015. if (!bufl)
  1016. break;
  1017. if (want_tuid && starts_with( nbuf, bufl, "X-TUID: ", 8 )) {
  1018. if (bufl < 8 + TUIDL) {
  1019. error( "Maildir error: malformed X-TUID header (UID %d)\n", uid );
  1020. continue;
  1021. }
  1022. memcpy( entry->tuid, nbuf + 8, TUIDL );
  1023. want_tuid = 0;
  1024. in_msgid = 0;
  1025. continue;
  1026. }
  1027. if (want_msgid && starts_with_upper( nbuf, bufl, "MESSAGE-ID:", 11 )) {
  1028. off = 11;
  1029. } else if (in_msgid) {
  1030. if (!isspace( nbuf[0] )) {
  1031. in_msgid = 0;
  1032. continue;
  1033. }
  1034. off = 1;
  1035. } else {
  1036. continue;
  1037. }
  1038. while (off < bufl && isspace( nbuf[off] ))
  1039. off++;
  1040. if (off == bufl) {
  1041. in_msgid = 1;
  1042. continue;
  1043. }
  1044. entry->msgid = nfstrndup( nbuf + off, bufl - off );
  1045. want_msgid = 0;
  1046. in_msgid = 0;
  1047. }
  1048. fclose( f );
  1049. }
  1050. }
  1051. ctx->uvok = 1;
  1052. }
  1053. return DRV_OK;
  1054. }
  1055. static void
  1056. maildir_init_msg( maildir_store_t *ctx, maildir_message_t *msg, msg_t *entry )
  1057. {
  1058. msg->base = entry->base;
  1059. entry->base = 0; /* prevent deletion */
  1060. msg->gen.msgid = entry->msgid;
  1061. entry->msgid = 0; /* prevent deletion */
  1062. msg->gen.size = entry->size;
  1063. msg->gen.srec = 0;
  1064. memcpy( msg->gen.tuid, entry->tuid, TUIDL );
  1065. if (entry->recent)
  1066. msg->gen.status |= M_RECENT;
  1067. if (ctx->gen.opts & OPEN_FLAGS) {
  1068. msg->gen.status |= M_FLAGS;
  1069. msg->gen.flags = maildir_parse_flags( ((maildir_store_conf_t *)ctx->gen.conf)->info_prefix, msg->base );
  1070. } else
  1071. msg->gen.flags = 0;
  1072. }
  1073. static void
  1074. maildir_app_msg( maildir_store_t *ctx, message_t ***msgapp, msg_t *entry )
  1075. {
  1076. maildir_message_t *msg = nfmalloc( sizeof(*msg) );
  1077. msg->gen.next = **msgapp;
  1078. **msgapp = &msg->gen;
  1079. *msgapp = &msg->gen.next;
  1080. msg->gen.uid = entry->uid;
  1081. msg->gen.status = 0;
  1082. maildir_init_msg( ctx, msg, entry );
  1083. }
  1084. static int
  1085. maildir_select_box( store_t *gctx, const char *name )
  1086. {
  1087. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  1088. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1089. maildir_cleanup( gctx );
  1090. gctx->msgs = 0;
  1091. ctx->excs.data = 0;
  1092. ctx->uvfd = -1;
  1093. #ifdef USE_DB
  1094. ctx->db = 0;
  1095. ctx->usedb = 0;
  1096. #endif /* USE_DB */
  1097. ctx->fresh[0] = ctx->fresh[1] = 0;
  1098. if (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) {
  1099. if (!name[5]) {
  1100. gctx->path = nfstrdup( conf->inbox );
  1101. ctx->is_inbox = 1;
  1102. } else {
  1103. gctx->path = maildir_join_path( conf, 1, name + 5 );
  1104. ctx->is_inbox = 0;
  1105. }
  1106. } else {
  1107. if (!(gctx->path = maildir_join_path( conf, 0, name )))
  1108. return DRV_CANCELED;
  1109. ctx->is_inbox = 0;
  1110. }
  1111. return gctx->path ? DRV_OK : DRV_BOX_BAD;
  1112. }
  1113. static void
  1114. maildir_open_box( store_t *gctx,
  1115. void (*cb)( int sts, void *aux ), void *aux )
  1116. {
  1117. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1118. int ret;
  1119. char uvpath[_POSIX_PATH_MAX];
  1120. if ((ret = maildir_validate( gctx->path, ctx->is_inbox, ctx )) != DRV_OK)
  1121. goto bail;
  1122. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  1123. #ifndef USE_DB
  1124. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) < 0) {
  1125. sys_error( "Maildir error: cannot write %s", uvpath );
  1126. cb( DRV_BOX_BAD, aux );
  1127. return;
  1128. }
  1129. #else
  1130. ctx->usedb = 0;
  1131. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1132. nfsnprintf( uvpath, sizeof(uvpath), "%s/.isyncuidmap.db", gctx->path );
  1133. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1134. if (((maildir_store_conf_t *)gctx->conf)->alt_map) {
  1135. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1136. goto dbok;
  1137. } else {
  1138. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  1139. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1140. goto fnok;
  1141. }
  1142. sys_error( "Maildir error: cannot write %s", uvpath );
  1143. cb( DRV_BOX_BAD, aux );
  1144. return;
  1145. } else {
  1146. dbok:
  1147. ctx->usedb = nfstrdup( uvpath );
  1148. }
  1149. }
  1150. fnok:
  1151. #endif /* USE_DB */
  1152. ret = maildir_uidval_lock( ctx );
  1153. bail:
  1154. cb( ret, aux );
  1155. }
  1156. static void
  1157. maildir_create_box( store_t *gctx,
  1158. void (*cb)( int sts, void *aux ), void *aux )
  1159. {
  1160. cb( maildir_validate( gctx->path, 1, (maildir_store_t *)gctx ), aux );
  1161. }
  1162. static int
  1163. maildir_confirm_box_empty( store_t *gctx )
  1164. {
  1165. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1166. msg_t_array_alloc_t msglist;
  1167. ctx->excs.size = ctx->minuid = ctx->maxuid = ctx->newuid = 0;
  1168. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1169. return DRV_BOX_BAD;
  1170. maildir_free_scan( &msglist );
  1171. return gctx->count ? DRV_BOX_BAD : DRV_OK;
  1172. }
  1173. static void
  1174. maildir_delete_box( store_t *gctx,
  1175. void (*cb)( int sts, void *aux ), void *aux )
  1176. {
  1177. int i, bl, ret = DRV_OK;
  1178. struct stat st;
  1179. char buf[_POSIX_PATH_MAX];
  1180. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", gctx->path );
  1181. if (stat( buf, &st )) {
  1182. if (errno != ENOENT) {
  1183. sys_error( "Maildir error: cannot access mailbox '%s'", gctx->path );
  1184. ret = DRV_BOX_BAD;
  1185. }
  1186. } else if (!S_ISDIR(st.st_mode)) {
  1187. error( "Maildir error: '%s' is no valid mailbox\n", gctx->path );
  1188. ret = DRV_BOX_BAD;
  1189. } else if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) == DRV_OK) {
  1190. nfsnprintf( buf + bl, sizeof(buf) - bl, ".uidvalidity" );
  1191. if (unlink( buf ) && errno != ENOENT)
  1192. goto badrm;
  1193. #ifdef USE_DB
  1194. nfsnprintf( buf + bl, sizeof(buf) - bl, ".isyncuidmap.db" );
  1195. if (unlink( buf ) && errno != ENOENT)
  1196. goto badrm;
  1197. #endif
  1198. /* We delete cur/ last, as it is the indicator for a present mailbox.
  1199. * That way an interrupted operation can be resumed. */
  1200. for (i = 3; --i >= 0; ) {
  1201. memcpy( buf + bl, subdirs[i], 4 );
  1202. if (rmdir( buf ) && errno != ENOENT) {
  1203. badrm:
  1204. sys_error( "Maildir error: cannot remove '%s'", buf );
  1205. ret = DRV_BOX_BAD;
  1206. break;
  1207. }
  1208. }
  1209. }
  1210. cb( ret, aux );
  1211. }
  1212. static int
  1213. maildir_finish_delete_box( store_t *gctx )
  1214. {
  1215. /* Subfolders are not deleted; the deleted folder is only "stripped of its mailboxness".
  1216. * Consequently, the rmdir may legitimately fail. This behavior follows the IMAP spec. */
  1217. if (rmdir( gctx->path ) && errno != ENOENT && errno != ENOTEMPTY) {
  1218. sys_error( "Maildir warning: cannot remove '%s'", gctx->path );
  1219. return DRV_BOX_BAD;
  1220. }
  1221. return DRV_OK;
  1222. }
  1223. static void
  1224. maildir_prepare_load_box( store_t *gctx, int opts )
  1225. {
  1226. if (opts & OPEN_SETFLAGS)
  1227. opts |= OPEN_OLD;
  1228. if (opts & OPEN_EXPUNGE)
  1229. opts |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  1230. gctx->opts = opts;
  1231. }
  1232. static void
  1233. maildir_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int seenuid, int_array_t excs,
  1234. void (*cb)( int sts, void *aux ), void *aux )
  1235. {
  1236. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1237. message_t **msgapp;
  1238. msg_t_array_alloc_t msglist;
  1239. int i;
  1240. ctx->minuid = minuid;
  1241. ctx->maxuid = maxuid;
  1242. ctx->newuid = newuid;
  1243. ctx->seenuid = seenuid;
  1244. ARRAY_SQUEEZE( &excs );
  1245. ctx->excs = excs;
  1246. if (maildir_scan( ctx, &msglist ) != DRV_OK) {
  1247. cb( DRV_BOX_BAD, aux );
  1248. return;
  1249. }
  1250. msgapp = &ctx->gen.msgs;
  1251. for (i = 0; i < msglist.array.size; i++)
  1252. maildir_app_msg( ctx, &msgapp, msglist.array.data + i );
  1253. maildir_free_scan( &msglist );
  1254. cb( DRV_OK, aux );
  1255. }
  1256. static int
  1257. maildir_rescan( maildir_store_t *ctx )
  1258. {
  1259. message_t **msgapp;
  1260. maildir_message_t *msg;
  1261. msg_t_array_alloc_t msglist;
  1262. int i;
  1263. ctx->fresh[0] = ctx->fresh[1] = 0;
  1264. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1265. return DRV_BOX_BAD;
  1266. for (msgapp = &ctx->gen.msgs, i = 0;
  1267. (msg = (maildir_message_t *)*msgapp) || i < msglist.array.size; )
  1268. {
  1269. if (!msg) {
  1270. #if 0
  1271. debug( "adding new message %d\n", msglist.array.data[i].uid );
  1272. maildir_app_msg( ctx, &msgapp, msglist.array.data + i );
  1273. #else
  1274. debug( "ignoring new message %d\n", msglist.array.data[i].uid );
  1275. #endif
  1276. i++;
  1277. } else if (i >= msglist.array.size) {
  1278. debug( "purging deleted message %d\n", msg->gen.uid );
  1279. msg->gen.status = M_DEAD;
  1280. msgapp = &msg->gen.next;
  1281. } else if (msglist.array.data[i].uid < msg->gen.uid) {
  1282. /* this should not happen, actually */
  1283. #if 0
  1284. debug( "adding new message %d\n", msglist.array.data[i].uid );
  1285. maildir_app_msg( ctx, &msgapp, msglist.array.data + i );
  1286. #else
  1287. debug( "ignoring new message %d\n", msglist.array.data[i].uid );
  1288. #endif
  1289. i++;
  1290. } else if (msglist.array.data[i].uid > msg->gen.uid) {
  1291. debug( "purging deleted message %d\n", msg->gen.uid );
  1292. msg->gen.status = M_DEAD;
  1293. msgapp = &msg->gen.next;
  1294. } else {
  1295. debug( "updating message %d\n", msg->gen.uid );
  1296. msg->gen.status &= ~(M_FLAGS|M_RECENT);
  1297. free( msg->base );
  1298. free( msg->gen.msgid );
  1299. maildir_init_msg( ctx, msg, msglist.array.data + i );
  1300. i++, msgapp = &msg->gen.next;
  1301. }
  1302. }
  1303. maildir_free_scan( &msglist );
  1304. return DRV_OK;
  1305. }
  1306. static int
  1307. maildir_again( maildir_store_t *ctx, maildir_message_t *msg,
  1308. const char *err, const char *fn, const char *fn2 )
  1309. {
  1310. int ret;
  1311. if (errno != ENOENT) {
  1312. sys_error( err, fn, fn2 );
  1313. return DRV_BOX_BAD;
  1314. }
  1315. if ((ret = maildir_rescan( ctx )) != DRV_OK)
  1316. return ret;
  1317. return (msg->gen.status & M_DEAD) ? DRV_MSG_BAD : DRV_OK;
  1318. }
  1319. static void
  1320. maildir_fetch_msg( store_t *gctx, message_t *gmsg, msg_data_t *data,
  1321. void (*cb)( int sts, void *aux ), void *aux )
  1322. {
  1323. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1324. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1325. int fd, ret;
  1326. struct stat st;
  1327. char buf[_POSIX_PATH_MAX];
  1328. for (;;) {
  1329. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1330. if ((fd = open( buf, O_RDONLY )) >= 0)
  1331. break;
  1332. if ((ret = maildir_again( ctx, msg, "Cannot open %s", buf, 0 )) != DRV_OK) {
  1333. cb( ret, aux );
  1334. return;
  1335. }
  1336. }
  1337. fstat( fd, &st );
  1338. data->len = st.st_size;
  1339. if (data->date == -1)
  1340. data->date = st.st_mtime;
  1341. data->data = nfmalloc( data->len );
  1342. if (read( fd, data->data, data->len ) != data->len) {
  1343. sys_error( "Maildir error: cannot read %s", buf );
  1344. close( fd );
  1345. cb( DRV_MSG_BAD, aux );
  1346. return;
  1347. }
  1348. close( fd );
  1349. if (!(gmsg->status & M_FLAGS))
  1350. data->flags = maildir_parse_flags( ((maildir_store_conf_t *)gctx->conf)->info_prefix, msg->base );
  1351. cb( DRV_OK, aux );
  1352. }
  1353. static int
  1354. maildir_make_flags( char info_delimiter, int flags, char *buf )
  1355. {
  1356. uint i, d;
  1357. buf[0] = info_delimiter;
  1358. buf[1] = '2';
  1359. buf[2] = ',';
  1360. for (d = 3, i = 0; i < as(Flags); i++)
  1361. if (flags & (1 << i))
  1362. buf[d++] = Flags[i];
  1363. buf[d] = 0;
  1364. return d;
  1365. }
  1366. static void
  1367. maildir_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1368. void (*cb)( int sts, int uid, void *aux ), void *aux )
  1369. {
  1370. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1371. const char *box;
  1372. int ret, fd, bl, uid;
  1373. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX], fbuf[NUM_FLAGS + 3], base[128];
  1374. bl = nfsnprintf( base, sizeof(base), "%ld.%d_%d.%s", (long)time( 0 ), Pid, ++MaildirCount, Hostname );
  1375. if (!to_trash) {
  1376. #ifdef USE_DB
  1377. if (ctx->usedb) {
  1378. if ((ret = maildir_set_uid( ctx, base, &uid )) != DRV_OK) {
  1379. free( data->data );
  1380. cb( ret, 0, aux );
  1381. return;
  1382. }
  1383. } else
  1384. #endif /* USE_DB */
  1385. {
  1386. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  1387. free( data->data );
  1388. cb( ret, 0, aux );
  1389. return;
  1390. }
  1391. nfsnprintf( base + bl, sizeof(base) - bl, ",U=%d", uid );
  1392. }
  1393. box = gctx->path;
  1394. } else {
  1395. uid = 0;
  1396. box = ctx->trash;
  1397. }
  1398. maildir_make_flags( ((maildir_store_conf_t *)gctx->conf)->info_delimiter, data->flags, fbuf );
  1399. nfsnprintf( buf, sizeof(buf), "%s/tmp/%s%s", box, base, fbuf );
  1400. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1401. if (errno != ENOENT || !to_trash) {
  1402. sys_error( "Maildir error: cannot create %s", buf );
  1403. free( data->data );
  1404. cb( DRV_BOX_BAD, 0, aux );
  1405. return;
  1406. }
  1407. if ((ret = maildir_validate( box, 1, ctx )) != DRV_OK) {
  1408. free( data->data );
  1409. cb( ret, 0, aux );
  1410. return;
  1411. }
  1412. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1413. sys_error( "Maildir error: cannot create %s", buf );
  1414. free( data->data );
  1415. cb( DRV_BOX_BAD, 0, aux );
  1416. return;
  1417. }
  1418. }
  1419. ret = write( fd, data->data, data->len );
  1420. free( data->data );
  1421. if (ret != data->len || (UseFSync && (ret = fsync( fd )))) {
  1422. if (ret < 0)
  1423. sys_error( "Maildir error: cannot write %s", buf );
  1424. else
  1425. error( "Maildir error: cannot write %s. Disk full?\n", buf );
  1426. close( fd );
  1427. cb( DRV_BOX_BAD, 0, aux );
  1428. return;
  1429. }
  1430. if (close( fd ) < 0) {
  1431. /* Quota exceeded may cause this. */
  1432. sys_error( "Maildir error: cannot write %s", buf );
  1433. cb( DRV_BOX_BAD, 0, aux );
  1434. return;
  1435. }
  1436. if (data->date) {
  1437. /* Set atime and mtime according to INTERNALDATE or mtime of source message */
  1438. struct utimbuf utimebuf;
  1439. utimebuf.actime = utimebuf.modtime = data->date;
  1440. if (utime( buf, &utimebuf ) < 0) {
  1441. sys_error( "Maildir error: cannot set times for %s", buf );
  1442. cb( DRV_BOX_BAD, 0, aux );
  1443. return;
  1444. }
  1445. }
  1446. /* Moving seen messages to cur/ is strictly speaking incorrect, but makes mutt happy. */
  1447. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%s%s", box, subdirs[!(data->flags & F_SEEN)], base, fbuf );
  1448. if (rename( buf, nbuf )) {
  1449. sys_error( "Maildir error: cannot rename %s to %s", buf, nbuf );
  1450. cb( DRV_BOX_BAD, 0, aux );
  1451. return;
  1452. }
  1453. cb( DRV_OK, uid, aux );
  1454. }
  1455. static void
  1456. maildir_find_new_msgs( store_t *gctx ATTR_UNUSED, int newuid ATTR_UNUSED,
  1457. void (*cb)( int sts, void *aux ) ATTR_UNUSED, void *aux ATTR_UNUSED )
  1458. {
  1459. assert( !"maildir_find_new_msgs is not supposed to be called" );
  1460. }
  1461. static void
  1462. maildir_set_msg_flags( store_t *gctx, message_t *gmsg, int uid ATTR_UNUSED, int add, int del,
  1463. void (*cb)( int sts, void *aux ), void *aux )
  1464. {
  1465. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  1466. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1467. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1468. char *s, *p;
  1469. uint i;
  1470. int j, ret, ol, fl, bbl, bl, tl;
  1471. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1472. bbl = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1473. memcpy( nbuf, gctx->path, bbl - 1 );
  1474. memcpy( nbuf + bbl - 1, "/cur/", 5 );
  1475. for (;;) {
  1476. bl = bbl + nfsnprintf( buf + bbl, sizeof(buf) - bbl, "%s/", subdirs[gmsg->status & M_RECENT] );
  1477. ol = strlen( msg->base );
  1478. if ((int)sizeof(buf) - bl < ol + 3 + NUM_FLAGS)
  1479. oob();
  1480. memcpy( buf + bl, msg->base, ol + 1 );
  1481. memcpy( nbuf + bl, msg->base, ol + 1 );
  1482. if ((s = strstr( nbuf + bl, conf->info_prefix ))) {
  1483. s += 3;
  1484. fl = ol - (s - (nbuf + bl));
  1485. for (i = 0; i < as(Flags); i++) {
  1486. if ((p = strchr( s, Flags[i] ))) {
  1487. if (del & (1 << i)) {
  1488. memmove( p, p + 1, fl - (p - s) );
  1489. fl--;
  1490. }
  1491. } else if (add & (1 << i)) {
  1492. for (j = 0; j < fl && Flags[i] > s[j]; j++);
  1493. fl++;
  1494. memmove( s + j + 1, s + j, fl - j );
  1495. s[j] = Flags[i];
  1496. }
  1497. }
  1498. tl = ol + 3 + fl;
  1499. } else {
  1500. tl = ol + maildir_make_flags( conf->info_delimiter, msg->gen.flags, nbuf + bl + ol );
  1501. }
  1502. if (!rename( buf, nbuf ))
  1503. break;
  1504. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot rename %s to %s", buf, nbuf )) != DRV_OK) {
  1505. cb( ret, aux );
  1506. return;
  1507. }
  1508. }
  1509. free( msg->base );
  1510. msg->base = nfstrndup( nbuf + bl, tl );
  1511. msg->gen.flags |= add;
  1512. msg->gen.flags &= ~del;
  1513. gmsg->status &= ~M_RECENT;
  1514. cb( DRV_OK, aux );
  1515. }
  1516. #ifdef USE_DB
  1517. static int
  1518. maildir_purge_msg( maildir_store_t *ctx, const char *name )
  1519. {
  1520. int ret;
  1521. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  1522. return ret;
  1523. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  1524. if ((ret = ctx->db->del( ctx->db, 0, &key, 0 ))) {
  1525. ctx->db->err( ctx->db, ret, "Maildir error: db->del()" );
  1526. return DRV_BOX_BAD;
  1527. }
  1528. return DRV_OK;
  1529. }
  1530. #endif /* USE_DB */
  1531. static void
  1532. maildir_trash_msg( store_t *gctx, message_t *gmsg,
  1533. void (*cb)( int sts, void *aux ), void *aux )
  1534. {
  1535. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1536. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1537. char *s;
  1538. int ret;
  1539. struct stat st;
  1540. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1541. for (;;) {
  1542. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1543. s = strstr( msg->base, ((maildir_store_conf_t *)gctx->conf)->info_prefix );
  1544. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%ld.%d_%d.%s%s", ctx->trash,
  1545. subdirs[gmsg->status & M_RECENT], (long)time( 0 ), Pid, ++MaildirCount, Hostname, s ? s : "" );
  1546. if (!rename( buf, nbuf ))
  1547. break;
  1548. if (!stat( buf, &st )) {
  1549. if ((ret = maildir_validate( ctx->trash, 1, ctx )) != DRV_OK) {
  1550. cb( ret, aux );
  1551. return;
  1552. }
  1553. if (!rename( buf, nbuf ))
  1554. break;
  1555. if (errno != ENOENT) {
  1556. sys_error( "Maildir error: cannot move %s to %s", buf, nbuf );
  1557. cb( DRV_BOX_BAD, aux );
  1558. return;
  1559. }
  1560. }
  1561. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot move %s to %s", buf, nbuf )) != DRV_OK) {
  1562. cb( ret, aux );
  1563. return;
  1564. }
  1565. }
  1566. gmsg->status |= M_DEAD;
  1567. gctx->count--;
  1568. #ifdef USE_DB
  1569. if (ctx->usedb) {
  1570. cb( maildir_purge_msg( ctx, msg->base ), aux );
  1571. return;
  1572. }
  1573. #endif /* USE_DB */
  1574. cb( DRV_OK, aux );
  1575. }
  1576. static void
  1577. maildir_close_box( store_t *gctx,
  1578. void (*cb)( int sts, void *aux ), void *aux )
  1579. {
  1580. #ifdef USE_DB
  1581. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1582. #endif /* USE_DB */
  1583. message_t *msg;
  1584. int basel, retry, ret;
  1585. char buf[_POSIX_PATH_MAX];
  1586. for (;;) {
  1587. retry = 0;
  1588. basel = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1589. for (msg = gctx->msgs; msg; msg = msg->next)
  1590. if (!(msg->status & M_DEAD) && (msg->flags & F_DELETED)) {
  1591. nfsnprintf( buf + basel, sizeof(buf) - basel, "%s/%s", subdirs[msg->status & M_RECENT], ((maildir_message_t *)msg)->base );
  1592. if (unlink( buf )) {
  1593. if (errno == ENOENT)
  1594. retry = 1;
  1595. else
  1596. sys_error( "Maildir error: cannot remove %s", buf );
  1597. } else {
  1598. msg->status |= M_DEAD;
  1599. gctx->count--;
  1600. #ifdef USE_DB
  1601. if (ctx->db && (ret = maildir_purge_msg( ctx, ((maildir_message_t *)msg)->base )) != DRV_OK) {
  1602. cb( ret, aux );
  1603. return;
  1604. }
  1605. #endif /* USE_DB */
  1606. }
  1607. }
  1608. if (!retry) {
  1609. cb( DRV_OK, aux );
  1610. return;
  1611. }
  1612. if ((ret = maildir_rescan( (maildir_store_t *)gctx )) != DRV_OK) {
  1613. cb( ret, aux );
  1614. return;
  1615. }
  1616. }
  1617. }
  1618. static void
  1619. maildir_cancel_cmds( store_t *gctx ATTR_UNUSED,
  1620. void (*cb)( void *aux ), void *aux )
  1621. {
  1622. cb( aux );
  1623. }
  1624. static void
  1625. maildir_commit_cmds( store_t *gctx )
  1626. {
  1627. (void) gctx;
  1628. }
  1629. static int
  1630. maildir_memory_usage( store_t *gctx ATTR_UNUSED )
  1631. {
  1632. return 0;
  1633. }
  1634. static int
  1635. maildir_fail_state( store_conf_t *gconf )
  1636. {
  1637. return ((maildir_store_conf_t *)gconf)->failed;
  1638. }
  1639. static int
  1640. maildir_parse_store( conffile_t *cfg, store_conf_t **storep )
  1641. {
  1642. maildir_store_conf_t *store;
  1643. if (strcasecmp( "MaildirStore", cfg->cmd ))
  1644. return 0;
  1645. store = nfcalloc( sizeof(*store) );
  1646. store->info_delimiter = FieldDelimiter;
  1647. store->gen.driver = &maildir_driver;
  1648. store->gen.name = nfstrdup( cfg->val );
  1649. while (getcline( cfg ) && cfg->cmd)
  1650. if (!strcasecmp( "Inbox", cfg->cmd ))
  1651. store->inbox = expand_strdup( cfg->val );
  1652. else if (!strcasecmp( "Path", cfg->cmd ))
  1653. store->gen.path = expand_strdup( cfg->val );
  1654. #ifdef USE_DB
  1655. else if (!strcasecmp( "AltMap", cfg->cmd ))
  1656. store->alt_map = parse_bool( cfg );
  1657. #endif /* USE_DB */
  1658. else if (!strcasecmp( "InfoDelimiter", cfg->cmd )) {
  1659. if (strlen( cfg->val ) != 1) {
  1660. error( "%s:%d: Info delimiter must be exactly one character long\n", cfg->file, cfg->line );
  1661. cfg->err = 1;
  1662. continue;
  1663. }
  1664. store->info_delimiter = cfg->val[0];
  1665. if (!ispunct( store->info_delimiter )) {
  1666. error( "%s:%d: Info delimiter must be a punctuation character\n", cfg->file, cfg->line );
  1667. cfg->err = 1;
  1668. continue;
  1669. }
  1670. } else if (!strcasecmp( "SubFolders", cfg->cmd )) {
  1671. if (!strcasecmp( "Verbatim", cfg->val )) {
  1672. store->sub_style = SUB_VERBATIM;
  1673. } else if (!strcasecmp( "Maildir++", cfg->val )) {
  1674. store->sub_style = SUB_MAILDIRPP;
  1675. } else if (!strcasecmp( "Legacy", cfg->val )) {
  1676. store->sub_style = SUB_LEGACY;
  1677. } else {
  1678. error( "%s:%d: Unrecognized SubFolders style\n", cfg->file, cfg->line );
  1679. cfg->err = 1;
  1680. }
  1681. } else
  1682. parse_generic_store( &store->gen, cfg );
  1683. if (!store->inbox)
  1684. store->inbox = expand_strdup( "~/Maildir" );
  1685. if (store->sub_style == SUB_MAILDIRPP && store->gen.path) {
  1686. error( "Maildir store '%s': Setting Path is incompatible with 'SubFolders Maildir++'\n", store->gen.name );
  1687. cfg->err = 1;
  1688. }
  1689. nfasprintf( &store->info_prefix, "%c2,", store->info_delimiter );
  1690. nfasprintf( &store->info_stop, "%c,", store->info_delimiter );
  1691. *storep = &store->gen;
  1692. return 1;
  1693. }
  1694. struct driver maildir_driver = {
  1695. 0, /* XXX DRV_CRLF? */
  1696. maildir_parse_store,
  1697. maildir_cleanup_drv,
  1698. maildir_alloc_store,
  1699. maildir_connect_store,
  1700. maildir_free_store,
  1701. maildir_free_store, /* _cancel_, but it's the same */
  1702. maildir_list_store,
  1703. maildir_select_box,
  1704. maildir_create_box,
  1705. maildir_open_box,
  1706. maildir_confirm_box_empty,
  1707. maildir_delete_box,
  1708. maildir_finish_delete_box,
  1709. maildir_prepare_load_box,
  1710. maildir_load_box,
  1711. maildir_fetch_msg,
  1712. maildir_store_msg,
  1713. maildir_find_new_msgs,
  1714. maildir_set_msg_flags,
  1715. maildir_trash_msg,
  1716. maildir_close_box,
  1717. maildir_cancel_cmds,
  1718. maildir_commit_cmds,
  1719. maildir_memory_usage,
  1720. maildir_fail_state,
  1721. };