Function: tramp-sh-handle-copy-directory

tramp-sh-handle-copy-directory is a byte-compiled function defined in tramp-sh.el.gz.

Signature

(tramp-sh-handle-copy-directory DIRNAME NEWNAME &optional KEEP-DATE PARENTS COPY-CONTENTS)

Documentation

Like copy-directory for Tramp files.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp-sh.el.gz
(defun tramp-sh-handle-copy-directory
  (dirname newname &optional keep-date parents copy-contents)
  "Like `copy-directory' for Tramp files."
  (tramp-skeleton-copy-directory
      dirname newname keep-date parents copy-contents
    (let* ((v1 (and (tramp-tramp-file-p dirname)
		    (tramp-dissect-file-name dirname)))
	   (v2 (and (tramp-tramp-file-p newname)
		    (tramp-dissect-file-name newname)))
	   (v (or v1 v2))
	   target)
      (cond
       ((and copy-directory-create-symlink
	     (setq target (file-symlink-p dirname))
	     (tramp-equal-remote dirname newname))
	(make-symbolic-link
	 target
	 (if (directory-name-p newname)
	     (concat newname (file-name-nondirectory dirname)) newname)
	 t))

       ;; Shortcut: if method, host, user are the same for both files,
       ;; we invoke `cp' on the remote host directly.
       ((and (not copy-contents)
	     (tramp-equal-remote dirname newname))
	(when (and (file-directory-p newname)
		   (not (directory-name-p newname)))
	  (tramp-error v 'file-already-exists newname))
	(setq dirname (directory-file-name (expand-file-name dirname))
	      newname (directory-file-name (expand-file-name newname)))
	(tramp-do-copy-or-rename-file-directly
	 'copy dirname newname
	 'ok-if-already-exists keep-date 'preserve-uid-gid))

       ;; scp or rsync DTRT.
       ((and (not copy-contents)
	     (tramp-get-method-parameter v 'tramp-copy-recursive)
	     ;; When DIRNAME and NEWNAME are remote, they must have
	     ;; the same method.  None of them must be multi-hop.
	     (or (and (null v1) (tramp-method-out-of-band-p v2 0))
		 (and (null v2) (tramp-method-out-of-band-p v1 0))
		 (and v1 v2
		      (tramp-method-out-of-band-p v1 0)
		      (tramp-method-out-of-band-p v2 0)
		      (string-equal
		       (tramp-file-name-method v1)
		       (tramp-file-name-method v2)))))
	(when (and (file-directory-p newname)
		   (not (directory-name-p newname)))
	  (tramp-error v 'file-already-exists newname))
	(setq dirname (directory-file-name (expand-file-name dirname))
	      newname (directory-file-name (expand-file-name newname)))
	(when (and (file-directory-p newname)
		   (not (string-equal (file-name-nondirectory dirname)
				      (file-name-nondirectory newname))))
	  (setq newname
		(expand-file-name (file-name-nondirectory dirname) newname)))
	(unless (file-directory-p (file-name-directory newname))
	  (make-directory (file-name-directory newname) parents))
	(tramp-do-copy-or-rename-file-out-of-band
	 'copy dirname newname 'ok-if-already-exists keep-date))

       ;; We must do it file-wise.
       (t (tramp-run-real-handler
	   #'copy-directory
	   (list dirname newname keep-date parents copy-contents)))))))