maildir.c 10 KB

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