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

From: Thierry Volpiatto <[email-masked]>
Date: Tue, 28 Feb 2012 08:14:28 +0100

Thierry Volpiatto <[email-masked]> writes:

> Thierry Volpiatto <[email-masked]> writes:
>
>> Stefan Monnier <[email-masked]> writes:
>>
>>>>> If you don't create "test1" and file-subdir-of-p is unable to handle a
>>>>> non--existing directory,I don't understand how you want to do.
>>>> I was saying that files-equal-p returns unexpected results in case both
>>>> files do not exist. I haven't spoken about file-subdir-of-p.
>>>
>>> Exactly.  I don't think noexist is needed for subdir-p (it should accept
>>> a non-existing dir for the `dir1' argument, without any need for
>>> a noexist arg and it should reject a non-existing dir2), but I can live
>>> with it, but file-equal-p only makes sense for existing files and should
>>> not have a `noexist' argument.
>> Ah! yes, thats sound good.
>> Just need to remove check of file-directory-p for dir1 in f-subdir-of-p.
> Note that the last check:
> (files-equal-p (file-truename root) dir2)
> can be:
> (files-equal-p root dir2)
> file-truename is not needed here.
Here the patch:

--8<---------------cut here---------------start------------->8---
diff --git a/lisp/files.el b/lisp/files.el
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4999,13 +4999,12 @@
 (defun file-subdir-of-p (dir1 dir2)
   "Return non-nil if DIR1 is a subdirectory of DIR2.
 A directory is considered to be a subdirectory of itself.
-Return nil if DIR1 or DIR2 are not existing directories."
+Return nil if top directory DIR2 is not an existing directory."
   (let ((handler (or (find-file-name-handler dir1 'file-subdir-of-p)
                      (find-file-name-handler dir2 'file-subdir-of-p))))
     (if handler
         (funcall handler 'file-subdir-of-p dir1 dir2)
-      (when (and (file-directory-p dir1)
-                 (file-directory-p dir2))
+      (when (file-directory-p dir2) ; Top dir must exist.
 	(setq dir1 (file-truename dir1)
 	      dir2 (file-truename dir2))
 	(let ((ls1  (or (split-string dir1 "/" t) '("/")))
@@ -5019,7 +5018,7 @@
 	    (setq ls1 (cdr ls1)
 		  ls2 (cdr ls2)))
 	  (unless mismatch
-	    (files-equal-p (file-truename root) dir2)))))))
+	    (files-equal-p root dir2)))))))
 
 (defun copy-directory (directory newname &optional keep-time parents copy-contents)
   "Copy DIRECTORY to NEWNAME.  Both args must be strings.
@@ -5065,12 +5064,7 @@
       (cond ((not (file-directory-p newname))
 	     ;; If NEWNAME is not an existing directory, create it;
 	     ;; that is where we will copy the files of DIRECTORY.
-	     (make-directory newname parents)
-             ;; `file-subdir-of-p' doesn't handle non--existing directories,
-             ;; so double check now if NEWNAME is not a subdir of DIRECTORY.
-             (and (file-subdir-of-p newname directory)
-                  (error "Cannot copy `%s' into its subdirectory `%s'"
-                         directory newname)))
+	     (make-directory newname parents))
 	    ;; If NEWNAME is an existing directory and COPY-CONTENTS
 	    ;; is nil, copy into NEWNAME/[DIRECTORY-BASENAME].
 	    ((not copy-contents)
--8<---------------cut here---------------end--------------->8---

-- 
  Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997