From 28dfe970daba771dd2379aab01560f6ff03bbd79 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 15 Aug 2016 17:14:17 +0200 Subject: [PATCH] use access time to remove old cache files >= android-21 In android-21, they exposed the formerly internal method for getting stat structs of files. From that, we can get the last access time, which is a much better way to determine which files to delete rather than last modified time. closes #644 --- .../org/fdroid/fdroid/installer/ApkCache.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/installer/ApkCache.java b/app/src/main/java/org/fdroid/fdroid/installer/ApkCache.java index ce627283b..587c9c87d 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/ApkCache.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/ApkCache.java @@ -141,12 +141,15 @@ public class ApkCache { } /** - * Recursively delete files in {@code dir} that were last modified - * {@code secondsAgo} seconds ago, e.g. when it was downloaded. + * Recursively delete files in {@code dir} that were last used + * {@code secondsAgo} seconds ago. On {@code android-21} and newer, this + * is based on the last access of the file, on older Android versions, it is + * based on the last time the file was modified, e.g. downloaded. * * @param dir The directory to recurse in * @param secondsAgo The number of seconds old that marks a file for deletion. */ + @TargetApi(21) public static void clearOldFiles(File dir, long secondsAgo) { if (dir == null) { return; @@ -161,8 +164,19 @@ public class ApkCache { clearOldFiles(f, olderThan); f.delete(); } - if (FileUtils.isFileOlder(f, olderThan)) { - f.delete(); + if (Build.VERSION.SDK_INT < 21) { + if (FileUtils.isFileOlder(f, olderThan)) { + f.delete(); + } + } else { + try { + StructStat stat = Os.lstat(f.getAbsolutePath()); + if (stat.st_atime < olderThan) { + f.delete(); + } + } catch (ErrnoException e) { + e.printStackTrace(); + } } } }