summaryrefslogtreecommitdiff
path: root/lib/mu-scanner.hh
blob: e124c5266d93d3a592242344e74e2cb93f1247d1 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
** Copyright (C) 2020-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/

#ifndef MU_SCANNER_HH__
#define MU_SCANNER_HH__

#include <functional>
#include <memory>
#include <utils/mu-result.hh>

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

namespace Mu {

/**
 * @brief Maildir scanner
 *
 * Scans maildir (trees) recursively, and calls the Handler callback for
 * directories & files.
 *
 * 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 '..' and 'tmp'
*/
class Scanner {
	public:
	enum struct HandleType {
		/*
		 * Mode: All
		 */
		File,
		EnterNewCur, /* cur/ or new/ */
		EnterDir,    /* some other directory */
		LeaveDir,
		/*
		 * Mode: Maildir
		 */
		Maildir,
	};

	/**
	 * Callback handler function
	 *
	 * path: full file-system path
	 * statbuf: stat result or nullptr (for Mode::MaildirsOnly)
	 * htype: HandleType. For Mode::MaildirsOnly only Maildir
	 */
	using Handler = std::function<
	    bool(const std::string& path, struct stat* statbuf, HandleType htype)>;

	/**
	 * Running mode for this Scanner
	 */
	enum struct Mode {
		All,		/**< Vanilla */
		MaildirsOnly	/**< Only return maildir to handler */
	};

	/**
	 * Construct a scanner object for scanning a directory, recursively.
	 *
	 * If handler is a directory
	 *
	 * @param root_dir root dir to start scanning
	 * @param handler handler function for some direntry
	 * @param options options to influence behavior
	 */
	Scanner(const std::string& root_dir, Handler handler, Mode mode = Mode::All);

	/**
	 * DTOR
	 */
	~Scanner();

	/**#
	 * Start the scan; this is a blocking call than runs until
	 * finished or (from another thread) stop() is called.
	 *
	 * @return Ok if starting worked; an Error otherwise
	 */
	Result<void> start();

	/**
	 * Request stopping the scan if it's running; otherwise do nothing
	 */
	void stop();

	/**
	 * Is a scan currently running?
	 *
	 * @return true or false
	 */
	bool is_running() const;

private:
	struct Private;
	std::unique_ptr<Private> priv_;
};

} // namespace Mu

#endif /* MU_SCANNER_HH__ */