From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Thierry Volpiatto Newsgroups: gmane.emacs.bugs Subject: bug#10489: 24.0.92; dired-do-copy may create infinite directory hierarchy Date: Sun, 15 Jan 2012 18:20:30 +0100 Message-ID: <87pqekopb5.fsf@gmail.com> References: <87mx9su32g.fsf@web.de> <87sjjkfvwt.fsf@gmail.com> <8362ggkquq.fsf@gnu.org> <87lipcrlga.fsf@gmail.com> <87fwfkc4pn.fsf@gmx.de> <87fwfjsw8t.fsf@gmail.com> <87aa5rdazl.fsf@gmx.de> <87d3anogf5.fsf@gmail.com> <011AEED9E81C4DEFA6B1E03B0F57F28F@us.oracle.com> <878vlbljnc.fsf@gmx.de> <8739bj8mu1.fsf@gmail.com> <87fwfjo24c.fsf@gmx.de> <87pqen76p4.fsf@gmail.com> <83fwfik92e.fsf@gnu.org> <87mx9q1sz7.fsf@gmail.com> <87vcodm8ns.fsf@gmx.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" X-Trace: dough.gmane.org 1326648083 27812 80.91.229.12 (15 Jan 2012 17:21:23 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sun, 15 Jan 2012 17:21:23 +0000 (UTC) Cc: 10489@debbugs.gnu.org To: Michael Albinus Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Jan 15 18:21:19 2012 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1RmTle-0000Qx-H8 for geb-bug-gnu-emacs@m.gmane.org; Sun, 15 Jan 2012 18:21:18 +0100 Original-Received: from localhost ([::1]:50617 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RmTld-0007px-NF for geb-bug-gnu-emacs@m.gmane.org; Sun, 15 Jan 2012 12:21:17 -0500 Original-Received: from eggs.gnu.org ([140.186.70.92]:58687) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RmTla-0007os-Kx for bug-gnu-emacs@gnu.org; Sun, 15 Jan 2012 12:21:15 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RmTlZ-0004kD-CC for bug-gnu-emacs@gnu.org; Sun, 15 Jan 2012 12:21:14 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:35913) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RmTlZ-0004k9-94 for bug-gnu-emacs@gnu.org; Sun, 15 Jan 2012 12:21:13 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.72) (envelope-from ) id 1RmTmM-00035L-8u for bug-gnu-emacs@gnu.org; Sun, 15 Jan 2012 12:22:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Thierry Volpiatto Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 15 Jan 2012 17:22:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 10489 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 10489-submit@debbugs.gnu.org id=B10489.132664809911825 (code B ref 10489); Sun, 15 Jan 2012 17:22:02 +0000 Original-Received: (at 10489) by debbugs.gnu.org; 15 Jan 2012 17:21:39 +0000 Original-Received: from localhost ([127.0.0.1]:58819 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1RmTly-00034g-IV for submit@debbugs.gnu.org; Sun, 15 Jan 2012 12:21:39 -0500 Original-Received: from mail-we0-f172.google.com ([74.125.82.172]:57459) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1RmTlv-00034Y-J3 for 10489@debbugs.gnu.org; Sun, 15 Jan 2012 12:21:37 -0500 Original-Received: by wera1 with SMTP id a1so767528wer.3 for <10489@debbugs.gnu.org>; Sun, 15 Jan 2012 09:20:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=Rr+I7JRV8srD+3gJuq4oYmi12ogkkhP9BFNb6t4al8Y=; b=vK3bKzIWp0s0MN3uFX0BluOvOp2qFistoasg5DnwuJ41rnL8xZq9+qvJKuGufPevO4 avpQXn42XrjW3YHiFrS0M/kKX8jAaolQ0l+JSfthyh3JMrejNO5vxzn+Cv9hv2a6BnRS 2AGwNk7d4kHidvOMY/LoMtLcxHMglr1vLdQco= Original-Received: by 10.216.29.7 with SMTP id h7mr2265271wea.29.1326648045346; Sun, 15 Jan 2012 09:20:45 -0800 (PST) Original-Received: from thierry-MM061 (86.78.88.79.rev.sfr.net. [79.88.78.86]) by mx.google.com with ESMTPS id v13sm19037257wbm.14.2012.01.15.09.20.41 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 15 Jan 2012 09:20:43 -0800 (PST) In-Reply-To: <87vcodm8ns.fsf@gmx.de> (Michael Albinus's message of "Sun, 15 Jan 2012 13:50:47 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.92 (gnu/linux) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:55765 Archived-At: --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Michael Albinus writes: > Thierry Volpiatto writes: > >> The last patch use this instead, which avoid duplication of code and is >> more readable. >> >> (defun files-copyable-p (from to) >> "Verify if file FROM is not the same than TO on this system." >> (let* ((fromname (file-name-as-directory (file-truename from))) >> (destname (file-name-as-directory (file-truename to))) >> (rem-fromname (and (equal "sudo" (file-remote-p fromname 'metho= d)) >> (string-match (system-name) >> (file-remote-p fromname 'host)) >> (file-remote-p fromname 'localname))) >> (rem-newname (and (equal "sudo" (file-remote-p destname 'metho= d)) >> (string-match (system-name) (file-remote-p d= estname 'host)) >> (file-remote-p destname 'localname)))) >> (not (equal (or rem-fromname fromname) >> (or rem-newname destname))))) > > Again, Thierry: it is the wrong approach, to implement such logic in > files.el (or another package not related to remote files). You do not > know all details of the packages handling it. In this example, you have > handled "/sudo:", but you haven't handled "/su:". And other missing > details. Ok > And you have called `file-truename' before comparing, whether the > "remoteness" of the files are equal. This is a performance issue, > because `file-truename' will always do expansive remote operations, even > if `from' and `to' are located on different hosts, and there is no > reason to find out their respective true names. Ok=20 > You are invited to implement `tramp-handle-file-eual-p', once > `file-equal-p' is added for Emacs 24.2. For the time being (Emacs 24.1), > it is sufficient to compare the result of `file-remote-p' as Drew has > shown in the example. Many examples given here, so if everybody is ok with The function given by Drew, we could commit the attached patch to fix this bug: =2D-=20 Thierry Get my Gnupg key: gpg --keyserver pgp.mit.edu --recv-keys 59F29997=20 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=Singlepatch-r118414ToTip.patch Content-Transfer-Encoding: quoted-printable ##Merge of all patches applied from revision 118409 ## patch-r118414: Bugfix bug#10489, dired-do-copy may create infinite direc= tory hierarchy. ## patch-r118411: * lisp/dired-aux.el (dired-copy-file-recursive): Use file= -equal-p. ##=20 diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el =2D-- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -1264,24 +1264,26 @@ =20 (defun dired-copy-file-recursive (from to ok-flag &optional preserve-time top recursive) + (when (file-equal-p from to) + (error "Can't copy directory `%s' on itself" from)) (let ((attrs (file-attributes from))) (if (and recursive =2D (eq t (car attrs)) =2D (or (eq recursive 'always) =2D (yes-or-no-p (format "Recursive copies of %s? " from)))) =2D ;; This is a directory. =2D (copy-directory from to preserve-time) + (eq t (car attrs)) + (or (eq recursive 'always) + (yes-or-no-p (format "Recursive copies of %s? " from)))) + ;; This is a directory. + (copy-directory from to preserve-time) ;; Not a directory. (or top (dired-handle-overwrite to)) (condition-case err =2D (if (stringp (car attrs)) =2D ;; It is a symlink =2D (make-symbolic-link (car attrs) to ok-flag) =2D (copy-file from to ok-flag preserve-time)) =2D (file-date-error =2D (push (dired-make-relative from) =2D dired-create-files-failures) =2D (dired-log "Can't set date on %s:\n%s\n" from err)))))) + (if (stringp (car attrs)) + ;; It is a symlink + (make-symbolic-link (car attrs) to ok-flag) + (copy-file from to ok-flag preserve-time)) + (file-date-error + (push (dired-make-relative from) + dired-create-files-failures) + (dired-log "Can't set date on %s:\n%s\n" from err)))))) =20 ;;;###autoload (defun dired-rename-file (file newname ok-if-already-exists) @@ -1378,7 +1380,7 @@ =20 ;; The basic function for half a dozen variations on cp/mv/ln/ln -s. (defun dired-create-files (file-creator operation fn-list name-constructor =2D &optional marker-char) + &optional marker-char) "Create one or more new files from a list of existing files FN-LIST. This function also handles querying the user, updating Dired buffers, and displaying a success or failure message. @@ -1402,9 +1404,9 @@ newfile's entry, or t to use the current marker character if the old file was marked." (let (dired-create-files-failures failures =2D skipped (success-count 0) (total (length fn-list))) + skipped (success-count 0) (total (leng= th fn-list))) (let (to overwrite-query =2D overwrite-backup-query) ; for dired-handle-overwrite + overwrite-backup-query) ; for dired-handle-overwrite (dolist (from fn-list) (setq to (funcall name-constructor from)) (if (equal to from) @@ -1430,10 +1432,25 @@ (cond ((integerp marker-char) marker-char) (marker-char (dired-file-marker from)) ; slow (t nil)))) =2D (when (and (file-directory-p from) =2D (file-directory-p to) =2D (eq file-creator 'dired-copy-file)) =2D (setq to (file-name-directory to))) + ;; Handle the `dired-copy-file' file-creator specially + ;; When copying a directory to another directory or + ;; possibly to itself. + ;; (e.g "~/foo" =3D> "~/test" or "~/foo" =3D>"~/foo") + ;; In this case the 'name-constructor' have set the destination + ;; 'to' to "~/test/foo" because the old + ;; emacs23 behavior of `copy-directory' + ;; was no not create the subdir and copy instead the contents = only. + ;; With it's new behavior (similar to cp shell command) we don= 't + ;; need such a construction, so modify the destination 'to' to + ;; "~/test/" instead of "~/test/foo/". + ;; If from and to are the same directory do the same, + ;; the error will be handled by `dired-copy-file-recursive'. + (let ((destname (file-name-directory to))) + (when (and (file-directory-p from) + (or (file-equal-p from destname) + (file-directory-p to)) + (eq file-creator 'dired-copy-file)) + (setq to destname))) (condition-case err (progn (funcall file-creator from to dired-overwrite-confirmed) @@ -1456,25 +1473,25 @@ (setq failures (nconc failures dired-create-files-failures)) (dired-log-summary (format "%s failed for %d file%s in %d requests" =2D operation (length failures) =2D (dired-plural-s (length failures)) =2D total) + operation (length failures) + (dired-plural-s (length failures)) + total) failures)) (failures (dired-log-summary (format "%s failed for %d of %d file%s" =2D operation (length failures) =2D total (dired-plural-s total)) + operation (length failures) + total (dired-plural-s total)) failures)) (skipped (dired-log-summary (format "%s: %d of %d file%s skipped" =2D operation (length skipped) total =2D (dired-plural-s total)) + operation (length skipped) total + (dired-plural-s total)) skipped)) (t (message "%s: %s file%s" =2D operation success-count (dired-plural-s success-count))))) + operation success-count (dired-plural-s success-count))))) (dired-move-to-filename)) (defun dired-do-create-files (op-symbol file-creator operation arg diff --git a/lisp/files.el b/lisp/files.el =2D-- a/lisp/files.el +++ b/lisp/files.el @@ -4902,6 +4902,12 @@ directory 'full directory-files-no-dot-files-regexp))) (delete-directory-internal directory))))) =20 +(defun file-equal-p (file1 file2) + "Return non-nil if FILE1 and FILE2 name the same file." + (and (equal (file-remote-p file1) (file-remote-p file2)) + (string=3D (file-truename (expand-file-name file1)) + (file-truename (expand-file-name file2))))) + (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 @@ -4928,10 +4934,12 @@ (format "Copy directory %s to: " dir) default-directory default-directory nil nil) current-prefix-arg t nil))) + (when (file-equal-p directory newname) + (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) =2D (find-file-name-handler newname 'copy-directory)))) + (find-file-name-handler newname 'copy-directory)))) (if handler (funcall handler 'copy-directory directory newname keep-time parents) =20 --=-=-=-- --==-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iEYEARECAAYFAk8TCt8ACgkQKNF/U1nymZffrQCg71Bt8G07cgdA2V8I+UMfYTuI m1MAoNFSXpSLlcLNdJURvrdtM7+V4SGb =/6uT -----END PGP SIGNATURE----- --==-=-=--