From 3ad9261c8c790746ac1df7c2fd02368713fc5a26 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 17 Aug 2017 13:33:54 +1000 Subject: [PATCH] catch corrupt media db, fix it in 'check media' --- anki/db.py | 2 +- anki/media.py | 13 ++++++++++++- anki/sync.py | 7 +++++-- aqt/sync.py | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/anki/db.py b/anki/db.py index f3a22a6bb..0a2fb4112 100644 --- a/anki/db.py +++ b/anki/db.py @@ -7,7 +7,7 @@ import time from sqlite3 import dbapi2 as sqlite -Error = sqlite.Error +DBError = sqlite.Error class DB: def __init__(self, path, timeout=0): diff --git a/anki/media.py b/anki/media.py index d7750899f..4e3ccf32d 100644 --- a/anki/media.py +++ b/anki/media.py @@ -11,7 +11,7 @@ import zipfile from io import StringIO from anki.utils import checksum, isWin, isMac, json -from anki.db import DB +from anki.db import DB, DBError from anki.consts import * from anki.latex import mungeQA @@ -112,6 +112,12 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0); # may have been deleted pass + def _deleteDB(self): + path = self.db._path + self.close() + os.unlink(path) + self.connect() + def dir(self): return self._dir @@ -290,6 +296,11 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0); if renamedFiles: return self.check(local=local) nohave = [x for x in allRefs if not x.startswith("_")] + # make sure the media DB is valid + try: + self.findChanges() + except DBError: + self._deleteDB() return (nohave, unused, invalid) def _normalizeNoteRefs(self, nid): diff --git a/anki/sync.py b/anki/sync.py index ae634446e..5e0de818c 100644 --- a/anki/sync.py +++ b/anki/sync.py @@ -7,7 +7,7 @@ import gzip import random import requests -from anki.db import DB +from anki.db import DB, DBError from anki.utils import ids2str, intTime, json, platDesc, checksum, devMode from anki.consts import * from .hooks import runHook @@ -701,7 +701,10 @@ class MediaSyncer: # check if there have been any changes runHook("sync", "findMedia") self.col.log("findChanges") - self.col.media.findChanges() + try: + self.col.media.findChanges() + except DBError: + return "corruptMediaDB" # begin session and check if in sync lastUsn = self.col.media.lastUsn() diff --git a/aqt/sync.py b/aqt/sync.py index bc18eba8d..2851993a8 100644 --- a/aqt/sync.py +++ b/aqt/sync.py @@ -447,7 +447,7 @@ class SyncThread(QThread): raise if ret == "noChanges": self.fireEvent("noMediaChanges") - elif ret == "sanityCheckFailed": + elif ret == "sanityCheckFailed" or ret == "corruptMediaDB": self.fireEvent("mediaSanity") else: self.fireEvent("mediaSuccess")