From edcdc77956bcb1f02ddd97a95e413f3dc0bc018e Mon Sep 17 00:00:00 2001 From: DerGenaue <9513365-DerGenaue@users.noreply.gitlab.com> Date: Wed, 2 Oct 2024 16:42:05 +0200 Subject: [PATCH] Logs during cache cleaning --- .../java/org/fdroid/fdroid/installer/ApkCache.java | 5 +++++ .../java/org/fdroid/fdroid/work/CleanCacheWorker.java | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 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 2bf9366ff..ad5ab2d42 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/ApkCache.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/ApkCache.java @@ -38,6 +38,7 @@ import java.io.IOException; public class ApkCache { private static final String CACHE_DIR = "apks"; + public static final String TAG = "ApkCache"; public enum ApkCacheState { MISS_OR_PARTIAL, CACHED, CORRUPTED } @@ -100,6 +101,7 @@ public class ApkCache { sanitizedApkFile.delete(); } + Utils.debugLog(TAG, "Copying APK to files temporarily: " + sanitizedApkFile.getAbsolutePath()); FileUtils.copyFile(apkFile, sanitizedApkFile); // verify copied file's hash with expected hash from Apk class @@ -109,6 +111,8 @@ public class ApkCache { } // 20 minutes the start of the install process, delete the file + // If the thread is killed or some issues occur, + // files are still cleaned up periodically by the CleanCacheWorker final File apkToDelete = sanitizedApkFile; new Thread() { @Override @@ -118,6 +122,7 @@ public class ApkCache { Thread.sleep(1200000); } catch (InterruptedException ignored) { } finally { + Utils.debugLog(TAG, "Deleting temporary APK from files: "+ apkToDelete.getAbsolutePath()); FileUtils.deleteQuietly(apkToDelete); } } diff --git a/app/src/main/java/org/fdroid/fdroid/work/CleanCacheWorker.java b/app/src/main/java/org/fdroid/fdroid/work/CleanCacheWorker.java index c4e07ecb0..99514c9ac 100644 --- a/app/src/main/java/org/fdroid/fdroid/work/CleanCacheWorker.java +++ b/app/src/main/java/org/fdroid/fdroid/work/CleanCacheWorker.java @@ -67,7 +67,7 @@ public class CleanCacheWorker extends Worker { .setConstraints(constraintsBuilder.build()) .build(); workManager.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, cleanCache); - Utils.debugLog(TAG, "Scheduled periodic work for cleaning the cache."); + Utils.debugLog(TAG, "Scheduled periodic work for cleaning the cache every " + (interval / (60*60*1000)) + " hours"); } /** @@ -87,13 +87,16 @@ public class CleanCacheWorker extends Worker { public Result doWork() { Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); try { + Utils.debugLog(TAG, "Starting cache cleaning job."); final Context context = getApplicationContext(); deleteExpiredApksFromCache(context); deleteStrayIndexFiles(context); deleteOldInstallerFiles(context); deleteOldIcons(context); + Utils.debugLog(TAG, "Finished cache cleaning job."); return Result.success(); } catch (Exception e) { + Utils.debugLog(TAG, "Failed cache cleaning job."); return Result.failure(); } } @@ -209,8 +212,10 @@ public class CleanCacheWorker extends Worker { } private static void deleteFileAndLog(final File file) { - file.delete(); - Utils.debugLog(TAG, "Deleted file: " + file); + if (file.delete()) { + // Only deletes empty directories + Utils.debugLog(TAG, "Deleted file: " + file); + } } private static class Impl21 {