maildir.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. /* check to make sure this looks like a valid maildir box */
  128. snprintf (buf, sizeof (buf), "%s/new", path);
  129. if (access (buf, F_OK))
  130. {
  131. perror ("access");
  132. return 0;
  133. }
  134. snprintf (buf, sizeof (buf), "%s/cur", path);
  135. if (access (buf, F_OK))
  136. {
  137. perror ("access");
  138. return 0;
  139. }
  140. m = calloc (1, sizeof (mailbox_t));
  141. m->path = strdup (path);
  142. /* check for the uidvalidity value */
  143. m->uidvalidity = read_uid (path, "isyncuidvalidity");
  144. if (m->uidvalidity == (unsigned int) -1)
  145. {
  146. free (m->path);
  147. free (m);
  148. return NULL;
  149. }
  150. /* load the current maxuid */
  151. if ((m->maxuid = read_uid (path, "isyncmaxuid")) == (unsigned int) -1)
  152. {
  153. free (m->path);
  154. free (m);
  155. return NULL;
  156. }
  157. if (fast)
  158. return m;
  159. cur = &m->msgs;
  160. for (; count < 2; count++)
  161. {
  162. /* read the msgs from the new subdir */
  163. snprintf (buf, sizeof (buf), "%s/%s", path,
  164. (count == 0) ? "new" : "cur");
  165. d = opendir (buf);
  166. if (!d)
  167. {
  168. free (m->path);
  169. free (m);
  170. perror ("opendir");
  171. return 0;
  172. }
  173. while ((e = readdir (d)))
  174. {
  175. if (*e->d_name == '.')
  176. continue; /* skip dot-files */
  177. *cur = calloc (1, sizeof (message_t));
  178. p = *cur;
  179. p->file = strdup (e->d_name);
  180. p->uid = -1;
  181. p->flags = 0;
  182. p->new = (count == 0);
  183. /* filename format is something like:
  184. * <unique-prefix>,U=<n>:2,<flags>
  185. * This is completely non-standard, but in order for mail
  186. * clients to understand the flags, we have to use the
  187. * standard :info as described by the qmail spec
  188. */
  189. s = strstr (p->file, ",U=");
  190. if (!s)
  191. s = strstr (p->file, "UID");
  192. if (!s)
  193. puts ("Warning, no UID for message");
  194. else
  195. {
  196. p->uid = strtol (s + 3, &s, 10);
  197. if (p->uid > m->maxuid)
  198. {
  199. m->maxuid = p->uid;
  200. m->maxuidchanged = 1;
  201. }
  202. /* Courier-IMAP names it files
  203. * unique,S=<size>:info
  204. * so we need to put the UID before the size, hence here
  205. * we check for a comma as a valid terminator as well,
  206. * since the format will be
  207. * unique,U=<uid>,S=<size>:info
  208. */
  209. if (*s && *s != ':' && *s != ',')
  210. {
  211. puts ("Warning, unable to parse UID");
  212. p->uid = -1; /* reset */
  213. }
  214. }
  215. s = strchr (p->file, ':');
  216. if (s)
  217. parse_info (p, s + 1);
  218. if (p->flags & D_DELETED)
  219. m->deleted++;
  220. cur = &p->next;
  221. }
  222. closedir (d);
  223. }
  224. return m;
  225. }
  226. /* permanently remove messages from a maildir mailbox. if `dead' is nonzero,
  227. * we only remove the messags marked dead.
  228. */
  229. int
  230. maildir_expunge (mailbox_t * mbox, int dead)
  231. {
  232. message_t **cur = &mbox->msgs;
  233. message_t *tmp;
  234. char path[_POSIX_PATH_MAX];
  235. while (*cur)
  236. {
  237. if ((dead == 0 && (*cur)->flags & D_DELETED) ||
  238. (dead && (*cur)->dead))
  239. {
  240. tmp = *cur;
  241. *cur = (*cur)->next;
  242. snprintf (path, sizeof (path), "%s/%s/%s",
  243. mbox->path, tmp->new ? "new" : "cur", tmp->file);
  244. if (unlink (path))
  245. perror ("unlink");
  246. free (tmp->file);
  247. free (tmp);
  248. }
  249. else
  250. cur = &(*cur)->next;
  251. }
  252. return 0;
  253. }
  254. static int
  255. update_maxuid (mailbox_t * mbox)
  256. {
  257. int fd;
  258. char buf[64];
  259. size_t len;
  260. unsigned int uid;
  261. char path[_POSIX_PATH_MAX];
  262. int ret = 0;
  263. snprintf (path, sizeof (path), "%s/isyncmaxuid", mbox->path);
  264. fd = open (path, O_RDWR | O_CREAT, 0600);
  265. if (fd == -1)
  266. {
  267. perror ("open");
  268. return -1;
  269. }
  270. /* lock the file */
  271. if (do_lock (fd, F_WRLCK))
  272. {
  273. close (fd);
  274. return -1;
  275. }
  276. /* read the file again just to make sure it wasn't updated while
  277. * we were doing something else
  278. */
  279. len = read (fd, buf, sizeof (buf) - 1);
  280. buf[len] = 0;
  281. uid = atol (buf);
  282. if (uid > mbox->maxuid)
  283. {
  284. puts ("Error, maxuid is now higher (fatal)");
  285. ret = -1;
  286. }
  287. if (!ret)
  288. {
  289. /* rewind */
  290. lseek (fd, 0, SEEK_SET);
  291. /* write out the file */
  292. snprintf (buf, sizeof (buf), "%u\n", mbox->maxuid);
  293. len = write (fd, buf, strlen (buf));
  294. if (len == (size_t) - 1)
  295. {
  296. perror ("write");
  297. ret = -1;
  298. }
  299. else
  300. {
  301. ret = ftruncate (fd, len);
  302. if (ret)
  303. perror ("ftruncate");
  304. }
  305. }
  306. ret |= do_lock (fd, F_UNLCK);
  307. ret |= close (fd);
  308. return ret;
  309. }
  310. int
  311. maildir_sync (mailbox_t * mbox)
  312. {
  313. message_t *cur = mbox->msgs;
  314. char path[_POSIX_PATH_MAX];
  315. char oldpath[_POSIX_PATH_MAX];
  316. char *p;
  317. int ret = 0;
  318. if (mbox->changed)
  319. {
  320. for (; cur; cur = cur->next)
  321. {
  322. if (cur->changed)
  323. {
  324. /* generate old path */
  325. snprintf (oldpath, sizeof (oldpath), "%s/%s/%s",
  326. mbox->path, cur->new ? "new" : "cur", cur->file);
  327. /* truncate old flags (if present) */
  328. p = strchr (cur->file, ':');
  329. if (p)
  330. *p = 0;
  331. /* generate new path - always put this in the cur/ directory
  332. * because its no longer new
  333. */
  334. snprintf (path, sizeof (path), "%s/cur/%s:2,%s%s%s%s",
  335. mbox->path,
  336. cur->file, (cur->flags & D_FLAGGED) ? "F" : "",
  337. (cur->flags & D_ANSWERED) ? "R" : "",
  338. (cur->flags & D_SEEN) ? "S" : "",
  339. (cur->flags & D_DELETED) ? "T" : "");
  340. if (rename (oldpath, path))
  341. perror ("rename");
  342. }
  343. }
  344. }
  345. if (mbox->maxuidchanged)
  346. ret = update_maxuid (mbox);
  347. return ret;
  348. }
  349. int
  350. maildir_set_uidvalidity (mailbox_t * mbox, unsigned int uidvalidity)
  351. {
  352. char path[_POSIX_PATH_MAX];
  353. char buf[16];
  354. int fd;
  355. int ret;
  356. snprintf (path, sizeof (path), "%s/isyncuidvalidity", mbox->path);
  357. fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  358. if (fd == -1)
  359. {
  360. perror ("open");
  361. return -1;
  362. }
  363. snprintf (buf, sizeof (buf), "%u\n", uidvalidity);
  364. ret = write (fd, buf, strlen (buf));
  365. if (ret == -1)
  366. perror ("write");
  367. else if ((size_t) ret != strlen (buf))
  368. ret = -1;
  369. else
  370. ret = 0;
  371. if (close (fd))
  372. {
  373. perror ("close");
  374. ret = -1;
  375. }
  376. if (ret)
  377. if (unlink (path))
  378. perror ("unlink");
  379. return (ret);
  380. }
  381. void
  382. maildir_close (mailbox_t * mbox)
  383. {
  384. free (mbox->path);
  385. free_message (mbox->msgs);
  386. memset (mbox, 0xff, sizeof (mailbox_t));
  387. free (mbox);
  388. }