[Gradle] cleanNativeDistributionCommonization: Don't try to delete .lock file

^KT-52172 Verification Pending
This commit is contained in:
Sebastian Sellmair
2022-12-13 09:31:05 +01:00
committed by Space Team
parent 8a01ad9010
commit 151da98331
2 changed files with 10 additions and 9 deletions
@@ -153,8 +153,9 @@ internal val Project.cleanNativeDistributionCommonizerTask: TaskProvider<Default
outputs.dir(commonizerDirectory)
doFirst {
NativeDistributionCommonizerLock(commonizerDirectory.get()).withLock {
delete(commonizerDirectory.get())
NativeDistributionCommonizerLock(commonizerDirectory.get()).withLock { lockFile ->
val files = commonizerDirectory.get().listFiles().orEmpty().toSet() - lockFile
delete(*files.toTypedArray())
}
}
}
@@ -19,29 +19,29 @@ internal class NativeDistributionCommonizerLock(
val lockedOutputDirectories = hashSetOf<File>()
}
fun <T> withLock(action: () -> T): T {
fun <T> withLock(action: (lockFile: File) -> T): T {
/* Enter intra-process wide lock */
intraProcessLock.withLock {
val lockFile = outputDirectory.resolve(".lock")
if (outputDirectory in lockedOutputDirectories) {
/* Already acquired this directory and re-entered: We can just execute the action */
return action()
return action(lockFile)
}
/* Lock output directory inter-process wide */
outputDirectory.mkdirs()
val lockfile = outputDirectory.resolve(".lock")
logInfo("Acquire lock: ${lockfile.path} ...")
logInfo("Acquire lock: ${lockFile.path} ...")
FileOutputStream(outputDirectory.resolve(".lock")).use { stream ->
val lock = stream.channel.lock()
assert(lock.isValid)
return try {
logInfo("Lock acquired: ${lockfile.path}")
logInfo("Lock acquired: ${lockFile.path}")
lockedOutputDirectories.add(outputDirectory)
action()
action(lockFile)
} finally {
lockedOutputDirectories.remove(outputDirectory)
lock.release()
logInfo("Lock released: ${lockfile.path}")
logInfo("Lock released: ${lockFile.path}")
}
}
}