aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
diff options
context:
space:
mode:
authorpacien2020-12-08 16:04:48 +0100
committerpacien2020-12-08 16:04:48 +0100
commit69445bffe7a17055ac47a41df5d33fe09b9aff3c (patch)
tree61a00dfe1c0d3085386f40c0c0d8c8cbb427a33b /app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
parent9c0aa8bd9dc694854b5c4b5eb07ef4d8942709a8 (diff)
downloadtincapp-69445bffe7a17055ac47a41df5d33fe09b9aff3c.tar.gz
context: read configuration from and write logs and temp files to private app storage
The external app public storage directory is no longer reliably accessible on Android 11 and above. This makes editing the configuration and accessing the log files impossible in some cases. Let's move to the app private storage, to be made accessible to the user by some other mean. This has the benefit of also protecting the private keys that need to be stored encrypted otherwise. We also split the configuration and cache directory into specialised sub-directories. GitHub: related to #103
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/context/AppPaths.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/context/AppPaths.kt24
1 files changed, 13 insertions, 11 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
index bd8316a..9aa2037 100644
--- a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
+++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
@@ -18,7 +18,6 @@
18 18
19package org.pacien.tincapp.context 19package org.pacien.tincapp.context
20 20
21import android.os.Environment
22import java.io.File 21import java.io.File
23import java.io.FileNotFoundException 22import java.io.FileNotFoundException
24 23
@@ -28,6 +27,10 @@ import java.io.FileNotFoundException
28 * @implNote Logs and PID files are stored in the cache directory for automatic collection. 27 * @implNote Logs and PID files are stored in the cache directory for automatic collection.
29 */ 28 */
30object AppPaths { 29object AppPaths {
30 private const val APP_LOG_DIR = "log"
31 private const val APP_TINC_RUNTIME_DIR = "run"
32 private const val APP_TINC_NETWORKS_DIR = "networks"
33
31 private const val TINCD_BIN = "libtincd.so" 34 private const val TINCD_BIN = "libtincd.so"
32 private const val TINC_BIN = "libtinc.so" 35 private const val TINC_BIN = "libtinc.so"
33 36
@@ -46,25 +49,24 @@ object AppPaths {
46 49
47 private val context by lazy { App.getContext() } 50 private val context by lazy { App.getContext() }
48 51
49 fun storageAvailable() = 52 private fun cacheDir() = context.cacheDir!!
50 Environment.getExternalStorageState().let { it == Environment.MEDIA_MOUNTED && it != Environment.MEDIA_MOUNTED_READ_ONLY }
51
52 fun internalCacheDir() = context.cacheDir!!
53 fun cacheDir() = context.externalCacheDir ?: internalCacheDir()
54 fun confDir() = context.getExternalFilesDir(null)!!
55 private fun binDir() = File(context.applicationInfo.nativeLibraryDir) 53 private fun binDir() = File(context.applicationInfo.nativeLibraryDir)
54 fun runtimeDir() = withDir(File(cacheDir(), APP_TINC_RUNTIME_DIR))
55 fun logDir() = withDir(File(cacheDir(), APP_LOG_DIR))
56 fun confDir() = withDir(File(context.filesDir!!, APP_TINC_NETWORKS_DIR))
56 57
57 fun confDir(netName: String) = File(confDir(), netName) 58 fun confDir(netName: String) = File(confDir(), netName)
58 fun hostsDir(netName: String) = File(confDir(netName), NET_HOSTS_DIR) 59 fun hostsDir(netName: String) = File(confDir(netName), NET_HOSTS_DIR)
59 fun netConfFile(netName: String) = File(confDir(netName), NET_CONF_FILE) 60 fun netConfFile(netName: String) = File(confDir(netName), NET_CONF_FILE)
60 fun tincConfFile(netName: String) = File(confDir(netName), NET_TINC_CONF_FILE) 61 fun tincConfFile(netName: String) = File(confDir(netName), NET_TINC_CONF_FILE)
61 fun invitationFile(netName: String) = File(confDir(netName), NET_INVITATION_FILE) 62 fun invitationFile(netName: String) = File(confDir(netName), NET_INVITATION_FILE)
62 fun logFile(netName: String) = File(cacheDir(), String.format(LOGFILE_FORMAT, netName)) 63 fun logFile(netName: String) = File(logDir(), String.format(LOGFILE_FORMAT, netName))
63 fun pidFile(netName: String) = File(context.cacheDir, String.format(PIDFILE_FORMAT, netName)) 64 fun pidFile(netName: String) = File(runtimeDir(), String.format(PIDFILE_FORMAT, netName))
64 fun appLogFile() = File(cacheDir(), APPLOG_FILE) 65 fun appLogFile() = File(logDir(), APPLOG_FILE)
65 fun crashFlagFile() = File(internalCacheDir(), CRASHFLAG_FILE) 66 fun crashFlagFile() = File(cacheDir(), CRASHFLAG_FILE)
66 67
67 fun existing(f: File) = f.apply { if (!exists()) throw FileNotFoundException(f.absolutePath) } 68 fun existing(f: File) = f.apply { if (!exists()) throw FileNotFoundException(f.absolutePath) }
69 fun withDir(f: File) = f.apply { if (!exists()) mkdirs() }
68 70
69 fun defaultEd25519PrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_ED25519_PRIVATE_KEY_FILE) 71 fun defaultEd25519PrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_ED25519_PRIVATE_KEY_FILE)
70 fun defaultRsaPrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_RSA_PRIVATE_KEY_FILE) 72 fun defaultRsaPrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_RSA_PRIVATE_KEY_FILE)