Backup corrupted db

This might allow some forensics if #2463 happens. Maybe it also
allows to recover some of the files manually.
This commit is contained in:
ByteHamster 2018-02-20 23:22:49 +01:00
parent c13acc624d
commit f90d3c2e7b
1 changed files with 28 additions and 4 deletions

View File

@ -3,7 +3,9 @@ package de.danoeh.antennapod.core.storage;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.DatabaseUtils;
import android.database.DefaultDatabaseErrorHandler;
import android.database.MergeCursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
@ -13,13 +15,12 @@ import android.media.MediaMetadataRetriever;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.event.ProgressEvent;
@ -35,6 +36,7 @@ import de.danoeh.antennapod.core.service.download.DownloadStatus;
import de.danoeh.antennapod.core.util.LongIntMap;
import de.danoeh.antennapod.core.util.flattr.FlattrStatus;
import de.greenrobot.event.EventBus;
import org.apache.commons.io.FileUtils;
// TODO Remove media column from feeditem table
@ -1643,6 +1645,28 @@ public class PodDBAdapter {
return db.rawQuery(FEED_STATISTICS_QUERY, null);
}
/**
* Called when a database corruption happens
*/
public static class PodDbErrorHandler implements DatabaseErrorHandler {
@Override
public void onCorruption(SQLiteDatabase db) {
Log.e(TAG, "Database corrupted: " + db.getPath());
File dbPath = new File(db.getPath());
File backupFolder = PodDBAdapter.context.getExternalFilesDir(null);
File backupFile = new File(backupFolder, "CorruptedDatabaseBackup.db");
try {
FileUtils.copyFile(dbPath, backupFile);
Log.d(TAG, "Dumped database to " + backupFile.getPath());
} catch (IOException e) {
Log.d(TAG, Log.getStackTraceString(e));
}
new DefaultDatabaseErrorHandler().onCorruption(db); // This deletes the database
}
}
/**
* Helper class for opening the Antennapod database.
*/
@ -1661,7 +1685,7 @@ public class PodDBAdapter {
*/
public PodDBHelper(final Context context, final String name,
final CursorFactory factory) {
super(context, name, factory, VERSION);
super(context, name, factory, VERSION, new PodDbErrorHandler());
this.context = context;
}