I am attempting to use Kotlin Native on Linux, Win...
# kotlin-native
p
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
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
@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.
o
I’d check macro-definition
/usr/include/sys/stat.h:#define      S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)     /* directory */
I sent you a bit simplified version, corrected it
👍 1