QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
9#ifndef SRC_UTIL_H_
10#define SRC_UTIL_H_
11
12#include "appsettings.h"
13#include <QProcessEnvironment>
14#include <QRegularExpression>
15#include <QString>
16#include <QStringList>
17
18#include <functional>
19#include <optional>
20
21class QFile;
22class QFileDevice;
23
25constexpr int MS_PER_SECOND = 1000;
26
40class Util {
41public:
54 static void applyPassProcessDefaults();
55
66 static auto gitCeiling(const QString &store) -> QString;
67
85 static auto repositoryPassClimbsTo(const QString &store) -> QString;
86
112 static auto gitStoreVariables(const QString &store, const QString &git = {},
113 std::optional<uint> asUser = std::nullopt)
114 -> QList<std::pair<QString, QString>>;
115
122 static void setGitOwnerForTests(std::optional<uint> user);
123
135 static auto mkpathForStore(const QString &path) -> bool;
136
143 static auto passUmask(const QString &value) -> int;
144
153 static auto findBinaryInPath(const QString &binary) -> QString;
167 static auto findBinaryInPath(const QString &binary,
168 const QStringList &searchPaths) -> QString;
173 static auto findPasswordStore() -> QString;
185 static auto expandTilde(const QString &path) -> QString;
197 static auto normalizeFolderPath(const QString &path) -> QString;
205 static auto configIsValid(const AppSettings &s) -> bool;
210 static auto endsWithGpg() -> const QRegularExpression &;
215 static auto protocolRegex() -> const QRegularExpression &;
234 static auto isLaunchableWebUrl(const QString &value) -> bool;
250 static auto linkifyUrls(const QString &text, bool *linked = nullptr)
251 -> QString;
256 static auto newLinesRegex() -> const QRegularExpression &;
266 static auto isValidKeyId(const QString &keyId) -> bool;
296 static auto regularFilesUnder(const QString &dir,
297 const QStringList &nameFilters,
298 QStringList *skipped = nullptr,
299 bool hiddenFiles = false) -> QStringList;
307 static auto directoriesUnder(const QString &dir) -> QStringList;
319 static auto isLinkedFolder(const QString &path) -> bool;
333 static auto isUnderLink(const QString &path, const QString &storeRoot,
334 bool includeSelf = true) -> bool;
346 static auto removeTree(const QString &dir) -> bool;
347
364 static auto replaceFile(const QString &from, const QString &to, bool replace)
365 -> bool;
366
378 static auto openRegularFile(const QString &path, QFile &file) -> bool;
379
387 static constexpr qint64 kMaxGpgIdBytes = 1024 * 1024;
388
397 static auto readRegularFile(const QString &path, qint64 maxBytes)
398 -> std::optional<QByteArray>;
399
405 using Filler = std::function<QString(QFileDevice &)>;
406
429 static auto stageFileReplacing(const QString &path, bool replace,
430 const Filler &fill, QString *error = nullptr)
431 -> bool;
432
441 static auto writeFileReplacing(const QString &path, const QByteArray &bytes,
442 bool replace, QString *error = nullptr)
443 -> bool;
444
457 static auto copyFileReplacing(const QString &src, const QString &dst,
458 bool replace, QString *error = nullptr) -> bool;
459
468 static auto syncToDisk(QFileDevice &file) -> bool;
469
470private:
476 static void initialiseEnvironment();
479 static QProcessEnvironment _env;
481 static bool _envInitialised;
482};
483
484#endif // SRC_UTIL_H_
AppSettings, the value object holding every flat QtPass setting.
Static helpers that have no better home: setting up the process and the Git environment the way pass ...
Definition util.h:40
static auto regularFilesUnder(const QString &dir, const QStringList &nameFilters, QStringList *skipped=nullptr, bool hiddenFiles=false) -> QStringList
Regular files under dir whose name matches one of nameFilters, found by walking only real directories...
Definition util.cpp:721
static auto passUmask(const QString &value) -> int
The umask pass would use for value of PASSWORD_STORE_UMASK.
Definition util.cpp:122
static auto protocolRegex() -> const QRegularExpression &
Returns a regex to match URL protocols.
Definition util.cpp:590
static auto repositoryPassClimbsTo(const QString &store) -> QString
The repository above store that pass itself would commit to, if any.
Definition util.cpp:146
static auto removeTree(const QString &dir) -> bool
Remove dir and everything in it, the way rm -rf does: a symbolic link or NTFS junction,...
Definition util.cpp:1239
static auto endsWithGpg() -> const QRegularExpression &
Returns a regex to match .gpg file extensions.
Definition util.cpp:582
static auto isUnderLink(const QString &path, const QString &storeRoot, bool includeSelf=true) -> bool
Whether the way from storeRoot down to path passes through a symbolic link or NTFS junction: path its...
Definition util.cpp:765
static void setGitOwnerForTests(std::optional< uint > user)
For tests: the user gitStoreVariables() compares owners with when no asUser is given,...
Definition util.cpp:284
static auto findPasswordStore() -> QString
Locate the password store directory.
Definition util.cpp:73
static auto configIsValid(const AppSettings &s) -> bool
Verify that the required configuration is complete.
Definition util.cpp:556
static auto writeFileReplacing(const QString &path, const QByteArray &bytes, bool replace, QString *error=nullptr) -> bool
Write bytes as the file path through stageFileReplacing().
Definition util.cpp:1165
static QProcessEnvironment _env
Definition util.h:479
static auto isLaunchableWebUrl(const QString &value) -> bool
Check whether a value is a safe, launchable web URL.
Definition util.cpp:596
static constexpr qint64 kMaxGpgIdBytes
Most bytes of a .gpg-id QtPass reads.
Definition util.h:387
static auto readRegularFile(const QString &path, qint64 maxBytes) -> std::optional< QByteArray >
Read path whole, as the regular file it is (openRegularFile()), when it holds at most maxBytes.
Definition util.cpp:973
static auto gitStoreVariables(const QString &store, const QString &git={}, std::optional< uint > asUser=std::nullopt) -> QList< std::pair< QString, QString > >
The variables that keep git in store's own repository, never one above it: GIT_CEILING_DIRECTORIES (g...
Definition util.cpp:376
static auto syncToDisk(QFileDevice &file) -> bool
Flush file's bytes to the device (fsync, FlushFileBuffers) before it is renamed into place,...
Definition util.cpp:992
static auto mkpathForStore(const QString &path) -> bool
Create path and any missing parents as pass would.
Definition util.cpp:438
static auto expandTilde(const QString &path) -> QString
Expand a leading current-user tilde in a path.
Definition util.cpp:88
static auto isLinkedFolder(const QString &path) -> bool
Whether path names a symbolic link or NTFS junction, with or without a trailing separator (which woul...
Definition util.cpp:761
static auto replaceFile(const QString &from, const QString &to, bool replace) -> bool
Give the file at from the name to, atomically and without following anything: the operating system's ...
Definition util.cpp:873
static void applyPassProcessDefaults()
Set up the process the way pass sets up its own, before anything runs git or gpg or writes into the s...
Definition util.cpp:106
static auto isValidKeyId(const QString &keyId) -> bool
Check whether a .gpg-id token may be passed to gpg as a key selector. Accepts any non-empty token tha...
Definition util.cpp:664
static auto copyFileReplacing(const QString &src, const QString &dst, bool replace, QString *error=nullptr) -> bool
Copy the regular file src to the name dst through stageFileReplacing(): src is read through a handle ...
Definition util.cpp:1179
std::function< QString(QFileDevice &)> Filler
Fills the temporary of stageFileReplacing() through its open handle; returns why it could not (transl...
Definition util.h:405
static auto newLinesRegex() -> const QRegularExpression &
Returns a regex to match newline characters.
Definition util.cpp:656
static auto openRegularFile(const QString &path, QFile &file) -> bool
Open path for reading as the regular file it is, not as what a link under that name points to: the ob...
Definition util.cpp:913
static auto normalizeFolderPath(const QString &path) -> QString
Ensure a folder path always ends with '/'.
Definition util.cpp:98
static auto linkifyUrls(const QString &text, bool *linked=nullptr) -> QString
Turn plain text into HTML with clickable links for safe web URLs.
Definition util.cpp:627
static auto stageFileReplacing(const QString &path, bool replace, const Filler &fill, QString *error=nullptr) -> bool
The staged write every file QtPass puts into a store goes through: a temporary created next to path (...
Definition util.cpp:1142
static void initialiseEnvironment()
Fill _env once: the process environment with the usual GPG install directories appended to PATH (MacG...
Definition util.cpp:46
static auto directoriesUnder(const QString &dir) -> QStringList
Every real, visible directory under dir, walked by the same rules as regularFilesUnder(): links,...
Definition util.cpp:749
static bool _envInitialised
Whether initialiseEnvironment() has run.
Definition util.h:481
static auto findBinaryInPath(const QString &binary) -> QString
Locate an executable by searching the process PATH and (on Windows) falling back to WSL.
Definition util.cpp:474
static auto gitCeiling(const QString &store) -> QString
GIT_CEILING_DIRECTORIES for a store, as pass sets it.
Definition util.cpp:135
Plain value-object holding all flat QtPass configuration.
Definition appsettings.h:36
constexpr int MS_PER_SECOND
Milliseconds in a second, for timers set from a setting in seconds.
Definition util.h:25