From 53dc13293cef62b4d0ba0e1ac664662951c0fb3a Mon Sep 17 00:00:00 2001 From: Thomas Roessler Date: Thu, 28 Mar 2002 13:25:22 +0000 Subject: [PATCH] patch-1.3.28-me.emptycheck.1 --- mailbox.h | 3 ++- main.c | 18 ++++++--------- mbox.c | 17 +++++++++++++- mh.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ mx.c | 27 +++++++++++++++++++++- mx.h | 5 ++++- 6 files changed, 122 insertions(+), 15 deletions(-) diff --git a/mailbox.h b/mailbox.h index 4ac8e8dc..1f125716 100644 --- a/mailbox.h +++ b/mailbox.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 1996-2000 Michael R. Elkins + * Copyright (C) 1996-2002 Michael R. Elkins * * 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 da15e3a1..b1478a15 100644 --- 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 9c72c324..fa0f2218 100644 --- a/mbox.c +++ b/mbox.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1996-2000 Michael R. Elkins + * Copyright (C) 1996-2002 Michael R. Elkins * * 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 85d6e8db..577e3385 100644 --- 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 ad57c69d..e1181292 100644 --- a/mx.c +++ b/mx.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1996-2000 Michael R. Elkins + * Copyright (C) 1996-2002 Michael R. Elkins * Copyright (C) 1999-2000 Thomas Roessler * * 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 e4fec202..145091be 100644 --- a/mx.h +++ b/mx.h @@ -1,6 +1,6 @@ /* * Copyright (C) 1996-2002 Michael R. Elkins - * Copyright (C) 1999-2000 Thomas Roessler + * Copyright (C) 1999-2002 Thomas Roessler * * 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 *); -- 2.40.0