From 227155d484766a620a2f9467a248c844fca26382 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Apr 2014 20:25:50 +0200 Subject: [PATCH 1/2] add debug backup/restore options to main menu Closes #543 --- .../keychain/provider/KeychainDatabase.java | 45 ++++++++++++++++++- .../keychain/ui/KeyListActivity.java | 34 ++++++++++++++ OpenKeychain/src/main/res/menu/key_list.xml | 12 +++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java index b3165d347..80fa914b2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java @@ -36,6 +36,9 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.UserIdsColumns; import org.sufficientlysecure.keychain.provider.KeychainContract.CertsColumns; import org.sufficientlysecure.keychain.util.Log; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; public class KeychainDatabase extends SQLiteOpenHelper { @@ -210,7 +213,7 @@ public class KeychainDatabase extends SQLiteOpenHelper { } } - if(!hasApgDb) + if(!hasApgDb || true) return; Log.d(Constants.TAG, "apg.db exists! Importing..."); @@ -300,4 +303,44 @@ public class KeychainDatabase extends SQLiteOpenHelper { Log.d(Constants.TAG, "All done, (not) deleting apg.db"); } + + private static void copy(File in, File out) throws IOException { + FileInputStream ss = new FileInputStream(in); + FileOutputStream ds = new FileOutputStream(out); + byte[] buf = new byte[512]; + while(ss.available() > 0) { + int count = ss.read(buf, 0, 512); + ds.write(buf, 0, count); + } + } + + public static void debugRead(Context context) throws IOException { + if(!Constants.DEBUG) { + return; + } + File in = context.getDatabasePath("debug.db"); + File out = context.getDatabasePath("openkeychain.db"); + if(!in.canRead()) { + throw new IOException("Cannot read " + in.getName()); + } + if(!out.canRead()) { + throw new IOException("Cannot write " + out.getName()); + } + copy(in, out); + } + + public static void debugWrite(Context context) throws IOException { + if(!Constants.DEBUG) { + return; + } + File in = context.getDatabasePath("openkeychain.db"); + File out = context.getDatabasePath("debug.db"); + if(!in.canRead()) { + throw new IOException("Cannot read " + in.getName()); + } + if(!out.canRead()) { + throw new IOException("Cannot write " + out.getName()); + } + copy(in, out); + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java index 8db643583..de16142d6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java @@ -22,9 +22,16 @@ import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; +import com.devspark.appmsg.AppMsg; + import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ExportHelper; +import org.sufficientlysecure.keychain.provider.KeychainContract; +import org.sufficientlysecure.keychain.provider.KeychainDatabase; +import org.sufficientlysecure.keychain.util.Log; + +import java.io.IOException; public class KeyListActivity extends DrawerActivity { @@ -46,6 +53,12 @@ public class KeyListActivity extends DrawerActivity { public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.key_list, menu); + + if(Constants.DEBUG) { + menu.findItem(R.id.menu_key_list_debug_read).setVisible(true); + menu.findItem(R.id.menu_key_list_debug_write).setVisible(true); + } + return true; } @@ -68,6 +81,27 @@ public class KeyListActivity extends DrawerActivity { mExportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true); return true; + case R.id.menu_key_list_debug_read: + try { + KeychainDatabase.debugRead(this); + AppMsg.makeText(this, "Restored from backup", AppMsg.STYLE_CONFIRM).show(); + getContentResolver().notifyChange(KeychainContract.KeyRings.CONTENT_URI, null); + } catch(IOException e) { + Log.e(Constants.TAG, "IO Error", e); + AppMsg.makeText(this, "IO Error: " + e.getMessage(), AppMsg.STYLE_ALERT).show(); + } + return true; + + case R.id.menu_key_list_debug_write: + try { + KeychainDatabase.debugWrite(this); + AppMsg.makeText(this, "Backup successful", AppMsg.STYLE_CONFIRM).show(); + } catch(IOException e) { + Log.e(Constants.TAG, "IO Error", e); + AppMsg.makeText(this, "IO Error: " + e.getMessage(), AppMsg.STYLE_ALERT).show(); + } + return true; + default: return super.onOptionsItemSelected(item); } diff --git a/OpenKeychain/src/main/res/menu/key_list.xml b/OpenKeychain/src/main/res/menu/key_list.xml index 3f80b616d..ebb7314b8 100644 --- a/OpenKeychain/src/main/res/menu/key_list.xml +++ b/OpenKeychain/src/main/res/menu/key_list.xml @@ -31,4 +31,16 @@ app:showAsAction="never" android:title="@string/menu_create_key_expert" /> + + + + From 57b264639ff209b2ed44aa8085cddd9393b7b50f Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Apr 2014 20:39:36 +0200 Subject: [PATCH 2/2] move old apg.db to apg_old.db (half measure~) --- .../keychain/provider/KeychainDatabase.java | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java index 80fa914b2..abbf3dc13 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java @@ -182,10 +182,6 @@ public class KeychainDatabase extends SQLiteOpenHelper { if (!db.isReadOnly()) { // Enable foreign key constraints db.execSQL("PRAGMA foreign_keys=ON;"); - // TODO remove, once we remove the "always migrate" debug stuff - // db.execSQL("DROP TABLE user_ids;"); - db.execSQL(CREATE_USER_IDS); - db.execSQL(CREATE_CERTS); } } @@ -208,12 +204,13 @@ public class KeychainDatabase extends SQLiteOpenHelper { for(String db : dbs) { if(db.equals("apg.db")) { hasApgDb = true; - break; + } else if(db.equals("apg_old.db")) { + Log.d(Constants.TAG, "Found apg_old.db"); } } } - if(!hasApgDb || true) + if(!hasApgDb) return; Log.d(Constants.TAG, "apg.db exists! Importing..."); @@ -286,9 +283,12 @@ public class KeychainDatabase extends SQLiteOpenHelper { } } + // Move to a different file (but don't delete, just to be safe) + Log.d(Constants.TAG, "All done - moving apg.db to apg_old.db"); + context.getDatabasePath("apg.db").renameTo(context.getDatabasePath("apg_old.db")); + } catch(IOException e) { - Log.e(Constants.TAG, "Error importing apg db!", e); - return; + Log.e(Constants.TAG, "Error importing apg.db!", e); } finally { if(c != null) { c.close(); @@ -298,10 +298,6 @@ public class KeychainDatabase extends SQLiteOpenHelper { } } - // TODO delete old db, if we are sure this works - // context.deleteDatabase("apg.db"); - Log.d(Constants.TAG, "All done, (not) deleting apg.db"); - } private static void copy(File in, File out) throws IOException {