Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e337edcb authored by herriojr's avatar herriojr Committed by Michael Bestas
Browse files

Fix Rename on VFat

Changed how the java console handles detecting if a file rename
collides with another file to more represent how the particular
filesystem implementation handles it (I'm looking at you vfat).

Change-Id: I6ac9ae848ee47fa33f02225c310c92f2a03fb50d
(cherry picked from commit bd4178a8)
parent 80077de4
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -477,7 +477,7 @@ public class ActionsDialog implements OnItemClickListener, OnItemLongClickListen
        final InputNameDialog inputNameDialog =
                new InputNameDialog(
                        this.mContext,
                        this.mOnSelectionListener.onRequestCurrentItems(),
                        this.mOnSelectionListener.onRequestCurrentDir(),
                        menuItem.getTitle().toString());
        inputNameDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
@@ -519,7 +519,7 @@ public class ActionsDialog implements OnItemClickListener, OnItemLongClickListen
        final InputNameDialog inputNameDialog =
                new InputNameDialog(
                        this.mContext,
                        this.mOnSelectionListener.onRequestCurrentItems(),
                        this.mOnSelectionListener.onRequestCurrentDir(),
                        fso,
                        allowFsoName,
                        menuItem.getTitle().toString());
+10 −7
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ public class InputNameDialog
    /**
     * @hide
     */
    final List<FileSystemObject> mFiles;
    final String mParent;
    /**
     * @hide
     */
@@ -81,8 +81,8 @@ public class InputNameDialog
     * @param dialogTitle The dialog title
     */
    public InputNameDialog(
            final Context context, List<FileSystemObject> files, String dialogTitle) {
        this(context, files, null, false, dialogTitle);
            final Context context, String parent, String dialogTitle) {
        this(context, parent, null, false, dialogTitle);
    }

    /**
@@ -95,15 +95,18 @@ public class InputNameDialog
     * @param dialogTitle The dialog title
     */
    public InputNameDialog(
            final Context context, final List<FileSystemObject> files,
            final FileSystemObject fso, boolean allowFsoName, final String dialogTitle) {
            final Context context,
            final String parent,
            final FileSystemObject fso,
            boolean allowFsoName,
            final String dialogTitle) {
        super();

        //Save the context
        this.mContext = context;

        //Save the files
        this.mFiles = files;
        this.mParent = parent;
        this.mFso = fso;
        this.mAllowFsoName = allowFsoName;
        this.mCancelled = true;
@@ -293,7 +296,7 @@ public class InputNameDialog
        }

        // Name exists
        if (FileHelper.isNameExists(this.mFiles, name)) {
        if (FileHelper.isNameExists(this.mContext, this.mParent, name)) {
            setMsg(
                InputNameDialog.this.mContext.getString(
                        R.string.input_name_dialog_message_name_exists), false);
+1 −1
Original line number Diff line number Diff line
@@ -302,7 +302,7 @@ public final class CompressActionPolicy extends ActionsPolicy {
                        String newName =
                                FileHelper.createNonExistingName(
                                        ctx,
                                        onSelectionListener.onRequestCurrentItems(),
                                        onSelectionListener.onRequestCurrentDir(),
                                        name,
                                        R.string.create_new_compress_file_regexp);
                        String newNameAbs =
+2 −2
Original line number Diff line number Diff line
@@ -138,10 +138,10 @@ public final class CopyMoveActionPolicy extends ActionsPolicy {
            final OnRequestRefreshListener onRequestRefreshListener) {

        // Create a non-existing name
        List<FileSystemObject> curFiles = onSelectionListener.onRequestCurrentItems();
        String curDir = onSelectionListener.onRequestCurrentDir();
        String  newName =
                FileHelper.createNonExistingName(
                        ctx, curFiles, fso.getName(), R.string.create_copy_regexp);
                        ctx, curDir, fso.getName(), R.string.create_copy_regexp);
        final File dst = new File(fso.getParent(), newName);
        File src = new File(fso.getFullPath());

+22 −13
Original line number Diff line number Diff line
@@ -26,11 +26,16 @@ import com.cyanogenmod.filemanager.FileManagerApplication;
import com.cyanogenmod.filemanager.R;
import com.cyanogenmod.filemanager.commands.SyncResultExecutable;
import com.cyanogenmod.filemanager.commands.java.Program;
import com.cyanogenmod.filemanager.commands.shell.InvalidCommandDefinitionException;
import com.cyanogenmod.filemanager.commands.shell.ResolveLinkCommand;
import com.cyanogenmod.filemanager.console.CancelledOperationException;
import com.cyanogenmod.filemanager.console.CommandNotFoundException;
import com.cyanogenmod.filemanager.console.Console;
import com.cyanogenmod.filemanager.console.ConsoleAllocException;
import com.cyanogenmod.filemanager.console.ExecutionException;
import com.cyanogenmod.filemanager.console.InsufficientPermissionsException;
import com.cyanogenmod.filemanager.console.NoSuchFileOrDirectory;
import com.cyanogenmod.filemanager.console.OperationTimeoutException;
import com.cyanogenmod.filemanager.console.java.JavaConsole;
import com.cyanogenmod.filemanager.model.AID;
import com.cyanogenmod.filemanager.model.BlockDevice;
@@ -58,6 +63,7 @@ import com.cyanogenmod.filemanager.util.MimeTypeHelper.MimeTypeCategory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.ClosedByInterruptException;
@@ -934,17 +940,17 @@ public final class FileHelper {
     * that is not current used by the filesystem.
     *
     * @param ctx The current context
     * @param files The list of files of the current directory
     * @param parentDir The directory in which we want to make the file
     * @param attemptedName The attempted name
     * @param regexp The resource of the regular expression to create the new name
     * @return String The new non-existing name
     */
    public static String createNonExistingName(
            final Context ctx, final List<FileSystemObject> files,
            final Context ctx, final String parentDir,
            final String attemptedName, int regexp) {
        // Find a non-exiting name
        String newName = attemptedName;
        if (!isNameExists(files, newName)) return newName;
        if (!isNameExists(ctx, parentDir, newName)) return newName;
        do {
            String name  = FileHelper.getName(newName);
            String ext  = FileHelper.getExtension(newName);
@@ -954,28 +960,31 @@ public final class FileHelper {
                ext = "." + ext; //$NON-NLS-1$
            }
            newName = ctx.getString(regexp, name, ext);
        } while (isNameExists(files, newName));
        } while (isNameExists(ctx, parentDir, newName));
        return newName;
    }

    /**
     * Method that checks if a name exists in the current directory.
     *
     * @param files The list of files of the current directory
     * @param context The application context
     * @param parentDir The full path to the parent directory
     * @param name The name to check
     * @return boolean Indicate if the name exists in the current directory
     */
    public static boolean isNameExists(List<FileSystemObject> files, String name) {
        //Verify if the name exists in the current file list
        int cc = files.size();
        for (int i = 0; i < cc; i++) {
            FileSystemObject fso = files.get(i);
            if (fso.getName().compareTo(name) == 0) {
                return true;
            }
    public static boolean isNameExists(Context context, String parentDir, String name) {
        if (parentDir == null || parentDir.equals(ROOT_DIRECTORY)) {
            parentDir = "";
        }
        //Verify if the name exists in the current file list
        try {
            return CommandHelper.getFileInfo(context, parentDir + "/" + name, null) != null;
        } catch (Exception e) {
            // This is a slight misreporting, however, I don't want to do a bunch of refactoring
            Log.i(TAG, "Failed to get file info: " + e.getMessage());
            return false;
        }
    }

    /**
     * Method that returns is a {@link FileSystemObject} can be handled by this application