Subject: Re: bug#28834: 25.1; dired-do-copy: allow to copy into a nonexistent directory

From: Tino Calancha <[email-masked]>
Date: Sun, 15 Oct 2017 13:42:48 +0900

"alexei28" <[email-masked]> writes:

> I want to copy file from "d:/TEMP/test/test.txt" to nonexistent directory "d:/TEMP/test/new/" with Dired
> Copy ‘d:/TEMP/test/test.txt’ to ‘d:/TEMP/test/new/test.txt’ failed:
>
> (file-error Copying file Operation not permitted d:/TEMP/test/test.txt d:/TEMP/test/new/test.txt)
>
> It would be good if Emacs can autocreate not exist directory when copy/move files.

It might has sense that Dired handle the creation of those destination dirs.
WDOT?

--8<-----------------------------cut here---------------start------------->8---
commit 1325ea21a069b5c539157390fb5df2eca76adecb
Author: Tino Calancha <[email-masked]>
Date:   Sun Oct 15 13:34:08 2017 +0900

    Dired: Allow to copy/rename files into a non-existent dir
    
    * lisp/dired-aux.el (dired--create-dirs): New defun.
    (dired-copy-file-recursive, dired-rename-file): Use it (Bug#28834).
    * doc/emacs/dired.texi (Operating on Files): Update manual.
    * etc/NEWS (Changes in Specialized Modes and Packages in Emacs 27.1)
    Announce feature.
    * lisp/dired-aux-tests.el (dired-test-bug28834): Add test.

diff --git a/doc/emacs/dired.texi b/doc/emacs/dired.texi
index db5dea329b..48013a3221 100644
--- a/doc/emacs/dired.texi
+++ b/doc/emacs/dired.texi
@@ -645,7 +645,8 @@ Operating on Files
 @item C @var{new} @key{RET}
 Copy the specified files (@code{dired-do-copy}).  The argument @var{new}
 is the directory to copy into, or (if copying a single file) the new
-name.  This is like the shell command @code{cp}.
+name.  Dired creates any non-existent directories in @var{new}.  This is
+like the shell command @code{cp}.
 
 @vindex dired-copy-preserve-time
 If @code{dired-copy-preserve-time} is non-@code{nil}, then copying
@@ -674,7 +675,8 @@ Operating on Files
 @cindex moving files (in Dired)
 @item R @var{new} @key{RET}
 Rename the specified files (@code{dired-do-rename}).  If you rename a
-single file, the argument @var{new} is the new name of the file.  If
+single file, the argument @var{new} is the new name of the file.  Dired
+creates any non-existent directories in @var{new}.  If
 you rename several files, the argument @var{new} is the directory into
 which to move the files (this is like the shell command @command{mv}).
 
diff --git a/etc/NEWS b/etc/NEWS
index 716b0309a5..08291fc39f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -56,6 +56,12 @@ whether '"' is also replaced in 'electric-quote-mode'.  If non-nil,
 
 * Changes in Specialized Modes and Packages in Emacs 27.1
 
+** Dired
+
+---
+*** dired-do-copy and dired-rename-file now create non-existent
+directories in the destination.
+
 ** Edebug
 
 +++
diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index 7e2252fcf1..fcea8f1b13 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1548,6 +1548,10 @@ dired-copy-file
 
 (declare-function make-symbolic-link "fileio.c")
 
+(defun dired--create-dirs (dir)
+  (unless (file-exists-p dir)
+    (dired-create-directory dir)))
+
 (defun dired-copy-file-recursive (from to ok-flag &optional
 				       preserve-time top recursive)
   (when (and (eq t (car (file-attributes from)))
@@ -1564,6 +1568,7 @@ dired-copy-file-recursive
 	  (if (stringp (car attrs))
 	      ;; It is a symlink
 	      (make-symbolic-link (car attrs) to ok-flag)
+            (dired--create-dirs (file-name-directory to))
 	    (copy-file from to ok-flag preserve-time))
 	(file-date-error
 	 (push (dired-make-relative from)
@@ -1573,6 +1578,7 @@ dired-copy-file-recursive
 ;;;###autoload
 (defun dired-rename-file (file newname ok-if-already-exists)
   (dired-handle-overwrite newname)
+  (dired--create-dirs (file-name-directory newname))
   (rename-file file newname ok-if-already-exists) ; error is caught in -create-files
   ;; Silently rename the visited file of any buffer visiting this file.
   (and (get-file-buffer file)
diff --git a/test/lisp/dired-aux-tests.el b/test/lisp/dired-aux-tests.el
index d41feb1592..d8114d8401 100644
--- a/test/lisp/dired-aux-tests.el
+++ b/test/lisp/dired-aux-tests.el
@@ -40,5 +40,25 @@
           (should-not (dired-do-shell-command "ls ? ./`?`" nil files)))
       (delete-file foo))))
 
+(ert-deftest dired-test-bug28834 ()
+  "test for https://debbugs.gnu.org/28834 ."
+  (let* ((from (make-temp-file "from"))
+         (foo (make-temp-file "foo" 'dir))
+         (bar (file-name-as-directory (expand-file-name "bar" foo)))
+         (qux (file-name-as-directory (expand-file-name "qux" foo)))
+         (tmpdir temporary-file-directory)
+         (to-cp (expand-file-name "foo-cp" bar))
+         (to-mv (expand-file-name "foo-mv" qux)))
+    (unwind-protect
+        (progn
+          (dired-copy-file-recursive from to-cp nil)
+          (should (file-exists-p to-cp))
+          (dired-rename-file from to-mv nil)
+          (should (file-exists-p to-mv)))
+      ;; clean up
+      (delete-directory foo 'recursive)
+      (delete-file from))))
+
+
 (provide 'dired-aux-tests)
 ;; dired-aux-tests.el ends here

--8<-----------------------------cut here---------------end--------------->8---
In GNU Emacs 27.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2017-10-15
Repository revision: eed3a3d9e95d2c5346a23c9d92ca4e5848330183