Skip to content

Commit

Permalink
Fixed a bug that was caused an NPE in DownloadRemoteDatabase.java.
Browse files Browse the repository at this point in the history
I think the problem was that the temp file being written to (with File.createTempFile()) was being deleted by the system before I had a chance to operate on it. The temp file idea is unnecessary anyway since the bytes are already i memory. The fix involves using the in memory bytes rather than going though a temp file.
  • Loading branch information
adrian committed Oct 10, 2010
1 parent a243913 commit 3a5a791
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry excluding="**/.svn/" kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="output" path="bin"/>
Expand Down
1 change: 0 additions & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
<string name="problem_downloading_db">Problem Downloading Database\n\n%s</string>
<string name="not_password_database">The downloaded file is not a UPM password database</string>
<string name="sync">Sync</string>
<string name="problem_checking_upm_db">There was a problem checking that the file is a UPM database\n\n%s</string>
<string name="problem_decrypying_db">There was a problem decrypting the database\n\n%s</string>
<string name="enter_password_cancalled">No password provided</string>
<string name="generic_error_with_message">Unexpected Error...\n\n%s</string>
Expand Down
35 changes: 18 additions & 17 deletions src/com/u17od/upm/DownloadRemoteDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
*/
package com.u17od.upm;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

Expand Down Expand Up @@ -104,7 +106,7 @@ private String getDatabaseFileNameFromURL() {
private class DownloadDatabase extends AsyncTask<Void, Void, Integer> {

private static final int PROBLEM_DOWNLOADING_DB = 1;
private static final int PROBLEM_CHECKING_DB = 2;
private static final int PROBLEM_SAVING_DB = 2;
private static final int NOT_UPM_DB = 3;

private String errorMessage;
Expand All @@ -127,19 +129,22 @@ protected Integer doInBackground(Void... params) {
String trustedHostname = settings.getString(Prefs.PREF_TRUSTED_HOSTNAME, "");

HTTPTransport transport = new HTTPTransport(getFileStreamPath(FullAccountList.CERT_FILE_NAME), trustedHostname);
File tempDB = null;
try {
tempDB = transport.getRemoteFile(urlEditText.getText().toString(),
userid.getText().toString(), password.getText().toString());
byte[] passwordDBBytes = transport.get(
urlEditText.getText().toString(),
userid.getText().toString(),
password.getText().toString());

if (PasswordDatabase.isPasswordDatabase(tempDB)) {
// Copy the downloaded file to the app's files dir.
if (PasswordDatabase.isPasswordDatabase(passwordDBBytes)) {
// Copy the downloaded bytes to the app's files dir.
// We retain the file name so that we know what file to delete
// on the server when doing a sync.
UPMApplication app = (UPMApplication) getApplication();
File destFile = new File(getFilesDir(), getDatabaseFileNameFromURL());
app.copyFile(tempDB, destFile, DownloadRemoteDatabase.this);
app.setPasswordDatabase(EnterMasterPassword.decryptedPasswordDatabase);

BufferedOutputStream buf = new BufferedOutputStream(
new FileOutputStream(destFile));
buf.write(passwordDBBytes);
buf.close();

EnterMasterPassword.databaseFileToDecrypt = destFile;
} else {
Expand All @@ -150,13 +155,9 @@ protected Integer doInBackground(Void... params) {
errorMessage = e.getMessage();
errorCode = PROBLEM_DOWNLOADING_DB;
} catch (IOException e) {
Log.e("DownloadRemoteDatabase", "IO problem", e);
Log.e("DownloadRemoteDatabase", "Problem writing to database file", e);
errorMessage = e.getMessage();
errorCode = PROBLEM_CHECKING_DB;
} finally {
if (tempDB != null) {
tempDB.delete();
}
errorCode = PROBLEM_SAVING_DB;
}

return errorCode;
Expand Down Expand Up @@ -185,9 +186,9 @@ protected void onPostExecute(Integer result) {
String.format(getString(R.string.problem_downloading_db), errorMessage),
true);
break;
case PROBLEM_CHECKING_DB:
case PROBLEM_SAVING_DB:
UIUtilities.showToast(DownloadRemoteDatabase.this,
String.format(getString(R.string.problem_checking_upm_db), errorMessage),
String.format(getString(R.string.problem_saving_db), errorMessage),
true);
break;
}
Expand Down
21 changes: 17 additions & 4 deletions src/com/u17od/upm/database/PasswordDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,30 @@ public int getRevision() {
return revision.getRevision();
}


public static boolean isPasswordDatabase(File file) throws IOException {

/**
* Check if the given bytes represent a password database by examining the
* header bytes for the UPM magic number.
* @param data
* @return
*/
public static boolean isPasswordDatabase(byte[] data) {
boolean isPasswordDatabase = false;
byte[] headerBytes = Util.getBytesFromFile(file, FILE_HEADER.getBytes().length);

// Extract the header bytes
byte[] headerBytes = new byte[FILE_HEADER.getBytes().length];
for (int i=0; i<headerBytes.length; i++) {
headerBytes[i] = data[i];
}

if (Arrays.equals(headerBytes, FILE_HEADER.getBytes())) {
isPasswordDatabase = true;
}

return isPasswordDatabase;
}


public EncryptionService getEncryptionService () {
return encryptionService;
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/u17od/upm/transport/HTTPTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public byte[] get(String urlToGet, String username, String password) throws Tran
conn = getConnection(url);

// Put the authentication details in the request
if (username != null) {
if (username != null && !username.trim().equals("")) {
conn.setRequestProperty ("Authorization", createAuthenticationString(username, password));
}

Expand Down

0 comments on commit 3a5a791

Please sign in to comment.