From 1978c49bea5b439d993067c055cec47e70db8fd6 Mon Sep 17 00:00:00 2001 From: "Jannis M. Hoffmann" Date: Sat, 7 Oct 2023 22:33:50 +0200 Subject: minor refactoring --- src/maildir-scan.cc | 110 +++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 57 deletions(-) (limited to 'src/maildir-scan.cc') diff --git a/src/maildir-scan.cc b/src/maildir-scan.cc index 2f3a9b4..65548f7 100644 --- a/src/maildir-scan.cc +++ b/src/maildir-scan.cc @@ -1,9 +1,10 @@ -/** -------------------------------------------------------------------- +/** * @file maildir-scan.cc * @brief Implementation of the Maildir class. * @author Andreas Aardal Hanssen * @date 2002-2005 - * ----------------------------------------------------------------- **/ + */ + #include "iodevice.h" #include "iofactory.h" #include "maildir.h" @@ -16,7 +17,8 @@ #include using namespace Binc; -using namespace ::std; +using std::endl; +using std::string; Lock::Lock(const string &path) { @@ -32,19 +34,18 @@ Lock::Lock(const string &path) struct stat mystat; bincWarning << "waiting for mailbox lock " << lock << "." << endl; if (lstat(lock.c_str(), &mystat) == 0) { - if ((time(0) - mystat.st_ctime) > 300) { - if (unlink(lock.c_str()) == 0) + if ((time(nullptr) - mystat.st_ctime) > 300) { + if (unlink(lock.c_str()) == 0) { continue; - else + } else { bincWarning << "failed to force mailbox lock: " << lock << ", " << string(strerror(errno)) << endl; + } } - } else { - if (errno != ENOENT) { - string err = "invalid lock " + lock + ": " + strerror(errno); - bincWarning << err << endl; - return; - } + } else if (errno != ENOENT) { + string err = "invalid lock " + lock + ": " + strerror(errno); + bincWarning << err << endl; + return; } // sleep one second. @@ -57,15 +58,14 @@ Lock::Lock(const string &path) Lock::~Lock() { // remove the lock - if (unlink(lock.c_str()) != 0) + if (unlink(lock.c_str()) != 0) { bincWarning << "failed to unlock mailbox: " << lock << ", " << strerror(errno) << endl; + } } -//------------------------------------------------------------------------ // scan the maildir. update flags, find messages in new/ and move them // to cur, setting the recent flag in memory only. check for expunged // messages. give newly arrived messages uids. -//------------------------------------------------------------------------ Maildir::ScanResult Maildir::scan(bool forceScan) { const string newpath = path + "/new/"; @@ -95,8 +95,8 @@ Maildir::ScanResult Maildir::scan(bool forceScan) old_bincimap_cache_st_mtime = oldstat.st_mtime; old_bincimap_cache_st_ctime = oldstat.st_ctime; } else { - old_bincimap_cache_st_mtime = 0; - old_bincimap_cache_st_ctime = 0; + old_bincimap_cache_st_mtime = {}; + old_bincimap_cache_st_ctime = {}; } } else { struct stat oldcurstat; @@ -147,10 +147,10 @@ Maildir::ScanResult Maildir::scan(bool forceScan) // An error with reading the cache files when it's not the first // time we scan the depot is treated as an error. if (!firstscan && !readOnly) { - old_cur_st_mtime = (time_t)0; - old_cur_st_ctime = (time_t)0; - old_new_st_mtime = (time_t)0; - old_new_st_ctime = (time_t)0; + old_cur_st_mtime = {}; + old_cur_st_ctime = {}; + old_new_st_mtime = {}; + old_new_st_ctime = {}; return TemporaryError; } mailboxchanged = true; @@ -161,7 +161,7 @@ Maildir::ScanResult Maildir::scan(bool forceScan) // open new/ directory DIR *pdir = opendir(newpath.c_str()); - if (pdir == 0) { + if (pdir == nullptr) { string reason = "failed to open \"" + newpath + "\" ("; reason += strerror(errno); reason += ")"; @@ -172,7 +172,7 @@ Maildir::ScanResult Maildir::scan(bool forceScan) // scan all entries struct dirent *pdirent; - while ((pdirent = readdir(pdir)) != 0) { + while ((pdirent = readdir(pdir)) != nullptr) { // "Unless you're writing messages to a maildir, the format of a // unique name is none of your business. A unique name can be // anything that doesn't contain a colon (or slash) and doesn't @@ -201,15 +201,16 @@ Maildir::ScanResult Maildir::scan(bool forceScan) // a rare race between readdir and stat force us to restart the scan. closedir(pdir); - if ((pdir = opendir(newpath.c_str())) == 0) { + if ((pdir = opendir(newpath.c_str())) == nullptr) { string reason = "Warning: opendir(\"" + newpath + "\") == 0 ("; reason += strerror(errno); reason += ")"; setLastError(reason); return PermanentError; } - } else + } else { bincWarning << "junk in Maildir: \"" << fullfilename << "\": " << strerror(errno); + } continue; } @@ -220,22 +221,19 @@ Maildir::ScanResult Maildir::scan(bool forceScan) // and st_mtime. the next time the mailbox is scanned, it must not // simply be skipped. :-) - vector::const_iterator newIt = newMessages.begin(); bool ours = false; - for (; newIt != newMessages.end(); ++newIt) { - if ((filename == (*newIt).getUnique()) - && ((*newIt).getInternalFlags() & MaildirMessage::Committed)) - { + for (const auto &newIt : newMessages) { + if ((filename == newIt.getUnique()) && (newIt.getInternalFlags() & MaildirMessage::Committed)) { ours = true; break; } } - if (!ours && ::time(0) <= mystat.st_mtime) { - old_cur_st_mtime = (time_t)0; - old_cur_st_ctime = (time_t)0; - old_new_st_mtime = (time_t)0; - old_new_st_ctime = (time_t)0; + if (!ours && ::time(nullptr) <= mystat.st_mtime) { + old_cur_st_mtime = {}; + old_cur_st_ctime = {}; + old_new_st_mtime = {}; + old_new_st_ctime = {}; continue; } @@ -261,7 +259,7 @@ Maildir::ScanResult Maildir::scan(bool forceScan) // Then, scan cur // open directory - if ((pdir = opendir(curpath.c_str())) == 0) { + if ((pdir = opendir(curpath.c_str())) == nullptr) { string reason = "Maildir::scan::opendir(\"" + curpath + "\") == 0 ("; reason += strerror(errno); reason += ")"; @@ -275,10 +273,10 @@ Maildir::ScanResult Maildir::scan(bool forceScan) index.clearFileNames(); // this is to sort recent messages by internaldate - multimap tempMessageMap; + std::multimap tempMessageMap; // scan all entries - while ((pdirent = readdir(pdir)) != 0) { + while ((pdirent = readdir(pdir)) != nullptr) { string filename = pdirent->d_name; if (filename[0] == '.') continue; @@ -289,12 +287,13 @@ Maildir::ScanResult Maildir::scan(bool forceScan) uniquename = filename.substr(0, pos); string tmp = filename.substr(pos); if ((pos = tmp.find("2,")) != string::npos) standard = tmp.substr(pos + 2); - } else + } else { uniquename = filename; + } unsigned char mflags = Message::F_NONE; - for (string::const_iterator i = standard.begin(); i != standard.end(); ++i) { - switch (*i) { + for (char i : standard) { + switch (i) { case 'R': mflags |= Message::F_ANSWERED; break; @@ -335,7 +334,7 @@ Maildir::ScanResult Maildir::scan(bool forceScan) closedir(pdir); - if ((pdir = opendir(newpath.c_str())) == 0) { + if ((pdir = opendir(newpath.c_str())) == nullptr) { string reason = "Warning: opendir(\"" + newpath + "\") == 0 ("; reason += strerror(errno); reason += ")"; @@ -354,7 +353,7 @@ Maildir::ScanResult Maildir::scan(bool forceScan) // If we have this message in memory already.. if (message) { - if (message->getInternalDate() == 0) { + if (message->getInternalDate() == time_t{}) { mailboxchanged = true; message->setInternalDate(mystat.st_mtime); } @@ -382,7 +381,7 @@ Maildir::ScanResult Maildir::scan(bool forceScan) m.setInternalDate(mystat.st_mtime); m.setStdFlag((mflags | Message::F_RECENT) & ~Message::F_EXPUNGED); m.setUnique(uniquename); - tempMessageMap.insert(make_pair((unsigned int)mystat.st_mtime, m)); + tempMessageMap.insert(std::make_pair((unsigned int)mystat.st_mtime, m)); mailboxchanged = true; } @@ -392,14 +391,11 @@ Maildir::ScanResult Maildir::scan(bool forceScan) // Recent messages are added, ordered by internaldate. { int readonlyuidnext = uidnext; - multimap::iterator i = tempMessageMap.begin(); - while (i != tempMessageMap.end()) { + + for (auto i = tempMessageMap.begin(); i != tempMessageMap.end(); ++i) { i->second.setUID(readOnly ? readonlyuidnext++ : uidnext++); - multimap::iterator itmp = i; - ++itmp; add(i->second); tempMessageMap.erase(i); - i = itmp; mailboxchanged = true; } } @@ -410,7 +406,7 @@ Maildir::ScanResult Maildir::scan(bool forceScan) // exist in the Maildir, are removed from the messages list. Mailbox::iterator jj = begin(SequenceSet::all(), INCLUDE_EXPUNGED | SQNR_MODE); while (jj != end()) { - MaildirMessage &message = (MaildirMessage &)*jj; + auto &message = dynamic_cast(*jj); if (message.isExpunged()) { mailboxchanged = true; @@ -434,12 +430,12 @@ Maildir::ScanResult Maildir::scan(bool forceScan) Mailbox::iterator ii = begin(SequenceSet::all(), INCLUDE_EXPUNGED | SQNR_MODE); for (; ii != end(); ++ii) { - MaildirMessage &message = (MaildirMessage &)*ii; + MaildirMessage &message = dynamic_cast(*ii); message.clearInternalFlag(MaildirMessage::JustArrived); - if (lastuid < message.getUID()) + if (lastuid < message.getUID()) { lastuid = message.getUID(); - else { + } else { bincWarning << "UID values are not strictly ascending in this" " mailbox: " << path << ". This is usually caused by " @@ -450,10 +446,10 @@ Maildir::ScanResult Maildir::scan(bool forceScan) if (!readOnly) { bumpUidValidity(path); - old_cur_st_mtime = (time_t)0; - old_cur_st_ctime = (time_t)0; - old_new_st_mtime = (time_t)0; - old_new_st_ctime = (time_t)0; + old_cur_st_mtime = {}; + old_cur_st_ctime = {}; + old_new_st_mtime = {}; + old_new_st_ctime = {}; return TemporaryError; } else { return PermanentError; -- cgit v1.2.3