maildir.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000 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 ? 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 = (count == 1) ? D_SEEN : 0;
  182. p->new = (count == 0);
  183. /* filename format is something like:
  184. * <unique-prefix>.UID<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, "UID");
  190. if (!s)
  191. puts ("Warning, no uid for message");
  192. else
  193. {
  194. p->uid = strtol (s + 3, &s, 10);
  195. if (p->uid > m->maxuid)
  196. {
  197. m->maxuid = p->uid;
  198. m->maxuidchanged = 1;
  199. }
  200. if (*s && *s != ':')
  201. {
  202. puts ("warning, unable to parse uid");
  203. p->uid = -1; /* reset */
  204. }
  205. }
  206. s = strchr (p->file, ':');
  207. if (s)
  208. parse_info (p, s + 1);
  209. if (p->flags & D_DELETED)
  210. m->deleted++;
  211. cur = &p->next;
  212. }
  213. closedir (d);
  214. }
  215. return m;
  216. }
  217. /* permanently remove messages from a maildir mailbox. if `dead' is nonzero,
  218. * we only remove the messags marked dead.
  219. */
  220. int
  221. maildir_expunge (mailbox_t * mbox, int dead)
  222. {
  223. message_t **cur = &mbox->msgs;
  224. message_t *tmp;
  225. char path[_POSIX_PATH_MAX];
  226. while (*cur)
  227. {
  228. if ((dead == 0 && (*cur)->flags & D_DELETED) ||
  229. (dead && (*cur)->dead))
  230. {
  231. tmp = *cur;
  232. *cur = (*cur)->next;
  233. snprintf (path, sizeof (path), "%s/%s/%s",
  234. mbox->path, tmp->new ? "new" : "cur", tmp->file);
  235. if (unlink (path))
  236. perror ("unlink");
  237. free (tmp->file);
  238. free (tmp);
  239. }
  240. else
  241. cur = &(*cur)->next;
  242. }
  243. return 0;
  244. }
  245. static int
  246. update_maxuid (mailbox_t * mbox)
  247. {
  248. int fd;
  249. char buf[64];
  250. size_t len;
  251. unsigned int uid;
  252. char path[_POSIX_PATH_MAX];
  253. int ret = 0;
  254. snprintf (path, sizeof (path), "%s/isyncmaxuid", mbox->path);
  255. fd = open (path, O_RDWR | O_CREAT, 0600);
  256. if (fd == -1)
  257. {
  258. perror ("open");
  259. return -1;
  260. }
  261. /* lock the file */
  262. if (do_lock (fd, F_WRLCK))
  263. {
  264. close (fd);
  265. return -1;
  266. }
  267. /* read the file again just to make sure it wasn't updated while
  268. * we were doing something else
  269. */
  270. len = read (fd, buf, sizeof (buf) - 1);
  271. buf[len] = 0;
  272. uid = atol (buf);
  273. if (uid > mbox->maxuid)
  274. {
  275. puts ("Error, maxuid is now higher (fatal)");
  276. ret = -1;
  277. }
  278. if (!ret)
  279. {
  280. /* rewind */
  281. lseek (fd, 0, SEEK_SET);
  282. /* write out the file */
  283. snprintf (buf, sizeof (buf), "%u\n", mbox->maxuid);
  284. len = write (fd, buf, strlen (buf));
  285. if (len == (size_t) - 1)
  286. {
  287. perror ("write");
  288. ret = -1;
  289. }
  290. else
  291. {
  292. ret = ftruncate (fd, len);
  293. if (ret)
  294. perror ("ftruncate");
  295. }
  296. }
  297. ret |= do_lock (fd, F_UNLCK);
  298. ret |= close (fd);
  299. return ret;
  300. }
  301. int
  302. maildir_sync (mailbox_t * mbox)
  303. {
  304. message_t *cur = mbox->msgs;
  305. char path[_POSIX_PATH_MAX];
  306. char oldpath[_POSIX_PATH_MAX];
  307. char *p;
  308. int ret = 0;
  309. if (mbox->changed)
  310. {
  311. for (; cur; cur = cur->next)
  312. {
  313. if (cur->changed)
  314. {
  315. /* generate old path */
  316. snprintf (oldpath, sizeof (oldpath), "%s/%s/%s",
  317. mbox->path, cur->new ? "new" : "cur", cur->file);
  318. /* truncate old flags (if present) */
  319. p = strchr (cur->file, ':');
  320. if (p)
  321. *p = 0;
  322. /* generate new path */
  323. snprintf (path, sizeof (path), "%s/%s/%s:2,%s%s%s%s",
  324. mbox->path, (cur->flags & D_SEEN) ? "cur" : "new",
  325. cur->file, (cur->flags & D_FLAGGED) ? "F" : "",
  326. (cur->flags & D_ANSWERED) ? "R" : "",
  327. (cur->flags & D_SEEN) ? "S" : "",
  328. (cur->flags & D_DELETED) ? "T" : "");
  329. if (rename (oldpath, path))
  330. perror ("rename");
  331. }
  332. }
  333. }
  334. if (mbox->maxuidchanged)
  335. ret = update_maxuid (mbox);
  336. return ret;
  337. }
  338. int
  339. maildir_set_uidvalidity (mailbox_t * mbox, unsigned int uidvalidity)
  340. {
  341. char path[_POSIX_PATH_MAX];
  342. char buf[16];
  343. int fd;
  344. int ret;
  345. snprintf (path, sizeof (path), "%s/isyncuidvalidity", mbox->path);
  346. fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600);
  347. if (fd == -1)
  348. {
  349. perror ("open");
  350. return -1;
  351. }
  352. snprintf (buf, sizeof (buf), "%u\n", uidvalidity);
  353. ret = write (fd, buf, strlen (buf));
  354. if (ret == -1)
  355. perror ("write");
  356. else if ((size_t) ret != strlen (buf))
  357. ret = -1;
  358. else
  359. ret = 0;
  360. if (close (fd))
  361. {
  362. perror ("close");
  363. ret = -1;
  364. }
  365. if (ret)
  366. if (unlink (path))
  367. perror ("unlink");
  368. return (ret);
  369. }
  370. void
  371. maildir_close (mailbox_t * mbox)
  372. {
  373. free (mbox->path);
  374. free_message (mbox->msgs);
  375. memset (mbox, 0xff, sizeof (mailbox_t));
  376. free (mbox);
  377. }