Lock file during a create event

I use KfsWatcher to watch a folder and I want to be sure the file is fully written before process it but I don’t know how to do it when the file is big. Maybe a lock with a mutex is the solution but how to implement it?

fun run() { runBlocking { val watcher = KfsDirectoryWatcher(this).apply { add(applicationProperties.watchedFolder) } logger.info("Start watching: ${applicationProperties.watchedFolder}") launch { watcher.onEventFlow.collect { event: KfsDirectoryWatcherEvent -> val file = File("${event.targetDirectory}/${event.path}") if (event.event == KfsEvent.Create) { // ensure the file has finished writing before manipulate it println(file) println(file.toPath().fileSize()) // I copy a 2.2GB file and it only display 856064 bytes } } } } } 

submitted by /u/Rtransat
[link] [comments]