| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.backup; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.Context; |
| 21 |
import android.os.ParcelFileDescriptor; |
| 22 |
import android.util.Log; |
| 23 |
|
| 24 |
import java.io.File; |
| 25 |
import java.io.FileDescriptor; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class FileBackupHelperBase { |
| 31 |
private static final String TAG = "FileBackupHelperBase"; |
| 32 |
|
| 33 |
long mPtr; |
| 34 |
Context mContext; |
| 35 |
boolean mExceptionLogged; |
| 36 |
|
| 37 |
FileBackupHelperBase(Context context) { |
| 38 |
mPtr = ctor(); |
| 39 |
mContext = context; |
| 40 |
} |
| 41 |
|
| 42 |
protected void finalize() throws Throwable { |
| 43 |
try { |
| 44 |
dtor(mPtr); |
| 45 |
} finally { |
| 46 |
super.finalize(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void performBackup_checked(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 55 |
ParcelFileDescriptor newState, String[] files, String[] keys) { |
| 56 |
if (files.length == 0) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
for (String f: files) { |
| 61 |
if (f.charAt(0) != '/') { |
| 62 |
throw new RuntimeException("files must have all absolute paths: " + f); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if (files.length != keys.length) { |
| 67 |
throw new RuntimeException("files.length=" + files.length |
| 68 |
+ " keys.length=" + keys.length); |
| 69 |
} |
| 70 |
|
| 71 |
FileDescriptor oldStateFd = oldState != null ? oldState.getFileDescriptor() : null; |
| 72 |
FileDescriptor newStateFd = newState.getFileDescriptor(); |
| 73 |
if (newStateFd == null) { |
| 74 |
throw new NullPointerException(); |
| 75 |
} |
| 76 |
|
| 77 |
int err = performBackup_native(oldStateFd, data.mBackupWriter, newStateFd, files, keys); |
| 78 |
|
| 79 |
if (err != 0) { |
| 80 |
|
| 81 |
throw new RuntimeException("Backup failed 0x" + Integer.toHexString(err)); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
boolean writeFile(File f, BackupDataInputStream in) { |
| 86 |
int result = -1; |
| 87 |
|
| 88 |
|
| 89 |
File parent = f.getParentFile(); |
| 90 |
parent.mkdirs(); |
| 91 |
|
| 92 |
result = writeFile_native(mPtr, f.getAbsolutePath(), in.mData.mBackupReader); |
| 93 |
if (result != 0) { |
| 94 |
|
| 95 |
if (!mExceptionLogged) { |
| 96 |
Log.e(TAG, "Failed restoring file '" + f + "' for app '" |
| 97 |
+ mContext.getPackageName() + "\' result=0x" |
| 98 |
+ Integer.toHexString(result)); |
| 99 |
mExceptionLogged = true; |
| 100 |
} |
| 101 |
} |
| 102 |
return (result == 0); |
| 103 |
} |
| 104 |
|
| 105 |
@UnsupportedAppUsage |
| 106 |
public void writeNewStateDescription(ParcelFileDescriptor fd) { |
| 107 |
int result = writeSnapshot_native(mPtr, fd.getFileDescriptor()); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
boolean isKeyInList(String key, String[] list) { |
| 112 |
for (String s: list) { |
| 113 |
if (s.equals(key)) { |
| 114 |
return true; |
| 115 |
} |
| 116 |
} |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
private static native long ctor(); |
| 121 |
private static native void dtor(long ptr); |
| 122 |
|
| 123 |
native private static int performBackup_native(FileDescriptor oldState, |
| 124 |
long data, FileDescriptor newState, String[] files, String[] keys); |
| 125 |
private static native int writeFile_native(long ptr, String filename, long backupReader); |
| 126 |
private static native int writeSnapshot_native(long ptr, FileDescriptor fd); |
| 127 |
} |
| 128 |
|
| 129 |
|