diff --git a/src/org/fdroid/fdroid/DB.java b/src/org/fdroid/fdroid/DB.java index 96fedc77c..32ca3b443 100644 --- a/src/org/fdroid/fdroid/DB.java +++ b/src/org/fdroid/fdroid/DB.java @@ -491,6 +491,10 @@ public class DB { updateApps = getApps(null, null, true); Log.d("FDroid", "AppUpdate: " + updateApps.size() + " apps before starting."); + // Wrap the whole update in a transaction. Make sure to call + // either endUpdate or cancelUpdate to commit or discard it, + // respectively. + db.beginTransaction(); } // Called when a repo update ends. Any applications that have not been @@ -521,12 +525,20 @@ public class DB { } } } + // Commit updates to the database. + db.setTransactionSuccessful(); + db.endTransaction(); Log.d("FDroid", "AppUpdate: " + updateApps.size() + " apps on completion."); updateApps = null; return; } + // Called instead of endUpdate if the update failed. + public void cancelUpdate() { + db.endTransaction(); + } + // Called during update to supply new details for an application (or // details of a completely new one). Calls to this must be wrapped by // a call to beginUpdate and a call to endUpdate. diff --git a/src/org/fdroid/fdroid/RepoXMLHandler.java b/src/org/fdroid/fdroid/RepoXMLHandler.java index 89d138c53..eba7107b0 100644 --- a/src/org/fdroid/fdroid/RepoXMLHandler.java +++ b/src/org/fdroid/fdroid/RepoXMLHandler.java @@ -223,6 +223,7 @@ public class RepoXMLHandler extends DefaultHandler { public static boolean doUpdates(Context ctx, DB db) { long startTime = System.currentTimeMillis(); + boolean success = true; db.beginUpdate(); Vector repos = db.getRepos(); for (DB.Repo repo : repos) { @@ -262,12 +263,12 @@ public class RepoXMLHandler extends DefaultHandler { jar.close(); if (certs == null) { Log.d("FDroid", "No signature found in index"); - return false; + return success = false; } if (certs.length != 1) { Log.d("FDroid", "Expected one signature - found " + certs.length); - return false; + return success = false; } byte[] sig = certs[0].getEncoded(); @@ -285,7 +286,7 @@ public class RepoXMLHandler extends DefaultHandler { if (!ssig.equals(repo.pubkey)) { Log.d("FDroid", "Index signature mismatch"); - return false; + return success = false; } } else { @@ -324,10 +325,12 @@ public class RepoXMLHandler extends DefaultHandler { } catch (Exception e) { Log.e("FDroid", "Exception updating from " + repo.address + ":\n" + Log.getStackTraceString(e)); - return false; + return success = false; } finally { ctx.deleteFile("tempindex.xml"); ctx.deleteFile("tempindex.jar"); + if (!success) + db.cancelUpdate(); } }