Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 496ef2f1 authored by Abhishek Aggarwal's avatar Abhishek Aggarwal
Browse files

fix(install): harden file move handling and startup cleanup

parent 5e16c152
Loading
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -124,8 +124,14 @@ class AppLoungeApplication : Application(), Configuration.Provider {
            )
        }

        // Robolectric eagerly creates the real Application for many unrelated tests.
        // Skip install DB cleanup there to avoid background Room work leaking across tests.
        if (!isRunningUnderRobolectric()) {
            removeStalledInstallationFromDb()
        }
    }

    private fun isRunningUnderRobolectric(): Boolean = Build.FINGERPRINT == "robolectric"

    private fun removeStalledInstallationFromDb() = coroutineScope.launch {
        val existingInstallations = appInstallDao.getItemInInstallation().toMutableList()
+6 −21
Original line number Diff line number Diff line
@@ -2,42 +2,27 @@ package foundation.e.apps.data.install

import timber.log.Timber
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream

object FileManager {
    const val BUFFER_SIZE = 1024

    fun moveFile(inputPath: String, inputFile: String, outputPath: String) {
        var inputStream: InputStream? = null
        var outputStream: OutputStream? = null
        try {
            // create output directory if it doesn't exist
            val dir = File(outputPath)
            if (!dir.exists()) {
                dir.mkdirs()
            }
            inputStream = FileInputStream(inputPath + inputFile)
            outputStream = FileOutputStream(outputPath + inputFile)
            val buffer = ByteArray(BUFFER_SIZE)
            var read: Int
            while (inputStream.read(buffer).also { read = it } != -1) {
                outputStream.write(buffer, 0, read)
            }
            // write the output file
            outputStream.flush()
            // delete the original file
            File(inputPath + inputFile).delete()

            val sourceFile = File(inputPath + inputFile)
            val targetFile = File(outputPath + inputFile)

            sourceFile.copyTo(targetFile, overwrite = true, bufferSize = BUFFER_SIZE)
            sourceFile.delete()
        } catch (e: FileNotFoundException) {
            Timber.e(e.stackTraceToString())
        } catch (e: Exception) {
            Timber.e(e.stackTraceToString())
        } finally {
            inputStream?.close()
            outputStream?.close()
        }
    }
}