Fix sonar issue: do not ignore result of File:delete().

This commit is contained in:
Benoit Marty
2023-02-01 14:46:00 +01:00
parent d55b6c5e5f
commit 6eaea5bf93
6 changed files with 44 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ package io.element.android.features.rageshake.logs
import android.content.Context
import android.util.Log
import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.core.data.tryOrNull
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
@@ -82,7 +83,7 @@ class VectorFileLogger(
for (i in 0..15) {
val file = File(cacheDirectory, "elementLogs.${i}.txt")
tryOrNull { file.delete() }
file.safeDelete()
}
fileHandler = tryOrNull(
@@ -101,7 +102,7 @@ class VectorFileLogger(
fun reset() {
// Delete all files
getLogFiles().map {
tryOrNull { it.delete() }
it.safeDelete()
}
}

View File

@@ -23,6 +23,7 @@ import io.element.android.features.rageshake.crash.CrashDataStore
import io.element.android.features.rageshake.logs.VectorFileLogger
import io.element.android.features.rageshake.screenshot.ScreenshotHolder
import io.element.android.libraries.androidutils.file.compressFile
import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.core.extensions.toOnOff
import io.element.android.libraries.core.mimetype.MimeTypes
import io.element.android.libraries.di.ApplicationContext
@@ -423,7 +424,7 @@ class BugReporter @Inject constructor(
// delete when the bug report has been successfully sent
for (file in mBugReportFiles) {
file.delete()
file.safeDelete()
}
if (null != listener) {
@@ -498,7 +499,7 @@ class BugReporter @Inject constructor(
val logCatErrFile = File(context.cacheDir.absolutePath, if (isErrorLogcat) LOG_CAT_ERROR_FILENAME else LOG_CAT_FILENAME)
if (logCatErrFile.exists()) {
logCatErrFile.delete()
logCatErrFile.safeDelete()
}
try {

View File

@@ -19,6 +19,7 @@ package io.element.android.features.rageshake.screenshot
import android.content.Context
import android.graphics.Bitmap
import io.element.android.libraries.androidutils.bitmap.writeBitmap
import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.di.SingleIn
@@ -38,6 +39,6 @@ class ScreenshotHolder @Inject constructor(
fun getFile() = file.takeIf { it.exists() && it.length() > 0 }
fun reset() {
file.delete()
file.safeDelete()
}
}

View File

@@ -26,4 +26,5 @@ android {
dependencies {
implementation(libs.timber)
implementation(libs.androidx.corektx)
implementation(projects.libraries.core)
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.androidutils.file
import io.element.android.libraries.core.data.tryOrNull
import timber.log.Timber
import java.io.File
fun File.safeDelete() {
tryOrNull(
onError = {
Timber.e(it, "Error, unable to delete file $path")
},
operation = {
if (delete().not()) {
Timber.w("Warning, unable to delete file $path")
}
}
)
}

View File

@@ -32,7 +32,7 @@ fun compressFile(file: File): File? {
val dstFile = file.resolveSibling(file.name + ".gz")
if (dstFile.exists()) {
dstFile.delete()
dstFile.safeDelete()
}
return try {