2

How do one access the common default paths using okio?

The paths I'm specifically interested in are:

  • Application directory (location of the executable(s) that are being run)
  • Working directory (where the app is run from, seems that it's relative to the FileSystem?)
  • Temporary directory

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?

Rohde Fischer
  • 1,248
  • 2
  • 10
  • 32

2 Answers2

2

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.

oldergod
  • 15,033
  • 7
  • 62
  • 88
  • In java.nio I'd probably do something like this: https://stackoverflow.com/a/320595/1441857 I know the korio library has a solution, but I'm a bit uncertain how they got it working. But if we can get a solution, that'd be nice so the answer can be complete :) – Rohde Fischer Nov 18 '22 at 16:56
  • Actually I think this is how it's done in korio: https://github.com/korlibs/korge/blob/fa3a118cdcc05bcb52303afe67e93e396fabe279/korio/src/linuxMain/kotlin/com/soywiz/korio/file/std/StandardBasePathsJs.kt – Rohde Fischer Nov 18 '22 at 16:59
1

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.

Dirk Hoffmann
  • 1,444
  • 17
  • 35