Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d2d9ceb7 authored by Joe Onorato's avatar Joe Onorato
Browse files

Make RestoreHelper and friends also write out the snapshot state.

parent abce4e87
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.backup;

import android.os.ParcelFileDescriptor;

import java.io.InputStream;

/** @hide */
@@ -27,5 +29,6 @@ public interface RestoreHelper {
     * <code>dataSize</code> bytes from <code>data</code>.
     */
    public void restoreEntity(BackupDataInputStream data);
    public void writeSnapshot(ParcelFileDescriptor fd);
}
+34 −37
Original line number Diff line number Diff line
@@ -17,69 +17,66 @@
package android.backup;

import android.content.Context;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.InputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;

class RestoreHelperBase {
    private static final String TAG = "RestoreHelperBase";
    private static final int BUF_SIZE = 8 * 1024;

    int mPtr;
    Context mContext;
    byte[] mBuf = new byte[BUF_SIZE];
    boolean mExceptionLogged;
    
    RestoreHelperBase(Context context) {
        mPtr = ctor();
        mContext = context;
    }

    void writeFile(File f, InputStream in) {
        boolean success = false;
        FileOutputStream out = null;
    protected void finalize() throws Throwable {
        try {
            // Create the enclosing directory.
            File parent = f.getParentFile();
            parent.mkdirs();
            dtor(mPtr);
        } finally {
            super.finalize();
        }
    }

            // Copy the file.
            int sum = 0;
            out = new FileOutputStream(f);
            byte[] buf = mBuf;
            int amt;
            while ((amt = in.read(buf)) > 0) {
                out.write(buf, 0, amt);
                sum += amt;
    void writeFile(File f, InputStream in) {
        if (!(in instanceof BackupDataInputStream)) {
            throw new IllegalStateException("input stream must be a BackupDataInputStream");
        }
        int result = -1;

            // TODO: Set the permissions of the file.
        // Create the enclosing directory.
        File parent = f.getParentFile();
        parent.mkdirs();

            // We're done
            success = true;
            out = null;
        } catch (IOException ex) {
            // Bail on this entity.  Only log one exception per helper object.
        result = writeFile_native(mPtr, f.getAbsolutePath(),
                ((BackupDataInputStream)in).mData.mBackupReader);
        if (result != 0) {
            // Bail on this entity.  Only log one failure per helper object.
            if (!mExceptionLogged) {
                Log.e(TAG, "Failed restoring file '" + f + "' for app '"
                    + mContext.getPackageName() + '\'', ex);
                        + mContext.getPackageName() + "\' result=0x"
                        + Integer.toHexString(result));
                mExceptionLogged = true;
            }
        }
        finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                }
            }
            if (!success) {
                // Something didn't work out, delete the file
                f.delete();
            }
    }

    public void writeSnapshot(ParcelFileDescriptor fd) {
        int result = writeSnapshot_native(mPtr, fd.getFileDescriptor());
        // TODO: Do something with the error.
    }

    private static native int ctor();
    private static native void dtor(int ptr);
    private static native int writeFile_native(int ptr, String filename, int backupReader);
    private static native int writeSnapshot_native(int ptr, FileDescriptor fd);
}

+15 −1
Original line number Diff line number Diff line
@@ -16,10 +16,12 @@

package android.backup;

import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/** @hide */
public class RestoreHelperDispatcher {
@@ -31,7 +33,7 @@ public class RestoreHelperDispatcher {
        mHelpers.put(keyPrefix, helper);
    }

    public void dispatch(BackupDataInput input) throws IOException {
    public void dispatch(BackupDataInput input, ParcelFileDescriptor newState) throws IOException {
        boolean alreadyComplained = false;

        BackupDataInputStream stream = new BackupDataInputStream(input);
@@ -60,5 +62,17 @@ public class RestoreHelperDispatcher {
            }
            input.skipEntityData(); // In case they didn't consume the data.
        }

        if (mHelpers.size() > 1) {
            throw new RuntimeException("RestoreHelperDispatcher won't get your your"
                    + " data in the right order yet.");
        }
        
        // Write out the state files
        for (RestoreHelper helper: mHelpers.values()) {
            // TODO: Write a header for the state
            helper.writeSnapshot(newState);
        }
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -119,7 +119,8 @@ LOCAL_SRC_FILES:= \
	com_android_internal_graphics_NativeUtils.cpp \
	android_backup_BackupDataInput.cpp \
	android_backup_BackupDataOutput.cpp \
	android_backup_FileBackupHelper.cpp
	android_backup_FileBackupHelper.cpp \
	android_backup_RestoreHelperBase.cpp

LOCAL_C_INCLUDES += \
	$(JNI_H_INCLUDE) \
+2 −0
Original line number Diff line number Diff line
@@ -158,6 +158,7 @@ extern int register_android_location_GpsLocationProvider(JNIEnv* env);
extern int register_android_backup_BackupDataInput(JNIEnv *env);
extern int register_android_backup_BackupDataOutput(JNIEnv *env);
extern int register_android_backup_FileBackupHelper(JNIEnv *env);
extern int register_android_backup_RestoreHelperBase(JNIEnv *env);

static AndroidRuntime* gCurRuntime = NULL;

@@ -1131,6 +1132,7 @@ static const RegJNIRec gRegJNI[] = {
    REG_JNI(register_android_backup_BackupDataInput),
    REG_JNI(register_android_backup_BackupDataOutput),
    REG_JNI(register_android_backup_FileBackupHelper),
    REG_JNI(register_android_backup_RestoreHelperBase),
};

/*
Loading