Skip to content

Commit b1d98df

Browse files
committed
chore: implement cleanup for old MMDB files
1 parent 721195c commit b1d98df

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

composeApp/src/commonMain/kotlin/org/ooni/probe/di/Dependencies.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ class Dependencies(
219219
engineHttpDo = engine::httpDo,
220220
json = json,
221221
preferencesRepository = preferenceRepository,
222+
fileSystem = FileSystem.SYSTEM,
223+
backgroundContext = backgroundContext
222224
)
223225
}
224226

composeApp/src/commonMain/kotlin/org/ooni/probe/domain/DownloadFile.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ class DownloadFile(
3636
}
3737
}
3838

39-
4039
/**
4140
* Perform a simple HTTP GET and return the raw response body bytes.
4241
* Uses Ktor HttpClient for cross-platform HTTP operations.
4342
*/
4443
suspend fun fetchBytes(url: String): Result<ByteArray, GetBytesException> {
45-
val client: HttpClient = HttpClient()
44+
val client = HttpClient()
4645

4746
return try {
4847
val response = client.get(url)
@@ -53,8 +52,8 @@ class DownloadFile(
5352
} else {
5453
Failure(
5554
GetBytesException(
56-
Exception("HTTP ${response.status.value} while GET $url: ${bytes.decodeToString()}")
57-
)
55+
Exception("HTTP ${response.status.value} while GET $url: ${bytes.decodeToString()}"),
56+
),
5857
)
5958
}
6059
} catch (e: Throwable) {

composeApp/src/commonMain/kotlin/org/ooni/probe/domain/FetchGeoIpDbUpdates.kt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package org.ooni.probe.domain
22

33
import co.touchlab.kermit.Logger
44
import kotlinx.coroutines.flow.first
5+
import kotlinx.coroutines.withContext
56
import kotlinx.serialization.SerialName
67
import kotlinx.serialization.Serializable
78
import kotlinx.serialization.SerializationException
89
import kotlinx.serialization.json.Json
10+
import okio.FileSystem
911
import okio.Path
12+
import okio.Path.Companion.toPath
1013
import org.ooni.engine.Engine
1114
import org.ooni.engine.Engine.MkException
1215
import org.ooni.engine.models.Failure
@@ -16,6 +19,7 @@ import org.ooni.engine.models.TaskOrigin
1619
import org.ooni.probe.data.models.GetBytesException
1720
import org.ooni.probe.data.models.SettingsKey
1821
import org.ooni.probe.data.repositories.PreferenceRepository
22+
import kotlin.coroutines.CoroutineContext
1923
import kotlin.time.Clock
2024

2125
class 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

Comments
 (0)