Search in sources :

Example 1 with Meta

use of de.slackspace.openkeepass.domain.Meta in project TinyKeePass by sorz.

the class FetchDatabaseTask method doInBackground.

@Override
protected String doInBackground(Void... voids) {
    File tmpDbFile = new File(cacheDir, DB_FILENAME);
    File dbFile = new File(filesDir, DB_FILENAME);
    try {
        OutputStream output = new BufferedOutputStream(new FileOutputStream(tmpDbFile));
        InputStream input = getInputStream(uri);
        IOUtils.copy(input, output);
        input.close();
        output.close();
    } catch (InterruptedIOException e) {
        // task cancelled
        return null;
    } catch (IOException e) {
        Log.w(TAG, "fail to open database file.", e);
        return e.getClass().getSimpleName() + ": " + e.getLocalizedMessage();
    }
    KeePassFile db;
    try {
        db = KeePassDatabase.getInstance(tmpDbFile).openDatabase(masterPassword);
    } catch (KeePassDatabaseUnreadableException | UnsupportedOperationException e) {
        Log.w(TAG, "cannot open database.", e);
        return e.getLocalizedMessage();
    } catch (NullPointerException e) {
        // happen on try to open a KDBX 4 database with Argon2 (openkeepass 8.0)
        Log.e(TAG, "Underlying library throw null pointer exception", e);
        return "Database broken or not support";
    }
    Meta meta = db.getMeta();
    Log.d(TAG, "Database opened, name: " + meta.getDatabaseName());
    if (!tmpDbFile.renameTo(dbFile)) {
        try {
            InputStream input = new FileInputStream(tmpDbFile);
            OutputStream output = new FileOutputStream(dbFile);
            IOUtils.copy(input, output);
            if (!tmpDbFile.delete())
                Log.w(TAG, "fail to delete temp database on cache");
            input.close();
            output.close();
        } catch (IOException e) {
            Log.e(TAG, "cannot copy new database.", e);
            return "Fail to save database";
        }
    }
    Context context = this.context.get();
    if (context != null)
        KeePassStorage.set(context, db);
    return null;
}
Also used : Context(android.content.Context) InterruptedIOException(java.io.InterruptedIOException) Meta(de.slackspace.openkeepass.domain.Meta) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) KeePassDatabaseUnreadableException(de.slackspace.openkeepass.exception.KeePassDatabaseUnreadableException) KeePassFile(de.slackspace.openkeepass.domain.KeePassFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) KeePassFile(de.slackspace.openkeepass.domain.KeePassFile)

Aggregations

Context (android.content.Context)1 KeePassFile (de.slackspace.openkeepass.domain.KeePassFile)1 Meta (de.slackspace.openkeepass.domain.Meta)1 KeePassDatabaseUnreadableException (de.slackspace.openkeepass.exception.KeePassDatabaseUnreadableException)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1