Skip to content

Commit

Permalink
Move cleanOldFiles to the NdkServiceRepository (#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
priettt authored Nov 20, 2024
1 parent ce5166e commit ec66742
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ 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
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,
Expand Down Expand Up @@ -77,7 +74,7 @@ internal class EmbraceNdkService(
if (configService.appFramework == AppFramework.UNITY) {
unityCrashId = Uuid.getEmbUuid()
}
cleanOldCrashFiles()
repository.cleanOldCrashFiles()
}
}
}
Expand Down Expand Up @@ -286,36 +283,6 @@ internal class EmbraceNdkService(
return null
}

private fun getNativeFiles(filter: FilenameFilter): Array<File> {
val ndkDirs: List<File> = 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<File> {
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<File> {
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 ->
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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<File> = getNativeFiles { _, name ->
name.startsWith(NATIVE_CRASH_FILE_PREFIX) && name.endsWith(NATIVE_CRASH_ERROR_FILE_SUFFIX)
}

private fun getNativeMapFiles(): Array<File> = 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 = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ interface NdkServiceRepository {
mapFile: File?,
nativeCrash: NativeCrashData?,
)

fun cleanOldCrashFiles()
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ class FakeNdkServiceRepository : NdkServiceRepository {
mapFiles[nativeCrashFile] = this
}
}

override fun cleanOldCrashFiles() {
// do nothing
}
}

0 comments on commit ec66742

Please sign in to comment.