]> granicus.if.org Git - mutt/commitdiff
patch-1.3.28-me.emptycheck.1
authorThomas Roessler <roessler@does-not-exist.org>
Thu, 28 Mar 2002 13:25:22 +0000 (13:25 +0000)
committerThomas Roessler <roessler@does-not-exist.org>
Thu, 28 Mar 2002 13:25:22 +0000 (13:25 +0000)
mailbox.h
main.c
mbox.c
mh.c
mx.c
mx.h

index 4ac8e8dc05e06d24a46302d141937a02bb2e5827..1f125716084a5fb33a4bd46454ac28ad0b44b3bc 100644 (file)
--- a/mailbox.h
+++ b/mailbox.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
+ * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
  * 
  *     This program is free software; you can redistribute it and/or modify
  *     it under the terms of the GNU General Public License as published by
@@ -75,5 +75,6 @@ int mx_is_pop (const char *);
 #endif
 
 int mx_access (const char*, int);
+int mx_check_empty (const char *);
 
 #endif
diff --git a/main.c b/main.c
index da15e3a14ff648a21842dbe4101cd5412e6504a7..b1478a1571e5081023eeb8357a0eeb9ae972843d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -838,19 +838,15 @@ int main (int argc, char **argv)
 
     if (flags & M_IGNORE)
     {
-      struct stat st;
-
       /* check to see if there are any messages in the folder */
-      if (stat (folder, &st) != 0)
-      {
-       mutt_endwin (strerror (errno));
-       exit (1);
-      }
-
-      if (st.st_size == 0)
+      switch (mx_check_empty (folder))
       {
-       mutt_endwin _("Mailbox is empty.");
-       exit (1);
+       case -1:
+         mutt_endwin (strerror (errno));
+         exit (1);
+       case 1:
+         mutt_endwin _("Mailbox is empty.");
+         exit (1);
       }
     }
 
diff --git a/mbox.c b/mbox.c
index 9c72c3246921933886476fb82174d26c7fd0a2ce..fa0f22185311cbc0d5c396525b091c2b6f6a0730 100644 (file)
--- a/mbox.c
+++ b/mbox.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
+ * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
  * 
  *     This program is free software; you can redistribute it and/or modify
  *     it under the terms of the GNU General Public License as published by
@@ -1227,3 +1227,18 @@ int mutt_reopen_mailbox (CONTEXT *ctx, int *index_hint)
   return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
 }
 
+/*
+ * Returns:
+ * 1 if the mailbox is not empty
+ * 0 if the mailbox is empty
+ * -1 on error
+ */
+int mbox_check_empty (const char *path)
+{
+  struct stat st;
+
+  if (stat (path, &st) == -1)
+    return -1;
+
+  return ((st.st_size == 0));
+}
diff --git a/mh.c b/mh.c
index 85d6e8dbf74524ff55907c7695b14b8457e8e1c3..577e33853851bfdcc8d3f0ed78ad3116c71b572a 100644 (file)
--- a/mh.c
+++ b/mh.c
@@ -1722,3 +1722,70 @@ FILE *maildir_open_find_message (const char *folder, const char *msg)
 
   return NULL;
 }
+
+
+/*
+ * Returns:
+ * 1 if there are no messages in the mailbox
+ * 0 if there are messages in the mailbox
+ * -1 on error
+ */
+int maildir_check_empty (const char *path)
+{
+  DIR *dp;
+  struct dirent *de;
+  int r = 1; /* assume empty until we find a message */
+  char realpath[_POSIX_PATH_MAX];
+  int iter = 0;
+
+  /* Strategy here is to look for any file not beginning with a period */
+
+  do {
+    /* we do "cur" on the first iteration since its more likely that we'll
+     * find old messages without having to scan both subdirs
+     */
+    snprintf (realpath, sizeof (realpath), "%s/%s", path,
+             iter == 0 ? "cur" : "new");
+    if ((dp = opendir (realpath)) == NULL)
+      return -1;
+    while ((de = readdir (dp)))
+    {
+      if (*de->d_name != '.')
+      {
+       r = 0;
+       break;
+      }
+    }
+    closedir (dp);
+    iter++;
+  } while (r && iter < 2);
+
+  return r;
+}
+
+/*
+ * Returns:
+ * 1 if there are no messages in the mailbox
+ * 0 if there are messages in the mailbox
+ * -1 on error
+ */
+int mh_check_empty (const char *path)
+{
+  DIR *dp;
+  struct dirent *de;
+  int r = 1; /* assume empty until we find a message */
+  
+  if ((dp = opendir (path)) == NULL)
+    return -1;
+  while ((de = readdir (dp)))
+  {
+    if (mh_valid_message (de->d_name))
+    {
+      r = 0;
+      break;
+    }
+  }
+  closedir (dp);
+  
+  return r;
+}
diff --git a/mx.c b/mx.c
index ad57c69dd73146b55ad280a6dc42cadadfcb36c0..e1181292ff2839e0a9d1ae452ce335f96fd8cba8 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
+ * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
  * Copyright (C) 1999-2000 Thomas Roessler <roessler@guug.de>
  * 
  *     This program is free software; you can redistribute it and/or modify
@@ -1630,3 +1630,28 @@ void mx_update_context (CONTEXT *ctx, int new_messages)
     }
   }
 }
+
+/*
+ * Return:
+ * 1 if the specified mailbox contains 0 messages.
+ * 0 if the mailbox contains messages
+ * -1 on error
+ */
+int mx_check_empty (const char *path)
+{
+  switch (mx_get_magic (path))
+  {
+    case M_MBOX:
+    case M_MMDF:
+    case M_KENDRA:
+      return mbox_check_empty (path);
+    case M_MH:
+      return mh_check_empty (path);
+    case M_MAILDIR:
+      return maildir_check_empty (path);
+    default:
+      errno = EINVAL;
+      return -1;
+  }
+  /* not reached */
+}
diff --git a/mx.h b/mx.h
index e4fec2024def5b1a381e79ae0f5ea72f21a31950..145091be85a09fcc339cb12b09a5cdff28e441e2 100644 (file)
--- a/mx.h
+++ b/mx.h
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
- * Copyright (C) 1999-2000 Thomas Roessler <roessler@guug.de>
+ * Copyright (C) 1999-2002 Thomas Roessler <roessler@guug.de>
  *
  *     This program is free software; you can redistribute it and/or modify
  *     it under the terms of the GNU General Public License as published by
@@ -57,14 +57,17 @@ int mbox_lock_mailbox (CONTEXT *, int, int);
 int mbox_parse_mailbox (CONTEXT *);
 int mmdf_parse_mailbox (CONTEXT *);
 void mbox_unlock_mailbox (CONTEXT *);
+int mbox_check_empty (const char *);
 
 int mh_read_dir (CONTEXT *, const char *);
 int mh_sync_mailbox (CONTEXT *, int *);
 int mh_check_mailbox (CONTEXT *, int *);
 int mh_buffy (const char *);
+int mh_check_empty (const char *);
 
 int maildir_read_dir (CONTEXT *);
 int maildir_check_mailbox (CONTEXT *, int *);
+int maindir_check_empty (const char *);
 
 int maildir_commit_message (CONTEXT *, MESSAGE *, HEADER *);
 int mh_commit_message (CONTEXT *, MESSAGE *, HEADER *);