commit-mailutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SCM] GNU Mailutils branch, master, updated. release-2.2-80-g756eb2b


From: Sergey Poznyakoff
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-80-g756eb2b
Date: Wed, 15 Sep 2010 23:03:29 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU Mailutils".

http://git.savannah.gnu.org/cgit/mailutils.git/commit/?id=756eb2bcfb781350f70e77d2e41fa8ae6e692cdd

The branch, master has been updated
       via  756eb2bcfb781350f70e77d2e41fa8ae6e692cdd (commit)
      from  ec989a562f5a64f36909dc0a151fc24b50a733be (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 756eb2bcfb781350f70e77d2e41fa8ae6e692cdd
Author: Sergey Poznyakoff <address@hidden>
Date:   Thu Sep 16 01:53:04 2010 +0300

    Implement `remove' method for mailboxes.
    
    * include/mailutils/mailbox.h (mu_mailbox_remove): New function.
    * include/mailutils/stream.h: Add some comments.
    * include/mailutils/sys/amd.h (_amd_data)<remove>: New method.
    (amd_remove_dir): New function.
    * include/mailutils/sys/mailbox.h (_mu_mailbox)<_remove>: New
    method.
    * libmailutils/amd.c (amd_remove_mbox): New function.
    (amd_init_mailbox): Initialize the _remove method.
    (amd_remove_dir): New function.
    * libmailutils/errors (MU_ERR_MBX_REMOVED)
    (MU_ERR_NOT_OPEN, MU_ERR_OPEN): New error codes.
    * libmailutils/mailbox.c: Keep state of the mailbox (open vs. not
    open, removed). Check it before doing anything on it.
    (_MU_MAILBOX_OPEN, _MU_MAILBOX_REMOVED, _MU_MAILBOX_MASK): New
    defines.
    (mu_mailbox_open): Set _MU_MAILBOX_OPEN if the operation succeeds.
    (mu_mailbox_close): Clear _MU_MAILBOX_OPEN if the operation succeeds.
    Refuse to run if the mailbox was not opened.
    (mu_mailbox_remove): New function.
    (all functions): return MU_ERR_MBX_NULL if the mbox argument is
    NULL.
    Check mailbox state on entry and proceed accordingly.
    * libproto/maildir/mbox.c: Implement _remove method.
    (maildir_remove): New function.
    (_mailbox_maildir_init): Initialize amd->_remove.
    * libproto/mbox/mbox.c: Implement _remove method.
    (mbox_remove): New function.
    (_mailbox_mbox_init): Initialize amd->_remove.
    * libproto/mh/mbox.c: Implement _remove method.
    (mh_remove): New function.
    (_mailbox_mh_init): Initialize amd->_remove.
    
    * libmailutils/tests/mbdel.c: New file.
    * libmailutils/tests/.gitignore: Add mbdel.
    * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Likewise.
    (LDADD): List all mailbox formats.
    
    * imap4d/delete.c (imap4d_delete): Use mu_mailbox_remove to
    delete the folder.  Fall back to remove() if it does not appear
    to be a mailbox.

-----------------------------------------------------------------------

Summary of changes:
 imap4d/delete.c                                  |   20 +++-
 include/mailutils/mailbox.h                      |    1 +
 include/mailutils/stream.h                       |    3 +-
 include/mailutils/sys/amd.h                      |    2 +
 include/mailutils/sys/mailbox.h                  |    1 +
 libmailutils/amd.c                               |  115 +++++++++++++++++++-
 libmailutils/errors                              |    4 +
 libmailutils/file_stream.c                       |    2 +-
 libmailutils/mailbox.c                           |  129 +++++++++++++--------
 libmailutils/tests/.gitignore                    |    1 +
 libmailutils/tests/Makefile.am                   |    5 +
 examples/muemail.c => libmailutils/tests/mbdel.c |   55 +++++-----
 libproto/maildir/mbox.c                          |   23 ++++
 libproto/mbox/mbox.c                             |   13 ++-
 libproto/mh/mbox.c                               |   10 ++-
 15 files changed, 302 insertions(+), 82 deletions(-)
 copy examples/muemail.c => libmailutils/tests/mbdel.c (51%)

diff --git a/imap4d/delete.c b/imap4d/delete.c
index b7d30ae..11f78be 100644
--- a/imap4d/delete.c
+++ b/imap4d/delete.c
@@ -37,7 +37,8 @@ imap4d_delete (struct imap4d_command *command, 
imap4d_tokbuf_t tok)
   const char *msg = "Completed";
   const char *delim = "/";
   char *name;
-
+  mu_mailbox_t tmpbox;
+  
   if (imap4d_tokbuf_argc (tok) != 3)
     return io_completion_response (command, RESP_BAD, "Invalid arguments");
   name = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1);
@@ -54,7 +55,22 @@ imap4d_delete (struct imap4d_command *command, 
imap4d_tokbuf_t tok)
   if (!name)
     return io_completion_response (command, RESP_NO, "Cannot remove");
 
-  if (remove (name) != 0)
+  rc = mu_mailbox_create (&tmpbox, name);
+  if (rc == 0)
+    {
+      rc = mu_mailbox_remove (tmpbox);
+      mu_mailbox_destroy (&tmpbox);
+      if (rc)
+       mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_remove", name, rc);
+    }
+  else
+    {
+      rc = remove (name);
+      if (rc)
+       mu_diag_funcall (MU_DIAG_ERROR, "remove", name, errno);
+    }
+
+  if (rc)
     {
       rc = RESP_NO;
       msg = "Cannot remove";
diff --git a/include/mailutils/mailbox.h b/include/mailutils/mailbox.h
index 1d1ce96..74d3407 100644
--- a/include/mailutils/mailbox.h
+++ b/include/mailutils/mailbox.h
@@ -49,6 +49,7 @@ extern void mu_mailbox_destroy         (mu_mailbox_t *);
 
 extern int  mu_mailbox_open            (mu_mailbox_t, int flag);
 extern int  mu_mailbox_close           (mu_mailbox_t);
+extern int  mu_mailbox_remove          (mu_mailbox_t mbox);
 extern int  mu_mailbox_flush           (mu_mailbox_t mbox, int expunge);
 extern int  mu_mailbox_get_folder      (mu_mailbox_t, mu_folder_t *);
 extern int  mu_mailbox_set_folder      (mu_mailbox_t, mu_folder_t);
diff --git a/include/mailutils/stream.h b/include/mailutils/stream.h
index d8567ac..95b2790 100644
--- a/include/mailutils/stream.h
+++ b/include/mailutils/stream.h
@@ -27,7 +27,6 @@ enum mu_buffer_type
     mu_buffer_full
   };
 
-
 #define MU_SEEK_SET      0
 #define MU_SEEK_CUR      1
 #define MU_SEEK_END      2
@@ -38,8 +37,10 @@ enum mu_buffer_type
 #define MU_STREAM_SEEK        0x00000004
 #define MU_STREAM_APPEND      0x00000008
 #define MU_STREAM_CREAT              0x00000010
+/* So far used only by TCP streams. */
 #define MU_STREAM_NONBLOCK    0x00000020
 #define MU_STREAM_AUTOCLOSE   0x00000040
+/* Not used. Intended for mailboxes only. */
 #define MU_STREAM_NONLOCK     0x00000080
 #define MU_STREAM_ALLOW_LINKS 0x00000100
 /* FIXME: This one affects only mailboxes */  
diff --git a/include/mailutils/sys/amd.h b/include/mailutils/sys/amd.h
index b72c3c1..52f0395 100644
--- a/include/mailutils/sys/amd.h
+++ b/include/mailutils/sys/amd.h
@@ -82,6 +82,7 @@ struct _amd_data
   int (*msg_cmp) (struct _amd_message *, struct _amd_message *);
   int (*message_uid) (mu_message_t msg, size_t *puid);
   size_t (*next_uid) (struct _amd_data *mhd);
+  int (*remove) (struct _amd_data *);
   
   /* List of messages: */
   size_t msg_count; /* number of messages in the list */
@@ -112,5 +113,6 @@ void amd_cleanup (void *arg);
 struct _amd_message *_amd_get_message (struct _amd_data *amd, size_t msgno);
 int amd_msg_lookup (struct _amd_data *amd, struct _amd_message *msg,
                    size_t *pret);
+int amd_remove_dir (const char *name);
 
 #endif             
diff --git a/include/mailutils/sys/mailbox.h b/include/mailutils/sys/mailbox.h
index 12af8fe..fe36269 100644
--- a/include/mailutils/sys/mailbox.h
+++ b/include/mailutils/sys/mailbox.h
@@ -54,6 +54,7 @@ struct _mu_mailbox
 
   int  (*_open)            (mu_mailbox_t, int);
   int  (*_close)           (mu_mailbox_t);
+  int  (*_remove)          (mu_mailbox_t);
   
   /* messages */
   int  (*_get_message)     (mu_mailbox_t, size_t, mu_message_t *);
diff --git a/libmailutils/amd.c b/libmailutils/amd.c
index 3cf3dde..a0dec4e 100644
--- a/libmailutils/amd.c
+++ b/libmailutils/amd.c
@@ -112,6 +112,7 @@ static int amd_envelope_date (mu_envelope_t envelope, char 
*buf, size_t len,
                              size_t *psize);
 static int amd_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
                                size_t *psize);
+static int amd_remove_mbox (mu_mailbox_t mailbox);
 
 
 static int amd_body_stream_read (mu_stream_t str, char *buffer,
@@ -309,7 +310,8 @@ amd_init_mailbox (mu_mailbox_t mailbox, size_t amd_size,
   mailbox->_is_updated = amd_is_updated;
 
   mailbox->_get_size = amd_get_size;
-
+  mailbox->_remove = amd_remove_mbox;
+  
   MU_DEBUG1 (mailbox->debug, MU_DEBUG_TRACE1, "amd_init(%s)\n", amd->name);
   *pamd = amd;
   return 0;
@@ -1100,6 +1102,40 @@ compute_mailbox_size (struct _amd_data *amd, const char 
*name, mu_off_t *psize)
 }
 
 static int
+amd_remove_mbox (mu_mailbox_t mailbox)
+{
+  int rc;
+  struct _amd_data *amd = mailbox->data;
+  
+  if (!amd->remove)
+    return ENOSYS;
+  rc = amd->remove (amd);
+  if (rc == 0)
+    {
+      char *name = make_size_file_name (amd);
+      if (!name)
+       return ENOMEM;
+      if (unlink (name) && errno != ENOENT)
+       rc = errno;
+      free (name);
+    }
+
+  if (rc == 0)
+    {
+      if (rmdir (amd->name) && errno != ENOENT)
+       {
+         rc = errno;
+         /* POSIX.1-2001 allows EEXIST to be returned if the directory
+            contained entries other than . and .. */
+         if (rc == EEXIST)
+           rc = ENOTEMPTY;
+       }
+    }
+  
+  return rc;
+}
+
+static int
 amd_expunge (mu_mailbox_t mailbox)
 {
   struct _amd_data *amd = mailbox->data;
@@ -1922,4 +1958,81 @@ amd_envelope_sender (mu_envelope_t envelope, char *buf, 
size_t len, size_t *psiz
   return 0;
 }
 
+
+int
+amd_remove_dir (const char *name)
+{
+  DIR *dir;
+  struct dirent *ent;
+  char *namebuf;
+  size_t namelen, namesize;
+  int rc = 0;
+  int has_subdirs = 0;
+  
+  namelen = strlen (name);
+  namesize = namelen + 128;
+  namebuf = malloc (namesize);
+  if (!namebuf)
+    return ENOMEM;
+  memcpy (namebuf, name, namelen);
+  if (namebuf[namelen - 1] != '/')
+    namebuf[namelen++] = '/';
+  
+  dir = opendir (name);
+  if (!dir)
+    return errno;
+  while ((ent = readdir (dir)))
+    {
+      struct stat st;
+      size_t len;
+
+      if (strcmp (ent->d_name, ".") == 0 ||
+         strcmp (ent->d_name, "..") == 0)
+       continue;
+      len = strlen (ent->d_name);
+      if (namelen + len >= namesize)
+       {
+         char *p;
+
+         namesize += len + 1;
+         p = realloc (namebuf, namesize);
+         if (!p)
+           {
+             rc = ENOMEM;
+             break;
+           }
+       }
+      strcpy (namebuf + namelen, ent->d_name);
+      if (stat (namebuf, &st) == 0 && S_ISDIR (st.st_mode))
+       {
+         has_subdirs = 1;
+         continue;
+       }
+      
+      if (unlink (namebuf))
+       {
+         rc = errno;
+         mu_diag_output (MU_DIAG_WARNING,
+                         "failed to remove %s: %s",
+                         namebuf, mu_strerror (rc));
+         break;
+       }
+    }
+  closedir (dir);
+  free (namebuf);
+
+  if (rc == 0 && !has_subdirs)
+    {
+      if (rmdir (name))
+       {
+         rc = errno;
+         /* POSIX.1-2001 allows EEXIST to be returned if the directory
+            contained entries other than . and .. */
+         if (rc == EEXIST)
+           rc = ENOTEMPTY;
+       }
+    }
+  return rc;
+}
+
 
diff --git a/libmailutils/errors b/libmailutils/errors
index 06eb98b..5b4d2f1 100644
--- a/libmailutils/errors
+++ b/libmailutils/errors
@@ -26,6 +26,10 @@ MU_ERR_OUT_NULL             _("Pointer to output null")
 MU_ERR_OUT_PTR_NULL         _("Pointer to output pointer null")
 
 MU_ERR_MBX_NULL             _("Mailbox null")
+MU_ERR_MBX_REMOVED          _("Mailbox removed")
+
+MU_ERR_NOT_OPEN             _("Resource not open")
+MU_ERR_OPEN                 _("Resource still open")
 
 MU_ERR_BAD_822_FORMAT       _("Format of RFC822 object is bad")
 MU_ERR_EMPTY_ADDRESS        _("Address contains no addr specs")
diff --git a/libmailutils/file_stream.c b/libmailutils/file_stream.c
index 0df1254..dccbd0d 100644
--- a/libmailutils/file_stream.c
+++ b/libmailutils/file_stream.c
@@ -121,7 +121,7 @@ fd_open (struct _mu_stream *str)
     {
       struct stat fdbuf, filebuf;
 
-      /* The next two stats should never fail.  */
+      /* The following two stats should never fail.  */
       if (fstat (fd, &fdbuf) == -1
          || lstat (fstr->filename, &filebuf) == -1)
        {
diff --git a/libmailutils/mailbox.c b/libmailutils/mailbox.c
index e1c7b59..9344f01 100644
--- a/libmailutils/mailbox.c
+++ b/libmailutils/mailbox.c
@@ -44,6 +44,11 @@
 #include <mailutils/sys/mailbox.h>
 #include <mailutils/sys/url.h>
 
+/* Mailbox-specific flags */
+#define _MU_MAILBOX_OPEN    0x10000000
+#define _MU_MAILBOX_REMOVED 0x20000000
+#define _MU_MAILBOX_MASK    0xF0000000
+
 static int
 mailbox_folder_create (mu_mailbox_t mbox, const char *name,
                       mu_record_t record)
@@ -287,7 +292,11 @@ mu_mailbox_destroy (mu_mailbox_t *pmbox)
 int
 mu_mailbox_open (mu_mailbox_t mbox, int flag)
 {
-  if (mbox == NULL || mbox->_open == NULL)
+  int rc;
+  
+  if (!mbox)
+    return MU_ERR_MBX_NULL;
+  if (mbox->_open == NULL)
     return MU_ERR_EMPTY_VFN;
   if (flag & MU_STREAM_QACCESS)
     {
@@ -296,16 +305,42 @@ mu_mailbox_open (mu_mailbox_t mbox, int flag)
                  | MU_STREAM_APPEND | MU_STREAM_CREAT))
        return EINVAL; /* FIXME: Better error code, please? */
     }
-  return mbox->_open (mbox, flag);
+  rc = mbox->_open (mbox, flag);
+  if (rc == 0)
+    mbox->flags |= _MU_MAILBOX_OPEN;
+  return rc;
 }
 
 int
 mu_mailbox_close (mu_mailbox_t mbox)
 {
+  int rc;
+
+  if (!mbox)
+    return MU_ERR_MBX_NULL;
+  if (!(mbox->flags & _MU_MAILBOX_OPEN))
+    return MU_ERR_NOT_OPEN;
   if (mbox == NULL || mbox->_close == NULL)
     return MU_ERR_EMPTY_VFN;
 
-  return mbox->_close (mbox);
+  rc = mbox->_close (mbox);
+  if (rc == 0)
+    mbox->flags &= ~_MU_MAILBOX_OPEN;
+  return rc;
+}
+
+int
+mu_mailbox_remove (mu_mailbox_t mbox)
+{
+  if (!mbox)
+    return MU_ERR_MBX_NULL;
+  if (mbox->flags & _MU_MAILBOX_OPEN)
+    return MU_ERR_OPEN;
+  if (mbox->flags & _MU_MAILBOX_REMOVED)
+    return MU_ERR_MBX_REMOVED;
+  if (!mbox->_remove)
+    return MU_ERR_EMPTY_VFN;
+  return mbox->_remove (mbox);
 }
 
 int
@@ -315,7 +350,11 @@ mu_mailbox_flush (mu_mailbox_t mbox, int expunge)
   int status = 0;
   
   if (!mbox)
-    return EINVAL;
+    return MU_ERR_MBX_NULL;
+  if (mbox->flags & _MU_MAILBOX_REMOVED)
+    return MU_ERR_MBX_REMOVED;
+  if (!(mbox->flags & _MU_MAILBOX_OPEN))
+    return _MU_MAILBOX_OPEN;
   if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
     return 0;
 
@@ -338,12 +377,29 @@ mu_mailbox_flush (mu_mailbox_t mbox, int expunge)
   return status;
 }
 
+#define _MBOX_CHECK_FLAGS(mbox)                        \
+  if (mbox == NULL)                            \
+    return MU_ERR_MBX_NULL;                    \
+  if (mbox->flags & _MU_MAILBOX_REMOVED)       \
+    return MU_ERR_MBX_REMOVED;                 \
+  if (!(mbox->flags & _MU_MAILBOX_OPEN))       \
+    return _MU_MAILBOX_OPEN
+
+#define _MBOX_CHECK(mbox,method)               \
+  _MBOX_CHECK_FLAGS(mbox);                     \
+  if (mbox->method == NULL)                    \
+    return MU_ERR_EMPTY_VFN
+
+#define _MBOX_CHECK_Q(mbox,method)             \
+  _MBOX_CHECK(mbox,method);                    \
+  if (mbox->flags & MU_STREAM_QACCESS)         \
+    return MU_ERR_BADOP
+
 /* messages */
 int
 mu_mailbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
 {
-  if (mbox == NULL || mbox->_append_message == NULL)
-    return MU_ERR_EMPTY_VFN;
+  _MBOX_CHECK_Q (mbox, _append_message);
   if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
     return EACCES;
   return mbox->_append_message (mbox, msg);
@@ -352,10 +408,7 @@ mu_mailbox_append_message (mu_mailbox_t mbox, mu_message_t 
msg)
 int
 mu_mailbox_get_message (mu_mailbox_t mbox, size_t msgno,  mu_message_t *pmsg)
 {
-  if (mbox == NULL || mbox->_get_message == NULL)
-    return MU_ERR_EMPTY_VFN;
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return MU_ERR_BADOP;
+  _MBOX_CHECK_Q (mbox, _get_message);
   return mbox->_get_message (mbox, msgno, pmsg);
 }
 
@@ -363,8 +416,7 @@ int
 mu_mailbox_quick_get_message (mu_mailbox_t mbox, mu_message_qid_t qid,
                              mu_message_t *pmsg)
 {
-  if (mbox == NULL || mbox->_quick_get_message == NULL)
-    return MU_ERR_EMPTY_VFN;
+  _MBOX_CHECK (mbox, _quick_get_message);
   if (!(mbox->flags & MU_STREAM_QACCESS))
     return MU_ERR_BADOP;
   return mbox->_quick_get_message (mbox, qid, pmsg);
@@ -373,38 +425,28 @@ mu_mailbox_quick_get_message (mu_mailbox_t mbox, 
mu_message_qid_t qid,
 int
 mu_mailbox_messages_count (mu_mailbox_t mbox, size_t *num)
 {
-  if (mbox == NULL || mbox->_messages_count == NULL)
-    return MU_ERR_EMPTY_VFN;
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return MU_ERR_BADOP;
+  _MBOX_CHECK_Q (mbox, _messages_count);
   return mbox->_messages_count (mbox, num);
 }
 
 int
 mu_mailbox_messages_recent (mu_mailbox_t mbox, size_t *num)
 {
-  if (mbox == NULL || mbox->_messages_recent == NULL)
-    return MU_ERR_EMPTY_VFN;
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return MU_ERR_BADOP;
+  _MBOX_CHECK_Q (mbox, _messages_recent);
   return mbox->_messages_recent (mbox, num);
 }
 
 int
 mu_mailbox_message_unseen (mu_mailbox_t mbox, size_t *num)
 {
-  if (mbox == NULL || mbox->_message_unseen == NULL)
-    return MU_ERR_EMPTY_VFN;
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return MU_ERR_BADOP;
+  _MBOX_CHECK_Q (mbox, _message_unseen);
   return mbox->_message_unseen (mbox, num);
 }
 
 int
 mu_mailbox_sync (mu_mailbox_t mbox)
 {
-  if (mbox == NULL || mbox->_sync == NULL)
-    return MU_ERR_EMPTY_VFN;
+  _MBOX_CHECK_Q (mbox, _sync);
   if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
     return 0;
   return mbox->_sync (mbox);
@@ -414,18 +456,13 @@ mu_mailbox_sync (mu_mailbox_t mbox)
 int
 mu_mailbox_save_attributes (mu_mailbox_t mbox)
 {
-  if (mbox == NULL || mbox->_sync == NULL)
-    return MU_ERR_EMPTY_VFN;
-  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
-    return EACCES;
-  return mbox->_sync (mbox);
+  return mu_mailbox_sync (mbox);
 }
 
 int
 mu_mailbox_expunge (mu_mailbox_t mbox)
 {
-  if (mbox == NULL || mbox->_expunge == NULL)
-    return MU_ERR_EMPTY_VFN;
+  _MBOX_CHECK_Q (mbox, _expunge);
   if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
     return EACCES;
   return mbox->_expunge (mbox);
@@ -434,7 +471,10 @@ mu_mailbox_expunge (mu_mailbox_t mbox)
 int
 mu_mailbox_is_updated (mu_mailbox_t mbox)
 {
-  if (mbox == NULL || mbox->_is_updated == NULL)
+  if (mbox == NULL ||
+      !(mbox->flags & _MU_MAILBOX_OPEN) ||
+      (mbox->flags & _MU_MAILBOX_REMOVED) ||
+      mbox->_is_updated == NULL)
     return 1;
   if (mbox->flags & MU_STREAM_QACCESS)
     return 1;
@@ -444,10 +484,7 @@ mu_mailbox_is_updated (mu_mailbox_t mbox)
 int
 mu_mailbox_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
 {
-  if (mbox == NULL || mbox->_scan == NULL)
-    return MU_ERR_EMPTY_VFN;
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return MU_ERR_BADOP;
+  _MBOX_CHECK_Q (mbox, _scan);
   return mbox->_scan (mbox, msgno, pcount);
 }
 
@@ -455,8 +492,8 @@ int
 mu_mailbox_get_size (mu_mailbox_t mbox, mu_off_t *psize)
 {
   int status;
-  if (mbox == NULL)
-    return MU_ERR_EMPTY_VFN;
+
+  _MBOX_CHECK_FLAGS (mbox);
   if (mbox->flags & MU_STREAM_QACCESS)
     return MU_ERR_BADOP;
   if (mbox->_get_size == NULL
@@ -489,20 +526,14 @@ mu_mailbox_get_size (mu_mailbox_t mbox, mu_off_t *psize)
 int
 mu_mailbox_uidvalidity (mu_mailbox_t mbox, unsigned long *pvalid)
 {
-  if (mbox == NULL || mbox->_uidvalidity == NULL)
-    return MU_ERR_EMPTY_VFN;
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return MU_ERR_BADOP;
+  _MBOX_CHECK_Q (mbox, _uidvalidity);
   return mbox->_uidvalidity (mbox, pvalid);
 }
 
 int
 mu_mailbox_uidnext (mu_mailbox_t mbox, size_t *puidnext)
 {
-  if (mbox == NULL || mbox->_uidnext == NULL)
-    return MU_ERR_EMPTY_VFN;
-  if (mbox->flags & MU_STREAM_QACCESS)
-    return MU_ERR_BADOP;
+  _MBOX_CHECK_Q (mbox, _uidnext);
   return mbox->_uidnext (mbox, puidnext);
 }
 
@@ -536,7 +567,7 @@ mu_mailbox_get_flags (mu_mailbox_t mbox, int *flags)
     return MU_ERR_MBX_NULL;
   if (!*flags)
     return MU_ERR_OUT_NULL;
-  *flags = mbox->flags;
+  *flags = mbox->flags & ~_MU_MAILBOX_MASK;
   return 0;
 }
 
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index 9d3e2d8..925b0df 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -11,5 +11,6 @@ encode2047
 fltst
 listop
 mailcap
+mbdel
 mimetest
 url-parse
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index f3092ce..89ca27c 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -47,6 +47,7 @@ noinst_PROGRAMS = \
  fltst\
  listop\
  mailcap\
+ mbdel\
  mimetest\
  url-parse
 
@@ -55,6 +56,10 @@ LDADD =\
  ${MU_LIB_MBOX}\
  ${MU_LIB_IMAP}\
  ${MU_LIB_POP}\
+ ${MU_LIB_NNTP}\
+ ${MU_LIB_MH}\
+ ${MU_LIB_MAILDIR}\
+ ${MU_LIB_MAILER}\
  ${MU_LIB_AUTH}\
  @address@hidden
  ${MU_LIB_MAILUTILS}
diff --git a/examples/muemail.c b/libmailutils/tests/mbdel.c
similarity index 51%
copy from examples/muemail.c
copy to libmailutils/tests/mbdel.c
index 6bceb25..802444b 100644
--- a/examples/muemail.c
+++ b/libmailutils/tests/mbdel.c
@@ -1,6 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2007, 2010 Free Software Foundation,
-   Inc.
+   Copyright (C) 2010 Free Software Foundation, Inc.
 
    GNU Mailutils is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -13,39 +12,43 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU Mailutils; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-   MA 02110-1301 USA */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-#include <stdlib.h>
+#include <unistd.h>
 #include <stdio.h>
-#include <mailutils/mutil.h>
-#include "mailutils/libargp.h"
-
-const char *capa[] = {
-  "address",
-  NULL
-};
+#include <stdlib.h>
+#include <mailutils/mailutils.h>
 
 int
-main (int argc, char *argv[])
+main (int argc, char **argv)
 {
-  int arg = 1;
-
-  if (mu_app_init (NULL, capa, NULL, argc, argv, 0, &arg, NULL))
-    exit (1);
-
-  if (!argv[arg])
-    printf ("current user -> %s\n", mu_get_user_email (0));
-  else
+  int rc;
+  mu_mailbox_t mbox;
+  
+  if (argc != 2)
     {
-      for (; argv[arg]; arg++)
-        printf ("%s -> %s\n", argv[arg], mu_get_user_email (argv[arg]));
+      fprintf (stderr, "usage: %s URL\n", argv[0]);
+      return 1;
     }
 
-  return 0;
+  mu_register_all_mbox_formats ();
+  
+  MU_ASSERT (mu_mailbox_create (&mbox, argv[1]));
+  rc = mu_mailbox_remove (mbox);
+  if (rc)
+    {
+      if (rc == ENOTEMPTY)
+       {
+         printf ("mailbox removed, but has subfolders\n");
+         rc = 0;
+       }
+      else
+       fprintf (stderr, "%s\n", mu_strerror (rc));
+    }
+  mu_mailbox_destroy (&mbox);
+  
+  return rc != 0;
 }
-
diff --git a/libproto/maildir/mbox.c b/libproto/maildir/mbox.c
index d2d7b9e..8a8b197 100644
--- a/libproto/maildir/mbox.c
+++ b/libproto/maildir/mbox.c
@@ -765,6 +765,28 @@ maildir_qfetch (struct _amd_data *amd, mu_message_qid_t 
qid)
   return 0;
 }
 
+
+static int
+maildir_remove (struct _amd_data *amd)
+{
+  int i;
+  static char *suf[3] = { NEWSUF, CURSUF, TMPSUF };
+  int rc = 0;
+
+  for (i = 0; rc == 0 && i < 3; i++)
+    {
+      char *name = maildir_mkfilename (amd->name, suf[i], NULL);
+      rc = amd_remove_dir (name);
+      if (rc)
+       mu_diag_output (MU_DIAG_WARNING,
+                       "removing contents of %s failed: %s", name,
+                       mu_strerror (rc));
+      free (name);
+    }
+  
+  return rc;
+}
+
      
 int
 _mailbox_maildir_init (mu_mailbox_t mailbox)
@@ -788,6 +810,7 @@ _mailbox_maildir_init (mu_mailbox_t mailbox)
   amd->msg_cmp = maildir_message_cmp;
   amd->message_uid = maildir_message_uid;
   amd->next_uid = maildir_next_uid;
+  amd->remove = maildir_remove;
   
   /* Set our properties.  */
   {
diff --git a/libproto/mbox/mbox.c b/libproto/mbox/mbox.c
index 9963c2f..1425358 100644
--- a/libproto/mbox/mbox.c
+++ b/libproto/mbox/mbox.c
@@ -174,6 +174,16 @@ mbox_close (mu_mailbox_t mailbox)
   return mu_stream_close (mailbox->stream);
 }
 
+static int
+mbox_remove (mu_mailbox_t mailbox)
+{
+  mbox_data_t mud = mailbox->data;
+
+  MU_DEBUG1 (mailbox->debug, MU_DEBUG_TRACE1,
+            "mbox_remove (%s)\n", mud->name);
+  return unlink (mud->name);
+}
+
 /* Cover function that calls the real thing, mbox_scan(), with
    notification set.  */
 static int
@@ -1479,7 +1489,8 @@ _mailbox_mbox_init (mu_mailbox_t mailbox)
 
   mailbox->_open = mbox_open;
   mailbox->_close = mbox_close;
-
+  mailbox->_remove = mbox_remove;
+  
   /* Overloading of the entire mailbox object methods.  */
   mailbox->_get_message = mbox_get_message;
   mailbox->_append_message = mbox_append_message;
diff --git a/libproto/mh/mbox.c b/libproto/mh/mbox.c
index b39ce6c..5995c5b 100644
--- a/libproto/mh/mbox.c
+++ b/libproto/mh/mbox.c
@@ -353,6 +353,13 @@ _mh_msg_init (struct _amd_data *amd, struct _amd_message 
*amm)
 }
 
 
+static int
+mh_remove (struct _amd_data *amd)
+{
+  return amd_remove_dir (amd->name);
+}
+
+
 
 int
 _mailbox_mh_init (mu_mailbox_t mailbox)
@@ -375,7 +382,8 @@ _mailbox_mh_init (mu_mailbox_t mailbox)
   amd->msg_cmp = mh_message_cmp;
   amd->message_uid = mh_message_uid;
   amd->next_uid = _mh_next_seq;
-  
+  amd->remove = mh_remove;
+
   /* Set our properties.  */
   {
     mu_property_t property = NULL;


hooks/post-receive
-- 
GNU Mailutils



reply via email to

[Prev in Thread] Current Thread [Next in Thread]