aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-04-10 00:15:26 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-04-10 00:15:26 +0530
commitf0868a1b7af3b8ce5a91ffb0e6dab96ba3d55097 (patch)
tree9c76e7cd5b5358f55d08fe32c58e16be2a8f1fb6
parentWIP: revisit async tunnel import (diff)
downloadwireguard-android-f0868a1b7af3b8ce5a91ffb0e6dab96ba3d55097.tar.xz
wireguard-android-f0868a1b7af3b8ce5a91ffb0e6dab96ba3d55097.zip
configStore: make all IO operations suspend
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
-rw-r--r--ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt10
-rw-r--r--ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt44
2 files changed, 33 insertions, 21 deletions
diff --git a/ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt b/ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt
index 69d2bba7..96deb2d2 100644
--- a/ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt
+++ b/ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt
@@ -19,7 +19,7 @@ interface ConfigStore {
* @return The configuration that was actually saved to persistent storage.
*/
@Throws(Exception::class)
- fun create(name: String, config: Config): Config
+ suspend fun create(name: String, config: Config): Config
/**
* Delete a persistent tunnel.
@@ -27,7 +27,7 @@ interface ConfigStore {
* @param name The name of the tunnel to delete.
*/
@Throws(Exception::class)
- fun delete(name: String)
+ suspend fun delete(name: String)
/**
* Enumerate the names of tunnels present in persistent storage.
@@ -44,7 +44,7 @@ interface ConfigStore {
* @return An in-memory representation of the configuration loaded from persistent storage.
*/
@Throws(Exception::class)
- fun load(name: String): Config
+ suspend fun load(name: String): Config
/**
* Rename the configuration for the tunnel given by `name`.
@@ -53,7 +53,7 @@ interface ConfigStore {
* @param replacement The new identifier for the configuration in persistent storage.
*/
@Throws(Exception::class)
- fun rename(name: String, replacement: String)
+ suspend fun rename(name: String, replacement: String)
/**
* Save the configuration for an existing tunnel given by `name`.
@@ -64,5 +64,5 @@ interface ConfigStore {
* @return The configuration that was actually saved to persistent storage.
*/
@Throws(Exception::class)
- fun save(name: String, config: Config): Config
+ suspend fun save(name: String, config: Config): Config
}
diff --git a/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt b/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt
index 99dca71e..1ecca7cc 100644
--- a/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt
+++ b/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt
@@ -9,6 +9,8 @@ import android.util.Log
import com.wireguard.android.R
import com.wireguard.config.BadConfigException
import com.wireguard.config.Config
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
@@ -21,21 +23,25 @@ import java.nio.charset.StandardCharsets
*/
class FileConfigStore(private val context: Context) : ConfigStore {
@Throws(IOException::class)
- override fun create(name: String, config: Config): Config {
+ override suspend fun create(name: String, config: Config): Config {
Log.d(TAG, "Creating configuration for tunnel $name")
val file = fileFor(name)
- if (!file.createNewFile())
- throw IOException(context.getString(R.string.config_file_exists_error, file.name))
- FileOutputStream(file, false).use { it.write(config.toWgQuickString().toByteArray(StandardCharsets.UTF_8)) }
+ withContext(Dispatchers.IO) {
+ if (!file.createNewFile())
+ throw IOException(context.getString(R.string.config_file_exists_error, file.name))
+ FileOutputStream(file, false).use { it.write(config.toWgQuickString().toByteArray(StandardCharsets.UTF_8)) }
+ }
return config
}
@Throws(IOException::class)
- override fun delete(name: String) {
+ override suspend fun delete(name: String) {
Log.d(TAG, "Deleting configuration for tunnel $name")
val file = fileFor(name)
- if (!file.delete())
- throw IOException(context.getString(R.string.config_delete_error, file.name))
+ withContext(Dispatchers.IO) {
+ if (!file.delete())
+ throw IOException(context.getString(R.string.config_delete_error, file.name))
+ }
}
override suspend fun enumerate(): Set<String> {
@@ -50,29 +56,35 @@ class FileConfigStore(private val context: Context) : ConfigStore {
}
@Throws(BadConfigException::class, IOException::class)
- override fun load(name: String): Config {
- FileInputStream(fileFor(name)).use { stream -> return Config.parse(stream) }
+ override suspend fun load(name: String): Config {
+ return withContext(Dispatchers.IO) {
+ FileInputStream(fileFor(name)).use { stream -> Config.parse(stream) }
+ }
}
@Throws(IOException::class)
- override fun rename(name: String, replacement: String) {
+ override suspend fun rename(name: String, replacement: String) {
Log.d(TAG, "Renaming configuration for tunnel $name to $replacement")
val file = fileFor(name)
val replacementFile = fileFor(replacement)
- if (!replacementFile.createNewFile()) throw IOException(context.getString(R.string.config_exists_error, replacement))
- if (!file.renameTo(replacementFile)) {
- if (!replacementFile.delete()) Log.w(TAG, "Couldn't delete marker file for new name $replacement")
- throw IOException(context.getString(R.string.config_rename_error, file.name))
+ withContext(Dispatchers.IO) {
+ if (!replacementFile.createNewFile()) throw IOException(context.getString(R.string.config_exists_error, replacement))
+ if (!file.renameTo(replacementFile)) {
+ if (!replacementFile.delete()) Log.w(TAG, "Couldn't delete marker file for new name $replacement")
+ throw IOException(context.getString(R.string.config_rename_error, file.name))
+ }
}
}
@Throws(IOException::class)
- override fun save(name: String, config: Config): Config {
+ override suspend fun save(name: String, config: Config): Config {
Log.d(TAG, "Saving configuration for tunnel $name")
val file = fileFor(name)
if (!file.isFile)
throw FileNotFoundException(context.getString(R.string.config_not_found_error, file.name))
- FileOutputStream(file, false).use { stream -> stream.write(config.toWgQuickString().toByteArray(StandardCharsets.UTF_8)) }
+ withContext(Dispatchers.IO) {
+ FileOutputStream(file, false).use { stream -> stream.write(config.toWgQuickString().toByteArray(StandardCharsets.UTF_8)) }
+ }
return config
}