From 55cfb3af202a3d9e5c83b6b04cac2a925a9c8acd Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Mon, 22 Aug 2022 19:40:37 +0200 Subject: [PATCH] Remove deprecated GFileUtils usage ^KT-53615 Fixed --- .../tasks/internal/CleanableStoreImpl.kt | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/internal/CleanableStoreImpl.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/internal/CleanableStoreImpl.kt index 57029757be5..18e167eed4b 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/internal/CleanableStoreImpl.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/internal/CleanableStoreImpl.kt @@ -5,9 +5,12 @@ package org.jetbrains.kotlin.gradle.tasks.internal -import org.gradle.util.GFileUtils import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.io.UncheckedIOException import java.nio.file.Files +import java.nio.file.attribute.FileTime import java.time.Instant internal class CleanableStoreImpl(dirPath: String) : CleanableStore { @@ -18,7 +21,7 @@ internal class CleanableStoreImpl(dirPath: String) : CleanableStore { override fun markUsed() { if (dir.exists()) { - GFileUtils.touchExisting(dir) + touchExisting(dir) } } @@ -33,4 +36,27 @@ internal class CleanableStoreImpl(dirPath: String) : CleanableStore { } ?.forEach { file -> file.deleteRecursively() } } + + private fun touchExisting(file: File) { + try { + Files.setLastModifiedTime(file.toPath(), FileTime.fromMillis(System.currentTimeMillis())) + } catch (e: IOException) { + if (file.isFile && file.length() == 0L) { + // On Linux, users cannot touch files they don't own but have write access to + // because the JDK uses futimes() instead of futimens() [note the 'n'!] + // see https://github.com/gradle/gradle/issues/7873 + touchFileByWritingEmptyByteArray(file) + } else { + throw UncheckedIOException("Could not update timestamp for $file", e) + } + } + } + + private fun touchFileByWritingEmptyByteArray(file: File) { + try { + FileOutputStream(file).use { it.write(ByteArray(0)) } + } catch (e: IOException) { + throw UncheckedIOException("Could not update timestamp for $file", e) + } + } } \ No newline at end of file