Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New, overhauled data storage management #1954

Merged
merged 11 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/gplay/java/com/owncloud/android/utils/PushUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import com.google.gson.Gson;
import com.owncloud.android.MainApp;
Expand Down Expand Up @@ -99,7 +100,7 @@ public static String bytesToHex(byte[] bytes) {
}

private static int generateRsa2048KeyPair() {
MainApp.migratePushKeys();
migratePushKeys();
String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator +
MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER;

Expand Down Expand Up @@ -335,4 +336,44 @@ private static int saveKeyToFile(Key key, String path) {

return -1;
}

public static void migratePushKeys() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be private void migratePushKeys() then :P

Context context = MainApp.getAppContext();

if (!PreferenceManager.getKeysMigration(context)) {
String oldKeyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder()
+ File.separator + "nc-keypair";
File oldPrivateKeyFile = new File(oldKeyPath, "push_key.priv");
File oldPublicKeyFile = new File(oldKeyPath, "push_key.pub");

String keyPath = context.getDir("nc-keypair", Context.MODE_PRIVATE).getAbsolutePath();
File privateKeyFile = new File(keyPath, "push_key.priv");
File publicKeyFile = new File(keyPath, "push_key.pub");

if ((privateKeyFile.exists() && publicKeyFile.exists()) ||
(!oldPrivateKeyFile.exists() && !oldPublicKeyFile.exists())) {
PreferenceManager.setKeysMigration(context, true);
} else {
if (oldPrivateKeyFile.exists()) {
try {
FileStorageUtils.moveFile(oldPrivateKeyFile, privateKeyFile);
} catch (IOException e) {
Log.e(TAG, "Failed to move old private key to new location");
}
}

if (oldPublicKeyFile.exists()) {
try {
FileStorageUtils.moveFile(oldPublicKeyFile, publicKeyFile);
} catch (IOException e) {
Log.e(TAG, "Failed to move old public key to new location");
}
}

if (privateKeyFile.exists() && publicKeyFile.exists()) {
PreferenceManager.setKeysMigration(context, true);
}
}
}
}
}
55 changes: 0 additions & 55 deletions src/main/java/com/owncloud/android/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import android.support.multidex.MultiDexApplication;
import android.support.v4.util.Pair;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.WindowManager;

import com.evernote.android.job.JobManager;
Expand Down Expand Up @@ -66,13 +65,6 @@
import com.owncloud.android.utils.PermissionUtil;
import com.owncloud.android.utils.ReceiversHelper;

import org.lukhnos.nnio.file.Files;
import org.lukhnos.nnio.file.Path;
import org.lukhnos.nnio.file.Paths;
import org.lukhnos.nnio.file.StandardCopyOption;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -245,53 +237,6 @@ public static void initAutoUpload() {
}
}

public static void migratePushKeys() {
Context context = getAppContext();

if (!PreferenceManager.getKeysMigration(context)) {
String oldKeyPath = getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair";
File oldPrivateKeyFile = new File(oldKeyPath, "push_key.priv");
File oldPublicKeyFile = new File(oldKeyPath, "push_key.pub");

Path oldPrivateKeyPath = Paths.get(oldPrivateKeyFile.toURI());
Path oldPublicKeyPath = Paths.get(oldPublicKeyFile.toURI());

String keyPath = getAppContext().getFilesDir().getAbsolutePath() +
File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair";
File privateKeyFile = new File(keyPath, "push_key.priv");
File publicKeyFile = new File(keyPath, "push_key.pub");

Path privateKeyPath = Paths.get(privateKeyFile.toURI());
Path publicKeyPath = Paths.get(publicKeyFile.toURI());


if ((privateKeyFile.exists() && publicKeyFile.exists()) ||
(!oldPrivateKeyFile.exists() && !oldPublicKeyFile.exists())) {
PreferenceManager.setKeysMigration(context, true);
} else {
if (oldPrivateKeyFile.exists()) {
try {
Files.move(oldPrivateKeyPath, privateKeyPath, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
Log.e(TAG, "Failed to move old private key to new location");
}
}

if (oldPublicKeyFile.exists()) {
try {
Files.move(oldPublicKeyPath, publicKeyPath, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
Log.e(TAG, "Failed to move old public key to new location");
}
}

if (privateKeyFile.exists() && publicKeyFile.exists()) {
PreferenceManager.setKeysMigration(context, true);
}
}
}
}

public static void notificationChannels() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && getAppContext() != null) {
Context context = getAppContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ public static boolean copyFile(File src, File target) {
return ret;
}

public static boolean moveFile(File sourceFile, File targetFile) throws IOException {
if (copyFile(sourceFile, targetFile)) {
return sourceFile.delete();
} else {
return false;
}
}

public static void deleteRecursively(File file, FileDataStorageManager storageManager) {
if (file.isDirectory()) {
for (File child : file.listFiles()) {
Expand Down