@@ -2,11 +2,14 @@ package org.ooni.probe.domain
22
33import co.touchlab.kermit.Logger
44import kotlinx.coroutines.flow.first
5+ import kotlinx.coroutines.withContext
56import kotlinx.serialization.SerialName
67import kotlinx.serialization.Serializable
78import kotlinx.serialization.SerializationException
89import kotlinx.serialization.json.Json
10+ import okio.FileSystem
911import okio.Path
12+ import okio.Path.Companion.toPath
1013import org.ooni.engine.Engine
1114import org.ooni.engine.Engine.MkException
1215import org.ooni.engine.models.Failure
@@ -16,6 +19,7 @@ import org.ooni.engine.models.TaskOrigin
1619import org.ooni.probe.data.models.GetBytesException
1720import org.ooni.probe.data.models.SettingsKey
1821import org.ooni.probe.data.repositories.PreferenceRepository
22+ import kotlin.coroutines.CoroutineContext
1923import kotlin.time.Clock
2024
2125class FetchGeoIpDbUpdates (
@@ -24,7 +28,9 @@ class FetchGeoIpDbUpdates(
2428 private val engineHttpDo : suspend (method: String , url: String , taskOrigin: TaskOrigin ) -> Result <String ?, Engine .MkException >,
2529 private val preferencesRepository : PreferenceRepository ,
2630 private val json : Json ,
27- ) {
31+ private val fileSystem : FileSystem ,
32+ private val backgroundContext : CoroutineContext ,
33+ ) {
2834 companion object {
2935 private const val GEOIP_DB_VERSION_DEFAULT : String = " 20250801"
3036 private const val GEOIP_DB_REPO : String = " aanorbel/oomplt-mmdb"
@@ -62,6 +68,10 @@ class FetchGeoIpDbUpdates(
6268
6369 downloadFile(url, target)
6470 .flatMap { downloadedPath ->
71+ withContext(backgroundContext){
72+ // Cleanup other mmdb files other than the latest
73+ cleanupOldMmdbFiles(latestVersion)
74+ }
6575 preferencesRepository.setValueByKey(
6676 SettingsKey .MMDB_VERSION ,
6777 latestVersion,
@@ -119,6 +129,36 @@ class FetchGeoIpDbUpdates(
119129
120130 private fun normalize (tag : String ): Int = tag.removePrefix(" v" ).trim().toInt()
121131
132+ /* *
133+ * Delete all .mmdb files in the cache directory except for the specified version.
134+ * @param keepVersion The version of the mmdb file to keep (e.g., "20250801")
135+ */
136+ private fun cleanupOldMmdbFiles (keepVersion : String ) {
137+ try {
138+ val cacheDirPath = cacheDir.toPath()
139+ if (! fileSystem.exists(cacheDirPath)) {
140+ return
141+ }
142+
143+ val files = fileSystem.list(cacheDirPath)
144+ val keepFileName = " $keepVersion .mmdb"
145+
146+ files.forEach { filePath ->
147+ val fileName = filePath.name
148+ if (fileName.endsWith(" .mmdb" ) && fileName != keepFileName) {
149+ try {
150+ fileSystem.delete(filePath)
151+ Logger .d { " Deleted old MMDB file: $fileName " }
152+ } catch (e: Exception ) {
153+ Logger .e(e) { " Failed to delete old MMDB file: $fileName " }
154+ }
155+ }
156+ }
157+ } catch (e: Exception ) {
158+ Logger .e(e) { " Failed to cleanup old MMDB files" }
159+ }
160+ }
161+
122162 @Serializable
123163 data class GhRelease (
124164 @SerialName(" tag_name" ) val tag : String ,
0 commit comments