blob: cb0737e3ad724cfab8ddd2f792f4e79c9ff2701e [file] [log] [blame]
Christopher Tate4a627c72011-04-01 14:43:32 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app.backup;
18
Christopher Tate79ec80d2011-06-24 14:58:49 -070019import android.content.Context;
20import android.content.pm.ApplicationInfo;
21import android.content.pm.PackageManager;
Christopher Tate75a99702011-05-18 16:28:19 -070022import android.os.ParcelFileDescriptor;
23import android.util.Log;
24
25import java.io.File;
26import java.io.FileInputStream;
27import java.io.FileOutputStream;
28import java.io.IOException;
29
30import libcore.io.ErrnoException;
31import libcore.io.Libcore;
32
Christopher Tate4a627c72011-04-01 14:43:32 -070033/**
34 * Global constant definitions et cetera related to the full-backup-to-fd
Christopher Tate79ec80d2011-06-24 14:58:49 -070035 * binary format. Nothing in this namespace is part of any API; it's all
36 * hidden details of the current implementation gathered into one location.
Christopher Tate4a627c72011-04-01 14:43:32 -070037 *
38 * @hide
39 */
40public class FullBackup {
Christopher Tate75a99702011-05-18 16:28:19 -070041 static final String TAG = "FullBackup";
Christopher Tate4a627c72011-04-01 14:43:32 -070042
Christopher Tate75a99702011-05-18 16:28:19 -070043 public static final String APK_TREE_TOKEN = "a";
44 public static final String OBB_TREE_TOKEN = "obb";
45 public static final String ROOT_TREE_TOKEN = "r";
46 public static final String DATA_TREE_TOKEN = "f";
47 public static final String DATABASE_TREE_TOKEN = "db";
48 public static final String SHAREDPREFS_TREE_TOKEN = "sp";
Christopher Tate416c39e2013-02-14 16:55:46 -080049 public static final String MANAGED_EXTERNAL_TREE_TOKEN = "ef";
Christopher Tate75a99702011-05-18 16:28:19 -070050 public static final String CACHE_TREE_TOKEN = "c";
51 public static final String SHARED_STORAGE_TOKEN = "shared";
52
53 public static final String APPS_PREFIX = "apps/";
Christopher Tateb0628bf2011-06-02 15:08:13 -070054 public static final String SHARED_PREFIX = SHARED_STORAGE_TOKEN + "/";
Christopher Tate75a99702011-05-18 16:28:19 -070055
56 public static final String FULL_BACKUP_INTENT_ACTION = "fullback";
57 public static final String FULL_RESTORE_INTENT_ACTION = "fullrest";
58 public static final String CONF_TOKEN_INTENT_EXTRA = "conftoken";
59
Christopher Tate79ec80d2011-06-24 14:58:49 -070060 /**
61 * @hide
62 */
Christopher Tate4a627c72011-04-01 14:43:32 -070063 static public native int backupToTar(String packageName, String domain,
64 String linkdomain, String rootpath, String path, BackupDataOutput output);
Christopher Tate75a99702011-05-18 16:28:19 -070065
Christopher Tate79ec80d2011-06-24 14:58:49 -070066 /**
67 * Copy data from a socket to the given File location on permanent storage. The
Christopher Tatef6d6fa82012-09-26 15:25:59 -070068 * modification time and access mode of the resulting file will be set if desired,
69 * although group/all rwx modes will be stripped: the restored file will not be
70 * accessible from outside the target application even if the original file was.
Christopher Tate79ec80d2011-06-24 14:58:49 -070071 * If the {@code type} parameter indicates that the result should be a directory,
72 * the socket parameter may be {@code null}; even if it is valid, no data will be
73 * read from it in this case.
74 * <p>
75 * If the {@code mode} argument is negative, then the resulting output file will not
76 * have its access mode or last modification time reset as part of this operation.
77 *
78 * @param data Socket supplying the data to be copied to the output file. If the
79 * output is a directory, this may be {@code null}.
80 * @param size Number of bytes of data to copy from the socket to the file. At least
81 * this much data must be available through the {@code data} parameter.
82 * @param type Must be either {@link BackupAgent#TYPE_FILE} for ordinary file data
83 * or {@link BackupAgent#TYPE_DIRECTORY} for a directory.
84 * @param mode Unix-style file mode (as used by the chmod(2) syscall) to be set on
Christopher Tatef6d6fa82012-09-26 15:25:59 -070085 * the output file or directory. group/all rwx modes are stripped even if set
86 * in this parameter. If this parameter is negative then neither
87 * the mode nor the mtime values will be applied to the restored file.
Christopher Tate79ec80d2011-06-24 14:58:49 -070088 * @param mtime A timestamp in the standard Unix epoch that will be imposed as the
89 * last modification time of the output file. if the {@code mode} parameter is
90 * negative then this parameter will be ignored.
91 * @param outFile Location within the filesystem to place the data. This must point
Christopher Tate46cc43c2013-02-19 14:08:59 -080092 * to a location that is writeable by the caller, preferably using an absolute path.
Christopher Tate79ec80d2011-06-24 14:58:49 -070093 * @throws IOException
94 */
95 static public void restoreFile(ParcelFileDescriptor data,
96 long size, int type, long mode, long mtime, File outFile) throws IOException {
97 if (type == BackupAgent.TYPE_DIRECTORY) {
Christopher Tate75a99702011-05-18 16:28:19 -070098 // Canonically a directory has no associated content, so we don't need to read
99 // anything from the pipe in this case. Just create the directory here and
100 // drop down to the final metadata adjustment.
101 if (outFile != null) outFile.mkdirs();
102 } else {
103 FileOutputStream out = null;
104
105 // Pull the data from the pipe, copying it to the output file, until we're done
106 try {
107 if (outFile != null) {
108 File parent = outFile.getParentFile();
109 if (!parent.exists()) {
110 // in practice this will only be for the default semantic directories,
111 // and using the default mode for those is appropriate.
Christopher Tate75a99702011-05-18 16:28:19 -0700112 parent.mkdirs();
113 }
114 out = new FileOutputStream(outFile);
115 }
116 } catch (IOException e) {
117 Log.e(TAG, "Unable to create/open file " + outFile.getPath(), e);
118 }
119
120 byte[] buffer = new byte[32 * 1024];
121 final long origSize = size;
122 FileInputStream in = new FileInputStream(data.getFileDescriptor());
123 while (size > 0) {
124 int toRead = (size > buffer.length) ? buffer.length : (int)size;
125 int got = in.read(buffer, 0, toRead);
126 if (got <= 0) {
127 Log.w(TAG, "Incomplete read: expected " + size + " but got "
128 + (origSize - size));
129 break;
130 }
131 if (out != null) {
132 try {
133 out.write(buffer, 0, got);
134 } catch (IOException e) {
135 // Problem writing to the file. Quit copying data and delete
136 // the file, but of course keep consuming the input stream.
137 Log.e(TAG, "Unable to write to file " + outFile.getPath(), e);
138 out.close();
139 out = null;
140 outFile.delete();
141 }
142 }
143 size -= got;
144 }
145 if (out != null) out.close();
146 }
147
148 // Now twiddle the state to match the backup, assuming all went well
Christopher Tate79ec80d2011-06-24 14:58:49 -0700149 if (mode >= 0 && outFile != null) {
Christopher Tate75a99702011-05-18 16:28:19 -0700150 try {
Christopher Tatef6d6fa82012-09-26 15:25:59 -0700151 // explicitly prevent emplacement of files accessible by outside apps
152 mode &= 0700;
Christopher Tate75a99702011-05-18 16:28:19 -0700153 Libcore.os.chmod(outFile.getPath(), (int)mode);
154 } catch (ErrnoException e) {
155 e.rethrowAsIOException();
156 }
157 outFile.setLastModified(mtime);
158 }
159 }
Christopher Tate4a627c72011-04-01 14:43:32 -0700160}