Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief NativeGrep, the content search that decrypts every entry on a worker
6 : : * thread instead of running `pass grep`.
7 : : */
8 : : #ifndef SRC_NATIVEGREP_H_
9 : : #define SRC_NATIVEGREP_H_
10 : :
11 : : #include <QList>
12 : : #include <QObject>
13 : : #include <QPair>
14 : : #include <QProcessEnvironment>
15 : : #include <QString>
16 : : #include <QStringList>
17 : : #include <atomic>
18 : : #include <memory>
19 : :
20 : : class QRegularExpression;
21 : : class QThread;
22 : :
23 : : /**
24 : : * @class NativeGrep
25 : : * @ingroup backend
26 : : * @brief Full-text search over a password store without `pass grep`: every
27 : : * `.gpg` file is decrypted with gpg on a worker thread and its lines matched
28 : : * against a QRegularExpression. Only the newest search may report; a search
29 : : * started while another runs asks that one to stop and supersedes it.
30 : : */
31 : : class NativeGrep : public QObject {
32 : : Q_OBJECT
33 : :
34 : : public:
35 : : /**
36 : : * @brief Create a searcher.
37 : : * @param parent Owner; results are delivered on its thread.
38 : : */
39 : : explicit NativeGrep(QObject *parent = nullptr);
40 : :
41 : : /**
42 : : * @brief Most matching lines taken from one entry.
43 : : *
44 : : * A compressed entry can expand into millions of lines within the
45 : : * plaintext limit, each a result and a row in the results list; past this
46 : : * many, the entry gets one line saying there are more.
47 : : */
48 : : static constexpr int kMaxMatchesPerEntry = 100;
49 : :
50 : : /**
51 : : * @brief Most matching lines a search collects over the whole store.
52 : : *
53 : : * The search stops once it has this many; the window says so.
54 : : */
55 : : static constexpr int kMaxMatches = 10000;
56 : :
57 : : /**
58 : : * @brief Longest preview of a matching line, in UTF-16 code units.
59 : : *
60 : : * A result shows where the pattern matched, not the line; a single line
61 : : * can be most of an entry's 64 MiB plaintext, and 10000 such copies would
62 : : * not fit in memory. A longer line is shown as a window of this many code
63 : : * units around the match (up to two more, one per edge, to keep a
64 : : * surrogate pair whole), "…" where the line goes on. A sequence of
65 : : * several code points (a ZWJ emoji, a flag) can still be cut between
66 : : * them.
67 : : */
68 : : static constexpr qsizetype kMaxPreviewLength = 200;
69 : :
70 : : /**
71 : : * @brief Whether a line in a result is the marker for "more matches in
72 : : * this entry" rather than a match.
73 : : *
74 : : * A match is a trimmed, non-empty line, so an empty one can only be the
75 : : * marker, which matchFile() and scanStore() put last. Not a match: not
76 : : * counted, not content to hide.
77 : : * @param line A line of a result.
78 : : * @return Whether it is the marker.
79 : : */
80 : : [[nodiscard]] static auto isMoreMarker(const QString &line) -> bool {
81 : : return line.isEmpty();
82 : : }
83 : :
84 : : /**
85 : : * @brief Whether a line in a result stands for a match in a line holding
86 : : * a one-time password secret (FileContent::isOtpLine(), or
87 : : * FileContent::isOtpPasswordLine() for the first line), whose text is
88 : : * left out.
89 : : *
90 : : * A match never holds a line feed, so this marker (one line feed) cannot
91 : : * be mistaken for one. The display panel never shows such a line either.
92 : : * @param line A line of a result.
93 : : * @return Whether it is the marker.
94 : : */
95 : : [[nodiscard]] static auto isOtpMarker(const QString &line) -> bool {
96 [ + + ]: 43 : return line == QLatin1String("\n");
97 : : }
98 : :
99 : : /**
100 : : * @brief Whether a line in a result is a match in the entry's first line,
101 : : * the password: passwordMatchText() is its text, shown only where the
102 : : * panel would show the password.
103 : : *
104 : : * Marked by a leading line feed, which no match holds.
105 : : * @param line A line of a result.
106 : : * @return Whether it is a match in the password.
107 : : */
108 : : [[nodiscard]] static auto isPasswordMatch(const QString &line) -> bool {
109 [ + - + + ]: 41 : return line.size() > 1 && line.front() == QLatin1Char('\n');
110 : : }
111 : :
112 : : /**
113 : : * @brief The text of a match isPasswordMatch() accepts.
114 : : * @param line A line of a result.
115 : : * @return The matched text, without its mark.
116 : : */
117 : : [[nodiscard]] static auto passwordMatchText(const QString &line) -> QString {
118 : 25 : return line.mid(1);
119 : : }
120 : :
121 : : /**
122 : : * @brief Whether a result is the marker for "the search stopped at
123 : : * kMaxMatches, more entries were not searched".
124 : : *
125 : : * No entry has an empty name, so a result without one can only be the
126 : : * marker, which scanStore() puts last.
127 : : * @param result A result.
128 : : * @return Whether it is the marker.
129 : : */
130 : : [[nodiscard]] static auto
131 : : isStoppedMarker(const QPair<QString, QStringList> &result) -> bool {
132 : : return result.first.isEmpty();
133 : : }
134 : : /**
135 : : * @brief Interrupt running searches and wait up to five seconds for them.
136 : : */
137 : : ~NativeGrep() override;
138 : :
139 : : /**
140 : : * @brief Start a search; finished() follows asynchronously, also for an
141 : : * empty or invalid pattern (with no results).
142 : : * @param pattern PCRE pattern.
143 : : * @param caseInsensitive Match regardless of case.
144 : : * @param gpgExe The gpg executable (a `wsl ` prefix routes paths through
145 : : * wslpath).
146 : : * @param storeDir The store to walk.
147 : : * @param env Environment for gpg (GNUPGHOME and friends).
148 : : */
149 : : void search(const QString &pattern, bool caseInsensitive,
150 : : const QString &gpgExe, const QString &storeDir,
151 : : const QProcessEnvironment &env);
152 : :
153 : : /**
154 : : * @brief Ask running searches to stop without waiting for them: the walk
155 : : * ends at the next file and a gpg that is decrypting is terminated by its
156 : : * own worker thread.
157 : : */
158 : : void cancel();
159 : :
160 : : /**
161 : : * @brief Decrypt one .gpg file and return its trimmed lines matching @p rx.
162 : : * @param env Environment for gpg.
163 : : * @param gpgExe The gpg executable.
164 : : * @param filePath The encrypted file.
165 : : * @param rx The pattern.
166 : : * @param cancel When set, gpg is not started, or is terminated while it
167 : : * runs; nullptr to run to completion.
168 : : * @param storeDir The store @p filePath is in; when given, the entry is
169 : : * read with Util::readRegularFileUnder(), so no folder on the way
170 : : * down from it is followed as a link either. The file itself is
171 : : * always read without following a link and handed to gpg on stdin.
172 : : * @return Matching lines, at most kMaxMatchesPerEntry and then a marker
173 : : * (isMoreMarker()) when there are more; empty when gpg fails, is
174 : : * cancelled or nothing matches. A match in the first line is
175 : : * marked (isPasswordMatch()), one in a line holding a one-time
176 : : * password secret is only a marker (isOtpMarker()), and a cut
177 : : * `name: value` line keeps its whole name, or shows none of its
178 : : * value when the name is too long to keep.
179 : : */
180 : : static auto matchFile(const QProcessEnvironment &env, const QString &gpgExe,
181 : : const QString &filePath, const QRegularExpression &rx,
182 : : const std::atomic_bool *cancel = nullptr,
183 : : const QString &storeDir = QString()) -> QStringList;
184 : : /**
185 : : * @brief Walk @p storeDir, decrypt every .gpg file and collect the matches.
186 : : * @param env Environment for gpg.
187 : : * @param gpgExe The gpg executable.
188 : : * @param storeDir The store.
189 : : * @param rx The pattern.
190 : : * @param cancel Stops the walk and the running gpg when set; nullptr to
191 : : * rely on QThread::isInterruptionRequested() alone.
192 : : * @return (entry name without .gpg, matching lines) per file with matches,
193 : : * at most kMaxMatches lines in all, an entry cut short ending in a
194 : : * marker (isMoreMarker()); when the walk stopped at that budget
195 : : * with entries left, a last result that is a marker
196 : : * (isStoppedMarker()). Empty when the search is stopped.
197 : : */
198 : : static auto scanStore(const QProcessEnvironment &env, const QString &gpgExe,
199 : : const QString &storeDir, const QRegularExpression &rx,
200 : : const std::atomic_bool *cancel = nullptr)
201 : : -> QList<QPair<QString, QStringList>>;
202 : :
203 : : signals:
204 : : /**
205 : : * @brief The newest search is over.
206 : : * @param results (entry, matching lines) pairs.
207 : : */
208 : : void finished(const QList<QPair<QString, QStringList>> &results);
209 : :
210 : : private:
211 : : /// A running search: its thread and the flag its gpg runs poll. The flag
212 : : /// is shared with the thread's lambda so it outlives this object if a
213 : : /// search is still winding down when it is destroyed.
214 : 59 : struct Worker {
215 : : /** @brief The thread the search runs on. */
216 : : QThread *thread;
217 : : /** @brief Set to ask the search to stop; polled by its gpg runs. */
218 : : std::shared_ptr<std::atomic_bool> cancel;
219 : : };
220 : : /**
221 : : * @brief Number of the newest search. A worker reports through finished()
222 : : * only when its own number still equals this.
223 : : */
224 : : int m_seq = 0;
225 : : /** @brief Searches still running, superseded ones included. */
226 : : QList<Worker> m_workers;
227 : : };
228 : :
229 : : #endif // SRC_NATIVEGREP_H_
|