Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
maildir-writecache.cc
Go to the documentation of this file.
1
7#include <dirent.h>
8#include <fcntl.h>
9#include <unistd.h>
10
11#include "globals.h"
12#include "maildir.h"
13
14using namespace ::std;
15
16//------------------------------------------------------------------------
18{
19 if (readOnly)
20 return true;
21
22 char *safename = strdup((path + "/.bincimap-cache-tmp-XXXXXX").c_str());
23 int fd = mkstemp(safename);
24 if (!fd) {
25 free(safename);
26 return false;
27 }
28
29 string safeName = safename;
30 free(safename);
31
32 FILE *fp = fdopen(fd, "w");
33 if (!fp) {
34 unlink(safeName.c_str());
35 return false;
36 }
37
38 if (uidvalidity == 0 || uidnext == 0) {
39 uidvalidity = time(0);
40 uidnext = messages.size() + 1;
41 }
42
43 fprintf(fp, "%s %u %u\n", BINC_CACHE, uidvalidity, uidnext);
45 for (; i != end(); ++i) {
46 MaildirMessage &message = (MaildirMessage &)*i;
47 fprintf(fp, "%u %u %u %s", message.getUID(),
48 (unsigned int) message.getInternalDate(), message.getSize(),
49 message.getUnique().c_str());
50 vector<string> cflags = message.getCustomFlags();
51 for (vector<string>::const_iterator it = cflags.begin();
52 it != cflags.end(); ++it) {
53 fprintf(fp, " %s", (*it).c_str());
54 }
55 fprintf(fp, "\n");
56 }
57
58 if (fflush(fp) || (fsync(fd) && (errno != EROFS || errno != EINVAL)) || fclose(fp)) {
59 unlink(safeName.c_str());
60 return false;
61 }
62
63 if (rename(safeName.c_str(), (path + "/bincimap-cache").c_str()) != 0) {
64 unlink(safeName.c_str());
65 return false;
66 }
67
68 int dfd = open(path.c_str(), O_RDONLY);
69 if (dfd == -1 || (fsync(fd) && (errno != EROFS || errno != EINVAL)) || close(dfd)) {
70 return false;
71 }
72
73 return true;
74}
bool readOnly
Definition: mailbox.h:125
@ INCLUDE_EXPUNGED
Definition: mailbox.h:68
Mailbox::iterator end(void) const
Definition: maildir.cc:156
bool writeCache(void)
Mailbox::iterator begin(const SequenceSet &bset, unsigned int mod=INCLUDE_EXPUNGED|SQNR_MODE) const
Definition: maildir.cc:146
The MaildirMessage class provides an interface for IMAP messages.
std::vector< std::string > getCustomFlags(void) const
unsigned int getSize(bool determine=false) const
unsigned int getUID(void) const
const std::string & getUnique(void) const
time_t getInternalDate(void) const
static SequenceSet & all(void)
Definition: imapparser.cc:261
Global constants.
#define BINC_CACHE
Definition: globals.h:12
Declaration of the Maildir class.