From bf1e1d9db512952f68757502cc4aa20ca4533d30 Mon Sep 17 00:00:00 2001 From: "Vassiliy.Kudryashov" Date: Tue, 4 Oct 2022 20:42:39 +0300 Subject: [PATCH 1/3] We create temporary files a lot, we have to clean it either --- utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt b/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt index bb4936b8da..d02e1bcc2c 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt @@ -72,6 +72,13 @@ object FileUtil { val creationTime = Files.readAttributes(it.toPath(), BasicFileAttributes::class.java).creationTime() TimeUnit.MILLISECONDS.toDays(currentTimeInMillis - creationTime.toMillis()) > daysLimit }.forEach { it.deleteRecursively() } + File(tempDirectoryPath).listFiles { _, name -> + name.matches(Regex("Graph-vis\\d{10,}")) + || name.matches(Regex("\\d{10,}-0")) + || name.matches(Regex("byteBuddyAgent\\d{10,}\\.jar")) + || name.matches(Regex("cmd-args\\d{10,}")) + || name.matches(Regex("mockitoboot\\d{10,}\\.jar")) + }?.forEach { it.deleteRecursively() } } fun createTempDirectory(prefix: String): Path { From bcc37a27692b8bb655d0219bbaaf8da647298b04 Mon Sep 17 00:00:00 2001 From: "Vassiliy.Kudryashov" Date: Tue, 4 Oct 2022 20:51:44 +0300 Subject: [PATCH 2/3] We create temporary files a lot, we have to clean it either --- .../main/kotlin/org/utbot/common/FileUtil.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt b/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt index d02e1bcc2c..8043814dbb 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt @@ -72,12 +72,18 @@ object FileUtil { val creationTime = Files.readAttributes(it.toPath(), BasicFileAttributes::class.java).creationTime() TimeUnit.MILLISECONDS.toDays(currentTimeInMillis - creationTime.toMillis()) > daysLimit }.forEach { it.deleteRecursively() } - File(tempDirectoryPath).listFiles { _, name -> - name.matches(Regex("Graph-vis\\d{10,}")) - || name.matches(Regex("\\d{10,}-0")) - || name.matches(Regex("byteBuddyAgent\\d{10,}\\.jar")) - || name.matches(Regex("cmd-args\\d{10,}")) - || name.matches(Regex("mockitoboot\\d{10,}\\.jar")) + File(tempDirectoryPath).listFiles { file -> + val name = file.name + if ((name.matches(Regex("Graph-vis\\d{10,}")) + || name.matches(Regex("\\d{10,}-0")) + || name.matches(Regex("byteBuddyAgent\\d{10,}\\.jar")) + || name.matches(Regex("cmd-args\\d{10,}")) + || name.matches(Regex("mockitoboot\\d{10,}\\.jar"))) + ) { + val creationTime = Files.readAttributes(file.toPath(), BasicFileAttributes::class.java).creationTime() + return@listFiles (TimeUnit.MILLISECONDS.toDays(currentTimeInMillis - creationTime.toMillis()) > daysLimit) + } + return@listFiles false }?.forEach { it.deleteRecursively() } } From 11ee5573d2cda34c6e17d6c55c930d90fcfcf0cf Mon Sep 17 00:00:00 2001 From: "Vassiliy.Kudryashov" Date: Thu, 6 Oct 2022 13:36:17 +0300 Subject: [PATCH 3/3] We create temporary files a lot, we have to clean it either The fix: our own temp files are moved to UTBot subfolder where existing auto-cleaning does the job --- .../main/kotlin/org/utbot/common/FileUtil.kt | 32 +++++++------------ .../org/utbot/framework/plugin/api/Api.kt | 3 +- .../engine/selectors/strategies/GraphViz.kt | 3 +- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt b/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt index 8043814dbb..4bb386ae7b 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt @@ -64,33 +64,23 @@ object FileUtil { * Deletes all the files and folders from the java unit-test temp directory that are older than [daysLimit]. */ fun clearTempDirectory(daysLimit: Int) { - val currentTimeInMillis = System.currentTimeMillis() - - val files = utBotTempDirectory.toFile().listFiles() ?: return - - files.filter { - val creationTime = Files.readAttributes(it.toPath(), BasicFileAttributes::class.java).creationTime() - TimeUnit.MILLISECONDS.toDays(currentTimeInMillis - creationTime.toMillis()) > daysLimit - }.forEach { it.deleteRecursively() } - File(tempDirectoryPath).listFiles { file -> - val name = file.name - if ((name.matches(Regex("Graph-vis\\d{10,}")) - || name.matches(Regex("\\d{10,}-0")) - || name.matches(Regex("byteBuddyAgent\\d{10,}\\.jar")) - || name.matches(Regex("cmd-args\\d{10,}")) - || name.matches(Regex("mockitoboot\\d{10,}\\.jar"))) - ) { - val creationTime = Files.readAttributes(file.toPath(), BasicFileAttributes::class.java).creationTime() - return@listFiles (TimeUnit.MILLISECONDS.toDays(currentTimeInMillis - creationTime.toMillis()) > daysLimit) - } - return@listFiles false - }?.forEach { it.deleteRecursively() } + (utBotTempDirectory.toFile().listFiles() ?: return).filter { isOld(it, daysLimit) } + .forEach { it.deleteRecursively() } + } + + private fun isOld(it: File, daysLimit: Int): Boolean { + val creationTime = Files.readAttributes(it.toPath(), BasicFileAttributes::class.java).creationTime() + return TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - creationTime.toMillis()) > daysLimit } fun createTempDirectory(prefix: String): Path { return createTempDirectory(utBotTempDirectory, prefix) } + fun createTempFile(prefix: String, suffix: String) : Path { + return Files.createTempFile(utBotTempDirectory, prefix, suffix) + } + /** * Copy the class file for given [classes] to temporary folder. * It can be used for Soot analysis. diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt index b7f39e6d54..6059ff3b20 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt @@ -52,6 +52,7 @@ import java.io.File import java.lang.reflect.Modifier import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract +import org.utbot.common.FileUtil const val SYMBOLIC_NULL_ADDR: Int = 0 @@ -1314,7 +1315,7 @@ enum class CodegenLanguage( // https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#commandlineargfile fun isolateCommandLineArgumentsToArgumentFile(arguments: List): String { - val argumentFile = File.createTempFile("cmd-args", "") + val argumentFile = FileUtil.createTempFile("cmd-args", "").toFile() argumentFile.writeText( arguments.joinToString(" ") { // If a filename contains embedded spaces, put the whole filename in double quotes, diff --git a/utbot-framework/src/main/kotlin/org/utbot/engine/selectors/strategies/GraphViz.kt b/utbot-framework/src/main/kotlin/org/utbot/engine/selectors/strategies/GraphViz.kt index 9385fd5ae1..fb603aad14 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/engine/selectors/strategies/GraphViz.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/engine/selectors/strategies/GraphViz.kt @@ -19,6 +19,7 @@ import java.awt.datatransfer.StringSelection import java.io.FileWriter import java.nio.file.Files import java.nio.file.Paths +import org.utbot.common.FileUtil private val logger = KotlinLogging.logger {} @@ -29,7 +30,7 @@ class GraphViz( ) : TraverseGraphStatistics(globalGraph) { // Files - private val graphVisDirectory = Files.createTempDirectory("Graph-vis") + private val graphVisDirectory = FileUtil.createTempDirectory("Graph-vis") private val graphVisPathString = graphVisDirectory.toString() private val graphJs = Paths.get(graphVisPathString, "graph.js").toFile()