diff options
author | pacien | 2020-12-08 16:10:42 +0100 |
---|---|---|
committer | pacien | 2020-12-08 16:10:42 +0100 |
commit | b1f98caca2f6960f7abb3ef7f7c27b903e1ef929 (patch) | |
tree | 71857113b01a354dbbd06e988d5fb9896aa9c6d5 /app/src/main/java/org/pacien | |
parent | 69445bffe7a17055ac47a41df5d33fe09b9aff3c (diff) | |
download | tincapp-b1f98caca2f6960f7abb3ef7f7c27b903e1ef929.tar.gz |
app: add configuration directory migrator
This adds an automatic configuration migrator which moves the configuration files previously located
on the external public-ish directory to the private app storage, so that existing users can seamlessly
transition to the next version.
The cache directory containing the logs is cleared instead of being migrated, because those files are
considered disposable.
GitHub: related to #103
Diffstat (limited to 'app/src/main/java/org/pacien')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/context/App.kt | 9 | ||||
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/context/ConfigurationDirectoryMigrator.kt | 63 |
2 files changed, 69 insertions, 3 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/context/App.kt b/app/src/main/java/org/pacien/tincapp/context/App.kt index 4d8d5d0..d825d78 100644 --- a/app/src/main/java/org/pacien/tincapp/context/App.kt +++ b/app/src/main/java/org/pacien/tincapp/context/App.kt | |||
@@ -28,6 +28,7 @@ import android.os.Handler | |||
28 | import androidx.annotation.StringRes | 28 | import androidx.annotation.StringRes |
29 | import org.pacien.tincapp.BuildConfig | 29 | import org.pacien.tincapp.BuildConfig |
30 | import org.pacien.tincapp.R | 30 | import org.pacien.tincapp.R |
31 | import org.slf4j.Logger | ||
31 | import org.slf4j.LoggerFactory | 32 | import org.slf4j.LoggerFactory |
32 | 33 | ||
33 | /** | 34 | /** |
@@ -39,15 +40,17 @@ class App : Application() { | |||
39 | appContext = applicationContext | 40 | appContext = applicationContext |
40 | handler = Handler() | 41 | handler = Handler() |
41 | AppLogger.configure() | 42 | AppLogger.configure() |
42 | setupCrashHandler() | ||
43 | 43 | ||
44 | val logger = LoggerFactory.getLogger(this.javaClass) | 44 | val logger = LoggerFactory.getLogger(this.javaClass) |
45 | setupCrashHandler(logger) | ||
46 | |||
45 | logger.info("Starting tinc app {} ({} build), running on {} ({})", | 47 | logger.info("Starting tinc app {} ({} build), running on {} ({})", |
46 | BuildConfig.VERSION_NAME, BuildConfig.BUILD_TYPE, Build.VERSION.CODENAME, Build.VERSION.RELEASE) | 48 | BuildConfig.VERSION_NAME, BuildConfig.BUILD_TYPE, Build.VERSION.CODENAME, Build.VERSION.RELEASE) |
49 | |||
50 | ConfigurationDirectoryMigrator().migrate() | ||
47 | } | 51 | } |
48 | 52 | ||
49 | private fun setupCrashHandler() { | 53 | private fun setupCrashHandler(logger: Logger) { |
50 | val logger = LoggerFactory.getLogger(this.javaClass) | ||
51 | val systemCrashHandler = Thread.getDefaultUncaughtExceptionHandler()!! | 54 | val systemCrashHandler = Thread.getDefaultUncaughtExceptionHandler()!! |
52 | val crashRecorder = CrashRecorder(logger, systemCrashHandler) | 55 | val crashRecorder = CrashRecorder(logger, systemCrashHandler) |
53 | Thread.setDefaultUncaughtExceptionHandler(crashRecorder) | 56 | Thread.setDefaultUncaughtExceptionHandler(crashRecorder) |
diff --git a/app/src/main/java/org/pacien/tincapp/context/ConfigurationDirectoryMigrator.kt b/app/src/main/java/org/pacien/tincapp/context/ConfigurationDirectoryMigrator.kt new file mode 100644 index 0000000..6ea914c --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/context/ConfigurationDirectoryMigrator.kt | |||
@@ -0,0 +1,63 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2020 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 | |||
19 | package org.pacien.tincapp.context | ||
20 | |||
21 | import org.apache.commons.io.FileExistsException | ||
22 | import org.apache.commons.io.FileUtils | ||
23 | import org.slf4j.LoggerFactory | ||
24 | |||
25 | /** | ||
26 | * Migrates the configuration from the external storage (used before version 0.32) to the internal storage. | ||
27 | * | ||
28 | * @author pacien | ||
29 | */ | ||
30 | class ConfigurationDirectoryMigrator { | ||
31 | private val log by lazy { LoggerFactory.getLogger(this.javaClass)!! } | ||
32 | private val context by lazy { App.getContext() } | ||
33 | private val paths = AppPaths | ||
34 | |||
35 | fun migrate() { | ||
36 | migrateConfigurationDirectory() | ||
37 | migrateLogDirectory() | ||
38 | } | ||
39 | |||
40 | private fun migrateConfigurationDirectory() { | ||
41 | val oldConfigDir = context.getExternalFilesDir(null) | ||
42 | if (oldConfigDir == null || oldConfigDir.listFiles().isNullOrEmpty()) return // nothing to do | ||
43 | |||
44 | log.info("Configuration files present in old configuration directory at {}.", oldConfigDir.absolutePath) | ||
45 | for (oldConfig in oldConfigDir.listFiles() ?: emptyArray()) { | ||
46 | try { | ||
47 | log.info("Migrating {} to {}", oldConfig.absolutePath, paths.confDir()) | ||
48 | FileUtils.moveToDirectory(oldConfig, paths.confDir(), true) | ||
49 | } catch (e: FileExistsException) { | ||
50 | log.warn("Could not migrate {}: target already exists.", oldConfig.absolutePath) | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
55 | private fun migrateLogDirectory() { | ||
56 | val oldLogDir = context.externalCacheDir | ||
57 | if (oldLogDir == null || oldLogDir.listFiles().isNullOrEmpty()) return // nothing to do | ||
58 | |||
59 | // There's no point moving the log files. Let's delete those instead. | ||
60 | log.info("Clearing old cache directory at {}", oldLogDir.absolutePath) | ||
61 | FileUtils.cleanDirectory(oldLogDir) | ||
62 | } | ||
63 | } | ||