Remove deprecated GFileUtils usage

^KT-53615 Fixed
This commit is contained in:
Yahor Berdnikau
2022-08-22 19:40:37 +02:00
committed by Space
parent 94b550b9dc
commit 55cfb3af20
@@ -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)
}
}
}