Skip to content

Commit

Permalink
Move rename operation to Filesystems
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelMess committed Jan 29, 2022
1 parent 4620cdf commit 03e016e
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 158 deletions.
130 changes: 30 additions & 100 deletions app/src/main/java/com/amaze/filemanager/filesystem/Operations.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

public class Operations {

private static Executor executor = AsyncTask.THREAD_POOL_EXECUTOR;
private static final Executor executor = AsyncTask.THREAD_POOL_EXECUTOR;

private static final String TAG = Operations.class.getSimpleName();

Expand Down Expand Up @@ -120,7 +120,7 @@ public static void mkdir(
@NonNull final ErrorCallBack errorCallBack) {

new AsyncTask<Void, Void, Void>() {
private Function<DocumentFile, Void> safCreateDirectory =
private final Function<DocumentFile, Void> safCreateDirectory =
input -> {
if (input != null && input.isDirectory()) {
boolean result = false;
Expand Down Expand Up @@ -251,9 +251,9 @@ public static void mkfile(

new AsyncTask<Void, Void, Void>() {

private DataUtils dataUtils = DataUtils.getInstance();
private final DataUtils dataUtils = DataUtils.getInstance();

private Function<DocumentFile, Void> safCreateFile =
private final Function<DocumentFile, Void> safCreateFile =
input -> {
if (input != null && input.isDirectory()) {
boolean result = false;
Expand Down Expand Up @@ -425,18 +425,6 @@ public static void rename(

private final DataUtils dataUtils = DataUtils.getInstance();

private Function<DocumentFile, Void> safRenameFile =
input -> {
boolean result = false;
try {
result = input.renameTo(newFile.getName(context));
} catch (Exception e) {
Log.w(getClass().getSimpleName(), "Failed to rename", e);
}
errorCallBack.done(newFile, result);
return null;
};

@Override
protected Void doInBackground(Void... params) {
// check whether file names for new file are valid or recursion occurs.
Expand All @@ -450,17 +438,17 @@ protected Void doInBackground(Void... params) {
errorCallBack.exists(newFile);
return null;
}
AmazeFile amazeFile = new AmazeFile(oldFile.getPath());
// FIXME: smbFile1 should be created from SmbUtil too so it can be mocked
AmazeFile amazeFile1 = new AmazeFile(newFile.getPath());

if (oldFile.isSmb()) {
AmazeFile smbFile = new AmazeFile(oldFile.getPath());
// FIXME: smbFile1 should be created from SmbUtil too so it can be mocked
AmazeFile smbFile1 = new AmazeFile(newFile.getPath());
if (newFile.exists()) {
errorCallBack.exists(newFile);
return null;
}
smbFile.renameTo(smbFile1, () -> context);
if (!smbFile.exists(() -> context) && smbFile1.exists(() -> context)) {
amazeFile.renameTo(amazeFile1, () -> context);
if (!amazeFile.exists(() -> context) && amazeFile1.exists(() -> context)) {
errorCallBack.done(newFile, true);
return null;
} else {
Expand All @@ -474,104 +462,46 @@ protected Void doInBackground(Void... params) {
Log.e(TAG, "Error creating HybridFileParcelable", exceptionThrownDuringBuildParcelable);
}
}
return null;
} else if (oldFile.isSftp()) {
SshClientUtils.execute(
new SFtpClientTemplate<Void>(oldFile.getPath()) {
@Override
public Void execute(@NonNull SFTPClient client) {
try {
client.rename(
SshClientUtils.extractRemotePathFrom(oldFile.getPath()),
SshClientUtils.extractRemotePathFrom(newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (IOException e) {
String errmsg =
context.getString(
R.string.cannot_rename_file,
HybridFile.parseAndFormatUriForDisplay(oldFile.getPath()),
e.getMessage());
Log.e(TAG, errmsg);
ArrayList<HybridFileParcelable> failedOps = new ArrayList<>();
// Nobody care the size or actual permission here. Put a simple "r" and zero
// here
failedOps.add(
new HybridFileParcelable(
boolean result = amazeFile.renameTo(amazeFile1, () -> context);

if(!result) {
ArrayList<HybridFileParcelable> failedOps = new ArrayList<>();
// Nobody care the size or actual permission here. Put a simple "r" and zero
// here
failedOps.add(
new HybridFileParcelable(
oldFile.getPath(),
"r",
oldFile.lastModified(),
0,
oldFile.isDirectory(context)));
context.sendBroadcast(
new Intent(TAG_INTENT_FILTER_GENERAL)
context.sendBroadcast(
new Intent(TAG_INTENT_FILTER_GENERAL)
.putParcelableArrayListExtra(TAG_INTENT_FILTER_FAILED_OPS, failedOps));
errorCallBack.done(newFile, false);
}
return null;
}
});
} else if (oldFile.isDropBoxFile()) {
CloudStorage cloudStorageDropbox = DropboxAccount.INSTANCE.getAccount();
try {
cloudStorageDropbox.move(
CloudUtil.stripPath(OpenMode.DROPBOX, oldFile.getPath()),
CloudUtil.stripPath(OpenMode.DROPBOX, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isBoxFile()) {
CloudStorage cloudStorageBox = BoxAccount.INSTANCE.getAccount();
try {
cloudStorageBox.move(
CloudUtil.stripPath(OpenMode.BOX, oldFile.getPath()),
CloudUtil.stripPath(OpenMode.BOX, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isOneDriveFile()) {
CloudStorage cloudStorageOneDrive = OnedriveAccount.INSTANCE.getAccount();
try {
cloudStorageOneDrive.move(
CloudUtil.stripPath(OpenMode.ONEDRIVE, oldFile.getPath()),
CloudUtil.stripPath(OpenMode.ONEDRIVE, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isGoogleDriveFile()) {
CloudStorage cloudStorageGdrive = GoogledriveAccount.INSTANCE.getAccount();
try {
cloudStorageGdrive.move(
CloudUtil.stripPath(OpenMode.GDRIVE, oldFile.getPath()),
CloudUtil.stripPath(OpenMode.GDRIVE, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}

errorCallBack.done(newFile, result);
} else if (oldFile.isDropBoxFile() || oldFile.isBoxFile() || oldFile.isOneDriveFile()
|| oldFile.isGoogleDriveFile()) {
boolean result = amazeFile.renameTo(amazeFile1, () -> context);
errorCallBack.done(newFile, result);
} else if (oldFile.isOtgFile()) {
if (checkOtgNewFileExists(newFile, context)) {
errorCallBack.exists(newFile);
return null;
}
safRenameFile.apply(OTGUtil.getDocumentFile(oldFile.getPath(), context, false));
boolean result = amazeFile.renameTo(amazeFile1, () -> context);
errorCallBack.done(newFile, result);
return null;
} else if (oldFile.isDocumentFile()) {
if (checkDocumentFileNewFileExists(newFile, context)) {
errorCallBack.exists(newFile);
return null;
}
safRenameFile.apply(
OTGUtil.getDocumentFile(
oldFile.getPath(),
SafRootHolder.getUriRoot(),
context,
OpenMode.DOCUMENT_FILE,
false));
boolean result = amazeFile.renameTo(amazeFile1, () -> context);
errorCallBack.done(newFile, result);
return null;
} else {
File file = new File(oldFile.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.amaze.filemanager.file_operations.filesystem.filetypes.ContextProvide
import com.amaze.filemanager.filesystem.FileProperties.getDeviceStorageRemainingSpace
import com.amaze.filemanager.filesystem.SafRootHolder.uriRoot
import com.amaze.filemanager.filesystem.SafRootHolder.volumeLabel
import com.amaze.filemanager.filesystem.otg.OtgAmazeFilesystem
import com.amaze.filemanager.utils.OTGUtil.getDocumentFile
import java.io.FileNotFoundException
import java.io.IOException
Expand Down Expand Up @@ -172,12 +173,28 @@ object DocumentFileAmazeFilesystem: AmazeFilesystem() {
return false
}

override fun rename(f1: AmazeFile, f2: AmazeFile, contextProvider: ContextProvider): Boolean {
TODO("Not yet implemented")
override fun rename(file1: AmazeFile, file2: AmazeFile, contextProvider: ContextProvider): Boolean {
val context = contextProvider.getContext() ?: return false
val uriRoot = uriRoot ?: return false

val documentFile = getDocumentFile(
file1.path,
uriRoot,
context,
OpenMode.DOCUMENT_FILE,
false
) ?: return false

return try {
documentFile.renameTo(file2.name)
} catch (e: Exception) {
Log.e(TAG, "Error renaming DocumentFile file", e)
false
}
}

override fun setLastModifiedTime(f: AmazeFile, time: Long): Boolean {
TODO("Not yet implemented")
return false //Can't set
}

override fun setReadOnly(f: AmazeFile): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.amaze.filemanager.filesystem.otg

import android.util.Log
import com.amaze.filemanager.utils.OTGUtil.getDocumentFile
import com.amaze.filemanager.file_operations.filesystem.filetypes.AmazeFilesystem
import com.amaze.filemanager.file_operations.filesystem.filetypes.AmazeFile
import com.amaze.filemanager.file_operations.filesystem.filetypes.AmazeFilesystem
import com.amaze.filemanager.file_operations.filesystem.filetypes.ContextProvider
import com.amaze.filemanager.utils.OTGUtil
import com.amaze.filemanager.utils.OTGUtil.getDocumentFile
import java.io.*
import java.util.ArrayList

object OtgAmazeFilesystem : AmazeFilesystem() {
@JvmStatic
Expand Down Expand Up @@ -162,19 +162,23 @@ object OtgAmazeFilesystem : AmazeFilesystem() {
}
val context = contextProvider.getContext() ?: return false
val parent = f.parent ?: return false
val parentDirectory = getDocumentFile(parent, context, true)
if (parentDirectory!!.isDirectory) {
val parentDirectory = getDocumentFile(parent, context, true) ?: return false
if (parentDirectory.isDirectory) {
parentDirectory.createDirectory(f.name)
return true
}
return false
}

override fun rename(f1: AmazeFile, f2: AmazeFile, contextProvider: ContextProvider): Boolean {
val context = contextProvider.getContext()
val contentResolver = context!!.contentResolver
val documentSourceFile = getDocumentFile(f1.path, context, true)
return documentSourceFile!!.renameTo(f2.path)
override fun rename(file1: AmazeFile, file2: AmazeFile, contextProvider: ContextProvider): Boolean {
val context = contextProvider.getContext() ?: return false
val documentSourceFile = getDocumentFile(file1.path, context, false) ?: return false
return try {
documentSourceFile.renameTo(file2.name)
} catch (e: UnsupportedOperationException) {
Log.e(TAG, "Error renaming OTG file", e)
false
}
}

override fun setLastModifiedTime(f: AmazeFile, time: Long): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.amaze.filemanager.filesystem.ssh;

import static com.amaze.filemanager.ui.activities.MainActivity.TAG_INTENT_FILTER_FAILED_OPS;
import static com.amaze.filemanager.ui.activities.MainActivity.TAG_INTENT_FILTER_GENERAL;
import static net.schmizz.sshj.sftp.FileMode.Type.DIRECTORY;
import static net.schmizz.sshj.sftp.FileMode.Type.REGULAR;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.amaze.filemanager.R;
import com.amaze.filemanager.file_operations.filesystem.filetypes.AmazeFile;
import com.amaze.filemanager.file_operations.filesystem.filetypes.AmazeFilesystem;
import com.amaze.filemanager.file_operations.filesystem.filetypes.ContextProvider;
import com.amaze.filemanager.filesystem.HybridFile;
import com.amaze.filemanager.filesystem.HybridFileParcelable;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.Buffer;
Expand Down Expand Up @@ -358,17 +365,27 @@ public Void execute(@NonNull SFTPClient client) {
}

@Override
public boolean rename(AmazeFile f1, AmazeFile f2, @NonNull ContextProvider contextProvider) {
public boolean rename(AmazeFile file1, AmazeFile file2, @NonNull ContextProvider contextProvider) {
@Nullable final Context context = contextProvider.getContext();

if(context == null) {
Log.e(TAG, "Error getting context for renaming ssh file");
return false;
}

Boolean retval =
SshClientUtils.<Boolean>execute(
new SFtpClientTemplate(f1.getPath()) {
SshClientUtils.execute(
new SFtpClientTemplate<Boolean>(file1.getPath()) {
@Override
public Boolean execute(@NonNull SFTPClient client) throws IOException {
public Boolean execute(@NonNull SFTPClient client) {
try {
client.rename(f1.getPath(), f2.getPath());
client.rename(
SshClientUtils.extractRemotePathFrom(file1.getPath()),
SshClientUtils.extractRemotePathFrom(file2.getPath())
);
return true;
} catch (IOException e) {
Log.e(TAG, "Error renaming over SFTP", e);
Log.e(TAG, "Error renaming ssh file", e);
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@
<string name="disableIpcSignature">الغاء اتاحة التحقق من توقيع IPC (بالنسبة لوحدات خدمة SMBv1 القديمة-UNSAFE !) </string>
<string name="cannot_delete_file">لا يمكن حذف الملف </string>
<string name="cannot_delete_file_with_reason">لا يمكن حذف الملف-%s </string>
<string name="cannot_rename_file">لا يمكن اعادة تسمية الملف %s-%s </string>
<string name="cannot_read_directory">لا يمكن قراءة الدليل %s-%s </string>
<string name="just_once">مرة واحدة فقط </string>
<string name="always">دائما </string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ andernfalls wird nach Treffern gesucht.</string>
<string name="disableIpcSignature">IPC Signaturprüfung deaktivieren (Für veraltete SMBv1 Server - UNSICHER!)</string>
<string name="cannot_delete_file">Datei kann nicht gelöscht werden</string>
<string name="cannot_delete_file_with_reason">Datei %s kann nicht gelöscht werden</string>
<string name="cannot_rename_file">Datei %s - %s kann nicht umbenannt werden</string>
<string name="cannot_read_directory">Verzeichnis %s - %s kann nicht gelesen werden</string>
<string name="just_once">Nur einmal</string>
<string name="always">Immer</string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,6 @@
<string name="disableIpcSignature">Desactive la verificación de firma de IPC (para servidores SMBv1 heredados - ¡INSEGURO!) </string>
<string name="cannot_delete_file">No se puede eliminar el archivo</string>
<string name="cannot_delete_file_with_reason">No se puede eliminar el archivo - %s</string>
<string name="cannot_rename_file">No se puede renombrar el archivo %s - %s</string>
<string name="cannot_read_directory">No se puede leer el directorio %s - %s</string>
<string name="just_once">Sólo una vez</string>
<string name="always">Siempre</string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,6 @@
<string name="disableIpcSignature">Désactiver la vérification de la signature IPC (Pour les anciens serveurs SMBv1 - RISQUÉ!)</string>
<string name="cannot_delete_file">Impossible de supprimer le fichier</string>
<string name="cannot_delete_file_with_reason">Impossible de supprimer le fichier - %s</string>
<string name="cannot_rename_file">Impossible de renommer le fichier %s - %s</string>
<string name="cannot_read_directory">Impossible de lire le répertoire %s - %s</string>
<string name="just_once">Juste une fois</string>
<string name="always">Toujours</string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-he/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,6 @@
<string name="disableIpcSignature">השבתת בדיקת חתימת IPC (לשרתי SMBv1 מיושנים - מסוכן!)</string>
<string name="cannot_delete_file">לא ניתן למחוק קובץ</string>
<string name="cannot_delete_file_with_reason">לא ניתן למחוק קובץ - %s </string>
<string name="cannot_rename_file">לא ניתן לשנות את שם הקובץ %s - %s</string>
<string name="cannot_read_directory">לא ניתן לקרוא את התיקייה %s - %s</string>
<string name="just_once">רק פעם אחת</string>
<string name="always">תמיד</string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-hu/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,6 @@
<string name="disableIpcSignature">IPC-aláírás ellenőrzés letiltása (Örökölt SMBv1-kiszolgálókhoz - UNSAFE!)</string>
<string name="cannot_delete_file">Nem lehet törölni a fájlt</string>
<string name="cannot_delete_file_with_reason">Nem lehet törölni a fájlt - %s </string>
<string name="cannot_rename_file">Nem lehet átnevezni a fájlt %s - %s</string>
<string name="cannot_read_directory">Nem lehet olvasni a könyvtárat %s - %s</string>
<string name="just_once">Csak egyszer</string>
<string name="always">Mindig</string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-id/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,6 @@
<string name="disableIpcSignature">Nonaktifkan pemeriksaaan tanda tangan IPC (Untuk server SMBv1 lama - TIDAK AMAN!)</string>
<string name="cannot_delete_file">Tidak bisa menghapus berkas</string>
<string name="cannot_delete_file_with_reason">Tidak bisa menghapus berkas - %s </string>
<string name="cannot_rename_file">Tidak bisa mengubah nama berkas %s - %s</string>
<string name="cannot_read_directory">Tidak bisa membaca direktori %s - %s</string>
<string name="just_once">Hanya Sekali</string>
<string name="always">Selalu</string>
Expand Down
Loading

0 comments on commit 03e016e

Please sign in to comment.