Rohde Fischer
Rohde Fischer

Reputation: 1482

Okio native/multiplatform default paths

How do one access the common default paths using okio?

The paths I'm specifically interested in are:

For the temporary directory I found FileSystem.SYSTEM_TEMPORARY_DIRECTORY, is this the correct/best way?

Can I, as I suspect get working directory by assuming that it's local to the FileSystem, if so, is this reliable, or just how it happens to be right now?

What about the application directory?

I've seen that a users home directory isn't implemented due to the ambiguity of it, and issues with platforms such as Android where the notion of a home directory is a bit weird. And for that reason I suspect there's no direct helpers/variables in okio, and that I need to work around the system directly, is that correct?

Upvotes: 2

Views: 1675

Answers (2)

Dirk Hoffmann
Dirk Hoffmann

Reputation: 1563

I had a look at https://github.com/korlibs/korge (recommendation of @oldergod above)

To determin the current working directory (aka application directory)
I came up with these functions, that work on native via POSIX and jvm via File(".")

commonMain:

import okio.Path

expect fun cwd(): Path

jvmMain:

import okio.Path
import okio.Path.Companion.toPath
import java.io.File

// one of:
// actual fun cwd(): Path = File(File(".").absolutePath).canonicalPath.toPath()
actual fun cwd(): Path = File(".").absolutePath.toPath(normalize = true)

nativeMain:

import kotlinx.cinterop.*
import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath
import platform.posix.PATH_MAX
import platform.posix.getcwd

actual fun cwd(): Path = memScoped {
    val temp = allocArray<ByteVar>(PATH_MAX + 1)
    getcwd(temp, PATH_MAX.convert())
    temp.toKString()
}.toPath(normalize = true)

maybe usefull for somebody coming across this thread.

Upvotes: 1

oldergod
oldergod

Reputation: 15010

Application directory (location of the executable(s) that are being run)

No clue. What would you use with java.nio?

Working directory (where the app is run from, seems that it's relative to the FileSystem?)

I think you could work with ".".toPath(), does it not work?

Temporary directory

As you said, FileSystem.SYSTEM_TEMPORARY_DIRECTORY.

Upvotes: 2

Related Questions