aboutsummaryrefslogtreecommitdiffstats
path: root/src/mailsync.cpp
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-08-29 11:25:02 +0200
committerDanilo M. <danix@danix.xyz>2026-08-29 11:25:02 +0200
commit3fd999907ae8344f76ee4e5be1ac278a26f452ca (patch)
tree17c01d44daee0147ecaca6cccd393471e6379cf1 /src/mailsync.cpp
parent8c78dd139a77e896c72b7fc8b799af3c0df34344 (diff)
downloadqtmaildir-3fd999907ae8344f76ee4e5be1ac278a26f452ca.tar.gz
qtmaildir-3fd999907ae8344f76ee4e5be1ac278a26f452ca.zip
feat: have the sync script report what it did
Item 174, and half of item 125. The premise was corrected before any code. The note asks for an external `notmuch new` to clear the pending count; it must not. That count means tag mutations not yet known to have reached the MAIL STORE, which is the server: an edit is in notmuch the moment it is made, and what is outstanding is mbsync pushing the renamed Maildir files. `notmuch new` re-indexes local files and pushes nothing, so clearing on it would tell the user their work was safe to quit on while it was still local. The entry's own proposal to watch notmuch_database_get_revision() was rejected for the same reason: a revision moves when mail ARRIVES too, and in neither case does it say anything about the server. What was actually wrong was the reporting channel. The application inferred a finished run from an inode in /proc/locks and from grepping the log for its RUN END banner, which made a human-readable line into wire format and could not say WHICH channels a run carried. The local sync path has always narrowed its clear to the accounts it carried; the external path could not, and cleared everything, so an edit to an account a run never touched was reported as delivered. So the script reports instead of leaving evidence to be inferred. It writes ~/.local/state/qtmaildir/syncstatus.json atomically at the end of every run, including a skip, naming the channels, both exit statuses and a state of ok, failed or skipped. MailSync::readStatus() reads it, MainWindow prefers it over the log banner and narrows the clear through Account::syncChannel(). A skipped run clears nothing, which is item 125's first half: the application can now see that a run happened and carried nothing. The log banner and lastRunOutcome() stay as the fallback for a missing file, which is what a first run after upgrading looks like. This is the user's own framing of the scope: the script was written for another system and adapted, and is now qtmaildir's only consumer, so it serves the application rather than the reverse. Two facts made it safe to act on: their crontab runs mailsync.sh and nothing else touches mail, and ~/bin/mailsync.sh is a symlink into this repo, so an edit is live on the next tick. Two bugs found while wiring it in, both recorded in the closed item. A test read the developer's real sync state, twice: a [sync] section naming only `log` leaves syncStatus() defaulting to the real file, so two tests asserting that a FAILED run leaves the count alone read the last real cron run, found ok, and cleared. Pinning only `status` has the mirror problem. noSyncTestReadsTheRealSyncState() is the guard, modelled on noTestCanSeeTheRealLockTable(). And Qt::ISODate carries no milliseconds. The status file is preferred only when it describes THIS run, compared against when the lock appeared, so a stale success cannot outrank a fresh failure; but the script writes date -Iseconds, and a round trip of "now" comes back 329 ms behind, measured. A fast sync's own file therefore parsed as stale and fell back to the log, with nothing failing to say so. One second of slack matches the precision the format carries. Design: docs/superpowers/specs/2026-08-29-sync-status-file-design.md Suite: 43 of 44, with undoMovesTheMessageBack failing as it does on master (item 136).
Diffstat (limited to 'src/mailsync.cpp')
-rw-r--r--src/mailsync.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/mailsync.cpp b/src/mailsync.cpp
index 10e5ef7..caf61ef 100644
--- a/src/mailsync.cpp
+++ b/src/mailsync.cpp
@@ -21,6 +21,9 @@
#include <QCoreApplication>
#include <QDir>
#include <QFile>
+#include <QJsonArray>
+#include <QJsonDocument>
+#include <QJsonObject>
#include <QRegularExpression>
namespace {
@@ -251,6 +254,93 @@ QString MailSync::defaultLogPath()
return QDir::homePath() + QStringLiteral("/.local/state/mailsync.log");
}
+QString MailSync::defaultStatusPath()
+{
+ // Hardcoded to match assets/mailsync.sh for the same reason defaultLogPath
+ // is: the script builds it from $HOME, and QStandardPaths would derive a
+ // path the script never writes.
+ //
+ // Under the application's own state directory rather than beside
+ // mailsync.log, and the split is deliberate: the log belongs to the script
+ // and a human reads it, while this file is the interface between the two
+ // programs.
+ return QDir::homePath()
+ + QStringLiteral("/.local/state/qtmaildir/syncstatus.json");
+}
+
+SyncStatus MailSync::readStatus(const QString &statusPath)
+{
+ // Every failure below returns this untouched, so Unknown is the default
+ // rather than something each branch has to remember to set.
+ SyncStatus status;
+
+ if (statusPath.isEmpty())
+ return status;
+
+ QFile file(statusPath);
+ if (!file.open(QIODevice::ReadOnly))
+ return status;
+
+ // The whole file: it holds one run and is a few hundred bytes. The cap is
+ // against a path that is not the file we think it is, since a reader on the
+ // UI thread must not swallow something enormous by mistake.
+ constexpr qint64 kMaxBytes = 64 * 1024;
+ if (file.size() > kMaxBytes)
+ return status;
+
+ QJsonParseError error{};
+ const QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error);
+ if (error.error != QJsonParseError::NoError || !doc.isObject())
+ return status;
+
+ const QJsonObject object = doc.object();
+
+ // Refused rather than guessed at, the rule the rules file already follows:
+ // a later version may mean something different by these same field names,
+ // and acting on it would be worse than observing nothing. The script writes
+ // 1 and both sides bump together.
+ if (object.value(QStringLiteral("version")).toInt() != 1)
+ return status;
+
+ const QString state = object.value(QStringLiteral("state")).toString();
+ if (state == QLatin1String("ok"))
+ status.state = SyncState::Ok;
+ else if (state == QLatin1String("failed"))
+ status.state = SyncState::Failed;
+ else if (state == QLatin1String("skipped"))
+ status.state = SyncState::Skipped;
+ else
+ return status; // An unrecognised state is not a fourth kind of run.
+
+ const QJsonArray channels =
+ object.value(QStringLiteral("channels")).toArray();
+ for (const QJsonValue &value : channels) {
+ const QString channel = value.toString();
+ if (channel.isEmpty())
+ continue;
+ // "-a" is the script's word for "every channel", not the name of one.
+ // Kept as a flag so a caller cannot match it against configured
+ // channels, find nothing, and clear nothing on the run that carried
+ // everything.
+ if (channel == QLatin1String("-a"))
+ status.everyChannel = true;
+ else
+ status.channels.append(channel);
+ }
+
+ status.mbsyncStatus =
+ object.value(QStringLiteral("mbsync_status")).toInt(-1);
+ status.notmuchStatus =
+ object.value(QStringLiteral("notmuch_status")).toInt(-1);
+
+ status.started = QDateTime::fromString(
+ object.value(QStringLiteral("started")).toString(), Qt::ISODate);
+ status.ended = QDateTime::fromString(
+ object.value(QStringLiteral("ended")).toString(), Qt::ISODate);
+
+ return status;
+}
+
SyncOutcome MailSync::lastRunOutcome(const QString &logPath)
{
if (logPath.isEmpty())