summaryrefslogtreecommitdiff
path: root/src/maildir-create.cc
blob: 97b8f7107e7cf5bf85219f655c8ccf36710f1c49 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * @file maildir-create.cc
 * @brief  Implementation of the Maildir class.
 * @author Andreas Aardal Hanssen
 * @date 2002-2005
 */

#include "maildir.h"

#include <errno.h>

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

using namespace Binc;
using std::string;

bool Binc::Maildir::createMailbox(const string &s_in,
                                  mode_t mode,
                                  uid_t owner,
                                  gid_t group,
                                  bool root)
{
  if (s_in != "." && mkdir(s_in.c_str(), mode) == -1) {
    setLastError("unable to create " + s_in + ": " + string(strerror(errno)));
    return false;
  }

  // Allow uidvalidity, which is generated from time(0), to
  // increase with one second to avoid race conditions.
  sleep(1);

  if (mkdir((s_in + "/cur").c_str(), mode) == -1) {
    setLastError("unable to create " + s_in + "/cur: " + string(strerror(errno)));
    return false;
  }

  if (mkdir((s_in + "/new").c_str(), mode) == -1) {
    setLastError("unable to create " + s_in + "/new: " + string(strerror(errno)));
    return false;
  }

  if (mkdir((s_in + "/tmp").c_str(), mode) == -1) {
    setLastError("unable to create " + s_in + "/tmp: " + string(strerror(errno)));
    return false;
  }

  if (owner == 0 && group == 0) return true;

  if (chown(s_in.c_str(), owner, group) == -1) {
    setLastError("unable to chown " + s_in + ": " + string(strerror(errno)));
    return false;
  }

  if (chown((s_in + "/cur").c_str(), owner, group) == -1) {
    setLastError("unable to chown " + s_in + "/cur: " + string(strerror(errno)));
    return false;
  }

  if (chown((s_in + "/new").c_str(), owner, group) == -1) {
    setLastError("unable to chown " + s_in + "/new: " + string(strerror(errno)));
    return false;
  }

  if (chown((s_in + "/tmp").c_str(), owner, group) == -1) {
    setLastError("unable to chown " + s_in + "/tmp: " + string(strerror(errno)));
    return false;
  }

  return true;
}