diff options
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.kt | 29 |
1 files changed, 17 insertions, 12 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..9e69790 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | 2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon |
3 | * Copyright (C) 2017-2020 Pacien TRAN-GIRARD | 3 | * Copyright (C) 2017-2023 Pacien TRAN-GIRARD |
4 | * | 4 | * |
5 | * This program is free software: you can redistribute it and/or modify | 5 | * This program is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by | 6 | * it under the terms of the GNU General Public License as published by |
@@ -18,7 +18,6 @@ | |||
18 | 18 | ||
19 | package org.pacien.tincapp.context | 19 | package org.pacien.tincapp.context |
20 | 20 | ||
21 | import android.os.Environment | ||
22 | import java.io.File | 21 | import java.io.File |
23 | import java.io.FileNotFoundException | 22 | import 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 | */ |
30 | object AppPaths { | 29 | object 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,27 @@ 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 privateCacheDir() = context.cacheDir!! |
50 | Environment.getExternalStorageState().let { it == Environment.MEDIA_MOUNTED && it != Environment.MEDIA_MOUNTED_READ_ONLY } | 53 | private fun publicCacheDir() = context.externalCacheDir!! |
51 | 54 | private fun publicFilesDir() = context.getExternalFilesDir(null) | |
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) | 55 | private fun binDir() = File(context.applicationInfo.nativeLibraryDir) |
56 | 56 | ||
57 | fun runtimeDir() = withDir(File(privateCacheDir(), APP_TINC_RUNTIME_DIR)) | ||
58 | fun logDir() = withDir(File(publicCacheDir(), APP_LOG_DIR)) | ||
59 | fun confDir() = withDir(File(publicFilesDir(), APP_TINC_NETWORKS_DIR)) | ||
60 | |||
57 | fun confDir(netName: String) = File(confDir(), netName) | 61 | fun confDir(netName: String) = File(confDir(), netName) |
58 | fun hostsDir(netName: String) = File(confDir(netName), NET_HOSTS_DIR) | 62 | fun hostsDir(netName: String) = File(confDir(netName), NET_HOSTS_DIR) |
59 | fun netConfFile(netName: String) = File(confDir(netName), NET_CONF_FILE) | 63 | fun netConfFile(netName: String) = File(confDir(netName), NET_CONF_FILE) |
60 | fun tincConfFile(netName: String) = File(confDir(netName), NET_TINC_CONF_FILE) | 64 | fun tincConfFile(netName: String) = File(confDir(netName), NET_TINC_CONF_FILE) |
61 | fun invitationFile(netName: String) = File(confDir(netName), NET_INVITATION_FILE) | 65 | fun invitationFile(netName: String) = File(confDir(netName), NET_INVITATION_FILE) |
62 | fun logFile(netName: String) = File(cacheDir(), String.format(LOGFILE_FORMAT, netName)) | 66 | fun logFile(netName: String) = File(logDir(), String.format(LOGFILE_FORMAT, netName)) |
63 | fun pidFile(netName: String) = File(context.cacheDir, String.format(PIDFILE_FORMAT, netName)) | 67 | fun pidFile(netName: String) = File(runtimeDir(), String.format(PIDFILE_FORMAT, netName)) |
64 | fun appLogFile() = File(cacheDir(), APPLOG_FILE) | 68 | fun appLogFile() = File(logDir(), APPLOG_FILE) |
65 | fun crashFlagFile() = File(internalCacheDir(), CRASHFLAG_FILE) | 69 | fun crashFlagFile() = File(privateCacheDir(), CRASHFLAG_FILE) |
66 | 70 | ||
67 | fun existing(f: File) = f.apply { if (!exists()) throw FileNotFoundException(f.absolutePath) } | 71 | fun existing(f: File) = f.apply { if (!exists()) throw FileNotFoundException(f.absolutePath) } |
72 | fun withDir(f: File) = f.apply { if (!exists()) mkdirs() } | ||
68 | 73 | ||
69 | fun defaultEd25519PrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_ED25519_PRIVATE_KEY_FILE) | 74 | 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) | 75 | fun defaultRsaPrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_RSA_PRIVATE_KEY_FILE) |