How do I check if the file exists by its relative ...
# kotlin-native
b
How do I check if the file exists by its relative path? e.g.
existingDir/nonExistingDir/../existingFile
always return false with both, access ans stat
n
I'm assuming you are talking about Linux. In that case you should use the
fopen
function ( https://man7.org/linux/man-pages/man3/fopen.3.html ) from the POSIX library (automatically included with all Kotlin Native Linux targets), which will return null if the file doesn't exist. Do note that the function does accept a relative path (relative to the Working Directory).
b
Hmm, seems a bit redundant to open a file only to close it right after.
On a side note, is there a way to convert relative path to nonexistent file to absolute? realpath returns null on nonexistent files.
n
The
fopen
function technique is slightly cumbersome, however it is very simple to use and gets the job done in a very short space of time.
There isn't anything for converting a relative path to an absolute path. One could work around this by using the
getcwd
function ( https://man7.org/linux/man-pages/man3/getcwd.3.html ), and append the relative path to what is returned from the function.
Are you using Symbolic Links?
b
No no symlinks, just trying to build File api for native
🆗 1
Also getcwd doesn't really help as this would still not expand .. and . properly
1
n
Everyone seems to be building their own File IO type API for Kotlin Native 😆 . Waiting for KotlinX IO ( https://github.com/Kotlin/kotlinx-io ) to gain File IO support.
b
Gotta do something to get things moving until that one's ready 😀