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