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

From: Eli Zaretskii <[email-masked]>
Date: Fri, 13 Jan 2012 09:23:57 +0200

> From: Thierry Volpiatto <[email-masked]>
> Date: Thu, 12 Jan 2012 22:33:38 +0100
> 
> Hi, here a patch that should fix this.

Thanks.

> +  (when (string= (file-name-as-directory from)
> +                 (file-name-as-directory to))
> +    (error "Can't copy to same directory"))
>    (let ((attrs (file-attributes from)))
>      (if (and recursive
>  	     (eq t (car attrs))
> @@ -1431,7 +1434,10 @@
>                           (marker-char (dired-file-marker from)) ; slow
>                           (t nil))))
>  	    (when (and (file-directory-p from)
> -		       (file-directory-p to)
> +		       (or (file-directory-p to)
> +                           (string= (file-name-as-directory from)
> +                                    (file-name-as-directory
> +                                     (file-name-directory 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,9 @@
>  	    (format "Copy directory %s to: " dir)
>  	    default-directory default-directory nil nil)
>  	   current-prefix-arg t nil)))
> +  (when (string= (file-name-as-directory directory)
> +                 (file-name-as-directory newname))
> +    (error "Can't copy to same directory"))
>    ;; 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)

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.

Thanks again for working on this.