Merge "Clean up temp AtomicFile files" into rvc-dev am: d97051473d am: 32774d472d am: 34fe7cbafc

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11884298

Change-Id: I84b1dc4969dda389389f259d76e36f10e0e45d2b
This commit is contained in:
Julia Reynolds
2020-06-18 16:17:48 +00:00
committed by Automerger Merge Worker
2 changed files with 28 additions and 19 deletions

View File

@@ -65,6 +65,7 @@ public class NotificationHistoryDatabase {
private static final int HISTORY_RETENTION_DAYS = 1; private static final int HISTORY_RETENTION_DAYS = 1;
private static final int HISTORY_RETENTION_MS = 24 * 60 * 60 * 1000; private static final int HISTORY_RETENTION_MS = 24 * 60 * 60 * 1000;
private static final long WRITE_BUFFER_INTERVAL_MS = 1000 * 60 * 20; private static final long WRITE_BUFFER_INTERVAL_MS = 1000 * 60 * 20;
private static final long INVALID_FILE_TIME_MS = -1;
private static final String ACTION_HISTORY_DELETION = private static final String ACTION_HISTORY_DELETION =
NotificationHistoryDatabase.class.getSimpleName() + ".CLEANUP"; NotificationHistoryDatabase.class.getSimpleName() + ".CLEANUP";
@@ -130,8 +131,8 @@ public class NotificationHistoryDatabase {
} }
// Sort with newest files first // Sort with newest files first
Arrays.sort(files, (lhs, rhs) -> Long.compare(Long.parseLong(rhs.getName()), Arrays.sort(files, (lhs, rhs) -> Long.compare(safeParseLong(rhs.getName()),
Long.parseLong(lhs.getName()))); safeParseLong(lhs.getName())));
for (File file : files) { for (File file : files) {
mHistoryFiles.addLast(new AtomicFile(file)); mHistoryFiles.addLast(new AtomicFile(file));
@@ -252,22 +253,19 @@ public class NotificationHistoryDatabase {
for (int i = mHistoryFiles.size() - 1; i >= 0; i--) { for (int i = mHistoryFiles.size() - 1; i >= 0; i--) {
final AtomicFile currentOldestFile = mHistoryFiles.get(i); final AtomicFile currentOldestFile = mHistoryFiles.get(i);
try { final long creationTime = safeParseLong(
final long creationTime = Long.parseLong( currentOldestFile.getBaseFile().getName());
currentOldestFile.getBaseFile().getName()); if (DEBUG) {
if (DEBUG) { Slog.d(TAG, "File " + currentOldestFile.getBaseFile().getName()
Slog.d(TAG, "File " + currentOldestFile.getBaseFile().getName() + " created on " + creationTime);
+ " created on " + creationTime); }
}
if (creationTime <= retentionBoundary.getTimeInMillis()) { if (creationTime <= retentionBoundary.getTimeInMillis()) {
deleteFile(currentOldestFile);
} else {
// all remaining files are newer than the cut off; schedule jobs to delete
scheduleDeletion(
currentOldestFile.getBaseFile(), creationTime, retentionDays);
}
} catch (NumberFormatException e) {
deleteFile(currentOldestFile); deleteFile(currentOldestFile);
} else {
// all remaining files are newer than the cut off; schedule jobs to delete
scheduleDeletion(
currentOldestFile.getBaseFile(), creationTime, retentionDays);
} }
} }
} }
@@ -331,6 +329,17 @@ public class NotificationHistoryDatabase {
} }
} }
private static long safeParseLong(String fileName) {
// AtomicFile will create copies of the numeric files with ".new" and ".bak"
// over the course of its processing. If these files still exist on boot we need to clean
// them up
try {
return Long.parseLong(fileName);
} catch (NumberFormatException e) {
return INVALID_FILE_TIME_MS;
}
}
private final BroadcastReceiver mFileCleaupReceiver = new BroadcastReceiver() { private final BroadcastReceiver mFileCleaupReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {

View File

@@ -149,7 +149,7 @@ public class NotificationHistoryDatabaseTest extends UiServiceTestCase {
} }
@Test @Test
public void testPrune_badFileName() { public void testPrune_badFileName_noCrash() {
GregorianCalendar cal = new GregorianCalendar(); GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(10); cal.setTimeInMillis(10);
int retainDays = 1; int retainDays = 1;
@@ -159,7 +159,7 @@ public class NotificationHistoryDatabaseTest extends UiServiceTestCase {
// add 5 files with a creation date of "today", but the file names are bad // add 5 files with a creation date of "today", but the file names are bad
for (long i = cal.getTimeInMillis(); i >= 5; i--) { for (long i = cal.getTimeInMillis(); i >= 5; i--) {
File file = mock(File.class); File file = mock(File.class);
when(file.getName()).thenReturn(i + ".txt"); when(file.getName()).thenReturn(i + ".bak");
AtomicFile af = new AtomicFile(file); AtomicFile af = new AtomicFile(file);
mDataBase.mHistoryFiles.addLast(af); mDataBase.mHistoryFiles.addLast(af);
} }