summaryrefslogtreecommitdiff
path: root/src/maildir-expunge.cc
blob: 75c8f3dd3f45c5d9dfe2d4b271d1e5713306440e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**  --------------------------------------------------------------------
 *  @file maildir-expunge.cc
 *  @brief  Implementation of the Maildir class.
 *  @author Andreas Aardal Hanssen
 *  @date 2002-2005
 *  -----------------------------------------------------------------  **/
#include "iodevice.h"
#include "iofactory.h"
#include "maildir.h"
#include "maildirmessage.h"

#include <errno.h>

#include <unistd.h>

using namespace ::std;
using namespace Binc;

//------------------------------------------------------------------------
void Maildir::expungeMailbox(void)
{
  if (readOnly) return;

  Mailbox::iterator i = begin(SequenceSet::all(), SQNR_MODE | INCLUDE_EXPUNGED);

  bool success = true;
  for (; success && i != end(); ++i) {
    MaildirMessage &message = reinterpret_cast<MaildirMessage &>(*i);

    if ((message.getStdFlags() & Message::F_DELETED) == 0) continue;

    message.setExpunged();

    const string &id = message.getUnique();

    // The message might be gone already
    MaildirIndexItem *item = index.find(id);
    if (!item) continue;

    string fpath = path + "/cur/" + item->fileName;

    while (unlink(fpath.c_str()) != 0) {
      if (errno != ENOENT) {
        bincWarning << "unable to remove " << fpath << ": " << strerror(errno) << endl;
        break;
      }

      if (!scanFileNames()) {
        success = false;
        break;
      }

      if ((item = index.find(id)))
        break;
      else
        fpath = path + "/cur/" + item->fileName;
    }
  }
}