Эх сурвалжийг харах

use copy & delete instead of move

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 7 жил өмнө
parent
commit
88498e4071

+ 42 - 1
src/gplay/java/com/owncloud/android/utils/PushUtils.java

@@ -26,6 +26,7 @@ import android.accounts.OperationCanceledException;
 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;
@@ -99,7 +100,7 @@ public class PushUtils {
     }
     
     private static int generateRsa2048KeyPair() {
-        MainApp.migratePushKeys();
+        migratePushKeys();
         String keyPath = MainApp.getAppContext().getFilesDir().getAbsolutePath() + File.separator + 
                 MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER;
 
@@ -335,4 +336,44 @@ public class PushUtils {
 
         return -1;
     }
+
+    public static void migratePushKeys() {
+        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);
+                }
+            }
+        }
+    }
 }

+ 0 - 55
src/main/java/com/owncloud/android/MainApp.java

@@ -38,7 +38,6 @@ import android.support.annotation.StringRes;
 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;
@@ -66,13 +65,6 @@ import com.owncloud.android.utils.FilesSyncHelper;
 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;
@@ -245,53 +237,6 @@ public class MainApp extends MultiDexApplication {
         }
     }
 
-    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();

+ 8 - 0
src/main/java/com/owncloud/android/utils/FileStorageUtils.java

@@ -355,6 +355,14 @@ public class FileStorageUtils {
         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()) {