diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/context/ConfigurationDirectoryMigrator.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/context/ConfigurationDirectoryMigrator.kt | 63 |
1 files changed, 63 insertions, 0 deletions
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 | } | ||