consolidate: split into two steps, can pick up at second step if anything fails

This commit is contained in:
Vincent Breitmoser 2014-08-19 15:45:42 +02:00
parent 8d668d170c
commit c725239a69
7 changed files with 112 additions and 26 deletions

View File

@ -68,6 +68,9 @@ public final class Constants {
public static final String KEY_SERVERS_DEFAULT_VERSION = "keyServersDefaultVersion"; public static final String KEY_SERVERS_DEFAULT_VERSION = "keyServersDefaultVersion";
public static final String WRITE_VERSION_HEADER = "writeVersionHeader"; public static final String WRITE_VERSION_HEADER = "writeVersionHeader";
public static final String FIRST_TIME = "firstTime"; public static final String FIRST_TIME = "firstTime";
public static final String CACHED_CONSOLIDATE = "cachedConsolidate";
public static final String CACHED_CONSOLIDATE_SECRETS = "cachedConsolidateSecrets";
public static final String CACHED_CONSOLIDATE_PUBLICS = "cachedConsolidatePublics";
} }
public static final class Defaults { public static final class Defaults {

View File

@ -25,6 +25,7 @@ import org.spongycastle.bcpg.CompressionAlgorithmTags;
import org.spongycastle.bcpg.HashAlgorithmTags; import org.spongycastle.bcpg.HashAlgorithmTags;
import org.spongycastle.openpgp.PGPEncryptedData; import org.spongycastle.openpgp.PGPEncryptedData;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Constants.Pref;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -134,6 +135,36 @@ public class Preferences {
editor.commit(); editor.commit();
} }
public boolean getCachedConsolidate() {
return mSharedPreferences.getBoolean(Pref.CACHED_CONSOLIDATE, false);
}
public void setCachedConsolidate(boolean value) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(Pref.CACHED_CONSOLIDATE, value);
editor.commit();
}
public int getCachedConsolidateNumPublics() {
return mSharedPreferences.getInt(Pref.CACHED_CONSOLIDATE_PUBLICS, 100);
}
public void setCachedConsolidateNumPublics(int value) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt(Pref.CACHED_CONSOLIDATE_PUBLICS, value);
editor.commit();
}
public int getCachedConsolidateNumSecrets() {
return mSharedPreferences.getInt(Pref.CACHED_CONSOLIDATE_SECRETS, 100);
}
public void setCachedConsolidateNumSecrets(int value) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt(Pref.CACHED_CONSOLIDATE_SECRETS, value);
editor.commit();
}
public boolean isFirstTime() { public boolean isFirstTime() {
return mSharedPreferences.getBoolean(Constants.Pref.FIRST_TIME, true); return mSharedPreferences.getBoolean(Constants.Pref.FIRST_TIME, true);
} }

View File

