From e46347aa54e6d825271b22f7e76e44ec2eee274d Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Thu, 21 Oct 2021 19:15:48 +0300 Subject: [PATCH] lib/index/scanner: tidy up a bit Use strcmp instead of hand-optimized. Avoid tmp/ directories. --- lib/index/mu-scanner.cc | 35 +++++++++++++---------------------- lib/index/mu-scanner.hh | 8 ++++---- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/lib/index/mu-scanner.cc b/lib/index/mu-scanner.cc index 2108b096..fbc9317b 100644 --- a/lib/index/mu-scanner.cc +++ b/lib/index/mu-scanner.cc @@ -1,5 +1,5 @@ /* -** Copyright (C) 2020 Dirk-Jan C. Binnema +** Copyright (C) 2020-2021 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -58,43 +58,34 @@ struct Scanner::Private { }; static bool -is_special_dir(const struct dirent* dentry) +is_special_dir(const char* d_name) { - const auto d_name{dentry->d_name}; return d_name[0] == '\0' || (d_name[1] == '\0' && d_name[0] == '.') || (d_name[2] == '\0' && d_name[0] == '.' && d_name[1] == '.'); } -static bool -is_new_cur(const char* dirname) -{ - if (dirname[0] == 'c' && dirname[1] == 'u' && dirname[2] == 'r' && dirname[3] == '\0') - return true; - - if (dirname[0] == 'n' && dirname[1] == 'e' && dirname[2] == 'w' && dirname[3] == '\0') - return true; - - return false; -} - bool Scanner::Private::process_dentry(const std::string& path, struct dirent* dentry, bool is_maildir) { - if (is_special_dir(dentry)) + const auto d_name{dentry->d_name}; + + if (is_special_dir(d_name) || std::strcmp(d_name, "tmp") == 0) return true; // ignore. - const auto fullpath{path + "/" + dentry->d_name}; - struct stat statbuf; + const auto fullpath{path + "/" + d_name}; + struct stat statbuf { + }; if (::stat(fullpath.c_str(), &statbuf) != 0) { g_warning("failed to stat %s: %s", fullpath.c_str(), g_strerror(errno)); return false; } if (S_ISDIR(statbuf.st_mode)) { - const auto new_cur = is_new_cur(dentry->d_name); - const auto htype = new_cur ? Scanner::HandleType::EnterNewCur - : Scanner::HandleType::EnterDir; - const auto res = handler_(fullpath, &statbuf, htype); + const auto new_cur = + std::strcmp(d_name, "cur") == 0 || std::strcmp(d_name, "new") == 0; + const auto htype = new_cur ? Scanner::HandleType::EnterNewCur + : Scanner::HandleType::EnterDir; + const auto res = handler_(fullpath, &statbuf, htype); if (!res) return true; // skip diff --git a/lib/index/mu-scanner.hh b/lib/index/mu-scanner.hh index c03d94e1..0d2f1c1b 100644 --- a/lib/index/mu-scanner.hh +++ b/lib/index/mu-scanner.hh @@ -35,13 +35,13 @@ namespace Mu { /// Scans maildir (trees) recursively, and calls the Handler callback for /// directories & files. /// -/// It filters out (i.e., does call the handler for): +/// It filters out (i.e., does *not* call the handler for): /// - files starting with '.' /// - files that do not live in a cur / new leaf maildir -/// - directories '.' and '..' +/// - directories '.' and '..' and 'tmp' /// class Scanner { - public: + public: enum struct HandleType { File, EnterNewCur, /* cur/ or new/ */ @@ -90,7 +90,7 @@ class Scanner { */ bool is_running() const; - private: + private: struct Private; std::unique_ptr priv_; };