Browse Source

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

feature/jme/update_rust_sdk
Benoit Marty 2 years ago
parent
commit
fce1569ee3
  1. 5
      features/rageshake/src/main/kotlin/io/element/android/features/rageshake/logs/VectorFileLogger.kt
  2. 5
      features/rageshake/src/main/kotlin/io/element/android/features/rageshake/reporter/BugReporter.kt
  3. 3
      features/rageshake/src/main/kotlin/io/element/android/features/rageshake/screenshot/ScreenshotHolder.kt
  4. 1
      libraries/androidutils/build.gradle.kts
  5. 34
      libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/file/File.kt
  6. 2
      libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/file/compressFile.kt

5
features/rageshake/src/main/kotlin/io/element/android/features/rageshake/logs/VectorFileLogger.kt

@ -18,6 +18,7 @@ package io.element.android.features.rageshake.logs @@ -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( @@ -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( @@ -101,7 +102,7 @@ class VectorFileLogger(
fun reset() {
// Delete all files
getLogFiles().map {
tryOrNull { it.delete() }
it.safeDelete()
}
}

5
features/rageshake/src/main/kotlin/io/element/android/features/rageshake/reporter/BugReporter.kt

@ -23,6 +23,7 @@ import io.element.android.features.rageshake.crash.CrashDataStore @@ -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( @@ -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( @@ -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 {

3
features/rageshake/src/main/kotlin/io/element/android/features/rageshake/screenshot/ScreenshotHolder.kt

@ -19,6 +19,7 @@ package io.element.android.features.rageshake.screenshot @@ -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( @@ -38,6 +39,6 @@ class ScreenshotHolder @Inject constructor(
fun getFile() = file.takeIf { it.exists() && it.length() > 0 }
fun reset() {
file.delete()
file.safeDelete()
}
}

1
libraries/androidutils/build.gradle.kts

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

34
libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/file/File.kt

@ -0,0 +1,34 @@ @@ -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")
}
}
)
}

2
libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/file/compressFile.kt

@ -32,7 +32,7 @@ fun compressFile(file: File): 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 {

Loading…
Cancel
Save