@ -28,6 +28,8 @@ import android.os.RemoteException;
import android.support.v4.util.LongSparseArray; import android.support.v4.util.LongSparseArray;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKey; import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKey;
import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing;
@ -58,6 +60,7 @@ import org.sufficientlysecure.keychain.service.OperationResults.SaveKeyringResul
import org.sufficientlysecure.keychain.util.FileImportCache; import org.sufficientlysecure.keychain.util.FileImportCache;
import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.IterableIterator;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.ProgressFixedScaler;
import org.sufficientlysecure.keychain.util.ProgressScaler; import org.sufficientlysecure.keychain.util.ProgressScaler;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -823,11 +826,9 @@ public class ProviderHelper {
} }
public ConsolidateResult consolidateDatabase(Progressable progress) { public ConsolidateResult consolidateDatabaseStep1(Progressable progress) {
// 1a. fetch all secret keyrings into a cache file // 1a. fetch all secret keyrings into a cache file
int numSecrets, numPublics;
log(LogLevel.START, LogType.MSG_CON, mIndent); log(LogLevel.START, LogType.MSG_CON, mIndent);
mIndent += 1; mIndent += 1;
@ -836,7 +837,7 @@ public class ProviderHelper {
log(LogLevel.DEBUG, LogType.MSG_CON_SAVE_SECRET, mIndent); log(LogLevel.DEBUG, LogType.MSG_CON_SAVE_SECRET, mIndent);
mIndent += 1; mIndent += 1;
final Cursor cursor = mContentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), new String[] { final Cursor cursor = mContentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), new String[]{
KeyRings.PRIVKEY_DATA, KeyRings.FINGERPRINT, KeyRings.HAS_ANY_SECRET KeyRings.PRIVKEY_DATA, KeyRings.FINGERPRINT, KeyRings.HAS_ANY_SECRET
}, KeyRings.HAS_ANY_SECRET + " = 1", null, null); }, KeyRings.HAS_ANY_SECRET + " = 1", null, null);
@ -844,7 +845,7 @@ public class ProviderHelper {
return new ConsolidateResult(ConsolidateResult.RESULT_ERROR, mLog); return new ConsolidateResult(ConsolidateResult.RESULT_ERROR, mLog);
} }
numSecrets = cursor.getCount(); Preferences.getPreferences(mContext).setCachedConsolidateNumSecrets(cursor.getCount());
FileImportCache<ParcelableKeyRing> cache = FileImportCache<ParcelableKeyRing> cache =
new FileImportCache<ParcelableKeyRing>(mContext, "consolidate_secret.pcl"); new FileImportCache<ParcelableKeyRing>(mContext, "consolidate_secret.pcl");
@ -894,7 +895,7 @@ public class ProviderHelper {
log(LogLevel.DEBUG, LogType.MSG_CON_SAVE_PUBLIC, mIndent); log(LogLevel.DEBUG, LogType.MSG_CON_SAVE_PUBLIC, mIndent);
mIndent += 1; mIndent += 1;
final Cursor cursor = mContentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), new String[] { final Cursor cursor = mContentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), new String[]{
KeyRings.PUBKEY_DATA, KeyRings.FINGERPRINT KeyRings.PUBKEY_DATA, KeyRings.FINGERPRINT
}, null, null, null); }, null, null, null);
@ -902,7 +903,7 @@ public class ProviderHelper {
return new ConsolidateResult(ConsolidateResult.RESULT_ERROR, mLog); return new ConsolidateResult(ConsolidateResult.RESULT_ERROR, mLog);
} }
numPublics = cursor.getCount(); Preferences.getPreferences(mContext).setCachedConsolidateNumSecrets(cursor.getCount());
FileImportCache<ParcelableKeyRing> cache = FileImportCache<ParcelableKeyRing> cache =
new FileImportCache<ParcelableKeyRing>(mContext, "consolidate_public.pcl"); new FileImportCache<ParcelableKeyRing>(mContext, "consolidate_public.pcl");
@ -946,19 +947,39 @@ public class ProviderHelper {
mIndent -= 1; mIndent -= 1;
} }
Preferences.getPreferences(mContext).setCachedConsolidate(true);
return consolidateDatabaseStep2(progress);
}
public ConsolidateResult consolidateDatabaseStep2(Progressable progress) {
Preferences prefs = Preferences.getPreferences(mContext);
if ( ! prefs.getCachedConsolidate()) {
log(LogLevel.ERROR, LogType.MSG_CON_ERROR_BAD_STATE);
return new ConsolidateResult(ConsolidateResult.RESULT_ERROR, mLog);
}
// Set flag that we have a cached consolidation here
int numSecrets = prefs.getCachedConsolidateNumSecrets();
int numPublics = prefs.getCachedConsolidateNumPublics();
// 2. wipe database (IT'S DANGEROUS) // 2. wipe database (IT'S DANGEROUS)
log(LogLevel.DEBUG, LogType.MSG_CON_DB_CLEAR, mIndent); log(LogLevel.DEBUG, LogType.MSG_CON_DB_CLEAR, mIndent);
new KeychainDatabase(mContext).clearDatabase(); new KeychainDatabase(mContext).clearDatabase();
FileImportCache<ParcelableKeyRing> cacheSecret =
new FileImportCache<ParcelableKeyRing>(mContext, "consolidate_secret.pcl");
FileImportCache<ParcelableKeyRing> cachePublic =
new FileImportCache<ParcelableKeyRing>(mContext, "consolidate_public.pcl");
// 3. Re-Import secret keyrings from cache // 3. Re-Import secret keyrings from cache
try { try {
log(LogLevel.DEBUG, LogType.MSG_CON_REIMPORT_SECRET, mIndent, numSecrets); log(LogLevel.DEBUG, LogType.MSG_CON_REIMPORT_SECRET, mIndent, numSecrets);
mIndent += 1; mIndent += 1;
FileImportCache<ParcelableKeyRing> cache = new PgpImportExport(mContext, this, new ProgressFixedScaler(progress, 10, 25, 100, R.string.progress_con_reimport))
new FileImportCache<ParcelableKeyRing>(mContext, "consolidate_secret.pcl"); .importKeyRings(cacheSecret.readCache(false), numSecrets);
new PgpImportExport(mContext, this, new ProgressScaler(progress, 10, 25, 100))
.importKeyRings(cache.readCache(), numSecrets);
} catch (IOException e) { } catch (IOException e) {
Log.e(Constants.TAG, "error importing secret"); Log.e(Constants.TAG, "error importing secret");
return new ConsolidateResult(ConsolidateResult.RESULT_ERROR, mLog); return new ConsolidateResult(ConsolidateResult.RESULT_ERROR, mLog);
@ -966,15 +987,13 @@ public class ProviderHelper {
mIndent -= 1; mIndent -= 1;
} }
// 3. Re-Import public keyrings from cache // 4. Re-Import public keyrings from cache
try { try {
log(LogLevel.DEBUG, LogType.MSG_CON_REIMPORT_PUBLIC, mIndent, numPublics); log(LogLevel.DEBUG, LogType.MSG_CON_REIMPORT_PUBLIC, mIndent, numPublics);
mIndent += 1; mIndent += 1;
FileImportCache<ParcelableKeyRing> cache =
new FileImportCache<ParcelableKeyRing>(mContext, "consolidate_public.pcl");
new PgpImportExport(mContext, this, new ProgressScaler(progress, 25, 99, 100)) new PgpImportExport(mContext, this, new ProgressScaler(progress, 25, 99, 100))
.importKeyRings(cache.readCache(), numPublics); .importKeyRings(cachePublic.readCache(false), numPublics);
} catch (IOException e) { } catch (IOException e) {
Log.e(Constants.TAG, "error importing public"); Log.e(Constants.TAG, "error importing public");
return new ConsolidateResult(ConsolidateResult.RESULT_ERROR, mLog); return new ConsolidateResult(ConsolidateResult.RESULT_ERROR, mLog);
@ -982,6 +1001,22 @@ public class ProviderHelper {
mIndent -= 1; mIndent -= 1;
} }
Preferences.getPreferences(mContext).setCachedConsolidate(false);
// 5. Delete caches
try {
cacheSecret.delete();
} catch (IOException e) {
// doesn't really matter
Log.e(Constants.TAG, "IOException during delete of secret cache", e);
}
try {
cachePublic.delete();
} catch (IOException e) {
// doesn't really matter
Log.e(Constants.TAG, "IOException during deletion of public cache", e);
}
progress.setProgress(100, 100); progress.setProgress(100, 100);
log(LogLevel.OK, LogType.MSG_CON_SUCCESS, mIndent); log(LogLevel.OK, LogType.MSG_CON_SUCCESS, mIndent);
mIndent -= 1; mIndent -= 1;

View File

@ -490,10 +490,7 @@ public class KeychainIntentService extends IntentService
PgpImportExport pgpImportExport = new PgpImportExport(this, this); PgpImportExport pgpImportExport = new PgpImportExport(this, this);
ImportKeyResult result = pgpImportExport.importKeyRings(entries); ImportKeyResult result = pgpImportExport.importKeyRings(entries);
Bundle resultData = new Bundle(); sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result);
resultData.putParcelable(RESULT_IMPORT, result);
sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, resultData);
} catch (Exception e) { } catch (Exception e) {
sendErrorToHandler(e); sendErrorToHandler(e);
} }
@ -670,12 +667,8 @@ public class KeychainIntentService extends IntentService
} }
} else if (ACTION_CONSOLIDATE.equals(action)) { } else if (ACTION_CONSOLIDATE.equals(action)) {
ConsolidateResult result = new ProviderHelper(this).consolidateDatabase(this); ConsolidateResult result = new ProviderHelper(this).consolidateDatabaseStep1(this);
sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result);
Bundle resultData = new Bundle();
resultData.putParcelable(RESULT_CONSOLIDATE, result);
sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, resultData);
} }
} }

