[Server+Client] Tombstone deck deletes with purge and base snapshots

This commit is contained in:
Lukas Brübach 2026-10-11 16:05:22 +02:00
parent bdb9de0e6e
commit 91b8d771f9
8 changed files with 182 additions and 23 deletions

View file

@ -11,6 +11,7 @@
#include <QUuid>
#include <libcockatrice/deck_list/deck_list.h>
#include <libcockatrice/network/client/abstract/abstract_client.h>
#include <libcockatrice/protocol/pb/command_deck_del.pb.h>
#include <libcockatrice/protocol/pb/command_deck_download.pb.h>
#include <libcockatrice/protocol/pb/command_deck_list.pb.h>
#include <libcockatrice/protocol/pb/command_deck_new_dir.pb.h>
@ -89,6 +90,7 @@ void DeckSyncManager::syncNow()
skippedCount = 0;
pendingUploads.clear();
pendingDownloads.clear();
pendingDeletes.clear();
ensuredDirs.clear();
ensuredDirs.insert(QString());
localFiles.clear();
@ -209,13 +211,26 @@ void DeckSyncManager::onListFinished(const Response &response, const CommandCont
}
auto serverIt = serverFiles.find(serverPath);
if (serverIt == serverFiles.end()) {
const DeckSyncRecord known = sidecar.get(it.key());
if (known.remoteId != -1) {
// The server no longer lists this deck: it was deleted elsewhere.
// Apply the delete locally, keeping a backup of the file.
const QString absolutePath = deckPath + "/" + it.key();
const QString backupPath =
absolutePath + BACKUP_SUFFIX + QString::number(QDateTime::currentDateTime().toSecsSinceEpoch());
if (!QFile::copy(absolutePath, backupPath) || !QFile::remove(absolutePath)) {
++skippedCount;
continue;
}
sidecar.remove(it.key());
++downloadedCount;
continue;
}
UploadOp op;
op.relativePath = it.key();
op.serverPath = serverPath;
op.deckId = -1;
op.guid = sidecar.contains(it.key()) && !sidecar.get(it.key()).guid.isEmpty()
? sidecar.get(it.key()).guid
: QUuid::createUuid().toString(QUuid::WithoutBraces);
op.guid = !known.guid.isEmpty() ? known.guid : QUuid::createUuid().toString(QUuid::WithoutBraces);
pendingUploads.append(op);
continue;
}
@ -230,6 +245,7 @@ void DeckSyncManager::onListFinished(const Response &response, const CommandCont
record.lastSyncSecs = nowSecs;
record.deleted = false;
sidecar.set(it.key(), record);
snapshotLocalFile(it.key(), record.guid);
++unchangedCount;
continue;
}
@ -276,6 +292,18 @@ void DeckSyncManager::onListFinished(const Response &response, const CommandCont
pendingDownloads.append(op);
}
const QMap<QString, DeckSyncRecord> knownRecords = sidecar.records();
for (auto it = knownRecords.begin(); it != knownRecords.end(); ++it) {
if (localFiles.contains(it.key())) {
continue;
}
if (it.value().remoteId == -1) {
sidecar.remove(it.key());
continue;
}
pendingDeletes.append(it.key());
}
sidecar.save();
processNext();
}
@ -291,7 +319,12 @@ void DeckSyncManager::processNext()
if (!syncing) {
return;
}
if (!pendingUploads.isEmpty()) {
if (!pendingDeletes.isEmpty()) {
const QString relativePath = pendingDeletes.first();
DeckSyncSidecar sidecar(sidecarPath);
sidecar.load();
sendDelete(relativePath, sidecar.get(relativePath).remoteId);
} else if (!pendingUploads.isEmpty()) {
processNextUpload();
} else if (!pendingDownloads.isEmpty()) {
sendDownload(pendingDownloads.first());
@ -386,7 +419,12 @@ void DeckSyncManager::sendUpload(const UploadOp &op)
cmd.set_color_identity(colorIdentityProvider(loaded->deckList).toStdString());
}
PendingCommand *pend = AbstractClient::prepareSessionCommand(cmd);
pend->setExtraData(op.relativePath);
QVariantMap context;
context.insert("path", op.relativePath);
context.insert("serverPath", op.serverPath);
context.insert("deckId", op.deckId);
context.insert("guid", op.guid);
pend->setExtraData(context);
connect(pend, &PendingCommand::finished, this, &DeckSyncManager::onUploadFinished);
client->sendCommand(pend);
}
@ -398,8 +436,22 @@ void DeckSyncManager::onUploadFinished(const Response &response,
if (!syncing) {
return;
}
const QString relativePath = extraData.toString();
const QVariantMap context = extraData.toMap();
const QString relativePath = context.value("path").toString();
const int deckId = context.value("deckId", -1).toInt();
if (response.response_code() != Response::RespOk) {
if (response.response_code() == Response::RespNameNotFound && deckId != -1) {
// The server row is gone (purged tombstone or removed elsewhere):
// upload the deck as new instead of updating a dead id.
UploadOp retry;
retry.relativePath = relativePath;
retry.serverPath = context.value("serverPath").toString();
retry.deckId = -1;
retry.guid = context.value("guid").toString();
pendingUploads.prepend(retry);
processNext();
return;
}
++skippedCount;
processNext();
return;
@ -430,6 +482,7 @@ void DeckSyncManager::onUploadFinished(const Response &response,
record.deleted = false;
sidecar.set(relativePath, record);
sidecar.save();
snapshotLocalFile(relativePath, record.guid);
++uploadedCount;
processNext();
}
@ -530,10 +583,66 @@ void DeckSyncManager::onDownloadFinished(const Response &response,
record.deleted = false;
sidecar.set(relativePath, record);
sidecar.save();
snapshotLocalFile(relativePath, record.guid);
++downloadedCount;
processNext();
}
void DeckSyncManager::sendDelete(const QString &relativePath, int deckId)
{
emit statusMessage(tr("Deleting %1 from server...").arg(relativePath));
Command_DeckDel cmd;
cmd.set_deck_id(deckId);
PendingCommand *pend = AbstractClient::prepareSessionCommand(cmd);
pend->setExtraData(relativePath);
connect(pend, &PendingCommand::finished, this, &DeckSyncManager::onDeleteFinished);
client->sendCommand(pend);
}
void DeckSyncManager::onDeleteFinished(const Response &response,
const CommandContainer & /*commandContainer*/,
const QVariant &extraData)
{
if (!syncing) {
return;
}
const QString relativePath = extraData.toString();
for (int i = 0; i < pendingDeletes.size(); ++i) {
if (pendingDeletes.at(i) == relativePath) {
pendingDeletes.removeAt(i);
break;
}
}
// Both outcomes converge the record: pushed deletes and server-side
// deletes (already tombstoned, answering NameNotFound) are done.
if (response.response_code() == Response::RespOk || response.response_code() == Response::RespNameNotFound) {
DeckSyncSidecar sidecar(sidecarPath);
sidecar.load();
sidecar.remove(relativePath);
sidecar.save();
} else {
++skippedCount;
}
processNext();
}
void DeckSyncManager::snapshotLocalFile(const QString &relativePath, const QString &guid)
{
if (guid.isEmpty()) {
return;
}
QFile source(deckPath + "/" + relativePath);
if (!source.open(QIODevice::ReadOnly)) {
return;
}
QDir().mkpath(deckPath + "/" + SYNC_META_DIR + "/bases");
QFile snapshot(deckPath + "/" + SYNC_META_DIR + "/bases/" + guid + ".cod");
if (!snapshot.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
return;
}
snapshot.write(source.readAll());
}
void DeckSyncManager::failSync(const QString &error)
{
syncing = false;