1 /*
2  * Copyright (C) 2012 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 
17 package com.android.providers.contacts.debug;
18 
19 import com.android.providers.contacts.util.Hex;
20 
21 import android.content.Context;
22 import android.net.Uri;
23 import android.util.Log;
24 
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.security.SecureRandom;
31 import java.util.zip.Deflater;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipOutputStream;
34 
35 /**
36  * Compress all files under the app data dir into a single zip file.
37  *
38  * Make sure not to output dump filenames anywhere, including logcat.
39  */
40 public class DataExporter {
41     private static String TAG = "DataExporter";
42 
43     public static final String ZIP_MIME_TYPE = "application/zip";
44 
45     public static final String DUMP_FILE_DIRECTORY_NAME = "dumpedfiles";
46 
47     public static final String OUT_FILE_SUFFIX = "-contacts-db.zip";
48     public static final String VALID_FILE_NAME_REGEX = "[0-9A-Fa-f]+-contacts-db\\.zip";
49 
50     /**
51      * Compress all files under the app data dir into a single zip file, and return the content://
52      * URI to the file, which can be read via {@link DumpFileProvider}.
53      */
exportData(Context context)54     public static Uri exportData(Context context) throws IOException {
55         final String fileName = generateRandomName() + OUT_FILE_SUFFIX;
56         final File outFile = getOutputFile(context, fileName);
57 
58         // Remove all existing ones.
59         removeDumpFiles(context);
60 
61         Log.i(TAG, "Dump started...");
62 
63         ensureOutputDirectory(context);
64 
65         try (ZipOutputStream os = new ZipOutputStream(new FileOutputStream(outFile))) {
66             os.setLevel(Deflater.BEST_COMPRESSION);
67             addDirectory(context, os, context.getFilesDir().getParentFile(), "contacts-files");
68         }
69         Log.i(TAG, "Dump finished.");
70         return DumpFileProvider.AUTHORITY_URI.buildUpon().appendPath(fileName).build();
71     }
72 
73     /** @return long random string for a file name */
generateRandomName()74     private static String generateRandomName() {
75         final SecureRandom rng = new SecureRandom();
76         final byte[] random = new byte[256 / 8];
77         rng.nextBytes(random);
78 
79         return Hex.encodeHex(random, true);
80     }
81 
ensureValidFileName(String fileName)82     public static void ensureValidFileName(String fileName) {
83         // Do not allow queries to use relative paths to leave the root directory. Otherwise they
84         // can gain access to other files such as the contacts database.
85         if (fileName.contains("..")) {
86             throw new IllegalArgumentException(".. path specifier not allowed. Bad file name: " +
87                     fileName);
88         }
89         // White list dump files.
90         if (!fileName.matches(VALID_FILE_NAME_REGEX)) {
91             throw new IllegalArgumentException("Only " + VALID_FILE_NAME_REGEX +
92                     " files are supported. Bad file name: " + fileName);
93         }
94     }
95 
getOutputDirectory(Context context)96     private static File getOutputDirectory(Context context) {
97         return new File(context.getCacheDir(), DUMP_FILE_DIRECTORY_NAME);
98     }
99 
ensureOutputDirectory(Context context)100     private static void ensureOutputDirectory(Context context) {
101         final File directory = getOutputDirectory(context);
102         if (!directory.exists()) {
103             directory.mkdir();
104         }
105     }
106 
getOutputFile(Context context, String fileName)107     public static File getOutputFile(Context context, String fileName) {
108         return new File(getOutputDirectory(context), fileName);
109     }
110 
dumpFileExists(Context context)111     public static boolean dumpFileExists(Context context) {
112         return getOutputDirectory(context).exists();
113     }
114 
removeDumpFiles(Context context)115     public static void removeDumpFiles(Context context) {
116         removeFileOrDirectory(getOutputDirectory(context));
117     }
118 
removeFileOrDirectory(File file)119     private static void removeFileOrDirectory(File file) {
120         if (!file.exists()) return;
121 
122         if (file.isFile()) {
123             Log.i(TAG, "Removing " + file);
124             file.delete();
125             return;
126         }
127 
128         if (file.isDirectory()) {
129             for (File child : file.listFiles()) {
130                 removeFileOrDirectory(child);
131             }
132             Log.i(TAG, "Removing " + file);
133             file.delete();
134         }
135     }
136 
137     /**
138      * Add all files under {@code current} to {@code os} zip stream
139      */
addDirectory(Context context, ZipOutputStream os, File current, String storedPath)140     private static void addDirectory(Context context, ZipOutputStream os, File current,
141             String storedPath) throws IOException {
142         for (File child : current.listFiles()) {
143             final String childStoredPath = storedPath + "/" + child.getName();
144 
145             if (child.isDirectory()) {
146                 // Don't need the cache directory, which also contains the dump files.
147                 if (child.equals(context.getCacheDir())) {
148                     continue;
149                 }
150                 // This check is redundant as the output directory should be in the cache dir,
151                 // but just in case...
152                 if (child.getName().equals(DUMP_FILE_DIRECTORY_NAME)) {
153                     continue;
154                 }
155                 addDirectory(context, os, child, childStoredPath);
156             } else if (child.isFile()) {
157                 addFile(os, child, childStoredPath);
158             } else {
159                 // Shouldn't happen; skip.
160             }
161         }
162     }
163 
164     /**
165      * Add a single file {@code current} to {@code os} zip stream using the file name
166      * {@code storedPath}.
167      */
addFile(ZipOutputStream os, File current, String storedPath)168     private static void addFile(ZipOutputStream os, File current, String storedPath)
169             throws IOException {
170         Log.i(TAG, "Adding " + current.getAbsolutePath() + " ...");
171         final InputStream is = new FileInputStream(current);
172         os.putNextEntry(new ZipEntry(storedPath));
173 
174         final byte[] buf = new byte[32 * 1024];
175         int totalLen = 0;
176         while (true) {
177             int len = is.read(buf);
178             if (len <= 0) {
179                 break;
180             }
181             os.write(buf, 0, len);
182             totalLen += len;
183         }
184         os.closeEntry();
185         Log.i(TAG, "Added " + current.getAbsolutePath() + " as " + storedPath +
186                 " (" + totalLen + " bytes)");
187     }
188 }
189