From 11f3c4b03f40460160c1f23b634941a867fd817b Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Mon, 12 Aug 2019 02:15:33 +0300 Subject: [PATCH] Clarify the error message when File.copyTo fails (KT-27545) --- libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt b/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt index 553261cf248..d61b9a2d0f3 100644 --- a/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt +++ b/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt @@ -184,15 +184,10 @@ public fun File.copyTo(target: File, overwrite: Boolean = false, bufferSize: Int } if (target.exists()) { - val stillExists = if (!overwrite) true else !target.delete() - - if (stillExists) { - throw FileAlreadyExistsException( - file = this, - other = target, - reason = "The destination file already exists." - ) - } + if (!overwrite) + throw FileAlreadyExistsException(file = this, other = target, reason = "The destination file already exists.") + else if (!target.delete()) + throw FileAlreadyExistsException(file = this, other = target, reason = "Tried to overwrite the destination, but failed to delete it.") } if (this.isDirectory) {