Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
maildir-delete.cc
Go to the documentation of this file.
1
7#include "maildir.h"
8
9#include <fcntl.h>
10#include <unistd.h>
11#include <errno.h>
12
13#include <sys/types.h>
14#include <sys/stat.h>
15
16#include <dirent.h>
17#include <unistd.h>
18
19using namespace ::std;
20using namespace Binc;
21
22namespace {
23
24 bool recursiveDelete(const string &path)
25 {
26 DIR *mydir = opendir(path.c_str());
27 if (mydir == 0)
28 return false;
29
30 struct dirent *mydirent;
31 while ((mydirent = readdir(mydir)) != 0) {
32 string d = mydirent->d_name;
33 if (d == "." || d == "..")
34 continue;
35
36 string f = path + "/" + d;
37
38 struct stat mystat;
39 if (lstat(f.c_str(), &mystat) != 0) {
40 if (errno == ENOENT)
41 continue;
42 return false;
43 }
44
45 if (S_ISDIR(mystat.st_mode)) {
46 if (!recursiveDelete(f)) {
47 closedir(mydir);
48 return false;
49 }
50 if (rmdir(f.c_str()) != 0 && errno != ENOENT) {
51 closedir(mydir);
52 return false;
53 }
54 } else {
55 if (unlink(f.c_str()) != 0 && errno != ENOENT) {
56 closedir(mydir);
57 return false;
58 }
59 }
60 }
61
62 closedir(mydir);
63 return true;
64 }
65}
66
67//------------------------------------------------------------------------
68bool Binc::Maildir::deleteMailbox(const string &s_in)
69{
70 if (s_in == ".") {
71 setLastError("disallowed by rule");
72 return false;
73 }
74
75 if (!recursiveDelete(s_in)) {
76 setLastError("error deleting Maildir - status is undefined");
77 return false;
78 }
79
80 if (rmdir(s_in.c_str()) != 0) {
81 setLastError("error deleting Maildir: "
82 + string(strerror(errno))
83 + " - status is undefined");
84 return false;
85 }
86
87 return true;
88}
void setLastError(const std::string &error) const
Definition: mailbox.cc:109
bool deleteMailbox(const std::string &s)
Declaration of the Maildir class.
Definition: bincimapd.cc:9