1 /* 2 * Copyright (C) 2019 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 package com.google.android.car.bugreport; 17 18 import android.content.Context; 19 20 import java.io.File; 21 22 /** 23 * File utilities. 24 * 25 * Thread safety and file operations: All file operations should happen on the same worker 26 * thread for thread safety. This is provided by running both bugreport service and file upload 27 * service on a asynctask. Asynctasks are by default executed on the same worker thread in serial. 28 * 29 * There is one exception to the rule above: 30 * Voice recorder works on main thread, however this is not a thread safety problem because: 31 * i. voice recorder always works before starting to collect rest of the bugreport 32 * ii. a bug report cannot be moved to upload (pending) directory before it is completely 33 * collected. 34 */ 35 public class FileUtils { 36 private static final String PREFIX = "bugreport-"; 37 // bug reports waiting to be uploaded 38 private static final String PENDING_DIR = "bug_reports_pending"; 39 // temporary directory, used for zipping files 40 private static final String TEMP_DIR = "bug_reports_temp"; 41 42 private static final String FS = "@"; 43 getPendingDir(Context context)44 private static File getPendingDir(Context context) { 45 File dir = new File(context.getDataDir(), PENDING_DIR); 46 dir.mkdirs(); 47 return dir; 48 } 49 50 /** 51 * Creates and returns file directory for storing bug report files before they are zipped into 52 * a single file. 53 */ createTempDir(Context context, String timestamp)54 static File createTempDir(Context context, String timestamp) { 55 File dir = getTempDir(context, timestamp); 56 dir.mkdirs(); 57 return dir; 58 } 59 60 /** 61 * Returns path to the directory for storing bug report files before they are zipped into a 62 * single file. 63 */ getTempDir(Context context, String timestamp)64 static File getTempDir(Context context, String timestamp) { 65 return new File(context.getDataDir(), TEMP_DIR + "/" + timestamp); 66 } 67 68 /** 69 * Returns zip file directory with the given timestamp and ldap 70 */ getZipFile(Context context, String timestamp, String ldap)71 static File getZipFile(Context context, String timestamp, String ldap) { 72 File zipdir = getPendingDir(context); 73 return new File(zipdir, PREFIX + ldap + FS + timestamp + ".zip"); 74 } 75 76 /** 77 * Returns a {@link File} object pointing to a path in a temp directory under current users 78 * {@link Context#getDataDir}. 79 * 80 * @param context - an application context. 81 * @param timestamp - generates file for this timestamp 82 * @param suffix - a filename suffix. 83 * @return A file. 84 */ getFileWithSuffix(Context context, String timestamp, String suffix)85 static File getFileWithSuffix(Context context, String timestamp, String suffix) { 86 return new File(createTempDir(context, timestamp), timestamp + suffix); 87 } 88 89 /** 90 * Returns a {@link File} object pointing to a path in a temp directory under current users 91 * {@link Context#getDataDir}. 92 * 93 * @param context - an application context. 94 * @param timestamp - generates file for this timestamp. 95 * @param name - a filename 96 * @return A file. 97 */ getFile(Context context, String timestamp, String name)98 static File getFile(Context context, String timestamp, String name) { 99 return new File(getTempDir(context, timestamp), name); 100 } 101 102 103 /** 104 * Deletes a directory and its contents recursively 105 * 106 * @param directory to delete 107 */ deleteDirectory(File directory)108 static void deleteDirectory(File directory) { 109 File[] files = directory.listFiles(); 110 if (files != null) { 111 for (File file : files) { 112 if (!file.isDirectory()) { 113 file.delete(); 114 } else { 115 deleteDirectory(file); 116 } 117 } 118 } 119 directory.delete(); 120 } 121 } 122