aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tradefed/util/Bugreport.java
blob: 666ad1e57881f6f4dcbf58c98ad96acea2efa77e (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
/*
 * Copyright (C) 2016 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.tradefed.util;

import com.android.tradefed.log.ITestLogger;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.result.FileInputStreamSource;
import com.android.tradefed.result.InputStreamSource;
import com.android.tradefed.result.LogDataType;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Object holding the bugreport files references, compatible of flat bugreport and zipped bugreport
 * (bugreportz).
 */
public class Bugreport implements Closeable {
    private File mBugreport;
    private boolean mIsZipped;

    public Bugreport(File bugreportFile, boolean isZipped) {
        mBugreport = bugreportFile;
        mIsZipped = isZipped;
    }

    /**
     * Return true if it's a zipped bugreport, false otherwise.
     */
    public boolean isZipped() {
        return mIsZipped;
    }

    /**
     * Helper to log the Bugreport whether its zipped or not.
     *
     * @param dataName the name of the data once logged.
     * @param logger a {@link ITestLogger} to receive the log.
     */
    public void log(String dataName, ITestLogger logger) {
        LogDataType type = isZipped() ? LogDataType.BUGREPORTZ : LogDataType.BUGREPORT;
        InputStreamSource source = new FileInputStreamSource(mBugreport);
        logger.testLog(dataName, type, source);
        source.cancel();
    }

    /**
     * Return a {@link File} pointing to the bugreport main file. For a flat bugreport, it returns
     * the flat bugreport itself. For a zipped bugreport, it returns the main entry file.
     * The returned file is a copy and should be appropriately managed by the user.
     */
    public File getMainFile() {
        if (mBugreport == null) {
            return null;
        }
        if (!mIsZipped) {
            return mBugreport;
        } else {
            ZipFile zip;
            File mainEntry = null;
            try {
                zip = new ZipFile(mBugreport);
                // We get the main_entry.txt that contains the bugreport name.
                mainEntry = ZipUtil2.extractFileFromZip(zip, "main_entry.txt");
                if (mainEntry == null) {
                    CLog.w("main_entry.txt was not found inside the bugreport");
                    return null;
                }
                String bugreportName = FileUtil.readStringFromFile(mainEntry).trim();
                CLog.d("bugreport name: '%s'", bugreportName);
                return ZipUtil2.extractFileFromZip(zip, bugreportName);
            } catch (IOException e) {
                CLog.e("Error while unzipping bugreportz");
                CLog.e(e);
            } finally {
                FileUtil.deleteFile(mainEntry);
            }
        }
        return null;
    }

    /**
     * Returns the list of files contained inside the zipped bugreport. Null if it's not a zipped
     * bugreport.
     */
    public List<String> getListOfFiles() {
        if (mBugreport == null) {
            return null;
        }
        List<String> list = new ArrayList<>();
        if (!mIsZipped) {
            return null;
        }
        ZipFile zipBugreport = null;
        try {
            zipBugreport = new ZipFile(mBugreport);
            for (ZipArchiveEntry entry : Collections.list(zipBugreport.getEntries())) {
                list.add(entry.getName());
            }
        } catch (IOException e) {
            CLog.e("Error reading the list of files in the bugreport");
            CLog.e(e);
        } finally {
            ZipUtil2.closeZip(zipBugreport);
        }
        return list;
    }

    /**
     * Return the {@link File} associated with the name in the bugreport. Null if not found or if
     * name is null. Non zipped bugreport always return null.
     * The returned file is a copy and should be appropriately managed by the user.
     */
    public File getFileByName(String name) {
        if (mBugreport == null || name == null) {
            return null;
        }
        if (mIsZipped) {
            return extractFileBugreport(name);
        }
        return null;
    }

    /**
     * Helper to extract and return a file from the zipped bugreport.
     */
    private File extractFileBugreport(String name) {
        ZipFile zip;
        File bugreport = null;
        try {
            zip = new ZipFile(mBugreport);
            bugreport = ZipUtil2.extractFileFromZip(zip, name);
            return bugreport;
        } catch (IOException e) {
            CLog.e("Error while unzipping bugreportz");
            CLog.e(e);
        }
        return null;
    }

    /**
     * Clean up the files held by the bugreport object. Must be called when the object is not used
     * anymore.
     */
    @Override
    public void close() throws IOException {
        FileUtil.deleteFile(mBugreport);
    }
}