The same, but clearer and less code?

This commit is contained in:
Ciaran Gultnieks 2011-02-16 21:04:02 +00:00
parent 95bca18bd3
commit f5d8419d69

View File

@ -223,123 +223,128 @@ public class RepoXMLHandler extends DefaultHandler {
public static boolean doUpdates(Context ctx, DB db) { public static boolean doUpdates(Context ctx, DB db) {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
boolean success = true; boolean success = false;
db.beginUpdate(); db.beginUpdate();
Vector<DB.Repo> repos = db.getRepos(); try
for (DB.Repo repo : repos) { {
if (repo.inuse) { Vector<DB.Repo> repos = db.getRepos();
for (DB.Repo repo : repos) {
try { if (repo.inuse) {
if (repo.pubkey != null) { try {
// This is a signed repo - we download the jar file, if (repo.pubkey != null) {
// check the signature, and extract the index...
Log.d("FDroid", "Getting signed index from " // This is a signed repo - we download the jar file,
+ repo.address); // check the signature, and extract the index...
getRemoteFile(ctx, repo.address + "/index.jar", Log.d("FDroid", "Getting signed index from "
"tempindex.jar"); + repo.address);
String jarpath = ctx.getFilesDir() + "/tempindex.jar"; getRemoteFile(ctx, repo.address + "/index.jar",
JarFile jar = new JarFile(jarpath); "tempindex.jar");
JarEntry je = (JarEntry) jar.getEntry("index.xml"); String jarpath = ctx.getFilesDir() + "/tempindex.jar";
File efile = new File(ctx.getFilesDir(), JarFile jar = new JarFile(jarpath);
"/tempindex.xml"); JarEntry je = (JarEntry) jar.getEntry("index.xml");
InputStream in = new BufferedInputStream(jar File efile = new File(ctx.getFilesDir(),
.getInputStream(je), 8192); "/tempindex.xml");
OutputStream out = new BufferedOutputStream( InputStream in = new BufferedInputStream(jar
new FileOutputStream(efile), 8192); .getInputStream(je), 8192);
byte[] buffer = new byte[8192]; OutputStream out = new BufferedOutputStream(
while (true) { new FileOutputStream(efile), 8192);
int nBytes = in.read(buffer); byte[] buffer = new byte[8192];
if (nBytes <= 0) while (true) {
break; int nBytes = in.read(buffer);
out.write(buffer, 0, nBytes); if (nBytes <= 0)
break;
out.write(buffer, 0, nBytes);
}
out.flush();
out.close();
in.close();
java.security.cert.Certificate[] certs = je
.getCertificates();
jar.close();
if (certs == null) {
Log.d("FDroid", "No signature found in index");
return false;
}
if (certs.length != 1) {
Log.d("FDroid", "Expected one signature - found "
+ certs.length);
return false;
}
byte[] sig = certs[0].getEncoded();
byte[] csig = new byte[sig.length * 2];
for (int j = 0; j < sig.length; j++) {
byte v = sig[j];
int d = (v >> 4) & 0xf;
csig[j * 2] = (byte) (d >= 10 ? ('a' + d - 10)
: ('0' + d));
d = v & 0xf;
csig[j * 2 + 1] = (byte) (d >= 10 ? ('a' + d - 10)
: ('0' + d));
}
String ssig = new String(csig);
if (!ssig.equals(repo.pubkey)) {
Log.d("FDroid", "Index signature mismatch");
return false;
}
} else {
// It's an old-fashioned unsigned repo...
Log.d("FDroid", "Getting unsigned index from "
+ repo.address);
getRemoteFile(ctx, repo.address + "/index.xml",
"tempindex.xml");
} }
out.flush();
out.close(); // Process the index...
in.close(); SAXParserFactory spf = SAXParserFactory.newInstance();
java.security.cert.Certificate[] certs = je SAXParser sp = spf.newSAXParser();
.getCertificates(); XMLReader xr = sp.getXMLReader();
jar.close(); RepoXMLHandler handler = new RepoXMLHandler(repo.address,
if (certs == null) { db);
Log.d("FDroid", "No signature found in index"); xr.setContentHandler(handler);
return success = false;
InputStreamReader isr = new FileReader(new File(ctx
.getFilesDir()
+ "/tempindex.xml"));
InputSource is = new InputSource(isr);
xr.parse(is);
if (handler.pubkey != null && repo.pubkey == null) {
// We read an unsigned index, but that indicates that
// a signed version is now available...
Log
.d("FDroid",
"Public key found - switching to signed repo for future updates");
repo.pubkey = handler.pubkey;
db.updateRepoByAddress(repo);
} }
if (certs.length != 1) {
Log.d("FDroid", "Expected one signature - found " } catch (Exception e) {
+ certs.length); Log.e("FDroid", "Exception updating from " + repo.address
return success = false; + ":\n" + Log.getStackTraceString(e));
} return false;
} finally {
byte[] sig = certs[0].getEncoded(); ctx.deleteFile("tempindex.xml");
byte[] csig = new byte[sig.length * 2]; ctx.deleteFile("tempindex.jar");
for (int j = 0; j < sig.length; j++) {
byte v = sig[j];
int d = (v >> 4) & 0xf;
csig[j * 2] = (byte) (d >= 10 ? ('a' + d - 10)
: ('0' + d));
d = v & 0xf;
csig[j * 2 + 1] = (byte) (d >= 10 ? ('a' + d - 10)
: ('0' + d));
}
String ssig = new String(csig);
if (!ssig.equals(repo.pubkey)) {
Log.d("FDroid", "Index signature mismatch");
return success = false;
}
} else {
// It's an old-fashioned unsigned repo...
Log.d("FDroid", "Getting unsigned index from "
+ repo.address);
getRemoteFile(ctx, repo.address + "/index.xml",
"tempindex.xml");
} }
// Process the index...
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
RepoXMLHandler handler = new RepoXMLHandler(repo.address,
db);
xr.setContentHandler(handler);
InputStreamReader isr = new FileReader(new File(ctx
.getFilesDir()
+ "/tempindex.xml"));
InputSource is = new InputSource(isr);
xr.parse(is);
if (handler.pubkey != null && repo.pubkey == null) {
// We read an unsigned index, but that indicates that
// a signed version is now available...
Log
.d("FDroid",
"Public key found - switching to signed repo for future updates");
repo.pubkey = handler.pubkey;
db.updateRepoByAddress(repo);
}
} catch (Exception e) {
Log.e("FDroid", "Exception updating from " + repo.address
+ ":\n" + Log.getStackTraceString(e));
return success = false;
} finally {
ctx.deleteFile("tempindex.xml");
ctx.deleteFile("tempindex.jar");
if (!success)
db.cancelUpdate();
} }
} }
success=true;
db.endUpdate();
Log.d("FDroid", "Update completed in "
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " seconds.");
return true;
} finally {
if(!success)
db.cancelUpdate();
} }
db.endUpdate();
Log.d("FDroid", "Update completed in "
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " seconds.");
return true;
} }
} }