Improve ref counting when deduplicating attachments on disk

Co-authored-by: trevor-signal <131492920+trevor-signal@users.noreply.github.com>
This commit is contained in:
automated-signal
2026-02-13 08:22:45 -06:00
committed by GitHub
parent 6a5182eaa8
commit 95fd8bccb4
11 changed files with 374 additions and 53 deletions

View File

@@ -2932,12 +2932,19 @@ function getAndProtectExistingAttachmentPath(
plaintextHash,
version,
contentType,
}: { plaintextHash: string; version: number; contentType: string }
messageId,
}: {
plaintextHash: string;
version: number;
contentType: string;
messageId: string;
}
): ExistingAttachmentData | undefined {
if (!isValidPlaintextHash(plaintextHash)) {
logger.error('getAndProtectExistingAttachmentPath: Invalid plaintextHash');
return;
}
if (version < 2) {
logger.error(
'getAndProtectExistingAttachmentPath: Invalid version',
@@ -2985,8 +2992,8 @@ function getAndProtectExistingAttachmentPath(
(${existingData.thumbnailPath}),
(${existingData.screenshotPath})
)
INSERT OR REPLACE INTO attachments_protected_from_deletion(path)
SELECT path
INSERT OR REPLACE INTO attachments_protected_from_deletion(path, messageId)
SELECT path, ${messageId}
FROM existingMessageAttachmentPaths
WHERE path IS NOT NULL;
`;
@@ -2997,11 +3004,13 @@ function getAndProtectExistingAttachmentPath(
function _protectAttachmentPathFromDeletion(
db: WritableDB,
path: string
{ path, messageId }: { path: string; messageId: string }
): void {
const [protectQuery, protectParams] = sql`
INSERT OR REPLACE INTO attachments_protected_from_deletion(path)
VALUES (${path});
INSERT OR REPLACE INTO attachments_protected_from_deletion
(path, messageId)
VALUES
(${path}, ${messageId});
`;
db.prepare(protectQuery).run(protectParams);
}