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: Sat, 21 Jan 2012 14:01:28 +0100 Message-ID: <87ehuti507.fsf@gmail.com> References: <87mx9su32g.fsf@web.de> <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> <87pqekopb5.fsf@gmail.com> <87hazwoost.fsf@gmail.com> <87ty3w9639.fsf@gmx.de> <8762gckckt.fsf@gmail.com> <87pqek9269.fsf@gmx.de> <87r4z0yqfx.fsf@gmail.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: dough.gmane.org 1327150934 23423 80.91.229.12 (21 Jan 2012 13:02:14 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 21 Jan 2012 13:02:14 +0000 (UTC) Cc: 10489@debbugs.gnu.org, Michael Albinus To: Andreas Schwab Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Jan 21 14:02:07 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 1Roaa6-0005ks-HG for geb-bug-gnu-emacs@m.gmane.org; Sat, 21 Jan 2012 14:02:06 +0100 Original-Received: from localhost ([::1]:51658 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Roaa5-0005id-Vd for geb-bug-gnu-emacs@m.gmane.org; Sat, 21 Jan 2012 08:02:05 -0500 Original-Received: from eggs.gnu.org ([140.186.70.92]:35715) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Roaa1-0005hr-Oi for bug-gnu-emacs@gnu.org; Sat, 21 Jan 2012 08:02:03 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RoaZz-0008G8-UO for bug-gnu-emacs@gnu.org; Sat, 21 Jan 2012 08:02:01 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:33278) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RoaZz-0008G2-Pf for bug-gnu-emacs@gnu.org; Sat, 21 Jan 2012 08:01:59 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.72) (envelope-from ) id 1Roaa2-0001u0-FW for bug-gnu-emacs@gnu.org; Sat, 21 Jan 2012 08:02: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: Sat, 21 Jan 2012 13:02: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: patch Original-Received: via spool by 10489-submit@debbugs.gnu.org id=B10489.13271509087290 (code B ref 10489); Sat, 21 Jan 2012 13:02:02 +0000 Original-Received: (at 10489) by debbugs.gnu.org; 21 Jan 2012 13:01:48 +0000 Original-Received: from localhost ([127.0.0.1]:38665 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1RoaZn-0001tW-9d for submit@debbugs.gnu.org; Sat, 21 Jan 2012 08:01:48 -0500 Original-Received: from mail-ww0-f46.google.com ([74.125.82.46]:55332) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1RoaZk-0001tJ-BZ for 10489@debbugs.gnu.org; Sat, 21 Jan 2012 08:01:46 -0500 Original-Received: by wgbdt14 with SMTP id dt14so1440031wgb.15 for <10489@debbugs.gnu.org>; Sat, 21 Jan 2012 05:01:35 -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=t67Q7JKhA2Ti+pO6wpw1Psre7PRB5EY97M55J+cYKLk=; b=GjNWUM4g6UM8rcIlK9vWA4KQHFuSm60FRzsSBwMJwGRpEUoPY2k36zJJbUCdWIrpH+ 800fxKZwYfHLt/2mBXrv9wPLLHrxNEfoFC1wUs6U6YtkFoIy0W+4WoXzmmqAof34U//7 VKQFW0ZHvpicLl1zMbULRHgiFRMeGQXYE0GII= Original-Received: by 10.180.77.228 with SMTP id v4mr2670511wiw.8.1327150894405; Sat, 21 Jan 2012 05:01:34 -0800 (PST) Original-Received: from thierry-MM061 (204.77.197.77.rev.sfr.net. [77.197.77.204]) by mx.google.com with ESMTPS id dr5sm19670125wib.0.2012.01.21.05.01.30 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 21 Jan 2012 05:01:32 -0800 (PST) In-Reply-To: (Andreas Schwab's message of "Mon, 16 Jan 2012 15:09:48 +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:55888 Archived-At: --=-=-= Content-Type: text/plain Andreas Schwab writes: >> So, any objections to apply my patch to trunk with these changes? > > You also need to check whether the target is a subdirectory of the > source. To clarify: --8<---------------cut here---------------start------------->8--- thierry@thierry-MM061:~$ cd ~/tmp/Test/ thierry@thierry-MM061:~/tmp/Test$ ls -R .: Test1 ./Test1: Test2 ./Test1/Test2: Test3 ./Test1/Test2/Test3: thierry@thierry-MM061:~/tmp/Test$ LC_ALL=C cp -r ~/tmp/Test/ ~/tmp/Test/Test1/Test2/Test3/ cp: cannot copy a directory, `/home/thierry/tmp/Test/', into itself, `/home/thierry/tmp/Test/Test1/Test2/Test3/Test' --8<---------------cut here---------------end--------------->8--- So we need to check this. (See `file-subdir-of-p' in this patch) --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=Singlepatch-r118414ToTip.patch Content-Description: bugfix ##Merge of all patches applied from revision 118409 ## patch-r118414: Bugfix bug#10489, dired-do-copy may create infinite directory hierarchy. ## patch-r118411: * lisp/dired-aux.el (dired-copy-file-recursive): Use file-equal-p. ## patch-r118412: * lisp/files.el (file-subdir-of-p): Check if file1 is subdir of file2. ## diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -1264,24 +1264,27 @@ (defun dired-copy-file-recursive (from to ok-flag &optional preserve-time top recursive) + (when (or (files-equal-p from to) + (file-subdir-of-p from to)) + (error "Can't copy directory `%s' on itself" from)) (let ((attrs (file-attributes from))) (if (and recursive - (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) + (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 - (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)))))) + (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)))))) ;;;###autoload (defun dired-rename-file (file newname ok-if-already-exists) @@ -1378,7 +1381,7 @@ ;; 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 - &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. @@ -1401,10 +1404,14 @@ Optional MARKER-CHAR is a character with which to mark every newfile's entry, or t to use the current marker character if the old file was marked." - (let (dired-create-files-failures failures - skipped (success-count 0) (total (length fn-list))) - (let (to overwrite-query - overwrite-backup-query) ; for dired-handle-overwrite + (let (dired-create-files-failures + failures + skipped + (success-count 0) + (total (length fn-list))) + (let (to + overwrite-query + overwrite-backup-query) ; for dired-handle-overwrite (dolist (from fn-list) (setq to (funcall name-constructor from)) (if (equal to from) @@ -1430,10 +1437,25 @@ (cond ((integerp marker-char) marker-char) (marker-char (dired-file-marker from)) ; slow (t nil)))) - (when (and (file-directory-p from) - (file-directory-p to) - (eq file-creator 'dired-copy-file)) - (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" => "~/test" or "~/foo" =>"~/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 (files-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 +1478,25 @@ (setq failures (nconc failures dired-create-files-failures)) (dired-log-summary (format "%s failed for %d file%s in %d requests" - operation (length failures) - (dired-plural-s (length failures)) - total) + operation (length failures) + (dired-plural-s (length failures)) + total) failures)) (failures (dired-log-summary (format "%s failed for %d of %d file%s" - operation (length failures) - 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" - operation (length skipped) total - (dired-plural-s total)) + operation (length skipped) total + (dired-plural-s total)) skipped)) (t (message "%s: %s file%s" - 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 --- a/lisp/files.el +++ b/lisp/files.el @@ -4902,6 +4902,35 @@ directory 'full directory-files-no-dot-files-regexp))) (delete-directory-internal directory))))) +(defun files-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)) + (equal (file-attributes (file-truename (expand-file-name file1))) + (file-attributes (file-truename (expand-file-name file2)))))) + +(defun file-subdir-of-p (file1 file2) + "Check if FILE1 is a subdirectory of FILE2 on current filesystem. +If directory FILE1 is the same than directory FILE2, return non--nil." + (when (and (not (or (file-remote-p file1) + (file-remote-p file2))) + (not (string= file1 "/")) + (file-directory-p file1) + (file-directory-p file2)) + (or (string= file2 "/") + (loop with f1 = (expand-file-name file1) + with f2 = (expand-file-name file2) + with ls1 = (split-string f1 "/" t) + with ls2 = (split-string f2 "/" t) + for p = (string-match "^/" f1) + for i in ls1 + for j in ls2 + when (string= i j) + concat (if p (concat "/" i) (concat i "/")) + into root + finally return + (string= (file-truename (directory-file-name root)) + (file-truename (directory-file-name f2))))))) + (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 +4957,13 @@ (format "Copy directory %s to: " dir) default-directory default-directory nil nil) current-prefix-arg t nil))) + (when (or (files-equal-p directory newname) + (file-subdir-of-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) - (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) --=-=-= Content-Type: text/plain -- Thierry Get my Gnupg key: gpg --keyserver pgp.mit.edu --recv-keys 59F29997 --=-=-=--