From 9aedb4a5e0ab9ddab3dbadce3a42f483530568e6 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 16 May 2012 05:35:58 +0900 Subject: [PATCH] don't allow empty files in media folder --- anki/consts.py | 2 +- anki/media.py | 4 ++++ anki/storage.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/anki/consts.py b/anki/consts.py index 11815097a..b3595dc9b 100644 --- a/anki/consts.py +++ b/anki/consts.py @@ -42,7 +42,7 @@ MODEL_STD = 0 MODEL_CLOZE = 1 # deck schema & syncing vars -SCHEMA_VERSION = 8 +SCHEMA_VERSION = 9 SYNC_ZIP_SIZE = int(2.5*1024*1024) SYNC_URL = os.environ.get("SYNC_URL") or "https://beta.ankiweb.net/sync/" SYNC_VER = 3 diff --git a/anki/media.py b/anki/media.py index e8a4105e1..f61c8b2e5 100644 --- a/anki/media.py +++ b/anki/media.py @@ -394,6 +394,10 @@ create table log (fname text primary key, type int); continue if f.lower() == "thumbs.db": continue + # empty files are invalid; clean them up and continue + if not os.path.getsize(f): + os.unlink(f) + continue # newly added? if f not in self.cache: added.append(f) diff --git a/anki/storage.py b/anki/storage.py index 5c852a549..661e5b886 100644 --- a/anki/storage.py +++ b/anki/storage.py @@ -117,6 +117,21 @@ def _upgrade(col, ver): col.db.execute( "update cards set due = due / 1000 where due > 4294967296") col.db.execute("update col set ver = 8") + if ver < 9: + # adding an empty file to a zip makes python's zip code think it's a + # folder, so remove any empty files + changed = False + for f in os.listdir(col.media.dir()): + if not os.path.getsize(f): + os.unlink(f) + col.media.db.execute( + "delete from log where fname = ?", f) + col.media.db.execute( + "delete from media where fname = ?", f) + changed = True + if changed: + col.media.db.commit() + col.db.execute("update col set ver = 9") def _upgradeClozeModel(col, m): m['type'] = MODEL_CLOZE