1/* 2 * Copyright (C) 2017 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 */ 16package com.android.server.pm; 17 18import android.util.Slog; 19 20import com.android.internal.util.ArrayUtils; 21 22import java.io.BufferedOutputStream; 23import java.io.BufferedReader; 24import java.io.File; 25import java.io.FileInputStream; 26import java.io.FileOutputStream; 27import java.io.IOException; 28import java.io.InputStreamReader; 29import java.io.PrintWriter; 30import java.nio.ByteBuffer; 31import java.nio.charset.StandardCharsets; 32import java.util.Arrays; 33import java.util.Comparator; 34import java.util.function.Consumer; 35 36public class ShortcutDumpFiles { 37 private static final String TAG = ShortcutService.TAG; 38 private static final boolean DEBUG = ShortcutService.DEBUG; 39 private final ShortcutService mService; 40 41 public ShortcutDumpFiles(ShortcutService service) { 42 mService = service; 43 } 44 45 public boolean save(String filename, Consumer<PrintWriter> dumper) { 46 try { 47 final File directory = mService.getDumpPath(); 48 directory.mkdirs(); 49 if (!directory.exists()) { 50 Slog.e(TAG, "Failed to create directory: " + directory); 51 return false; 52 } 53 54 final File path = new File(directory, filename); 55 56 if (DEBUG) { 57 Slog.d(TAG, "Dumping to " + path); 58 } 59 60 try (PrintWriter pw = new PrintWriter(new BufferedOutputStream( 61 new FileOutputStream(path)))) { 62 dumper.accept(pw); 63 } 64 return true; 65 } catch (RuntimeException|IOException e) { 66 Slog.w(TAG, "Failed to create dump file: " + filename, e); 67 return false; 68 } 69 } 70 71 public boolean save(String filename, byte[] utf8bytes) { 72 return save(filename, pw -> pw.println(StandardCharsets.UTF_8.decode( 73 ByteBuffer.wrap(utf8bytes)).toString())); 74 } 75 76 public void dumpAll(PrintWriter pw) { 77 try { 78 final File directory = mService.getDumpPath(); 79 final File[] files = directory.listFiles(f -> f.isFile()); 80 if (!directory.exists() || ArrayUtils.isEmpty(files)) { 81 pw.print(" No dump files found."); 82 return; 83 } 84 Arrays.sort(files, Comparator.comparing(f -> f.getName())); 85 86 for (File path : files) { 87 pw.print("*** Dumping: "); 88 pw.println(path.getName()); 89 90 pw.print("mtime: "); 91 pw.println(ShortcutService.formatTime(path.lastModified())); 92 93 try (BufferedReader reader = new BufferedReader(new InputStreamReader( 94 new FileInputStream(path)))) { 95 String line = null; 96 while ((line = reader.readLine()) != null) { 97 pw.println(line); 98 } 99 } 100 } 101 } catch (RuntimeException|IOException e) { 102 Slog.w(TAG, "Failed to print dump files", e); 103 } 104 } 105} 106