maildir.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-1 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 "isync.h"
  30. static int
  31. do_lock (int fd, int flag)
  32. {
  33. struct flock lck;
  34. struct stat sb;
  35. if (fstat (fd, &sb))
  36. {
  37. perror ("fstat");
  38. return -1;
  39. }
  40. memset (&lck, 0, sizeof (lck));
  41. lck.l_type = flag;
  42. lck.l_whence = SEEK_SET;
  43. lck.l_start = 0;
  44. lck.l_len = sb.st_size;
  45. if (fcntl (fd, F_SETLK, &lck))
  46. {
  47. perror ("fcntl");
  48. close (fd);
  49. return -1;
  50. }
  51. return 0;
  52. }
  53. /* 2,<flags> */
  54. static void
  55. parse_info (message_t * m, char *s)
  56. {
  57. if (*s == '2' && *(s + 1) == ',')
  58. {
  59. s += 2;
  60. while (*s)
  61. {
  62. if (*s == 'F')
  63. m->flags |= D_FLAGGED;
  64. else if (*s == 'R')
  65. m->flags |= D_ANSWERED;
  66. else if (*s == 'T')
  67. m->flags |= D_DELETED;
  68. else if (*s == 'S')
  69. m->flags |= D_SEEN;
  70. s++;
  71. }
  72. }
  73. }
  74. static unsigned int
  75. read_uid (const char *path, const char *file)
  76. {
  77. char full[_POSIX_PATH_MAX];
  78. int fd;
  79. int ret;
  80. int len;
  81. char buf[64];
  82. unsigned int uid = 0;
  83. snprintf (full, sizeof (full), "%s/%s", path, file);
  84. fd = open (full, O_RDONLY);
  85. if (fd == -1)
  86. {
  87. if (errno != ENOENT)
  88. {
  89. perror ("open");
  90. return -1;
  91. }
  92. return 0; /* doesn't exist */
  93. }
  94. ret = do_lock (fd, F_RDLCK);
  95. if (!ret)
  96. {
  97. len = read (fd, buf, sizeof (buf) - 1);
  98. if (len == -1)
  99. ret = -1;
  100. else
  101. {
  102. buf[len] = 0;
  103. uid = atol (buf);
  104. }
  105. }
  106. ret |= do_lock (fd, F_UNLCK);
  107. close (fd);
  108. return ret ? (unsigned int) ret : uid;
  109. }
  110. /* open a maildir mailbox. if `fast' is nonzero, we just check to make
  111. * sure its a valid mailbox and don't actually parse it. any IMAP messages
  112. * with the \Recent flag set are guaranteed not to be in the mailbox yet,
  113. * so we can save a lot of time when the user just wants to fetch new messages
  114. * without syncing the flags.
  115. */
  116. mailbox_t *
  117. maildir_open (const char *path, int fast)
  118. {
  119. char buf[_POSIX_PATH_MAX];
  120. DIR *d;
  121. struct dirent *e;
  122. message_t **cur;
  123. message_t *p;
  124. mailbox_t *m;
  125. char *s;
  126. int count = 0;
  127. m = calloc (1, sizeof (mailbox_t));
  128. /* filename expansion happens here, not in the config parser */
  129. m->path = expand_strdup (path);
  130. /* check to make sure this looks like a valid maildir box */
  131. snprintf (buf, sizeof (buf), "%s/new", m->path);
  132. if (access (buf, F_OK))
  133. {
  134. free (m->path);
  135. free (m);
  136. perror ("access");
  137. return 0;
  138. }
  139. snprintf (buf, sizeof (buf), "%s/cur", m->path);
  140. if (access (buf, F_OK))
  141. {
  142. free (m->path);
  143. free (m);
  144. perror ("access");
  145. return 0;
  146. }
  147. /* check for the uidvalidity value */
  148. m->uidvalidity = read_uid (m->path, "isyncuidvalidity");
  149. if (m->uidvalidity == (unsigned int) -1)
  150. {
  151. free (m->path);
  152. free (m);
  153. return NULL;
  154. }
  155. /* load the current maxuid */
  156. if ((m->maxuid = read_uid (m->path, "isyncmaxuid")) == (unsigned int) -1)
  157. {
  158. free (m->path);
  159. free (m);
  160. return NULL;
  161. }
  162. if (fast)
  163. return m;
  164. cur = &m->msgs;
  165. for (; count < 2; count++)
  166. {
  167. /* read the msgs from the new subdir */
  168. snprintf (buf, sizeof (buf), "%s/%s", m->path,
  169. (count == 0) ? "new" : "cur");
  170. d = opendir (buf);
  171. if (!d)
  172. {
  173. free (m->path);
  174. free (m);
  175. perror ("opendir");
  176. return 0;
  177. }
  178. while ((e = readdir (d)))
  179. {
  180. if (*e->d_name == '.')
  181. continue; /* skip dot-files */
  182. *cur = calloc (1, sizeof (message_t));
  183. p = *cur;
  184. p->file = strdup (e->d_name);
  185. p->uid = -1;
  186. p->flags = 0;
  187. p->new = (count == 0);
  188. /* filename format is something like:
  189. * <unique-prefix>,U=<n>:2,<flags>
  190. * This is completely non-standard, but in order for mail
  191. * clients to understand the flags, we have to use the
  192. * standard :info as described by the qmail spec
  193. */
  194. s = strstr (p->file, ",U=");
  195. if (!s)
  196. s = strstr (p->file, "UID");
  197. if (!s)
  198. puts ("Warning, no UID for message");
  199. else
  200. {
  201. p->uid = strtol (s + 3, &s, 10);
  202. if (p->uid > m->maxuid)
  203. {
  204. m->maxuid = p->uid;
  205. m->maxuidchanged = 1;
  206. }
  207. /* Courier-IMAP names it files
  208. * unique,S=<size>:info
  209. * so we need to put the UID before the size, hence here
  210. * we check for a comma as a valid terminator as well,
  211. * since the format will be
  212. * unique,U=<uid>,S=<size>:info
  213. */
  214. if (*s && *s != ':' && *s != ',')
  215. {
  216. puts ("Warning, unable to parse UID");
  217. p->uid = -1; /* reset */
  218. }
  219. }
  220. s = strchr (p->file, ':');
  221. if (s)
  222. parse_info (p, s + 1);
  223. if (p->flags & D_DELETED)
  224. m->deleted++;
  225. cur = &p->next;
  226. }
  227. closedir (d);
  228. }
  229. return m;
  230. }
  231. /* permanently remove messages from a maildir mailbox. if `dead' is nonzero,
  232. * we only remove the messags marked dead.
  233. */
  234. int
  235. maildir_expunge (mailbox_t * mbox, int dead)
  236. {
  237. message_t **cur = &mbox->msgs;
  238. message_t *tmp;
  239. char path[_POSIX_PATH_MAX];
  240. while (*cur)
  241. {
  242. if ((dead == 0 && (*cur)->flags & D_DELETED) ||
  243. (dead && (*cur)->dead))
  244. {
  245. tmp = *cur;
  246. *cur = (*cur)->next;
  247. snprintf (path, sizeof (path), "%s/%s/%s",
  248. mbox->path, tmp->new ? "new" : "cur", tmp->file);
  249. if (unlink (path))
  250. perror ("unlink");
  251. free (tmp->file);
  252. free (tmp);
  253. }
  254. else
  255. cur = &(*cur)->next;
  256. }
  257. return 0;
  258. }
  259. static int
  260. update_maxuid (mailbox_t * mbox)
  261. {
  262. int fd;
  263. char buf[64];
  264. size_t len;
  265. unsigned int uid;
  266. char path[_POSIX_PATH_MAX];
  267. int ret = 0;
  268. snprintf (path, sizeof (path), "%s/isyncmaxuid", mbox->path);
  269. fd = open (path, O_RDWR | O_CREAT, 0600);
  270. if (fd == -1)
  271. {
  272. perror ("open");
  273. return -1;
  274. }
  275. /* lock the file */
  276. if (do_lock (fd, F_WRLCK))
  277. {
  278. close (fd);
  279. return -1;
  280. }
  281. /* read the file again just to make sure it wasn't updated while
  282. * we were doing something else
  283. */
  284. len = read (fd, buf, sizeof (buf) - 1);
  285. buf[len] = 0;
  286. uid = atol (buf);
  287. if (uid > mbox->maxuid)
  288. {
  289. puts ("Error, maxuid is now higher (fatal)");
  290. ret = -1;
  291. }
  292. if (!ret)
  293. {
  294. /* rewind */
  295. lseek (fd, 0, SEEK_SET);
  296. /* write out the file */
  297. snprintf (buf, sizeof (buf), "%u\n", mbox->maxuid);
  298. len = write (fd, buf, strlen (buf));
  299. if (len == (size_t) - 1)
  300. {
  301. perror ("write");
  302. ret = -1;
  303. }
  304. else
  305. {
  306. ret = ftruncate (fd, len);
  307. if (ret)
  308. perror ("ftruncate");
  309. }
  310. }
  311. ret |= do_lock (fd, F_UNLCK);
  312. ret |= close (fd);
  313. return ret;
  314. }
  315. int
  316. maildir_sync (mailbox_t * mbox)
  317. {
  318. message_t *cur = mbox->msgs;
  319. char path[_POSIX_PATH_MAX];
  320. char oldpath[_POSIX_PATH_MAX];
  321. char *p;
  322. int ret = 0;
  323. if (mbox->changed)
  324. {
  325. for (; cur; cur = cur->next)
  326. {
  327. if (cur->changed)
  328. {
  329. /* generate old path */
  330. snprintf (oldpath, sizeof (oldpath), "%s/%s/%s",
  331. mbox->path, cur->new ? "new" : "cur", cur->file);
  332. /* truncate old flags (if present) */
  333. p = strchr (cur->file, ':');
  334. if (p)
  335. *p = 0;
  336. /* generate new path - always put this in the cur/ directory
  337. * because its no longer new
  338. */
  339. snprintf (path, sizeof (path), "%s/cur/%s:2,%s%s%s%s",
  340. mbox->path,
  341. cur->file, (cur->flags & D_FLAGGED) ? "F" : "",
  342. (cur->flags & D_ANSWERED) ? "R" : "",
  343. (cur->flags & D_SEEN) ? "S" : "",
  344. (cur->flags & D_DELETED) ? "T" : "");
  345. if (rename (oldpath, path))
  346. perror ("rename");
  347. }
  348. }
  349. }
  350. if (mbox->maxuidchanged)
  351. ret = update_maxuid (mbox);
  352. return ret;
  353. }
  354. int
  355. maildir_set_uidvalidity (mailbox_t * mbox, unsigned int uidvalidity)
  356. {
  357. char path[_POSIX_PATH_MAX];
  358. char buf[16];
  359. int fd;
  360. int ret;
  361. snprintf (path, sizeof (path), "%s/isyncuidvalidity", mbox->path);
  362. fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  363. if (fd == -1)
  364. {
  365. perror ("open");
  366. return -1;
  367. }
  368. snprintf (buf, sizeof (buf), "%u\n", uidvalidity);
  369. ret = write (fd, buf, strlen (buf));
  370. if (ret == -1)
  371. perror ("write");
  372. else if ((size_t) ret != strlen (buf))
  373. ret = -1;
  374. else
  375. ret = 0;
  376. if (close (fd))
  377. {
  378. perror ("close");
  379. ret = -1;
  380. }
  381. if (ret)
  382. if (unlink (path))
  383. perror ("unlink");
  384. return (ret);
  385. }
  386. #define _24_HOURS (3600 * 24)
  387. static void
  388. maildir_clean_tmp (const char *mbox)
  389. {
  390. char path[_POSIX_PATH_MAX];
  391. DIR *dirp;
  392. struct dirent *entry;
  393. struct stat info;
  394. time_t now;
  395. snprintf (path, sizeof (path), "%s/tmp", mbox);
  396. dirp = opendir (path);
  397. if (dirp == NULL)
  398. {
  399. fprintf (stderr, "maildir_clean_tmp: opendir: %s: %s (errno %d)\n", path, strerror (errno), errno);
  400. return;
  401. }
  402. /* assuming this scan will take less than a second, we only need to
  403. * check the time once before the following loop.
  404. */
  405. time (&now);
  406. while ((entry = readdir (dirp)))
  407. {
  408. snprintf (path, sizeof (path), "%s/tmp/%s", mbox, entry->d_name);
  409. if (stat (path, &info))
  410. fprintf (stderr, "maildir_clean_tmp: stat: %s: %s (errno %d)\n", path, strerror (errno), errno);
  411. else if (S_ISREG (info.st_mode) && now - info.st_ctime >= _24_HOURS)
  412. {
  413. /* this should happen infrequently enough that it won't be
  414. * bothersome to the user to display when it occurs.
  415. */
  416. printf ("Warning: removing stale file %s\n", path);
  417. if (unlink (path))
  418. fprintf (stderr, "maildir_clean_tmp: unlink: %s: %s (errno %d)\n", path, strerror (errno), errno);
  419. }
  420. }
  421. }
  422. void
  423. maildir_close (mailbox_t * mbox)
  424. {
  425. /* per the maildir(5) specification, delivery agents are supposed to
  426. * set a 24-hour timer on items placed in the `tmp' directory.
  427. */
  428. maildir_clean_tmp (mbox->path);
  429. free (mbox->path);
  430. free_message (mbox->msgs);
  431. memset (mbox, 0xff, sizeof (mailbox_t));
  432. free (mbox);
  433. }