xxfast / KStore

A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisation and kotlinx.io
https://xxfast.github.io/KStore/
Apache License 2.0
470 stars 15 forks source link

java.io.IOException: failed to create directory: tmp #67

Closed vbudilov closed 8 months ago

vbudilov commented 10 months ago

Thanks for this awesome library!

I've set it up as per your instructions but when I go to use it I'm getting the following exceptions (essentially it can't create the directory or it can't store the file due to a read-only FS. Did I miss a permission request or something?

How I initialized it

private val store: KStore<JWTTokens> = storeOf(filePath = "tmp/jwt.session")

with 'tmp'

java.io.IOException: failed to create directory: tmp
                                                                                                        at okio.JvmSystemFileSystem.createDirectory(JvmSystemFileSystem.kt:116)
                                                                                                        at okio.FileSystem.createDirectory(FileSystem.kt:95)

without the 'tmp' prefix

java.io.FileNotFoundException: jwt.session: open failed: EROFS (Read-only file system)

vbudilov commented 10 months ago

Just to confirm, this is happening only on Android. Here's my main.android.kt file:

import android.annotation.SuppressLint
import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext

@SuppressLint("StaticFieldLeak")
var context: Context? = null

@Composable
fun MainView() {
    context = LocalContext.current

    App()
}

actual fun pathTo(id: String): String = "${context?.filesDir?.path}/$id.json"
xxfast commented 8 months ago

context?.filesDir?.path gives you the absolute path - and I think you need the relative one. You should be able to do

actual fun pathTo(id: String): String = "${context.filesDir}/$id.json"

Also, don't keep a static reference to context 😅

xxfast commented 8 months ago

Updated docs with setting up platform-specific paths here