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

Commit 967dc870 authored by emancebo's avatar emancebo Committed by Zhao Wei Liew
Browse files

Fix rename of files ending with . on sdcard

If sdcard is formatted as VFAT, then file names named as "foo." will
be silently saved as "foo".  This causes us to report the wrong target
file to media scanner, which can cause an extra file to be shown in
MTP mode on Windows.  The workaround is to rescan the parent dir to
discover the correct filename to report to media scanner.

Change-Id: Ia58a341ef6190efe3dca8b13bbc081b6e2d4e9c1
Issue-Id: QRDL-983
parent 96809f82
Loading
Loading
Loading
Loading
+33 −7
Original line number Original line Diff line number Diff line
@@ -792,6 +792,22 @@ public final class CommandHelper {
        return result;
        return result;
    }
    }


    private static void recursiveScan(@NonNull final Context context,
                                      @Nullable String srcPath,
                                      @NonNull String destPath) {
        recursiveScan(context, srcPath, destPath, -1);
    }

    private static class FileSystemObjectWithDepth {
        public FileSystemObject fso;
        public int depth;

        public FileSystemObjectWithDepth(FileSystemObject fso, int depth) {
            this.fso = fso;
            this.depth = depth;
        }
    }

    /**
    /**
     *
     *
     * @param context
     * @param context
@@ -800,17 +816,19 @@ public final class CommandHelper {
     */
     */
    private static void recursiveScan(@NonNull final Context context,
    private static void recursiveScan(@NonNull final Context context,
                                      @Nullable String srcPath,
                                      @Nullable String srcPath,
                                      @NonNull String destPath) {
                                      @NonNull String destPath,
                                      int maxDepth) {
        ArrayList<String> paths = new ArrayList<>();
        ArrayList<String> paths = new ArrayList<>();
        Stack<FileSystemObject> pathsToScan = new Stack<>();
        Stack<FileSystemObjectWithDepth> pathsToScan = new Stack<>();
        try {
        try {
            FileSystemObject fso = getFileInfo(context, destPath, null);
            FileSystemObject fso = getFileInfo(context, destPath, null);
            if (fso == null) {
            if (fso == null) {
                return;
                return;
            }
            }
            pathsToScan.push(fso);
            pathsToScan.push(new FileSystemObjectWithDepth(fso, 0));
            while (!pathsToScan.isEmpty()) {
            while (!pathsToScan.isEmpty()) {
                fso = pathsToScan.pop();
                FileSystemObjectWithDepth fsowd = pathsToScan.pop();
                fso = fsowd.fso;
                if (srcPath != null) {
                if (srcPath != null) {
                    String src = fso.getFullPath().replace(destPath, srcPath);
                    String src = fso.getFullPath().replace(destPath, srcPath);
                    // Add the path to be deleted
                    // Add the path to be deleted
@@ -818,7 +836,7 @@ public final class CommandHelper {
                }
                }
                // Add the path to be added
                // Add the path to be added
                paths.add(MediaHelper.normalizeMediaPath(fso.getFullPath()));
                paths.add(MediaHelper.normalizeMediaPath(fso.getFullPath()));
                if (fso instanceof Directory) {
                if (fso instanceof Directory && (maxDepth == -1 || fsowd.depth < maxDepth)) {
                    try {
                    try {
                        List<FileSystemObject> files =
                        List<FileSystemObject> files =
                                CommandHelper.listFiles(context, fso.getFullPath(), null);
                                CommandHelper.listFiles(context, fso.getFullPath(), null);
@@ -830,7 +848,7 @@ public final class CommandHelper {
                            if (file instanceof ParentDirectory) {
                            if (file instanceof ParentDirectory) {
                                continue;
                                continue;
                            }
                            }
                            pathsToScan.push(file);
                            pathsToScan.push(new FileSystemObjectWithDepth(file, fsowd.depth + 1));
                        }
                        }
                    } catch (IOException
                    } catch (IOException
                            | ConsoleAllocException
                            | ConsoleAllocException
@@ -939,7 +957,15 @@ public final class CommandHelper {
            File parent = new File(dst).getParentFile();
            File parent = new File(dst).getParentFile();
            if ((parent != null && !VirtualMountPointConsole.isVirtualStorageResource(parent
            if ((parent != null && !VirtualMountPointConsole.isVirtualStorageResource(parent
                    .getAbsolutePath()))) {
                    .getAbsolutePath()))) {
                recursiveScan(context, src, dst);
                // Scan source
                MediaScannerConnection.scanFile(context, new String[] {
                        MediaHelper.normalizeMediaPath(src) }, null, null);

                // Recursive scan of the parent dir of the dest. This mitigates a VFAT
                // issue where a file named "foo." will silently be renamed as "foo".
                // This ensures that we report the correct filename to the media scanner
                // by re-reading the filename from the file system.
                recursiveScan(context, null, parent.getAbsolutePath(), 1);
            }
            }
        }
        }