From ec667420bdb43c06bdc398a0b502674ef2a132d1 Mon Sep 17 00:00:00 2001 From: Francisco Prieto Date: Wed, 20 Nov 2024 16:02:40 -0300 Subject: [PATCH] Move cleanOldFiles to the NdkServiceRepository (#1698) --- .../internal/ndk/EmbraceNdkService.kt | 80 +------------------ .../ndk/EmbraceNdkServiceRepository.kt | 31 +++++++ .../internal/ndk/NdkServiceRepository.kt | 2 + .../fakes/FakeNdkServiceRepository.kt | 4 + 4 files changed, 38 insertions(+), 79 deletions(-) diff --git a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt index 506df3dbc5..e580ef38e8 100644 --- a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt +++ b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkService.kt @@ -26,7 +26,6 @@ import io.embrace.android.embracesdk.internal.serialization.PlatformSerializer import io.embrace.android.embracesdk.internal.session.id.SessionIdTracker import io.embrace.android.embracesdk.internal.session.lifecycle.ProcessStateListener import io.embrace.android.embracesdk.internal.session.lifecycle.ProcessStateService -import io.embrace.android.embracesdk.internal.storage.NATIVE_CRASH_FILE_FOLDER import io.embrace.android.embracesdk.internal.storage.StorageService import io.embrace.android.embracesdk.internal.utils.Uuid import io.embrace.android.embracesdk.internal.worker.BackgroundWorker @@ -34,10 +33,8 @@ import java.io.BufferedReader import java.io.File import java.io.FileInputStream import java.io.FileNotFoundException -import java.io.FilenameFilter import java.io.IOException import java.io.InputStreamReader -import java.util.LinkedList internal class EmbraceNdkService( private val context: Context, @@ -77,7 +74,7 @@ internal class EmbraceNdkService( if (configService.appFramework == AppFramework.UNITY) { unityCrashId = Uuid.getEmbUuid() } - cleanOldCrashFiles() + repository.cleanOldCrashFiles() } } } @@ -286,36 +283,6 @@ internal class EmbraceNdkService( return null } - private fun getNativeFiles(filter: FilenameFilter): Array { - val ndkDirs: List = storageService.listFiles { file, name -> - file.isDirectory && name == NATIVE_CRASH_FILE_FOLDER - } - - val matchingFiles = ndkDirs.flatMap { dir -> - dir.listFiles(filter)?.toList() ?: emptyList() - }.toTypedArray() - - return matchingFiles - } - - private fun getNativeErrorFiles(): Array { - val nativeCrashFilter = FilenameFilter { _: File?, name: String -> - name.startsWith( - NATIVE_CRASH_FILE_PREFIX - ) && name.endsWith(NATIVE_CRASH_ERROR_FILE_SUFFIX) - } - return getNativeFiles(nativeCrashFilter) - } - - private fun getNativeMapFiles(): Array { - val nativeCrashFilter = FilenameFilter { _: File?, name: String -> - name.startsWith( - NATIVE_CRASH_FILE_PREFIX - ) && name.endsWith(NATIVE_CRASH_MAP_FILE_SUFFIX) - } - return getNativeFiles(nativeCrashFilter) - } - private fun readMapFile(mapFile: File): String? { try { FileInputStream(mapFile).use { fin -> @@ -333,50 +300,6 @@ internal class EmbraceNdkService( } } - private fun cleanOldCrashFiles() { - backgroundWorker.submit { - val sortedFiles = repository.sortNativeCrashes(true) - val deleteCount = sortedFiles.size - MAX_NATIVE_CRASH_FILES_ALLOWED - if (deleteCount > 0) { - val files = LinkedList(sortedFiles) - for (i in 0 until deleteCount) { - runCatching { - files[i].delete() - } - } - } - - // delete error files that don't have matching crash files - val errorFiles = getNativeErrorFiles() - for (errorFile in errorFiles) { - if (hasNativeCrashFile(errorFile)) { - continue - } - errorFile.delete() - } - - // delete map files that don't have matching crash files - val mapFiles = getNativeMapFiles() - for (mapFile in mapFiles) { - if (hasNativeCrashFile(mapFile)) { - continue - } - mapFile.delete() - } - } - } - - private fun hasNativeCrashFile(file: File): Boolean { - val filename = file.absolutePath - if (!filename.contains(".")) { - return false - } - val crashFilename = - filename.substring(0, filename.lastIndexOf('.')) + NATIVE_CRASH_FILE_SUFFIX - val crashFile = File(crashFilename) - return crashFile.exists() - } - private fun updateAppState(newAppState: String) { if (sharedObjectLoader.loaded.get()) { delegate._updateAppState(newAppState) @@ -433,7 +356,6 @@ internal class EmbraceNdkService( internal const val NATIVE_CRASH_FILE_SUFFIX = ".crash" internal const val NATIVE_CRASH_ERROR_FILE_SUFFIX = ".error" internal const val NATIVE_CRASH_MAP_FILE_SUFFIX = ".map" - private const val MAX_NATIVE_CRASH_FILES_ALLOWED = 4 private const val EMB_DEVICE_META_DATA_SIZE = 2048 private const val HANDLER_CHECK_DELAY_MS = 5000 } diff --git a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkServiceRepository.kt b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkServiceRepository.kt index 33a4f85291..d8dd67e062 100644 --- a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkServiceRepository.kt +++ b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/EmbraceNdkServiceRepository.kt @@ -10,6 +10,8 @@ import io.embrace.android.embracesdk.internal.storage.StorageService import java.io.File import java.io.FilenameFilter +private const val MAX_NATIVE_CRASH_FILES_ALLOWED = 4 + /** * Encapsulates the logic of managing Files to get, sort and or delete them */ @@ -94,6 +96,35 @@ internal class EmbraceNdkServiceRepository( errorFile?.delete() mapFile?.delete() } + + override fun cleanOldCrashFiles() { + val sortedFiles = sortNativeCrashes(true) + val deleteCount = sortedFiles.size - MAX_NATIVE_CRASH_FILES_ALLOWED + if (deleteCount > 0) { + sortedFiles.take(deleteCount).forEach { file -> + runCatching { file.delete() } + } + } + + // delete error files that don't have matching crash files + getNativeErrorFiles().filterNot { hasNativeCrashFile(it) }.forEach { it.delete() } + + // delete map files that don't have matching crash files + getNativeMapFiles().filterNot { hasNativeCrashFile(it) }.forEach { it.delete() } + } + + private fun getNativeErrorFiles(): Array = getNativeFiles { _, name -> + name.startsWith(NATIVE_CRASH_FILE_PREFIX) && name.endsWith(NATIVE_CRASH_ERROR_FILE_SUFFIX) + } + + private fun getNativeMapFiles(): Array = getNativeFiles { _, name -> + name.startsWith(NATIVE_CRASH_FILE_PREFIX) && name.endsWith(NATIVE_CRASH_MAP_FILE_SUFFIX) + } + + private fun hasNativeCrashFile(file: File): Boolean { + val crashFilename = file.absolutePath.substringBeforeLast('.') + NATIVE_CRASH_FILE_SUFFIX + return File(crashFilename).exists() + } } typealias CleanupFunction = ( diff --git a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/NdkServiceRepository.kt b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/NdkServiceRepository.kt index bbb0f1b390..2efed4c522 100644 --- a/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/NdkServiceRepository.kt +++ b/embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/ndk/NdkServiceRepository.kt @@ -32,4 +32,6 @@ interface NdkServiceRepository { mapFile: File?, nativeCrash: NativeCrashData?, ) + + fun cleanOldCrashFiles() } diff --git a/embrace-test-fakes/src/main/kotlin/io/embrace/android/embracesdk/fakes/FakeNdkServiceRepository.kt b/embrace-test-fakes/src/main/kotlin/io/embrace/android/embracesdk/fakes/FakeNdkServiceRepository.kt index 55486ceb74..ba37ce3a0e 100644 --- a/embrace-test-fakes/src/main/kotlin/io/embrace/android/embracesdk/fakes/FakeNdkServiceRepository.kt +++ b/embrace-test-fakes/src/main/kotlin/io/embrace/android/embracesdk/fakes/FakeNdkServiceRepository.kt @@ -36,4 +36,8 @@ class FakeNdkServiceRepository : NdkServiceRepository { mapFiles[nativeCrashFile] = this } } + + override fun cleanOldCrashFiles() { + // do nothing + } }