14#include <QJsonDocument>
25constexpr int kRecordFormat = 1;
28constexpr int kMaxSeeded = 200000;
29constexpr int kLockTimeoutMs = 2000;
43auto load() -> std::optional<Record> {
45 if (!QFileInfo::exists(path)) {
49 if (!file.open(QIODevice::ReadOnly)) {
52 QJsonParseError parse{};
53 const QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &parse);
54 if (parse.error != QJsonParseError::NoError || !doc.isObject() ||
55 doc.object().value(QLatin1String(
"format")).toInt() != kRecordFormat) {
58 return Record{doc.object().value(QLatin1String(
"entries")).toObject(),
59 doc.object().value(QLatin1String(
"stores")).toObject()};
67auto save(
const Record &record) ->
bool {
69 root.insert(QLatin1String(
"format"), kRecordFormat);
70 root.insert(QLatin1String(
"entries"), record.entries);
71 root.insert(QLatin1String(
"stores"), record.stores);
73 if (!file.open(QIODevice::WriteOnly)) {
76 file.setPermissions(QFile::ReadOwner | QFile::WriteOwner);
77 const QByteArray bytes = QJsonDocument(root).toJson(QJsonDocument::Compact);
78 return file.write(bytes) == bytes.size() && file.commit();
90void collectEntries(
const QString &dir,
int limit, QStringList &found) {
91 const QFileInfoList children = QDir(dir).entryInfoList(
92 QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot);
93 for (
const QFileInfo &child : children) {
94 if (found.size() >= limit) {
98 if (child.fileName().startsWith(QLatin1Char(
'.')) || child.isSymLink() ||
103 collectEntries(child.absoluteFilePath(), limit, found);
104 }
else if (child.fileName().endsWith(QLatin1String(
".gpg"),
105 Qt::CaseInsensitive)) {
106 found << child.absoluteFilePath();
118auto entriesOf(
const QString &path,
int limit) -> QStringList {
119 const QFileInfo info(path);
120 if (info.isSymLink()) {
124 return {info.absoluteFilePath()};
127 collectEntries(path, limit, found);
135template <
typename Change>
void update(Change change) {
137 QDir().mkpath(QFileInfo(path).absolutePath());
138 QLockFile lock(path + QStringLiteral(
".lock"));
139 if (!lock.tryLock(kLockTimeoutMs)) {
140 qCWarning(lcQtPass) <<
"Entry record busy, not updated:" << path;
143 std::optional<Record> record = load();
145 qCWarning(lcQtPass) <<
"Entry record unreadable, not updated:" << path;
148 if (change(*record) && !
save(*record)) {
149 qCWarning(lcQtPass) <<
"Entry record not written:" << path;
157 QStringLiteral(
"/QtPass-entries.json");
162 QLockFile lock(path + QStringLiteral(
".lock"));
163 if (!lock.tryLock(kLockTimeoutMs)) {
166 const std::optional<Record> record = load();
172 for (
const QString &path : paths) {
173 entries << entriesOf(path, kMaxSeeded);
175 if (entries.isEmpty()) {
178 update([&entries](Record &record) {
179 for (
const QString &entry : std::as_const(entries)) {
187 if (storeRoot.isEmpty() || !QFileInfo(storeRoot).isDir()) {
197 static QSet<QString> tried;
198 const QMutexLocker locker(&mutex);
199 if (tried.contains(store)) {
204 update([&storeRoot, &store](Record &record) {
205 if (record.stores.contains(store)) {
210 const QString root = QFileInfo(storeRoot).canonicalFilePath();
211 for (
const QString &entry : entriesOf(root, kMaxSeeded)) {
214 record.stores.insert(store, 1);
static void seedStore(const QString &storeRoot)
Record every entry of a store the first time it is seen; later calls for the same store do nothing....
static auto recordFile() -> QString
Where the record is kept.
static void remember(const QStringList &paths)
Record entries this device has just written.
static auto known(const QString &entryFile) -> bool
Whether this device saved entryFile, or found it there when the store was seeded.
static auto key(const QString &gpgIdFile) -> QString
The settings key for a .gpg-id: a hash of its canonical path, so path spelling (case,...
static auto recordFile() -> QString
The file the accepted generations are recorded in, so a refusal can name it: removing it forgets what...
EntryRecord: the entry paths this device has saved, kept outside the store.
Rollback detection for signed .gpg-id files: the generation counter and the record of what was last s...
void save(const QWidget &window, const QString &key)
Save window's geometry under key.
The "qtpass" logging category.