maildir.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-2 Michael R. Elkins <me@mutt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <limits.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <dirent.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <sys/stat.h>
  28. #include <errno.h>
  29. #include <time.h>
  30. #include "isync.h"
  31. static int
  32. do_lock (int fd, int flag)
  33. {
  34. struct flock lck;
  35. struct stat sb;
  36. if (fstat (fd, &sb))
  37. {
  38. perror ("fstat");
  39. return -1;
  40. }
  41. memset (&lck, 0, sizeof (lck));
  42. lck.l_type = flag;
  43. lck.l_whence = SEEK_SET;
  44. lck.l_start = 0;
  45. lck.l_len = sb.st_size;
  46. if (fcntl (fd, F_SETLK, &lck))
  47. {
  48. perror ("fcntl");
  49. close (fd);
  50. return -1;
  51. }
  52. return 0;
  53. }
  54. /* 2,<flags> */
  55. static void
  56. parse_info (message_t * m, char *s)
  57. {
  58. if (*s == '2' && *(s + 1) == ',')
  59. {
  60. s += 2;
  61. while (*s)
  62. {
  63. if (*s == 'F')
  64. m->flags |= D_FLAGGED;
  65. else if (*s == 'R')
  66. m->flags |= D_ANSWERED;
  67. else if (*s == 'T')
  68. m->flags |= D_DELETED;
  69. else if (*s == 'S')
  70. m->flags |= D_SEEN;
  71. s++;
  72. }
  73. }
  74. }
  75. static unsigned int
  76. read_uid (const char *path, const char *file)
  77. {
  78. char full[_POSIX_PATH_MAX];
  79. int fd;
  80. int ret = 0;
  81. int len;
  82. char buf[64];
  83. unsigned int uid = 0;
  84. snprintf (full, sizeof (full), "%s/%s", path, file);
  85. fd = open (full, O_RDONLY);
  86. if (fd == -1)
  87. {
  88. if (errno != ENOENT)
  89. {
  90. perror (full);
  91. return -1;
  92. }
  93. return 0; /* doesn't exist */
  94. }
  95. len = read (fd, buf, sizeof (buf) - 1);
  96. if (len == -1)
  97. {
  98. perror ("read");
  99. ret = -1;
  100. }
  101. else
  102. {
  103. buf[len] = 0;
  104. uid = atol (buf);
  105. }
  106. close (fd);
  107. return ret ? (unsigned int) ret : uid;
  108. }
  109. /* NOTE: this is NOT NFS safe */
  110. static int
  111. maildir_lock (mailbox_t * m)
  112. {
  113. char path[_POSIX_PATH_MAX];
  114. snprintf (path, sizeof (path), "%s/isynclock", m->path);
  115. m->lockfd = open (path, O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR);
  116. if (m->lockfd == -1)
  117. {
  118. perror (path);
  119. return -1;
  120. }
  121. if (do_lock (m->lockfd, F_WRLCK))
  122. {
  123. close (m->lockfd);
  124. return -1;
  125. }
  126. return 0;
  127. }
  128. static void
  129. maildir_unlock (mailbox_t * m)
  130. {
  131. char path[_POSIX_PATH_MAX];
  132. snprintf (path, sizeof (path), "%s/isynclock", m->path);
  133. unlink (path);
  134. do_lock (m->lockfd, F_UNLCK);
  135. close (m->lockfd);
  136. }
  137. /* open a maildir mailbox.
  138. * if OPEN_FAST is set, we just check to make
  139. * sure its a valid mailbox and don't actually parse it. any IMAP messages
  140. * with the \Recent flag set are guaranteed not to be in the mailbox yet,
  141. * so we can save a lot of time when the user just wants to fetch new messages
  142. * without syncing the flags.
  143. * if OPEN_CREATE is set, we create the mailbox if it doesn't already exist.
  144. */
  145. mailbox_t *
  146. maildir_open (const char *path, int flags)
  147. {
  148. char buf[_POSIX_PATH_MAX];
  149. DIR *d;
  150. struct dirent *e;
  151. message_t **cur;
  152. message_t *p;
  153. mailbox_t *m;
  154. char *s;
  155. int count = 0;
  156. struct stat sb;
  157. const char *subdirs[] = { "cur", "new", "tmp" };
  158. int i;
  159. datum key;
  160. m = calloc (1, sizeof (mailbox_t));
  161. m->lockfd = -1;
  162. /* filename expansion happens here, not in the config parser */
  163. m->path = expand_strdup (path);
  164. if (stat (m->path, &sb))
  165. {
  166. if (errno == ENOENT && (flags & OPEN_CREATE))
  167. {
  168. if (mkdir (m->path, S_IRUSR | S_IWUSR | S_IXUSR))
  169. {
  170. fprintf (stderr, "ERROR: mkdir %s: %s (errno %d)\n",
  171. m->path, strerror (errno), errno);
  172. goto err;
  173. }
  174. for (i = 0; i < 3; i++)
  175. {
  176. snprintf (buf, sizeof (buf), "%s/%s", m->path, subdirs[i]);
  177. if (mkdir (buf, S_IRUSR | S_IWUSR | S_IXUSR))
  178. {
  179. fprintf (stderr, "ERROR: mkdir %s: %s (errno %d)\n",
  180. buf, strerror (errno), errno);
  181. goto err;
  182. }
  183. }
  184. }
  185. else
  186. {
  187. fprintf (stderr, "ERROR: stat %s: %s (errno %d)\n", m->path,
  188. strerror (errno), errno);
  189. goto err;
  190. }
  191. }
  192. else
  193. {
  194. /* check to make sure this looks like a valid maildir box */
  195. for (i = 0; i < 3; i++)
  196. {
  197. snprintf (buf, sizeof (buf), "%s/%s", m->path, subdirs[i]);
  198. if (stat (buf, &sb))
  199. {
  200. fprintf (stderr, "ERROR: stat %s: %s (errno %d)\n", buf,
  201. strerror (errno), errno);
  202. fprintf (stderr,
  203. "ERROR: %s does not appear to be a valid maildir style mailbox\n",
  204. m->path);
  205. goto err;
  206. }
  207. }
  208. }
  209. /* we need a mutex on the maildir because of the state files that isync
  210. * uses.
  211. */
  212. if (maildir_lock (m))
  213. goto err;
  214. /* check for the uidvalidity value */
  215. m->uidvalidity = read_uid (m->path, "isyncuidvalidity");
  216. if (m->uidvalidity == (unsigned int) -1)
  217. goto err;
  218. /* load the current maxuid */
  219. if ((m->maxuid = read_uid (m->path, "isyncmaxuid")) == (unsigned int) -1)
  220. goto err;
  221. if (flags & OPEN_FAST)
  222. return m;
  223. snprintf (buf, sizeof (buf), "%s/isyncuidmap", m->path);
  224. m->db = dbm_open (buf, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  225. if (m->db == NULL)
  226. {
  227. fputs ("ERROR: unable to open UID db\n", stderr);
  228. goto err;
  229. }
  230. cur = &m->msgs;
  231. for (; count < 2; count++)
  232. {
  233. /* read the msgs from the new subdir */
  234. snprintf (buf, sizeof (buf), "%s/%s", m->path,
  235. (count == 0) ? "new" : "cur");
  236. d = opendir (buf);
  237. if (!d)
  238. {
  239. perror ("opendir");
  240. goto err;
  241. }
  242. while ((e = readdir (d)))
  243. {
  244. if (*e->d_name == '.')
  245. continue; /* skip dot-files */
  246. *cur = calloc (1, sizeof (message_t));
  247. p = *cur;
  248. p->file = strdup (e->d_name);
  249. p->uid = -1;
  250. p->flags = 0;
  251. p->new = (count == 0);
  252. /* determine the UID for this message. The basename (sans
  253. * flags) is used as the key in the db
  254. */
  255. key.dptr = p->file;
  256. s = strchr (key.dptr, ':');
  257. key.dsize = s ? (size_t) (s - key.dptr) : strlen (key.dptr);
  258. key = dbm_fetch (m->db, key);
  259. if (key.dptr)
  260. {
  261. p->uid = *(int *) key.dptr;
  262. if (p->uid > m->maxuid)
  263. m->maxuid = p->uid;
  264. }
  265. else
  266. puts ("Warning, no UID for message");
  267. if (s)
  268. parse_info (p, s + 1);
  269. if (p->flags & D_DELETED)
  270. m->deleted++;
  271. cur = &p->next;
  272. }
  273. closedir (d);
  274. }
  275. return m;
  276. err:
  277. if (m->db)
  278. dbm_close (m->db);
  279. if (m->lockfd != -1)
  280. maildir_unlock (m);
  281. free (m->path);
  282. free (m);
  283. return NULL;
  284. }
  285. /* permanently remove messages from a maildir mailbox. if `dead' is nonzero,
  286. * we only remove the messags marked dead.
  287. */
  288. int
  289. maildir_expunge (mailbox_t * mbox, int dead)
  290. {
  291. message_t **cur = &mbox->msgs;
  292. message_t *tmp;
  293. char *s;
  294. datum key;
  295. char path[_POSIX_PATH_MAX];
  296. while (*cur)
  297. {
  298. if ((dead == 0 && (*cur)->flags & D_DELETED) ||
  299. (dead && (*cur)->dead))
  300. {
  301. tmp = *cur;
  302. snprintf (path, sizeof (path), "%s/%s/%s",
  303. mbox->path, tmp->new ? "new" : "cur", tmp->file);
  304. if (unlink (path))
  305. perror (path);
  306. /* remove the message from the UID map */
  307. key.dptr = tmp->file;
  308. s = strchr (key.dptr, ':');
  309. key.dsize = s ? (size_t) (s - key.dptr) : strlen (key.dptr);
  310. dbm_delete (mbox->db, key);
  311. *cur = (*cur)->next;
  312. free (tmp->file);
  313. free (tmp);
  314. }
  315. else
  316. cur = &(*cur)->next;
  317. }
  318. return 0;
  319. }
  320. int
  321. maildir_update_maxuid (mailbox_t * mbox)
  322. {
  323. int fd;
  324. char buf[64];
  325. size_t len;
  326. char path[_POSIX_PATH_MAX];
  327. int ret = 0;
  328. snprintf (path, sizeof (path), "%s/isyncmaxuid", mbox->path);
  329. fd = open (path, O_WRONLY | O_CREAT, 0600);
  330. if (fd == -1)
  331. {
  332. perror ("open");
  333. return -1;
  334. }
  335. /* write out the file */
  336. snprintf (buf, sizeof (buf), "%u\n", mbox->maxuid);
  337. len = write (fd, buf, strlen (buf));
  338. if (len == (size_t) - 1)
  339. {
  340. perror ("write");
  341. ret = -1;
  342. }
  343. if (close (fd))
  344. ret = -1;
  345. return ret;
  346. }
  347. #define _24_HOURS (3600 * 24)
  348. static void
  349. maildir_clean_tmp (const char *mbox)
  350. {
  351. char path[_POSIX_PATH_MAX];
  352. DIR *dirp;
  353. struct dirent *entry;
  354. struct stat info;
  355. time_t now;
  356. snprintf (path, sizeof (path), "%s/tmp", mbox);
  357. dirp = opendir (path);
  358. if (dirp == NULL)
  359. {
  360. fprintf (stderr, "maildir_clean_tmp: opendir: %s: %s (errno %d)\n",
  361. path, strerror (errno), errno);
  362. return;
  363. }
  364. /* assuming this scan will take less than a second, we only need to
  365. * check the time once before the following loop.
  366. */
  367. time (&now);
  368. while ((entry = readdir (dirp)))
  369. {
  370. snprintf (path, sizeof (path), "%s/tmp/%s", mbox, entry->d_name);
  371. if (stat (path, &info))
  372. fprintf (stderr, "maildir_clean_tmp: stat: %s: %s (errno %d)\n",
  373. path, strerror (errno), errno);
  374. else if (S_ISREG (info.st_mode) && now - info.st_ctime >= _24_HOURS)
  375. {
  376. /* this should happen infrequently enough that it won't be
  377. * bothersome to the user to display when it occurs.
  378. */
  379. printf ("Warning: removing stale file %s\n", path);
  380. if (unlink (path))
  381. fprintf (stderr,
  382. "maildir_clean_tmp: unlink: %s: %s (errno %d)\n",
  383. path, strerror (errno), errno);
  384. }
  385. }
  386. }
  387. void
  388. maildir_close (mailbox_t * mbox)
  389. {
  390. if (mbox->db)
  391. dbm_close (mbox->db);
  392. /* release the mutex on the mailbox */
  393. maildir_unlock (mbox);
  394. /* per the maildir(5) specification, delivery agents are supposed to
  395. * set a 24-hour timer on items placed in the `tmp' directory.
  396. */
  397. maildir_clean_tmp (mbox->path);
  398. free (mbox->path);
  399. free_message (mbox->msgs);
  400. memset (mbox, 0xff, sizeof (mailbox_t));
  401. free (mbox);
  402. }
  403. int
  404. maildir_set_uidvalidity (mailbox_t * mbox, unsigned int uidvalidity)
  405. {
  406. char path[_POSIX_PATH_MAX];
  407. char buf[16];
  408. int fd;
  409. int ret;
  410. snprintf (path, sizeof (path), "%s/isyncuidvalidity", mbox->path);
  411. fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  412. if (fd == -1)
  413. {
  414. perror ("open");
  415. return -1;
  416. }
  417. snprintf (buf, sizeof (buf), "%u\n", uidvalidity);
  418. ret = write (fd, buf, strlen (buf));
  419. if (ret == -1)
  420. perror ("write");
  421. else if ((size_t) ret != strlen (buf))
  422. ret = -1;
  423. else
  424. ret = 0;
  425. if (close (fd))
  426. {
  427. perror ("close");
  428. ret = -1;
  429. }
  430. if (ret)
  431. if (unlink (path))
  432. perror ("unlink");
  433. return (ret);
  434. }