I am attempting to use Kotlin Native on Linux, Windows and MacOS for a command line app where files are manipulated via cinterop. Given a String path, how can I determine if the path is a valid directory? I can see how to get a “stat” buffer but I cannot determine yet how to manipulate the st_mode field. A link to some sample code would be the ideal response. The crux seems to be in finding an S_ISDIR macro or in finding the bit pattern values associated with the st_mode field.
o
olonho
11/24/2018, 6:45 AM
Copy code
import kotlinx.cinterop.*
import platform.posix.*
val String.isDir: Boolean
get() = memScoped {
val statbuf = alloc<stat>()
if (stat(this@isDir, statbuf.ptr) != 0)
// no file
return false
return (statbuf.st_mode.toInt() and S_IFMT) == S_IFDIR
}
fun main() {
println("/tmp".isDir)
}
p
pajatopmr
11/24/2018, 7:53 AM
@olonho Is there something I could have read or searched for that would have led me to this answer on my own? I can find no Kotlin information on “stat” nor S_IFDIR but at least I can now see better how to apply a Kotlin solution if I grok the basic, underlying C code, so thank you for that.