Refactor storage management to have a centralized,

clearer way to get the Signal output directories

Closes #6476
// FREEBIE
This commit is contained in:
Jonas Vautherin
2017-03-29 00:20:35 +02:00
committed by Moxie Marlinspike
parent 757ccd0c36
commit dd3cef5ec2
6 changed files with 389 additions and 226 deletions

View File

@@ -1,121 +0,0 @@
/**
* Copyright (C) 2011 Whisper Systems
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.database;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class EncryptedBackupExporter {
public static void exportToSd(Context context) throws NoExternalStorageException, IOException {
verifyExternalStorageForExport();
exportDirectory(context, "");
}
public static void importFromSd(Context context) throws NoExternalStorageException, IOException {
verifyExternalStorageForImport();
importDirectory(context, "");
}
private static String getExportDirectoryPath() {
File sdDirectory = Environment.getExternalStorageDirectory();
return sdDirectory.getAbsolutePath() + File.separator + "TextSecureExport";
}
private static void verifyExternalStorageForExport() throws NoExternalStorageException {
if (!Environment.getExternalStorageDirectory().canWrite())
throw new NoExternalStorageException();
String exportDirectoryPath = getExportDirectoryPath();
File exportDirectory = new File(exportDirectoryPath);
if (!exportDirectory.exists())
exportDirectory.mkdir();
}
private static void verifyExternalStorageForImport() throws NoExternalStorageException {
if (!Environment.getExternalStorageDirectory().canRead() ||
!(new File(getExportDirectoryPath()).exists()))
throw new NoExternalStorageException();
}
private static void migrateFile(File from, File to) {
try {
if (from.exists()) {
FileChannel source = new FileInputStream(from).getChannel();
FileChannel destination = new FileOutputStream(to).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
}
} catch (IOException ioe) {
Log.w("EncryptedBackupExporter", ioe);
}
}
private static void exportDirectory(Context context, String directoryName) throws IOException {
File directory = new File(context.getFilesDir().getParent() + File.separatorChar + directoryName);
File exportDirectory = new File(getExportDirectoryPath() + File.separatorChar + directoryName);
if (directory.exists()) {
exportDirectory.mkdirs();
File[] contents = directory.listFiles();
for (int i=0;i<contents.length;i++) {
File localFile = contents[i];
if (localFile.isFile()) {
File exportedFile = new File(exportDirectory.getAbsolutePath() + File.separator + localFile.getName());
migrateFile(localFile, exportedFile);
} else {
exportDirectory(context, directoryName + File.separator + localFile.getName());
}
}
} else {
Log.w("EncryptedBackupExporter", "Could not find directory: " + directory.getAbsolutePath());
}
}
private static void importDirectory(Context context, String directoryName) throws IOException {
File directory = new File(getExportDirectoryPath() + File.separator + directoryName);
File importDirectory = new File(context.getFilesDir().getParent() + File.separator + directoryName);
if (directory.exists() && directory.isDirectory()) {
importDirectory.mkdirs();
File[] contents = directory.listFiles();
for (File exportedFile : contents) {
if (exportedFile.isFile()) {
File localFile = new File(importDirectory.getAbsolutePath() + File.separator + exportedFile.getName());
migrateFile(exportedFile, localFile);
} else if (exportedFile.isDirectory()) {
importDirectory(context, directoryName + File.separator + exportedFile.getName());
}
}
}
}
}

View File

@@ -2,10 +2,10 @@ package org.thoughtcrime.securesms.database;
import android.content.Context;
import android.os.Environment;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
import org.thoughtcrime.securesms.util.StorageUtil;
import java.io.File;
import java.io.IOException;
@@ -17,21 +17,15 @@ public class PlaintextBackupExporter {
public static void exportPlaintextToSd(Context context, MasterSecret masterSecret)
throws NoExternalStorageException, IOException
{
verifyExternalStorageForPlaintextExport();
exportPlaintext(context, masterSecret);
}
private static void verifyExternalStorageForPlaintextExport() throws NoExternalStorageException {
if (!Environment.getExternalStorageDirectory().canWrite())
throw new NoExternalStorageException();
}
public static File getPlaintextExportFile() {
return new File(Environment.getExternalStorageDirectory(), FILENAME);
public static File getPlaintextExportFile() throws NoExternalStorageException {
return new File(StorageUtil.getBackupDir(), FILENAME);
}
private static void exportPlaintext(Context context, MasterSecret masterSecret)
throws IOException
throws NoExternalStorageException, IOException
{
int count = DatabaseFactory.getSmsDatabase(context).getMessageCount();
XmlBackup.Writer writer = new XmlBackup.Writer(getPlaintextExportFile().getAbsolutePath(), count);

View File

@@ -17,34 +17,10 @@ import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class PlaintextBackupImporter {
public static void importPlaintextFromSd(Context context, MasterSecret masterSecret)
throws NoExternalStorageException, IOException
{
Log.w("PlaintextBackupImporter", "Importing plaintext...");
verifyExternalStorageForPlaintextImport();
importPlaintext(context, masterSecret);
}
private static void verifyExternalStorageForPlaintextImport() throws NoExternalStorageException {
if (!Environment.getExternalStorageDirectory().canRead() || !getPlaintextExportFile().exists())
throw new NoExternalStorageException();
}
private static File getPlaintextExportFile() {
File backup = PlaintextBackupExporter.getPlaintextExportFile();
File oldBackup = new File(Environment.getExternalStorageDirectory(), "TextSecurePlaintextBackup.xml");
if (!backup.exists() && oldBackup.exists()) {
return oldBackup;
}
return backup;
}
private static void importPlaintext(Context context, MasterSecret masterSecret)
throws IOException
{
Log.w("PlaintextBackupImporter", "importPlaintext()");
SmsDatabase db = DatabaseFactory.getSmsDatabase(context);
@@ -54,7 +30,7 @@ public class PlaintextBackupImporter {
ThreadDatabase threads = DatabaseFactory.getThreadDatabase(context);
XmlBackup backup = new XmlBackup(getPlaintextExportFile().getAbsolutePath());
MasterCipher masterCipher = new MasterCipher(masterSecret);
Set<Long> modifiedThreads = new HashSet<Long>();
Set<Long> modifiedThreads = new HashSet<>();
XmlBackup.XmlBackupItem item;
while ((item = backup.getNext()) != null) {
@@ -78,7 +54,7 @@ public class PlaintextBackupImporter {
addTranslatedTypeToStatement(statement, 8, item.getType());
addNullToStatement(statement, 9);
addStringToStatement(statement, 10, item.getSubject());
addEncryptedStingToStatement(masterCipher, statement, 11, item.getBody());
addEncryptedStringToStatement(masterCipher, statement, 11, item.getBody());
addStringToStatement(statement, 12, item.getServiceCenter());
addLongToStatement(statement, 13, threadId);
modifiedThreads.add(threadId);
@@ -98,7 +74,14 @@ public class PlaintextBackupImporter {
}
}
private static void addEncryptedStingToStatement(MasterCipher masterCipher, SQLiteStatement statement, int index, String value) {
private static File getPlaintextExportFile() throws NoExternalStorageException {
File backup = PlaintextBackupExporter.getPlaintextExportFile();
File oldBackup = new File(Environment.getExternalStorageDirectory(), "TextSecurePlaintextBackup.xml");
return !backup.exists() && oldBackup.exists() ? oldBackup : backup;
}
private static void addEncryptedStringToStatement(MasterCipher masterCipher, SQLiteStatement statement, int index, String value) {
if (value == null || value.equals("null")) {
statement.bindNull(index);
} else {
@@ -130,6 +113,4 @@ public class PlaintextBackupImporter {
ourType == MmsSmsColumns.Types.BASE_SENT_TYPE ||
ourType == MmsSmsColumns.Types.BASE_SENT_FAILED_TYPE;
}
}