summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/persistentlog/PersistentLogFileHandler.java
blob: 5f3922265517c9fee278a4cad3f7d55b34f3501a (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (C) 2017 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.dialer.persistentlog;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.AnyThread;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import android.support.v4.os.UserManagerCompat;
import com.android.dialer.common.LogUtil;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Handles serialization of byte arrays and read/write them to multiple rotating files. If a logText
 * file exceeds {@code fileSizeLimit} after a write, a new file will be used. if the total number of
 * files exceeds {@code fileCountLimit} the oldest ones will be deleted. The logs are stored in the
 * cache but the file index is stored in the data (clearing data will also clear the cache). The
 * logs will be stored under /cache_dir/persistent_log/{@code subfolder}, so multiple independent
 * logs can be created.
 *
 * <p>This class is NOT thread safe. All methods expect the constructor must be called on the same
 * worker thread.
 */
final class PersistentLogFileHandler {

  private static final String LOG_DIRECTORY = "persistent_log";
  private static final String NEXT_FILE_INDEX_PREFIX = "persistent_long_next_file_index_";

  private static final byte[] ENTRY_PREFIX = {'P'};
  private static final byte[] ENTRY_POSTFIX = {'L'};

  private static class LogCorruptionException extends Exception {

    public LogCorruptionException(String message) {
      super(message);
    }
  }

  private File logDirectory;
  private final String subfolder;
  private final int fileSizeLimit;
  private final int fileCountLimit;

  private SharedPreferences sharedPreferences;

  private File outputFile;
  private Context context;

  @MainThread
  PersistentLogFileHandler(String subfolder, int fileSizeLimit, int fileCountLimit) {
    this.subfolder = subfolder;
    this.fileSizeLimit = fileSizeLimit;
    this.fileCountLimit = fileCountLimit;
  }

  /** Must be called right after the logger thread is created. */
  @WorkerThread
  void initialize(Context context) {
    this.context = context;
    logDirectory = new File(new File(context.getCacheDir(), LOG_DIRECTORY), subfolder);
    initializeSharedPreference(context);
  }

  @WorkerThread
  private boolean initializeSharedPreference(Context context) {
    if (sharedPreferences == null && UserManagerCompat.isUserUnlocked(context)) {
      sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
      return true;
    }
    return sharedPreferences != null;
  }

  /**
   * Write the list of byte arrays to the current log file, prefixing each entry with its' length. A
   * new file will only be selected when the batch is completed, so the resulting file might be
   * larger then {@code fileSizeLimit}
   */
  @WorkerThread
  void writeLogs(List<byte[]> logs) throws IOException {
    if (outputFile == null) {
      selectNextFileToWrite();
    }
    outputFile.createNewFile();
    try (DataOutputStream outputStream =
        new DataOutputStream(new FileOutputStream(outputFile, true))) {
      for (byte[] log : logs) {
        outputStream.write(ENTRY_PREFIX);
        outputStream.writeInt(log.length);
        outputStream.write(log);
        outputStream.write(ENTRY_POSTFIX);
      }
      outputStream.close();
      if (outputFile.length() > fileSizeLimit) {
        selectNextFileToWrite();
      }
    }
  }

  void writeRawLogsForTest(byte[] data) throws IOException {
    if (outputFile == null) {
      selectNextFileToWrite();
    }
    outputFile.createNewFile();
    try (DataOutputStream outputStream =
        new DataOutputStream(new FileOutputStream(outputFile, true))) {
      outputStream.write(data);
      outputStream.close();
      if (outputFile.length() > fileSizeLimit) {
        selectNextFileToWrite();
      }
    }
  }

  /** Concatenate all log files in chronicle order and return a byte array. */
  @WorkerThread
  @NonNull
  private byte[] readBlob() throws IOException {
    File[] files = getLogFiles();

    ByteBuffer byteBuffer = ByteBuffer.allocate(getTotalSize(files));
    for (File file : files) {
      byteBuffer.put(readAllBytes(file));
    }
    return byteBuffer.array();
  }

  private static int getTotalSize(File[] files) {
    int sum = 0;
    for (File file : files) {
      sum += (int) file.length();
    }
    return sum;
  }

  /** Parses the content of all files back to individual byte arrays. */
  @WorkerThread
  @NonNull
  List<byte[]> getLogs() throws IOException {
    byte[] blob = readBlob();
    List<byte[]> logs = new ArrayList<>();
    try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(blob))) {
      byte[] log = readLog(input);
      while (log != null) {
        logs.add(log);
        log = readLog(input);
      }
    } catch (LogCorruptionException e) {
      LogUtil.e("PersistentLogFileHandler.getLogs", "logs corrupted, deleting", e);
      deleteLogs();
      return new ArrayList<>();
    }
    return logs;
  }

  private void deleteLogs() throws IOException {
    for (File file : getLogFiles()) {
      file.delete();
    }
    selectNextFileToWrite();
  }

  @WorkerThread
  private void selectNextFileToWrite() throws IOException {
    File[] files = getLogFiles();

    if (files.length == 0 || files[files.length - 1].length() > fileSizeLimit) {
      if (files.length >= fileCountLimit) {
        for (int i = 0; i <= files.length - fileCountLimit; i++) {
          files[i].delete();
        }
      }
      outputFile = new File(logDirectory, String.valueOf(getAndIncrementNextFileIndex()));
    } else {
      outputFile = files[files.length - 1];
    }
  }

  @NonNull
  @WorkerThread
  private File[] getLogFiles() {
    logDirectory.mkdirs();
    File[] files = logDirectory.listFiles();
    if (files == null) {
      files = new File[0];
    }
    Arrays.sort(
        files,
        (File lhs, File rhs) ->
            Long.compare(Long.valueOf(lhs.getName()), Long.valueOf(rhs.getName())));
    return files;
  }

  @Nullable
  @WorkerThread
  private byte[] readLog(DataInputStream inputStream) throws IOException, LogCorruptionException {
    try {
      byte[] prefix = new byte[ENTRY_PREFIX.length];
      if (inputStream.read(prefix) == -1) {
        // EOF
        return null;
      }
      if (!Arrays.equals(prefix, ENTRY_PREFIX)) {
        throw new LogCorruptionException("entry prefix mismatch");
      }
      int dataLength = inputStream.readInt();
      if (dataLength > fileSizeLimit) {
        throw new LogCorruptionException("data length over max size");
      }
      byte[] data = new byte[dataLength];
      inputStream.read(data);

      byte[] postfix = new byte[ENTRY_POSTFIX.length];
      inputStream.read(postfix);
      if (!Arrays.equals(postfix, ENTRY_POSTFIX)) {
        throw new LogCorruptionException("entry postfix mismatch");
      }
      return data;
    } catch (EOFException e) {
      return null;
    }
  }

  @NonNull
  @WorkerThread
  private static byte[] readAllBytes(File file) throws IOException {
    byte[] result = new byte[(int) file.length()];
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
      randomAccessFile.readFully(result);
    }
    return result;
  }

  @WorkerThread
  private int getAndIncrementNextFileIndex() throws IOException {
    if (!initializeSharedPreference(context)) {
      throw new IOException("Shared preference is not available");
    }

    int index = sharedPreferences.getInt(getNextFileKey(), 0);
    sharedPreferences.edit().putInt(getNextFileKey(), index + 1).commit();
    return index;
  }

  @AnyThread
  private String getNextFileKey() {
    return NEXT_FILE_INDEX_PREFIX + subfolder;
  }
}