diff --git a/legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.java b/legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.java deleted file mode 100644 index 4c68b3077df..00000000000 --- a/legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.java +++ /dev/null @@ -1,147 +0,0 @@ -package com.fsck.k9.helper; - - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; - -import timber.log.Timber; - -import org.apache.commons.io.IOUtils; - - -public class FileHelper { - public static void touchFile(final File parentDir, final String name) { - final File file = new File(parentDir, name); - try { - if (!file.exists()) { - if (!file.createNewFile()) { - Timber.d("Unable to create file: %s", file.getAbsolutePath()); - } - } else { - if (!file.setLastModified(System.currentTimeMillis())) { - Timber.d("Unable to change last modification date: %s", file.getAbsolutePath()); - } - } - } catch (Exception e) { - Timber.d(e, "Unable to touch file: %s", file.getAbsolutePath()); - } - } - - private static void copyFile(File from, File to) throws IOException { - FileInputStream in = new FileInputStream(from); - FileOutputStream out = new FileOutputStream(to); - try { - byte[] buffer = new byte[1024]; - int count; - while ((count = in.read(buffer)) > 0) { - out.write(buffer, 0, count); - } - out.close(); - } finally { - IOUtils.closeQuietly(in); - IOUtils.closeQuietly(out); - } - } - - public static void renameOrMoveByCopying(File from, File to) throws IOException { - deleteFileIfExists(to); - - boolean renameFailed = !from.renameTo(to); - if (renameFailed) { - copyFile(from, to); - - boolean deleteFromFailed = !from.delete(); - if (deleteFromFailed) { - Timber.e("Unable to delete source file after copying to destination!"); - } - } - } - - private static void deleteFileIfExists(File to) throws IOException { - boolean fileDoesNotExist = !to.exists(); - if (fileDoesNotExist) { - return; - } - - boolean deleteOk = to.delete(); - if (deleteOk) { - return; - } - - throw new IOException("Unable to delete file: " + to.getAbsolutePath()); - } - - public static boolean move(final File from, final File to) { - if (to.exists()) { - if (!to.delete()) { - Timber.d("Unable to delete file: %s", to.getAbsolutePath()); - } - } - - if (!to.getParentFile().mkdirs()) { - Timber.d("Unable to make directories: %s", to.getParentFile().getAbsolutePath()); - } - - try { - copyFile(from, to); - - boolean deleteFromFailed = !from.delete(); - if (deleteFromFailed) { - Timber.e("Unable to delete source file after copying to destination!"); - } - return true; - } catch (Exception e) { - Timber.w(e, "cannot move %s to %s", from.getAbsolutePath(), to.getAbsolutePath()); - return false; - } - } - - public static void moveRecursive(final File fromDir, final File toDir) { - if (!fromDir.exists()) { - return; - } - if (!fromDir.isDirectory()) { - if (toDir.exists()) { - if (!toDir.delete()) { - Timber.w("cannot delete already existing file/directory %s", toDir.getAbsolutePath()); - } - } - if (!fromDir.renameTo(toDir)) { - Timber.w("cannot rename %s to %s - moving instead", fromDir.getAbsolutePath(), toDir.getAbsolutePath()); - move(fromDir, toDir); - } - return; - } - if (!toDir.exists() || !toDir.isDirectory()) { - if (toDir.exists()) { - if (!toDir.delete()) { - Timber.d("Unable to delete file: %s", toDir.getAbsolutePath()); - } - } - if (!toDir.mkdirs()) { - Timber.w("cannot create directory %s", toDir.getAbsolutePath()); - } - } - File[] files = fromDir.listFiles(); - for (File file : files) { - if (file.isDirectory()) { - moveRecursive(file, new File(toDir, file.getName())); - if (!file.delete()) { - Timber.d("Unable to delete file: %s", toDir.getAbsolutePath()); - } - } else { - File target = new File(toDir, file.getName()); - if (!file.renameTo(target)) { - Timber.w("cannot rename %s to %s - moving instead", - file.getAbsolutePath(), target.getAbsolutePath()); - move(file, target); - } - } - } - if (!fromDir.delete()) { - Timber.w("cannot delete %s", fromDir.getAbsolutePath()); - } - } -} diff --git a/legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.kt b/legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.kt new file mode 100644 index 00000000000..b79b75bd317 --- /dev/null +++ b/legacy/core/src/main/java/com/fsck/k9/helper/FileHelper.kt @@ -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 + } + } +} diff --git a/legacy/core/src/test/java/com/fsck/k9/helper/FileHelperTest.kt b/legacy/core/src/test/java/com/fsck/k9/helper/FileHelperTest.kt new file mode 100644 index 00000000000..5b5d3a4b3b0 --- /dev/null +++ b/legacy/core/src/test/java/com/fsck/k9/helper/FileHelperTest.kt @@ -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) + } +}