View File

@ -391,6 +391,7 @@ public class OperationResultParcel implements Parcelable {
// consolidate // consolidate
MSG_CON (R.string.msg_con), MSG_CON (R.string.msg_con),
MSG_CON_ERROR_BAD_STATE (R.string.msg_con_error_bad_state),
MSG_CON_SAVE_SECRET (R.string.msg_con_save_secret), MSG_CON_SAVE_SECRET (R.string.msg_con_save_secret),
MSG_CON_SAVE_PUBLIC (R.string.msg_con_save_public), MSG_CON_SAVE_PUBLIC (R.string.msg_con_save_public),
MSG_CON_DB_CLEAR (R.string.msg_con_db_clear), MSG_CON_DB_CLEAR (R.string.msg_con_db_clear),

View File

@ -92,6 +92,10 @@ public class FileImportCache<E extends Parcelable> {
} }
public Iterator<E> readCache() throws IOException { public Iterator<E> readCache() throws IOException {
return readCache(true);
}
public Iterator<E> readCache(final boolean deleteAfterRead) throws IOException {
File cacheDir = mContext.getCacheDir(); File cacheDir = mContext.getCacheDir();
if (cacheDir == null) { if (cacheDir == null) {
@ -166,7 +170,10 @@ public class FileImportCache<E extends Parcelable> {
if (!closed) { if (!closed) {
try { try {
ois.close(); ois.close();
tempFile.delete(); if (deleteAfterRead) {
//noinspection ResultOfMethodCallIgnored
tempFile.delete();
}
} catch (IOException e) { } catch (IOException e) {
// nvm // nvm
} }
@ -177,4 +184,17 @@ public class FileImportCache<E extends Parcelable> {
}; };
} }
public boolean delete() throws IOException {
File cacheDir = mContext.getCacheDir();
if (cacheDir == null) {
// https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU
throw new IOException("cache dir is null!");
}
final File tempFile = new File(cacheDir, mFilename);
return tempFile.delete();
}
} }

View File

@ -302,6 +302,8 @@
<string name="progress_verifying_integrity">verifying integrity…</string> <string name="progress_verifying_integrity">verifying integrity…</string>
<string name="progress_deleting_securely">deleting \'%s\' securely…</string> <string name="progress_deleting_securely">deleting \'%s\' securely…</string>
<string name="progress_con_reimport">reimporting database…</string>
<!-- action strings --> <!-- action strings -->
<string name="hint_keyserver_search_hint">Name/Email/Key ID…</string> <string name="hint_keyserver_search_hint">Name/Email/Key ID…</string>
<string name="hint_keybase_search_hint">Name/Email/Proof/Key…</string> <string name="hint_keybase_search_hint">Name/Email/Proof/Key…</string>
@ -671,6 +673,7 @@
<!-- Consolidate --> <!-- Consolidate -->
<string name="msg_con">Consolidating database</string> <string name="msg_con">Consolidating database</string>
<string name="msg_con_error_bad_state">Consolidation started while no database was cached! This is probably a programming error, please file a bug report.</string>
<string name="msg_con_save_secret">Saving secret keyrings</string> <string name="msg_con_save_secret">Saving secret keyrings</string>
<string name="msg_con_save_public">Saving public keyrings</string> <string name="msg_con_save_public">Saving public keyrings</string>
<string name="msg_con_db_clear">Clearing database</string> <string name="msg_con_db_clear">Clearing database</string>