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

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

FileRestoreHelper and RestoreHelperDispatcher work.

parent 2cf3971e
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.backup;

import android.util.Log;

import java.io.InputStream;
import java.io.IOException;

+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.backup;

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

import java.io.File;

/** @hide */
public class FileRestoreHelper extends RestoreHelperBase implements RestoreHelper {
    private static final String TAG = "FileRestoreHelper";

    File mFilesDir;

    public FileRestoreHelper(Context context) {
        super(context);
        mFilesDir = context.getFilesDir();
    }

    public void restoreEntity(BackupDataInputStream data) {
        Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size()); // TODO: turn this off before ship
        File f = new File(mFilesDir, data.getKey());
        writeFile(f, data);
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -26,6 +26,6 @@ public interface RestoreHelper {
     * Do not close the <code>data</code> stream.  Do not read more than
     * <code>dataSize</code> bytes from <code>data</code>.
     */
    public void performRestore(BackupDataInputStream data);
    public void restoreEntity(BackupDataInputStream data);
}
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.backup;

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

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

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

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

    void writeFile(File f, InputStream in) {
        boolean success = false;
        FileOutputStream out = null;
        try {
            // Create the enclosing directory.
            File parent = f.getParentFile();
            parent.mkdirs();

            // 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;
            }

            // TODO: Set the permissions of the file.

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

+2 −2
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import java.util.HashMap;

/** @hide */
public class RestoreHelperDispatcher {
    HashMap<String,RestoreHelper> mHelpers;
    HashMap<String,RestoreHelper> mHelpers = new HashMap<String,RestoreHelper>();

    public void addHelper(String keyPrefix, RestoreHelper helper) {
        mHelpers.put(keyPrefix, helper);
@@ -38,7 +38,7 @@ public class RestoreHelperDispatcher {
                if (helper != null) {
                    stream.dataSize = input.getDataSize();
                    stream.key = rawKey.substring(pos+1);
                    helper.performRestore(stream);
                    helper.restoreEntity(stream);
                }
            }
            input.skipEntityData(); // In case they didn't consume the data.
Loading