diff options
-rwxr-xr-x | AntennaPodDbFixer.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/AntennaPodDbFixer.py b/AntennaPodDbFixer.py index f772c9d..9966209 100755 --- a/AntennaPodDbFixer.py +++ b/AntennaPodDbFixer.py @@ -9,6 +9,24 @@ import getopt def usage(): return sys.argv[0] + " [--help] [--verbose] [corrupt.db] [empty.db]" +def integrity_is_ok(fileName): + print("Checking integrity of " + fileName + ".", end="") + if verbose: + print("sqlite3 '" + fileName + "' 'PRAGMA integrity_check;'") + integrityCheck = subprocess.run( + ["sqlite3", fileName, "PRAGMA integrity_check;"], + capture_output=True, + text=True + ).stdout.strip() + is_ok = integrityCheck == "ok" + if is_ok: + print(" Detected no errors.") + else: + print(" Errors found.") + if verbose: + print(integrityCheck) + return is_ok + try: opts, args = getopt.gnu_getopt(sys.argv, "hv", ["help", "verbose"]) except getopt.GetoptError as err: @@ -54,6 +72,8 @@ if corruptedVersion == "0": exit(1) print("Corrupted file version: " + corruptedVersion) +integrity_is_ok(inputFilePath) + if len(args) >= 2: emptyFilePath = args[2] else: @@ -76,13 +96,16 @@ repairedFilePath = inputFilePath + "-repaired.db" sqlFilePath = inputFilePath + ".sql.tmp" workingcopyFilePath = inputFilePath + ".tmp" +if not integrity_is_ok(emptyFilePath): + print("Errors found in " + emptyFilePath + ". Giving up!", file=sys.stderr) + exit(1) + if verbose: print("cp '" + emptyFilePath + "' '" + repairedFilePath + "'") shutil.copyfile(emptyFilePath, repairedFilePath, follow_symlinks=True) if os.path.exists(sqlFilePath): os.remove(sqlFilePath) if os.path.exists(workingcopyFilePath): os.remove(workingcopyFilePath) - # Recover to SQL commands and insert back into a database print("Recovering database.") if verbose: @@ -157,4 +180,9 @@ if verbose: print("rm '" + sqlFilePath + "' '" + workingcopyFilePath + "'") os.remove(sqlFilePath) os.remove(workingcopyFilePath) + +if not integrity_is_ok(repairedFilePath): + print("Integrity check of " + repairedFilePath + " still found errors.", file=sys.stderr) + exit(1) + print("Done. Output file: " + repairedFilePath) |