summaryrefslogtreecommitdiffstats
path: root/src/com/android/providers/contacts/debug/DataExporter.java
blob: 6771c1a8c57304ab1ff3a226d77c8f4f1a0599de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2012 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 com.android.providers.contacts.debug;

import com.android.providers.contacts.util.Hex;
import com.google.common.io.Closeables;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.SecureRandom;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Compress all files under the app data dir into a single zip file.
 *
 * Make sure not to output dump filenames anywhere, including logcat.
 */
public class DataExporter {
    private static String TAG = "DataExporter";

    public static final String ZIP_MIME_TYPE = "application/zip";

    public static final String DUMP_FILE_DIRECTORY_NAME = "dumpedfiles";

    public static final String OUT_FILE_SUFFIX = "-contacts-db.zip";

    /**
     * Compress all files under the app data dir into a single zip file, and return the content://
     * URI to the file, which can be read via {@link DumpFileProvider}.
     */
    public static Uri exportData(Context context) throws IOException {
        final String fileName = generateRandomName() + OUT_FILE_SUFFIX;
        final File outFile = getOutputFile(context, fileName);

        // Remove all existing ones.
        removeDumpFiles(context);

        Log.i(TAG, "Dump started...");

        ensureOutputDirectory(context);
        final ZipOutputStream os = new ZipOutputStream(new FileOutputStream(outFile));
        try {
            addDirectory(context, os, context.getFilesDir().getParentFile(), "contacts-files");
        } finally {
            Closeables.closeQuietly(os);
        }
        Log.i(TAG, "Dump finished.");
        return DumpFileProvider.AUTHORITY_URI.buildUpon().appendPath(fileName).build();
    }

    /** @return long random string for a file name */
    private static String generateRandomName() {
        final SecureRandom rng = new SecureRandom();
        final byte[] random = new byte[256 / 8];
        rng.nextBytes(random);

        return Hex.encodeHex(random, true);
    }

    private static File getOutputDirectory(Context context) {
        return new File(context.getCacheDir(), DUMP_FILE_DIRECTORY_NAME);
    }

    private static void ensureOutputDirectory(Context context) {
        final File directory = getOutputDirectory(context);
        if (!directory.exists()) {
            directory.mkdir();
        }
    }

    public static File getOutputFile(Context context, String fileName) {
        return new File(getOutputDirectory(context), fileName);
    }

    public static boolean dumpFileExists(Context context) {
        return getOutputDirectory(context).exists();
    }

    public static void removeDumpFiles(Context context) {
        removeFileOrDirectory(getOutputDirectory(context));
    }

    private static void removeFileOrDirectory(File file) {
        if (!file.exists()) return;

        if (file.isFile()) {
            Log.i(TAG, "Removing " + file);
            file.delete();
            return;
        }

        if (file.isDirectory()) {
            for (File child : file.listFiles()) {
                removeFileOrDirectory(child);
            }
            Log.i(TAG, "Removing " + file);
            file.delete();
        }
    }

    /**
     * Add all files under {@code current} to {@code os} zip stream
     */
    private static void addDirectory(Context context, ZipOutputStream os, File current,
            String storedPath) throws IOException {
        for (File child : current.listFiles()) {
            final String childStoredPath = storedPath + "/" + child.getName();

            if (child.isDirectory()) {
                // Don't need the cache directory, which also contains the dump files.
                if (child.equals(context.getCacheDir())) {
                    continue;
                }
                // This check is redundant as the output directory should be in the cache dir,
                // but just in case...
                if (child.getName().equals(DUMP_FILE_DIRECTORY_NAME)) {
                    continue;
                }
                addDirectory(context, os, child, childStoredPath);
            } else if (child.isFile()) {
                addFile(os, child, childStoredPath);
            } else {
                // Shouldn't happen; skip.
            }
        }
    }

    /**
     * Add a single file {@code current} to {@code os} zip stream using the file name
     * {@code storedPath}.
     */
    private static void addFile(ZipOutputStream os, File current, String storedPath)
            throws IOException {
        Log.i(TAG, "Adding " + current.getAbsolutePath() + " ...");
        final InputStream is = new FileInputStream(current);
        os.putNextEntry(new ZipEntry(storedPath));

        final byte[] buf = new byte[32 * 1024];
        int totalLen = 0;
        while (true) {
            int len = is.read(buf);
            if (len <= 0) {
                break;
            }
            os.write(buf, 0, len);
            totalLen += len;
        }
        os.closeEntry();
        Log.i(TAG, "Added " + current.getAbsolutePath() + " as " + storedPath +
                " (" + totalLen + " bytes)");
    }
}