summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2024-07-03 10:52:12 +0200
committercos <cos>2024-07-03 11:31:23 +0200
commitc44a17383be213b28c4cf880d23a4bd80a48c2bd (patch)
tree05b13e89d06584aaec4c33f6957db0a91df34437
parent9b452109b4fd29b0eaa659605ccf4ab74ba9693d (diff)
downloadAntennaPodDbFixer-c44a17383be213b28c4cf880d23a4bd80a48c2bd.zip
Add integrity checking of db files
-rwxr-xr-xAntennaPodDbFixer.py30
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)