diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/context/StorageMigrator.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/context/StorageMigrator.kt | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/context/StorageMigrator.kt b/app/src/main/java/org/pacien/tincapp/context/StorageMigrator.kt new file mode 100644 index 0000000..e5740b8 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/context/StorageMigrator.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-2023 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.pacien.tincapp.extensions.Java.defaultMessage | ||
22 | import org.slf4j.LoggerFactory | ||
23 | import java.io.IOException | ||
24 | |||
25 | /** | ||
26 | * Migrates the configuration from the private storage (used before version 0.38) to the | ||
27 | * user-accessible storage (through the USB storage mode). | ||
28 | * | ||
29 | * @author pacien | ||
30 | */ | ||
31 | class StorageMigrator { | ||
32 | private val log by lazy { LoggerFactory.getLogger(this.javaClass)!! } | ||
33 | private val context by lazy { App.getContext() } | ||
34 | |||
35 | fun migrate() { | ||
36 | migrateConfigurationDirectory() | ||
37 | migrateLogDirectory() | ||
38 | } | ||
39 | |||
40 | private fun migrateConfigurationDirectory() { | ||
41 | val oldConfigDir = context.filesDir | ||
42 | if (oldConfigDir == null || oldConfigDir.listFiles().isNullOrEmpty()) return // nothing to do | ||
43 | |||
44 | try { | ||
45 | val newConfigDir = context.getExternalFilesDir(null)!! | ||
46 | log.info( | ||
47 | "Migrating files present in old configuration directory at {} to {}", | ||
48 | oldConfigDir.absolutePath, | ||
49 | newConfigDir.absolutePath | ||
50 | ) | ||
51 | |||
52 | oldConfigDir.copyRecursively(newConfigDir, overwrite = false) | ||
53 | oldConfigDir.deleteRecursively() | ||
54 | } catch (e: IOException) { | ||
55 | log.warn("Could not complete configuration directory migration: {}", e.defaultMessage()) | ||
56 | } | ||
57 | } | ||
58 | |||
59 | private fun migrateLogDirectory() { | ||
60 | val oldLogDir = context.cacheDir | ||
61 | if (oldLogDir == null || oldLogDir.listFiles().isNullOrEmpty()) return // nothing to do | ||
62 | |||
63 | try { | ||
64 | // There's no point moving the log files. Let's delete those instead. | ||
65 | log.info("Clearing old cache directory at {}", oldLogDir.absolutePath) | ||
66 | oldLogDir.deleteRecursively() | ||
67 | } catch (e: IOException) { | ||
68 | log.warn("Could not remove old cache directory: {}", e.defaultMessage()) | ||
69 | } | ||
70 | } | ||
71 | } | ||