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>>;
133 static void requireExplicitBareRepository(QProcessEnvironment &env);
134
141 static void setGitOwnerForTests(std::optional<uint> user);
142
149 static void
150 setBeforePlacingForTests(std::function<void(const QString &)> hook);
151
163 static auto mkpathForStore(const QString &path) -> bool;
164
171 static auto passUmask(const QString &value) -> int;
172
181 static auto findBinaryInPath(const QString &binary) -> QString;
195 static auto findBinaryInPath(const QString &binary,
196 const QStringList &searchPaths) -> QString;
201 static auto findPasswordStore() -> QString;
213 static auto expandTilde(const QString &path) -> QString;
228 static auto joinPath(const QString &dir, const QString &name) -> QString;
236 static auto isAbsoluteFsPath(const QString &path) -> bool;
248 static auto normalizeFolderPath(const QString &path) -> QString;
256 static auto configIsValid(const AppSettings &s) -> bool;
261 static auto endsWithGpg() -> const QRegularExpression &;
266 static auto protocolRegex() -> const QRegularExpression &;
287 static auto isLaunchableWebUrl(const QString &value) -> bool;
303 static auto linkifyUrls(const QString &text, bool *linked = nullptr)
304 -> QString;
306 static constexpr qsizetype kMaxSpaceRun = 1000;
319 [[nodiscard]] static auto boundSpaceRuns(const QString &text) -> QString;
321 static constexpr qsizetype kMaxShownName = 120;
338 [[nodiscard]] static auto shownName(const QString &name,
339 qsizetype max = kMaxShownName) -> QString;
352 [[nodiscard]] static auto plainToolTip(const QString &text) -> QString;
354 static constexpr qsizetype kMaxUnbrokenRun = 10000;
367 [[nodiscard]] static auto hasLongUnbrokenRun(QStringView text) -> bool;
378 [[nodiscard]] static auto isOtherBlockBreak(QChar c) -> bool;
386 [[nodiscard]] static auto withoutOtherBlockBreaks(QString text) -> QString;
404 [[nodiscard]] static auto displayable(const QString &text,
405 bool lineBreaks = true) -> QString;
410 static auto newLinesRegex() -> const QRegularExpression &;
420 static auto isValidKeyId(const QString &keyId) -> bool;
454 static auto regularFilesUnder(const QString &dir,
455 const QStringList &nameFilters,
456 QStringList *skipped = nullptr,
457 bool hiddenFiles = false,
458 QStringList *unlisted = nullptr) -> QStringList;
466 static auto directoriesUnder(const QString &dir) -> QStringList;
478 static auto isLinkedFolder(const QString &path) -> bool;
489 static auto hasUnreadableName(const QString &dir) -> bool;
503 static auto isUnderLink(const QString &path, const QString &storeRoot,
504 bool includeSelf = true) -> bool;
516 static auto removeTree(const QString &dir) -> bool;
517
534 static auto replaceFile(const QString &from, const QString &to, bool replace)
535 -> bool;
536
552 static auto openRegularFile(const QString &path, QFile &file) -> bool;
553
561 static constexpr qint64 kMaxGpgIdBytes = qint64{1024} * 1024;
562
571 static auto readRegularFile(const QString &path, qint64 maxBytes)
572 -> std::optional<QByteArray>;
573
597 static auto readRegularFileUnder(const QString &root, const QString &path,
598 qint64 maxBytes)
599 -> std::optional<QByteArray>;
600
611 static auto readRegularFileInStore(const QString &store, const QString &path,
612 qint64 maxBytes)
613 -> std::optional<QByteArray>;
614
620 using Filler = std::function<QString(QFileDevice &)>;
621
647 static auto stageFileReplacing(const QString &path, bool replace,
648 const Filler &fill, QString *error = nullptr)
649 -> bool;
650
659 static auto writeFileReplacing(const QString &path, const QByteArray &bytes,
660 bool replace, QString *error = nullptr)
661 -> bool;
662
675 static auto copyFileReplacing(const QString &src, const QString &dst,
676 bool replace, QString *error = nullptr) -> bool;
677
686 static auto syncToDisk(QFileDevice &file) -> bool;
687
688private:
694 static void initialiseEnvironment();
697 static QProcessEnvironment _env;
699 static bool _envInitialised;
700};
701
702#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 passUmask(const QString &value) -> int
The umask pass would use for value of PASSWORD_STORE_UMASK.
Definition util.cpp:142
static auto protocolRegex() -> const QRegularExpression &
Returns a regex to match URL protocols.
Definition util.cpp:767
static auto repositoryPassClimbsTo(const QString &store) -> QString
The repository above store that pass itself would commit to, if any.
Definition util.cpp:342
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:1985
static auto endsWithGpg() -> const QRegularExpression &
Returns a regex to match .gpg file extensions.
Definition util.cpp:759
static auto hasLongUnbrokenRun(QStringView text) -> bool
Whether text holds more than kMaxUnbrokenRun characters in a row without white space Qt wraps at (a n...
Definition util.cpp:976
static auto isAbsoluteFsPath(const QString &path) -> bool
Whether path is absolute to the file system: as QDir::isAbsolutePath(), except that a name starting w...
Definition util.cpp:106
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:1217
static auto withoutOtherBlockBreaks(QString text) -> QString
text with every block break other than a line feed (isOtherBlockBreak()) shown as a space,...
Definition util.cpp:967
static constexpr qsizetype kMaxShownName
Longest name shownName() leaves whole.
Definition util.h:321
static auto hasUnreadableName(const QString &dir) -> bool
Whether dir holds an entry whose name is not valid UTF-8.
Definition util.cpp:1192
static void setGitOwnerForTests(std::optional< uint > user)
For tests: the user gitStoreVariables() compares owners with when no asUser is given,...
Definition util.cpp:386
static auto findPasswordStore() -> QString
Locate the password store directory.
Definition util.cpp:81
static auto configIsValid(const AppSettings &s) -> bool
Verify that the required configuration is complete.
Definition util.cpp:733
static auto boundSpaceRuns(const QString &text) -> QString
text with every run of white space other than tabs and line feeds cut to kMaxSpaceRun characters,...
Definition util.cpp:1029
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:1911
static auto regularFilesUnder(const QString &dir, const QStringList &nameFilters, QStringList *skipped=nullptr, bool hiddenFiles=false, QStringList *unlisted=nullptr) -> QStringList
Regular files under dir whose name matches one of nameFilters, found by walking only real directories...
Definition util.cpp:1149
static QProcessEnvironment _env
Definition util.h:697
static void requireExplicitBareRepository(QProcessEnvironment &env)
Make git in env refuse a folder that is merely laid out as a bare repository (safe....
Definition util.cpp:511
static void setBeforePlacingForTests(std::function< void(const QString &)> hook)
For tests: called by stageFileReplacing() with the temporary's path after it is filled and before it ...
Definition util.cpp:393
static auto isLaunchableWebUrl(const QString &value) -> bool
Check whether a value is a safe, launchable web URL.
Definition util.cpp:843
static constexpr qsizetype kMaxUnbrokenRun
Longest run without white space hasLongUnbrokenRun() allows.
Definition util.h:354
static constexpr qint64 kMaxGpgIdBytes
Most bytes of a .gpg-id QtPass reads.
Definition util.h:561
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:1555
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:553
static auto syncToDisk(QFileDevice &file) -> bool
Flush file's bytes to the device (fsync, FlushFileBuffers) before it is renamed into place,...
Definition util.cpp:1722
static auto mkpathForStore(const QString &path) -> bool
Create path and any missing parents as pass would.
Definition util.cpp:615
static auto expandTilde(const QString &path) -> QString
Expand a leading current-user tilde in a path.
Definition util.cpp:96
static auto displayable(const QString &text, bool lineBreaks=true) -> QString
text with the characters that change how the text around them reads replaced by U+FFFD,...
Definition util.cpp:898
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:1213
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:1333
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:126
static auto readRegularFileUnder(const QString &root, const QString &path, qint64 maxBytes) -> std::optional< QByteArray >
readRegularFile() for a file under root, with no link followed anywhere on the way down from it.
Definition util.cpp:1708
static auto joinPath(const QString &dir, const QString &name) -> QString
name in dir, as the file system means it.
Definition util.cpp:110
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:1063
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:1925
static auto isOtherBlockBreak(QChar c) -> bool
Whether Qt's text document starts a new block at c, besides at a line feed: a carriage return,...
Definition util.cpp:962
static auto plainToolTip(const QString &text) -> QString
text as a tooltip that shows it as written.
Definition util.cpp:992
std::function< QString(QFileDevice &)> Filler
Fills the temporary of stageFileReplacing() through its open handle; returns why it could not (transl...
Definition util.h:620
static auto newLinesRegex() -> const QRegularExpression &
Returns a regex to match newline characters.
Definition util.cpp:1055
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:1529
static auto normalizeFolderPath(const QString &path) -> QString
Ensure a folder path always ends with '/'.
Definition util.cpp:118
static constexpr qsizetype kMaxSpaceRun
Longest run of white space boundSpaceRuns() leaves.
Definition util.h:306
static auto shownName(const QString &name, qsizetype max=kMaxShownName) -> QString
A name or path from the store, as a label or list shows it: one line, at most max characters.
Definition util.cpp:1000
static auto readRegularFileInStore(const QString &store, const QString &path, qint64 maxBytes) -> std::optional< QByteArray >
readRegularFileUnder() for a file of the store at store, spelled under the store as configured or und...
Definition util.cpp:1564
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:914
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:1885
static void initialiseEnvironment()
Fill _env once: the process environment with the usual GPG install directories appended to PATH (MacG...
Definition util.cpp:54
static auto directoriesUnder(const QString &dir) -> QStringList
Every real, visible directory under dir, walked by the same rules as regularFilesUnder(): links,...
Definition util.cpp:1180
static bool _envInitialised
Whether initialiseEnvironment() has run.
Definition util.h:699
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:651
static auto gitCeiling(const QString &store) -> QString
GIT_CEILING_DIRECTORIES for a store, as pass sets it.
Definition util.cpp:155
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