use of com.ichi2.anki.BackupManager in project AnkiChinaAndroid by ankichinateam.
the class BackupManager method repairCollection.
/**
* Run the sqlite3 command-line-tool (if it exists) on the collection to dump to a text file
* and reload as a new database. Recently this command line tool isn't available on many devices
*
* @param col Collection
* @return whether the repair was successful
*/
public static boolean repairCollection(Collection col) {
String deckPath = col.getPath();
File deckFile = new File(deckPath);
Time time = col.getTime();
Timber.i("BackupManager - RepairCollection - Closing Collection");
col.close();
// repair file
String execString = "sqlite3 " + deckPath + " .dump | sqlite3 " + deckPath + ".tmp";
Timber.i("repairCollection - Execute: %s", execString);
try {
String[] cmd = { "/system/bin/sh", "-c", execString };
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
if (!new File(deckPath + ".tmp").exists()) {
Timber.e("repairCollection - dump to " + deckPath + ".tmp failed");
return false;
}
if (!moveDatabaseToBrokenFolder(deckPath, false, time)) {
Timber.e("repairCollection - could not move corrupt file to broken folder");
return false;
}
Timber.i("repairCollection - moved corrupt file to broken folder");
File repairedFile = new File(deckPath + ".tmp");
return repairedFile.renameTo(deckFile);
} catch (IOException | InterruptedException e) {
Timber.e(e, "repairCollection - error");
}
return false;
}
Aggregations