drv_maildir.c 41 KB

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