aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
diff options
context:
space:
mode:
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.kt71
1 files changed, 71 insertions, 0 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
new file mode 100644
index 0000000..0b85565
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt
@@ -0,0 +1,71 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
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
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.tincapp.context
20
21import android.os.Environment
22import java.io.File
23import java.io.FileNotFoundException
24
25/**
26 * @author pacien
27 *
28 * @implNote Logs and PID files are stored in the cache directory for easy clean up.
29 */
30object AppPaths {
31 private const val TINCD_BIN = "libtincd.so"
32 private const val TINC_BIN = "libtinc.so"
33
34 private const val APPLOG_FILE = "tincapp.log"
35 private const val CRASHFLAG_FILE = "crash.flag"
36 private const val LOGFILE_FORMAT = "tinc.%s.log"
37 private const val PIDFILE_FORMAT = "tinc.%s.pid"
38
39 private const val NET_CONF_FILE = "network.conf"
40 private const val NET_TINC_CONF_FILE = "tinc.conf"
41 private const val NET_HOSTS_DIR = "hosts"
42 private const val NET_INVITATION_FILE = "invitation-data"
43 private const val NET_DEFAULT_ED25519_PRIVATE_KEY_FILE = "ed25519_key.priv"
44 private const val NET_DEFAULT_RSA_PRIVATE_KEY_FILE = "rsa_key.priv"
45
46 fun storageAvailable() =
47 Environment.getExternalStorageState().let { it == Environment.MEDIA_MOUNTED && it != Environment.MEDIA_MOUNTED_READ_ONLY }
48
49 private fun internalCacheDir() = App.getContext().cacheDir!!
50 fun cacheDir() = App.getContext().externalCacheDir!!
51 fun confDir() = App.getContext().getExternalFilesDir(null)!!
52 private fun binDir() = File(App.getContext().applicationInfo.nativeLibraryDir)
53
54 fun confDir(netName: String) = File(confDir(), netName)
55 fun hostsDir(netName: String) = File(confDir(netName), NET_HOSTS_DIR)
56 fun netConfFile(netName: String) = File(confDir(netName), NET_CONF_FILE)
57 fun tincConfFile(netName: String) = File(confDir(netName), NET_TINC_CONF_FILE)
58 fun invitationFile(netName: String) = File(confDir(netName), NET_INVITATION_FILE)
59 fun logFile(netName: String) = File(cacheDir(), String.format(LOGFILE_FORMAT, netName))
60 fun pidFile(netName: String) = File(App.getContext().cacheDir, String.format(PIDFILE_FORMAT, netName))
61 fun appLogFile() = File(cacheDir(), APPLOG_FILE)
62 fun crashFlagFile() = File(internalCacheDir(), CRASHFLAG_FILE)
63
64 fun existing(f: File) = f.apply { if (!exists()) throw FileNotFoundException(f.absolutePath) }
65
66 fun defaultEd25519PrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_ED25519_PRIVATE_KEY_FILE)
67 fun defaultRsaPrivateKeyFile(netName: String) = File(confDir(netName), NET_DEFAULT_RSA_PRIVATE_KEY_FILE)
68
69 fun tincd() = File(binDir(), TINCD_BIN)
70 fun tinc() = File(binDir(), TINC_BIN)
71}