Skip to content

Commit

Permalink
Merge pull request #8820 from wmontwe/change-filehelper-to-kotlin
Browse files Browse the repository at this point in the history
Change FileHelper to Kotlin
  • Loading branch information
wmontwe authored Feb 17, 2025
2 parents 90767b1 + 6304da2 commit 811ea8f
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 147 deletions.
147 changes: 0 additions & 147 deletions legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.java

This file was deleted.

73 changes: 73 additions & 0 deletions legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.fsck.k9.helper

import java.io.File
import java.io.IOException
import timber.log.Timber

object FileHelper {

@JvmStatic
@Suppress("TooGenericExceptionCaught")
fun touchFile(parentDir: File, name: String) {
val file = File(parentDir, name)
try {
if (!file.exists()) {
if (!file.createNewFile()) {
Timber.d("Unable to create file: %s", file.absolutePath)
}
} else {
if (!file.setLastModified(System.currentTimeMillis())) {
Timber.d("Unable to change last modification date: %s", file.absolutePath)
}
}
} catch (e: Exception) {
Timber.d(e, "Unable to touch file: %s", file.absolutePath)
}
}

@JvmStatic
@Throws(IOException::class)
fun renameOrMoveByCopying(from: File, to: File) {
deleteFileIfExists(to)
val renameFailed = !from.renameTo(to)
if (renameFailed) {
from.copyTo(target = to, overwrite = true)
val deleteFromFailed = !from.delete()
if (deleteFromFailed) {
Timber.e("Unable to delete source file after copying to destination!")
}
}
}

@Throws(IOException::class)
private fun deleteFileIfExists(file: File) {
if (file.exists() && !file.delete()) {
throw IOException("Unable to delete file: ${file.absolutePath}")
}
}

@Suppress("TooGenericExceptionCaught")
fun move(from: File, to: File): Boolean {
if (to.exists()) {
if (!to.delete()) {
Timber.d("Unable to delete file: %s", to.absolutePath)
}
}

val parent = to.parentFile
if (parent != null && !parent.mkdirs()) {
Timber.d("Unable to make directories: %s", parent.absolutePath)
}
return try {
from.copyTo(target = to, overwrite = true)
val deleteFromFailed = !from.delete()
if (deleteFromFailed) {
Timber.e("Unable to delete source file after copying to destination!")
}
true
} catch (e: Exception) {
Timber.w(e, "cannot move %s to %s", from.absolutePath, to.absolutePath)
false
}
}
}
68 changes: 68 additions & 0 deletions legacy/core/src/test/java/com/fsck/k9/helper/FileHelperTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.fsck.k9.helper

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isNotEqualTo
import assertk.assertions.isTrue
import java.io.File
import kotlin.test.Test
import org.junit.Rule
import org.junit.rules.TemporaryFolder

class FileHelperTest {

@Rule
@JvmField
val tempFolder = TemporaryFolder()

@Test
fun `touchFile should create file`() {
val parent = tempFolder.newFolder("parent")

FileHelper.touchFile(parent, "FileName")

assertThat(File(parent, "FileName").exists()).isTrue()
}

@Test
fun `touchFile should change file modification time`() {
val parent = tempFolder.newFolder("parent")
val lastModified = 12345L
val file = File(parent, "FileName")
file.createNewFile()
file.setLastModified(lastModified)

FileHelper.touchFile(parent, "FileName")

assertThat(file.lastModified()).isNotEqualTo(lastModified)
}

@Test
fun `renameOrMoveByCopying should move file when destination does not exist`() {
val source = tempFolder.newFile("source")
val destination = File(tempFolder.root, "destination")
val sourceContent = "content"
source.writeText(sourceContent)

FileHelper.renameOrMoveByCopying(source, destination)

assertThat(source.exists()).isFalse()
assertThat(destination.exists()).isTrue()
assertThat(destination.readText()).isEqualTo(sourceContent)
}

@Test
fun `renameOrMoveByCopying should move file when destination exists`() {
val source = tempFolder.newFile("source")
val destination = tempFolder.newFile("destination")
val sourceContent = "content"
source.writeText(sourceContent)

FileHelper.renameOrMoveByCopying(source, destination)

assertThat(source.exists()).isFalse()
assertThat(destination.exists()).isTrue()
assertThat(destination.readText()).isEqualTo(sourceContent)
}
}

0 comments on commit 811ea8f

Please sign in to comment.