bincimap

Log | Files | Refs | LICENSE

maildir-delete.cc (2817B)


      1 /* -*- Mode: c++; -*- */
      2 /*  --------------------------------------------------------------------
      3  *  Filename:
      4  *    maildir-delete.cc
      5  *  
      6  *  Description:
      7  *    Implementation of the Maildir class.
      8  *
      9  *  Authors:
     10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
     11  *
     12  *  Bugs:
     13  *
     14  *  ChangeLog:
     15  *
     16  *  --------------------------------------------------------------------
     17  *  Copyright 2002-2005 Andreas Aardal Hanssen
     18  *
     19  *  This program is free software; you can redistribute it and/or modify
     20  *  it under the terms of the GNU General Public License as published by
     21  *  the Free Software Foundation; either version 2 of the License, or
     22  *  (at your option) any later version.
     23  *
     24  *  This program is distributed in the hope that it will be useful,
     25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     27  *  GNU General Public License for more details.
     28  *
     29  *  You should have received a copy of the GNU General Public License
     30  *  along with this program; if not, write to the Free Software
     31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
     32  *  --------------------------------------------------------------------
     33  */
     34 #ifdef HAVE_CONFIG_H
     35 #include <config.h>
     36 #endif
     37 
     38 #include "maildir.h"
     39 
     40 #include <fcntl.h>
     41 #include <unistd.h>
     42 
     43 #include <sys/types.h>
     44 #include <sys/stat.h>
     45 
     46 #include <dirent.h>
     47 #include <unistd.h>
     48 
     49 using namespace ::std;
     50 using namespace Binc;
     51 
     52 namespace {
     53 
     54   bool recursiveDelete(const string &path)
     55   {
     56     DIR *mydir = opendir(path.c_str());
     57     if (mydir == 0)
     58       return false;
     59 
     60     struct dirent *mydirent;
     61     while ((mydirent = readdir(mydir)) != 0) {
     62       string d = mydirent->d_name;
     63       if (d == "." || d == "..")
     64 	continue;
     65 
     66       string f = path + "/" + d;
     67 
     68       struct stat mystat;
     69       if (lstat(f.c_str(), &mystat) != 0) {
     70 	if (errno == ENOENT)
     71 	  continue;
     72 	return false;
     73       }
     74 
     75       if (S_ISDIR(mystat.st_mode)) {
     76 	if (!recursiveDelete(f)) {
     77 	  closedir(mydir);
     78 	  return false;
     79 	}
     80 	if (rmdir(f.c_str()) != 0 && errno != ENOENT) {
     81 	  closedir(mydir);
     82 	  return false;
     83 	}
     84       } else {
     85 	if (unlink(f.c_str()) != 0 && errno != ENOENT) {
     86 	  closedir(mydir);
     87 	  return false;
     88 	}
     89       }
     90     }
     91 
     92     closedir(mydir);
     93     return true;
     94   }
     95 }
     96 
     97 //------------------------------------------------------------------------
     98 bool Binc::Maildir::deleteMailbox(const string &s_in)
     99 {
    100   if (s_in == ".") {
    101     setLastError("disallowed by rule");
    102     return false;
    103   }
    104 
    105   if (!recursiveDelete(s_in)) {
    106     setLastError("error deleting Maildir - status is undefined");
    107     return false;
    108   }
    109 
    110   if (rmdir(s_in.c_str()) != 0) {
    111     setLastError("error deleting Maildir: " 
    112 		 + string(strerror(errno))
    113 		 + " - status is undefined");
    114     return false;
    115   }
    116 
    117   return true;
    118 }