Subject: Re: bug#10489: 24.0.92; dired-do-copy may create infinite directory hierarchy

From: Thierry Volpiatto <[email-masked]>
Date: Fri, 13 Jan 2012 10:38:45 +0100

Eli Zaretskii <[email-masked]> writes:

> I don't think this will solves all the use cases.  File names are not
> strings, you cannot compare them as literal strings and hope to plumb
> all the leaks.
>
> Some situations which I think this patch will not handle correctly:
>
>  . file names with different letter-case on a case-insensitive file
>    system
>
>  . relative vs absolute file names
>
>  . file names that are hard links to the same directory (this includes
>    the infamous 8+3 short aliases on Windows)
>
> I didn't actually try the patch, so apologies if I missed something
> which makes these non-issues.
>
Here a version of the patch that use `equal' and handle also same error
on remotes files.
i.e (copy-directory "/sudo:host:/home/user/Test" "/home/user/Test")

-- 
  Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 
##Merge of all patches applied from revision 118413
## patch-r118414: Fix error when trying to copy directory on itself (bug#10489).
## patch-r118415: * lisp/dired-aux.el (dired-copy-file-recursive): Handle also remote file/dir.
## 
diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1264,6 +1264,11 @@
 
 (defun dired-copy-file-recursive (from to ok-flag &optional
 				       preserve-time top recursive)
+  (when (equal (or (file-remote-p (file-name-as-directory from) 'localname)
+                   (file-name-as-directory from))
+               (or (file-remote-p (file-name-as-directory to) 'localname)
+                   (file-name-as-directory to)))
+    (error "Can't copy directory `%s' on itself" from))
   (let ((attrs (file-attributes from)))
     (if (and recursive
 	     (eq t (car attrs))
@@ -1431,7 +1436,12 @@
                          (marker-char (dired-file-marker from)) ; slow
                          (t nil))))
 	    (when (and (file-directory-p from)
-		       (file-directory-p to)
+		       (or (equal (or (file-remote-p (file-name-as-directory from) 'localname)
+                                      (file-name-as-directory from))
+                                  (or (file-remote-p (file-name-as-directory to) 'localname)
+                                      (file-name-as-directory
+                                       (file-name-directory to))))
+                           (file-directory-p to))
 		       (eq file-creator 'dired-copy-file))
 	      (setq to (file-name-directory to)))
             (condition-case err
diff --git a/lisp/files.el b/lisp/files.el
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4928,6 +4928,11 @@
 	    (format "Copy directory %s to: " dir)
 	    default-directory default-directory nil nil)
 	   current-prefix-arg t nil)))
+  (when (equal (or (file-remote-p (file-name-as-directory directory) 'localname)
+                   (file-name-as-directory directory))
+               (or (file-remote-p (file-name-as-directory newname) 'localname)
+                   (file-name-as-directory newname)))
+    (error "Can't copy directory `%s' on itself" from))
   ;; If default-directory is a remote directory, make sure we find its
   ;; copy-directory handler.
   (let ((handler (or (find-file-name-handler directory 'copy-directory)