unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 10489@debbugs.gnu.org, Michael Albinus <michael.albinus@gmx.de>
Subject: bug#10489: 24.0.92; dired-do-copy may create infinite directory hierarchy
Date: Thu, 23 Feb 2012 23:10:18 +0100	[thread overview]
Message-ID: <874nuhxktx.fsf@gmail.com> (raw)
In-Reply-To: <jwv39a1sc8e.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Thu, 23 Feb 2012 12:18:51 -0500")

[-- Attachment #1: Type: text/plain, Size: 869 bytes --]

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Here a first shot of `copy-directory', with the first check disabled
>> (file-subdir-of-p) to test the detection of the inf-loop, can you have a
>> look?
>
> I think we can install the file-subdir-of-p test now and leave the rest
> for 24.2.  Can you (re)send the corresponding patch?  Note that
> (or (files-equal-p directory newname)
>     (file-subdir-of-p newname directory))
> should be replaced by just (file-subdir-of-p newname directory), because
> this primitive should be a "⊆" rather than "⊂".
Done, you should have received the patch. 

>
> I always prefer a patch rather than the resulting code, so I don't have
> to look for the source code to see what's changed.
Ok, here the patch for only `copy-directory' with the check by
`file-subdir-of-p' disabled for testing purpose.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: copy-directory --]
[-- Type: text/x-diff, Size: 6175 bytes --]

##Merge of all patches applied from revision 118951
## patch-r118952: Return Error when trying to copy a directory on itself.
## patch-r118953: * lisp/files.el (copy-directory): Improve error message.
## 
diff --git a/lisp/files.el b/lisp/files.el
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4935,6 +4935,7 @@
               (equal (file-attributes (file-truename root))
                      (file-attributes f2))))))
 
+(defvar copy-directory-newdir-inode nil)
 (defun copy-directory (directory newname &optional keep-time parents copy-contents)
   "Copy DIRECTORY to NEWNAME.  Both args must be strings.
 This function always sets the file modes of the output files to match
@@ -4961,54 +4962,63 @@
 	    (format "Copy directory %s to: " dir)
 	    default-directory default-directory nil nil)
 	   current-prefix-arg t nil)))
-  (when (file-subdir-of-p newname directory)
-    (error "Can't copy directory `%s' on itself" directory))
+  ;; (when (file-subdir-of-p newname directory)
+  ;;   (error "Can't copy directory `%s' on itself" 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)
-                     (find-file-name-handler newname 'copy-directory))))
-    (if handler
-	(funcall handler 'copy-directory directory newname keep-time parents)
-
-      ;; Compute target name.
-      (setq directory (directory-file-name (expand-file-name directory))
-	    newname   (directory-file-name (expand-file-name newname)))
-
-      (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))
-	    ;; If NEWNAME is an existing directory and COPY-CONTENTS
-	    ;; is nil, copy into NEWNAME/[DIRECTORY-BASENAME].
-	    ((not copy-contents)
-	     (setq newname (expand-file-name
-			    (file-name-nondirectory
-			     (directory-file-name directory))
-			    newname))
-	     (and (file-exists-p newname)
-		  (not (file-directory-p newname))
-		  (error "Cannot overwrite non-directory %s with a directory"
-			 newname))
-	     (make-directory newname t)))
-
-      ;; Copy recursively.
-      (dolist (file
-	       ;; We do not want to copy "." and "..".
-	       (directory-files directory 'full
-				directory-files-no-dot-files-regexp))
-	(if (file-directory-p file)
-	    (copy-directory file newname keep-time parents)
-	  (let ((target (expand-file-name (file-name-nondirectory file) newname))
-		(attrs (file-attributes file)))
-	    (if (stringp (car attrs))   ; Symbolic link
-		(make-symbolic-link (car attrs) target t)
-	      (copy-file file target t keep-time)))))
-
-      ;; Set directory attributes.
-      (let ((modes (file-modes directory))
-	    (times (and keep-time (nth 5 (file-attributes directory)))))
-	(if modes (set-file-modes newname modes))
-	(if times (set-file-times newname times))))))
+  (unwind-protect
+       (let ((handler (or (find-file-name-handler directory 'copy-directory)
+                          (find-file-name-handler newname 'copy-directory))))
+         (if handler
+             (funcall handler 'copy-directory directory newname keep-time parents)
+
+             ;; Compute target name.
+             (setq directory (file-truename (directory-file-name (expand-file-name directory)))
+                   newname   (file-truename (directory-file-name (expand-file-name newname))))
+             (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))
+                   ;; If NEWNAME is an existing directory and COPY-CONTENTS
+                   ;; is nil, copy into NEWNAME/[DIRECTORY-BASENAME].
+                   ((not copy-contents)
+                    (setq newname (expand-file-name
+                                   (file-name-nondirectory
+                                    (directory-file-name directory))
+                                   newname))
+             
+                    (and (file-exists-p newname)
+                         (not (file-directory-p newname))
+                         (error "Cannot overwrite non-directory %s with a directory"
+                                newname))
+                    (make-directory newname t)
+                    (unless copy-directory-newdir-inode
+                      (setq copy-directory-newdir-inode (nth 10 (file-attributes newname))))))
+
+             ;; Copy recursively.
+             (dolist (file
+                       ;; We do not want to copy "." and "..".
+                       (directory-files directory 'full
+                                        directory-files-no-dot-files-regexp))
+               (assert (not (equal (nth 10 (file-attributes file))
+                                   copy-directory-newdir-inode))
+                       nil "Unable to create directory `%s' in itself `%s'"
+                       (file-name-nondirectory (directory-file-name file))
+                       (file-name-directory (directory-file-name newname)))
+               (if (file-directory-p file)
+                   (copy-directory file newname keep-time parents)
+                   (let ((target (expand-file-name (file-name-nondirectory file) newname))
+                         (attrs (file-attributes file)))
+                     (if (stringp (car attrs)) ; Symbolic link
+                         (make-symbolic-link (car attrs) target t)
+                         (copy-file file target t keep-time)))))
+
+             ;; Set directory attributes.
+             (let ((modes (file-modes directory))
+                   (times (and keep-time (nth 5 (file-attributes directory)))))
+               (if modes (set-file-modes newname modes))
+               (if times (set-file-times newname times)))))
+    (setq copy-directory-newdir-inode nil)))
 \f
 (put 'revert-buffer-function 'permanent-local t)
 (defvar revert-buffer-function nil

[-- Attachment #3: Type: text/plain, Size: 84 bytes --]



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

  reply	other threads:[~2012-02-23 22:10 UTC|newest]

Thread overview: 174+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-12 19:35 bug#10489: 24.0.92; dired-do-copy may create infinite directory hierarchy Michael Heerdegen
2012-01-12 21:33 ` Thierry Volpiatto
2012-01-13  7:23   ` Eli Zaretskii
2012-01-13  8:38     ` Thierry Volpiatto
2012-01-13 10:31       ` Eli Zaretskii
2012-01-13 11:19         ` Thierry Volpiatto
2012-01-13 12:01         ` Juanma Barranquero
2012-01-13 12:41           ` Eli Zaretskii
2012-01-13 13:01           ` Michael Albinus
2012-01-13 13:11             ` Juanma Barranquero
2012-01-13 13:13               ` Juanma Barranquero
2012-01-13 13:18                 ` Juanma Barranquero
2012-01-13 13:32                   ` Michael Albinus
2012-01-13 13:27             ` Stefan Monnier
2012-01-13 14:06               ` Thierry Volpiatto
2012-01-13 14:44               ` Michael Albinus
2012-01-13 15:13                 ` Stefan Monnier
2012-01-13 15:17                   ` Juanma Barranquero
2012-01-13 15:29                     ` Michael Albinus
2012-01-13 16:59                   ` Drew Adams
2012-01-13  9:38     ` Thierry Volpiatto
2012-01-13  9:49       ` Michael Albinus
2012-01-13 11:00         ` Thierry Volpiatto
2012-01-13 12:48           ` Michael Albinus
2012-01-13 13:55             ` Thierry Volpiatto
2012-01-13 14:14               ` Drew Adams
2012-01-13 15:06                 ` Juanma Barranquero
2012-01-13 15:14                   ` Michael Albinus
2012-01-13 18:43                     ` Thierry Volpiatto
2012-01-13 18:57                       ` Drew Adams
2012-01-13 19:11                         ` Thierry Volpiatto
2012-01-13 19:21                           ` Drew Adams
2012-01-13 19:35                             ` Michael Albinus
2012-01-13 20:56                               ` Drew Adams
2012-01-13 18:59                       ` Thierry Volpiatto
2012-01-13 19:04                       ` Michael Albinus
2012-01-13 19:17                         ` Thierry Volpiatto
2012-01-14  8:00                           ` Eli Zaretskii
2012-01-14 10:25                             ` Thierry Volpiatto
2012-01-15 12:50                               ` Michael Albinus
2012-01-15 17:20                                 ` Thierry Volpiatto
2012-01-15 17:31                                   ` Thierry Volpiatto
2012-01-15 18:24                                     ` Michael Albinus
2012-01-15 19:09                                       ` Thierry Volpiatto
2012-01-15 19:49                                         ` Michael Albinus
2012-01-15 21:01                                           ` Thierry Volpiatto
2012-01-16  8:58                                           ` Thierry Volpiatto
2012-01-16 13:56                                             ` Stefan Monnier
2012-01-16 14:13                                               ` Michael Albinus
2012-01-16 15:18                                                 ` Stefan Monnier
2012-01-16 15:27                                                   ` Michael Albinus
2012-01-16 21:40                                                     ` Stefan Monnier
2012-02-21 16:53                                                       ` Thierry Volpiatto
2012-02-21 17:59                                                         ` Stefan Monnier
2012-02-21 19:46                                                           ` Michael Albinus
2012-02-21 20:58                                                           ` Thierry Volpiatto
2012-02-21 22:51                                                             ` Stefan Monnier
2012-02-22 21:37                                                               ` Thierry Volpiatto
2012-02-22 22:00                                                                 ` Stefan Monnier
2012-02-23  6:15                                                                   ` Thierry Volpiatto
2012-02-23 16:01                                                                   ` Thierry Volpiatto
2012-02-23 17:18                                                                     ` Stefan Monnier
2012-02-23 22:10                                                                       ` Thierry Volpiatto [this message]
2012-02-24  5:37                                                                       ` Thierry Volpiatto
2012-02-24  7:16                                                                         ` Thierry Volpiatto
2012-02-24  9:19                                                                           ` Eli Zaretskii
2012-02-24  9:49                                                                             ` Thierry Volpiatto
2012-02-24 12:18                                                                             ` Thierry Volpiatto
2012-02-24 12:54                                                                               ` Michael Albinus
2012-02-24 13:36                                                                                 ` Thierry Volpiatto
2012-02-24 15:00                                                                                   ` Michael Albinus
2012-02-24 14:33                                                                                 ` Eli Zaretskii
2012-02-24 15:19                                                                                   ` Michael Albinus
2012-02-24 19:42                                                                                     ` Eli Zaretskii
2012-02-24 20:35                                                                                       ` Michael Albinus
2012-02-25  6:21                                                                                         ` Eli Zaretskii
2012-02-27  8:39                                                                                           ` Michael Albinus
2012-02-27 17:40                                                                                             ` Eli Zaretskii
2012-02-24 14:45                                                                                 ` Thierry Volpiatto
2012-02-24 15:23                                                                                   ` Michael Albinus
2012-02-24 14:39                                                                               ` Eli Zaretskii
2012-02-24 14:50                                                                                 ` Thierry Volpiatto
2012-02-24 15:26                                                                                   ` Michael Albinus
2012-02-24 15:52                                                                                     ` Thierry Volpiatto
2012-02-24 16:17                                                                                       ` Michael Albinus
2012-02-24 16:02                                                                                     ` Thierry Volpiatto
2012-02-24 16:15                                                                                       ` Drew Adams
2012-02-24 16:25                                                                                         ` Michael Albinus
2012-02-24 16:42                                                                                           ` Drew Adams
2012-02-24 17:04                                                                                             ` Michael Albinus
2012-02-24 16:21                                                                                       ` Michael Albinus
2012-02-24 17:23                                                                                         ` Thierry Volpiatto
2012-02-24 18:43                                                                                           ` Michael Albinus
2012-02-24 20:06                                                                                             ` Thierry Volpiatto
2012-02-24 20:04                                                                                       ` Eli Zaretskii
2012-02-24 20:33                                                                                         ` Michael Albinus
2012-02-24 21:54                                                                                           ` Thierry Volpiatto
2012-02-25  8:56                                                                                             ` Michael Albinus
2012-02-25  9:08                                                                                               ` Thierry Volpiatto
2012-02-26  9:48                                                                                                 ` Michael Albinus
2012-02-26 19:48                                                                                                   ` Thierry Volpiatto
2012-02-26 21:40                                                                                                     ` Stefan Monnier
2012-02-27  6:45                                                                                                       ` Thierry Volpiatto
2012-02-27  7:45                                                                                                         ` Stefan Monnier
2012-02-27  8:04                                                                                                           ` Thierry Volpiatto
2012-02-27 10:34                                                                                                             ` Stefan Monnier
2012-02-27 11:06                                                                                                               ` Thierry Volpiatto
2012-02-27 11:10                                                                                                                 ` Michael Albinus
2012-02-27 11:34                                                                                                                   ` Thierry Volpiatto
2012-02-27 13:24                                                                                                                 ` Stefan Monnier
2012-02-27 14:59                                                                                                                   ` Thierry Volpiatto
2012-02-27 17:38                                                                                                                     ` Stefan Monnier
2012-02-27 18:34                                                                                                                       ` Thierry Volpiatto
2012-02-27 19:08                                                                                                                         ` Michael Albinus
2012-02-27 19:33                                                                                                                           ` Thierry Volpiatto
2012-02-27 19:49                                                                                                                             ` Michael Albinus
2012-02-27 21:58                                                                                                                           ` Stefan Monnier
2012-02-27 22:11                                                                                                                             ` Thierry Volpiatto
2012-02-28  6:12                                                                                                                               ` Thierry Volpiatto
2012-02-28  7:14                                                                                                                                 ` Thierry Volpiatto
2012-02-28  7:34                                                                                                                                   ` Michael Albinus
2012-02-28  8:15                                                                                                                                     ` Thierry Volpiatto
2012-02-28  8:31                                                                                                                                       ` Michael Albinus
2012-02-28  9:34                                                                                                                                         ` Thierry Volpiatto
2012-02-28 10:15                                                                                                                                           ` Michael Albinus
2012-02-28 19:29                                                                                                                                     ` Stefan Monnier
2012-02-28 19:53                                                                                                                                       ` Michael Albinus
2012-02-29  2:01                                                                                                                                         ` Stefan Monnier
2012-02-29 11:04                                                                                                                                           ` Michael Albinus
2012-02-29 16:48                                                                                                                                             ` Stefan Monnier
2012-02-29 17:52                                                                                                                                               ` Thierry Volpiatto
2012-03-01  2:33                                                                                                                                                 ` Stefan Monnier
2012-03-01  8:37                                                                                                                                               ` Michael Albinus
2012-02-27 10:40                                                                                                   ` Thierry Volpiatto
2012-02-27 11:03                                                                                                     ` Michael Albinus
2012-02-27 11:29                                                                                                       ` Thierry Volpiatto
2012-02-27 14:19                                                                                                         ` Drew Adams
2012-02-27 13:54                                                                                                     ` Chong Yidong
2012-02-27 15:15                                                                                                       ` Thierry Volpiatto
2012-02-25  7:05                                                                                           ` Eli Zaretskii
2012-02-25  9:56                                                                                             ` Stefan Monnier
2012-02-25 13:05                                                                                               ` Michael Albinus
2012-02-25 15:36                                                                                               ` Michael Albinus
2012-02-25 15:53                                                                                                 ` Thierry Volpiatto
2012-02-25 22:41                                                                                                   ` Stefan Monnier
2012-02-26  9:21                                                                                                     ` Michael Albinus
2012-02-26 21:38                                                                                                       ` Stefan Monnier
2012-02-27  8:19                                                                                                         ` Michael Albinus
2012-02-27 10:39                                                                                                           ` Stefan Monnier
2012-02-25 13:03                                                                                             ` Michael Albinus
2012-02-25 14:35                                                                                               ` Stefan Monnier
2012-02-25 14:56                                                                                                 ` Lennart Borgman
2012-02-21 19:43                                                         ` Michael Albinus
2012-02-21 21:03                                                           ` Thierry Volpiatto
2012-01-16 14:09                                             ` Andreas Schwab
2012-01-16 19:14                                               ` Thierry Volpiatto
2012-01-17  6:06                                                 ` Thierry Volpiatto
2012-01-21 13:01                                               ` Thierry Volpiatto
2012-01-21 16:02                                                 ` Thierry Volpiatto
2012-01-13 19:43                       ` Stefan Monnier
2012-01-13 22:51                         ` Michael Albinus
2012-01-14  1:55                           ` Stefan Monnier
2012-01-14  8:59                             ` Eli Zaretskii
2012-01-14 14:19                               ` Stefan Monnier
2012-01-14 15:55                                 ` Eli Zaretskii
2012-01-15  5:59                                 ` Thierry Volpiatto
2012-01-15 12:40                                   ` Michael Albinus
2012-01-15 17:28                                     ` Thierry Volpiatto
2012-01-13 15:31                   ` Drew Adams
2012-01-13 15:41                 ` Eli Zaretskii
2012-01-13 16:56                   ` Drew Adams
2012-01-15 18:42                     ` Drew Adams
2012-01-13 10:32       ` Eli Zaretskii
2012-03-22  2:18 ` Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874nuhxktx.fsf@gmail.com \
    --to=thierry.volpiatto@gmail.com \
    --cc=10489@debbugs.gnu.org \
    --cc=michael.albinus@gmx.de \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).