From 8ea7e61a077924bf9519fe2305fd87b89ccebf8a Mon Sep 17 00:00:00 2001 From: Belugary <53219544+Belugary@users.noreply.github.com> Date: Tue, 12 May 2026 21:02:57 +0800 Subject: [PATCH] fix: clean up -shm/-wal residuals left by sqlite3 verification (#87) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The post-decrypt verification step (sqlite3.connect(out_path) + table list, around line 163) opens the freshly-written .db in default journal mode. Even though the connection is closed cleanly, SQLite leaves behind empty -shm and -wal companion files in OUT_DIR. Downstream tools that later open the same .db will see those companion files and try to roll the (empty / stale) WAL forward, producing "database disk image is malformed" or silently masking the most recent pages. The decrypted DB itself is fine — the residuals are pure noise from the verification connection. Fix: after the verification block (success or failure), unconditionally os.remove() out_path + "-shm" and out_path + "-wal" if present. Errors during cleanup are swallowed. Tests: existing tests/ pass (168/168). The cleanup is additive and only runs after the existing verification path; no behavior change for callers that do not inspect OUT_DIR for companion files. Scope: 10 lines in decrypt_db.py. No public API change, no schema change, no new dependency. --- decrypt_db.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/decrypt_db.py b/decrypt_db.py index e952141..7daab5c 100644 --- a/decrypt_db.py +++ b/decrypt_db.py @@ -176,6 +176,16 @@ def main(): else: failed += 1 + # 清理 sqlite3.connect() 验证遗留的 -shm/-wal 空文件 + # 避免后续工具打开 .db 时优先读旧 WAL 报 "database disk image is malformed" + for suffix in ("-shm", "-wal"): + residual = out_path + suffix + if os.path.exists(residual): + try: + os.remove(residual) + except OSError: + pass + print(f"\n{'='*60}") print(f"结果: {success} 成功, {failed} 失败, {skipped} 跳过(无密钥), 共 {len(db_files)} 个") print(f"解密数据量: {total_bytes/1024/1024/1024:.1f}GB")