From 535d343fafb203f2fb00267b32427fc1febf0050 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Fri, 3 Jan 2014 06:15:45 +1100 Subject: [PATCH] Fixed update code to work with signed repos that haven't changed. --- src/org/fdroid/fdroid/DB.java | 11 ---- src/org/fdroid/fdroid/UpdateService.java | 3 +- src/org/fdroid/fdroid/net/Downloader.java | 4 ++ .../fdroid/fdroid/updater/RepoUpdater.java | 63 ++++++++++++------- .../fdroid/updater/SignedRepoUpdater.java | 12 ++-- .../fdroid/updater/UnsignedRepoUpdater.java | 5 +- 6 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/org/fdroid/fdroid/DB.java b/src/org/fdroid/fdroid/DB.java index 7fb3d1575..67be856f2 100644 --- a/src/org/fdroid/fdroid/DB.java +++ b/src/org/fdroid/fdroid/DB.java @@ -796,17 +796,6 @@ public class DB { } } - // Repopulate the details for the given app. - // If 'apkrepo' is non-zero, only apks from that repo are - // populated. - public void repopulateDetails(App app, int apkRepo) { - populateAppDetails(app); - - for (Apk apk : app.apks) { - populateApkDetails(apk, apkRepo); - } - } - // Return a list of apps matching the given criteria. Filtering is // also done based on compatibility and anti-features according to // the user's current preferences. diff --git a/src/org/fdroid/fdroid/UpdateService.java b/src/org/fdroid/fdroid/UpdateService.java index 7acf1e24c..b555849c0 100644 --- a/src/org/fdroid/fdroid/UpdateService.java +++ b/src/org/fdroid/fdroid/UpdateService.java @@ -207,7 +207,6 @@ public class UpdateService extends IntentService implements ProgressListener { sendStatus(STATUS_INFO, getString(R.string.status_checking_compatibility)); - apps = ((FDroidApp) getApplication()).getApps(); DB db = DB.getDB(); try { @@ -226,7 +225,7 @@ public class UpdateService extends IntentService implements ProgressListener { } if (keepapp) { DB.App app_k = null; - for (DB.App app2 : updatingApps) { + for (DB.App app2 : apps) { if (app2.id.equals(app.id)) { app_k = app2; break; diff --git a/src/org/fdroid/fdroid/net/Downloader.java b/src/org/fdroid/fdroid/net/Downloader.java index 6c765a416..27fc6bb79 100644 --- a/src/org/fdroid/fdroid/net/Downloader.java +++ b/src/org/fdroid/fdroid/net/Downloader.java @@ -135,4 +135,8 @@ public class Downloader { } } + public boolean hasChanged() { + return this.statusCode == 200; + } + } diff --git a/src/org/fdroid/fdroid/updater/RepoUpdater.java b/src/org/fdroid/fdroid/updater/RepoUpdater.java index bbdc9cb7a..6f7242e65 100644 --- a/src/org/fdroid/fdroid/updater/RepoUpdater.java +++ b/src/org/fdroid/fdroid/updater/RepoUpdater.java @@ -62,11 +62,14 @@ abstract public class RepoUpdater { } /** - * Return the index file if it is different than last time, - * otherwise returns null to indicate that the file has not changed. - * All error states will come via an UpdateException. + * For example, you may want to unzip a jar file to get the index inside, + * or if the file is not compressed, you can just return a reference to + * the downloaded file. + * + * @throws UpdateException All error states will come from here. */ - protected abstract File getIndexFile() throws UpdateException; + protected abstract File getIndexFromFile(File downloadedFile) throws + UpdateException; protected abstract String getIndexAddress(); @@ -86,14 +89,13 @@ abstract public class RepoUpdater { int status = downloader.download(); - repo.lastetag = downloader.getETag(); if (status == 304) { // The index is unchanged since we last read it. We just mark // everything that came from this repo as being updated. Log.d("FDroid", "Repo index for " + repo.address + " is up to date (by etag)"); } else if (status == 200) { - hasChanged = true; + // Nothing needed to be done here... } else { // Is there any code other than 200 which still returns // content? Just in case, lets try to clean up. @@ -152,28 +154,38 @@ abstract public class RepoUpdater { public void update() throws UpdateException { + File downloadedFile = null; File indexFile = null; try { - indexFile = getIndexFile(); - // Process the index... - SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); - XMLReader reader = parser.getXMLReader(); - RepoXMLHandler handler = new RepoXMLHandler(repo, apps, progressListener); + Downloader downloader = downloadIndex(); + hasChanged = downloader.hasChanged(); - if (isInteractive()) { - // Only bother spending the time to count the expected apps - // if we can show that to the user... - handler.setTotalAppCount(estimateAppCount(indexFile)); + if (hasChanged) { + + downloadedFile = downloader.getFile(); + repo.lastetag = downloader.getETag(); + + indexFile = getIndexFromFile(downloadedFile); + + // Process the index... + SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); + XMLReader reader = parser.getXMLReader(); + RepoXMLHandler handler = new RepoXMLHandler(repo, apps, progressListener); + + if (isInteractive()) { + // Only bother spending the time to count the expected apps + // if we can show that to the user... + handler.setTotalAppCount(estimateAppCount(indexFile)); + } + + reader.setContentHandler(handler); + InputSource is = new InputSource( + new BufferedReader(new FileReader(indexFile))); + + reader.parse(is); + updateRepo(handler.getPubKey(), handler.getMaxAge()); } - - reader.setContentHandler(handler); - InputSource is = new InputSource( - new BufferedReader(new FileReader(indexFile))); - - reader.parse(is); - updateRepo(handler.getPubKey(), handler.getMaxAge()); - } catch (SAXException e) { throw new UpdateException( repo, "Error parsing index for repo " + repo.address, e); @@ -187,9 +199,14 @@ abstract public class RepoUpdater { throw new UpdateException( repo, "Error parsing index for repo " + repo.address, e); } finally { + if (downloadedFile != null && + downloadedFile != indexFile && downloadedFile.exists()) { + downloadedFile.delete(); + } if (indexFile != null && indexFile.exists()) { indexFile.delete(); } + } } diff --git a/src/org/fdroid/fdroid/updater/SignedRepoUpdater.java b/src/org/fdroid/fdroid/updater/SignedRepoUpdater.java index 2c41d9e5f..ae303c2a8 100644 --- a/src/org/fdroid/fdroid/updater/SignedRepoUpdater.java +++ b/src/org/fdroid/fdroid/updater/SignedRepoUpdater.java @@ -91,23 +91,19 @@ public class SignedRepoUpdater extends RepoUpdater { * check the signature, and extract the index file */ @Override - protected File getIndexFile() throws UpdateException { + protected File getIndexFromFile(File downloadedFile) throws + UpdateException { Date updateTime = new Date(System.currentTimeMillis()); Log.d("FDroid", "Getting signed index from " + repo.address + " at " + Utils.LOG_DATE_FORMAT.format(updateTime)); - Downloader downloader = downloadIndex(); - File indexJar = downloader.getFile(); + File indexJar = downloadedFile; File indexXml = null; // Don't worry about checking the status code for 200. If it was a // successful download, then we will have a file ready to use: if (indexJar != null && indexJar.exists()) { - try { - indexXml = extractIndexFromJar(indexJar); - } finally { - indexJar.delete(); - } + indexXml = extractIndexFromJar(indexJar); } return indexXml; } diff --git a/src/org/fdroid/fdroid/updater/UnsignedRepoUpdater.java b/src/org/fdroid/fdroid/updater/UnsignedRepoUpdater.java index e99e30589..6fd6d5699 100644 --- a/src/org/fdroid/fdroid/updater/UnsignedRepoUpdater.java +++ b/src/org/fdroid/fdroid/updater/UnsignedRepoUpdater.java @@ -14,10 +14,9 @@ public class UnsignedRepoUpdater extends RepoUpdater { } @Override - protected File getIndexFile() throws UpdateException { + protected File getIndexFromFile(File file) throws UpdateException { Log.d("FDroid", "Getting unsigned index from " + getIndexAddress()); - Downloader downloader = downloadIndex(); - return downloader.getFile(); + return file; } @Override