summaryrefslogtreecommitdiff
path: root/core/java/android/os/FileUtils.java
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2013-03-14 14:11:57 -0700
committerJeff Sharkey <jsharkey@android.com>2013-03-14 15:04:12 -0700
commitd9526907d1a51ef0b35bfbbeee43fa209d8b5bbf (patch)
tree50c702ba28640043ea40c152424f4beff44893fc /core/java/android/os/FileUtils.java
parent67bea2ef32198e5e246715998c1d907b73225761 (diff)
Move deleteOlderFiles() to framework, tests.
Supports both age and count based constraints. Added light docs and tests to verify behavior. Bug: 8387555 Change-Id: If4d0dac8bc54ce705c2a339bc09a60ea748728b8
Diffstat (limited to 'core/java/android/os/FileUtils.java')
-rw-r--r--core/java/android/os/FileUtils.java45
1 files changed, 44 insertions, 1 deletions
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java
index 2bec1c176905..9666d9af8cc4 100644
--- a/core/java/android/os/FileUtils.java
+++ b/core/java/android/os/FileUtils.java
@@ -16,6 +16,8 @@
package android.os;
+import android.util.Log;
+
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -25,6 +27,8 @@ import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Comparator;
import java.util.regex.Pattern;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
@@ -34,6 +38,8 @@ import java.util.zip.CheckedInputStream;
* @hide
*/
public class FileUtils {
+ private static final String TAG = "FileUtils";
+
public static final int S_IRWXU = 00700;
public static final int S_IRUSR = 00400;
public static final int S_IWUSR = 00200;
@@ -161,7 +167,8 @@ public class FileUtils {
} else if (max < 0) { // "tail" mode: keep the last N
int len;
boolean rolled = false;
- byte[] last = null, data = null;
+ byte[] last = null;
+ byte[] data = null;
do {
if (last != null) rolled = true;
byte[] tmp = last; last = data; data = tmp;
@@ -237,4 +244,40 @@ public class FileUtils {
}
}
}
+
+ /**
+ * Delete older files in a directory until only those matching the given
+ * constraints remain.
+ *
+ * @param minCount Always keep at least this many files.
+ * @param minAge Always keep files younger than this age.
+ */
+ public static void deleteOlderFiles(File dir, int minCount, long minAge) {
+ if (minCount < 0 || minAge < 0) {
+ throw new IllegalArgumentException("Constraints must be positive or 0");
+ }
+
+ final File[] files = dir.listFiles();
+ if (files == null) return;
+
+ // Sort with newest files first
+ Arrays.sort(files, new Comparator<File>() {
+ @Override
+ public int compare(File lhs, File rhs) {
+ return (int) (rhs.lastModified() - lhs.lastModified());
+ }
+ });
+
+ // Keep at least minCount files
+ for (int i = minCount; i < files.length; i++) {
+ final File file = files[i];
+
+ // Keep files newer than minAge
+ final long age = System.currentTimeMillis() - file.lastModified();
+ if (age > minAge) {
+ Log.d(TAG, "Deleting old file " + file);
+ file.delete();
+ }
+ }
+ }
}