Эх сурвалжийг харах

added code to clean the tmp directory in a maildir to comply with
maildir(5)

Michael Elkins 24 жил өмнө
parent
commit
c4050700c0
2 өөрчлөгдсөн 44 нэмэгдсэн , 3 устгасан
  1. 0 3
      TODO
  2. 44 0
      maildir.c

+ 0 - 3
TODO

@@ -6,6 +6,3 @@ finish implementing --quiet
 
 
 isync gets confused when new mail is delivered while in the middle of an
 isync gets confused when new mail is delivered while in the middle of an
 IMAP session.  need to handled those asynchronous notifications properly.
 IMAP session.  need to handled those asynchronous notifications properly.
-
-in order to be in total compliance with the maildir(5) spec, we need to
-add code to clean the tmp directory on the 24-hour timer.

+ 44 - 0
maildir.c

@@ -427,9 +427,53 @@ maildir_set_uidvalidity (mailbox_t * mbox, unsigned int uidvalidity)
     return (ret);
     return (ret);
 }
 }
 
 
+#define _24_HOURS (3600 * 24)
+
+static void
+maildir_clean_tmp (const char *mbox)
+{
+    char path[_POSIX_PATH_MAX];
+    DIR *dirp;
+    struct dirent *entry;
+    struct stat info;
+    time_t now;
+
+    snprintf (path, sizeof (path), "%s/tmp", mbox);
+    dirp = opendir (path);
+    if (dirp == NULL)
+    {
+	fprintf (stderr, "maildir_clean_tmp: opendir: %s: %s (errno %d)\n", path, strerror (errno), errno);
+	return;
+    }
+    /* assuming this scan will take less than a second, we only need to
+     * check the time once before the following loop.
+     */
+    time (&now);
+    while ((entry = readdir (dirp)))
+    {
+	snprintf (path, sizeof (path), "%s/tmp/%s", path, entry->d_name);
+	if (stat (path, &info))
+	    fprintf (stderr, "maildir_clean_tmp: stat: %s: %s (errno %d)\n", path, strerror (errno), errno);
+	else if (now - info.st_ctime >= _24_HOURS)
+	{
+	    /* this should happen infrequently enough that it won't be
+	     * bothersome to the user to display when it occurs.
+	     */
+	    printf ("Warning: removing stale file %s\n", path);
+	    if (unlink (path))
+		fprintf (stderr, "maildir_clean_tmp: unlink: %s: %s (errno %d)\n", path, strerror (errno), errno);
+	}
+    }
+}
+
 void
 void
 maildir_close (mailbox_t * mbox)
 maildir_close (mailbox_t * mbox)
 {
 {
+    /* per the maildir(5) specification, delivery agents are supposed to
+     * set a 24-hour timer on items placed in the `tmp' directory.
+     */
+    maildir_clean_tmp (mbox->path);
+
     free (mbox->path);
     free (mbox->path);
     free_message (mbox->msgs);
     free_message (mbox->msgs);
     memset (mbox, 0xff, sizeof (mailbox_t));
     memset (mbox, 0xff, sizeof (mailbox_t));