Function: tramp-do-copy-or-rename-file

tramp-do-copy-or-rename-file is a byte-compiled function defined in tramp-sh.el.gz.

Signature

(tramp-do-copy-or-rename-file OP FILENAME NEWNAME &optional OK-IF-ALREADY-EXISTS KEEP-DATE PRESERVE-UID-GID PRESERVE-EXTENDED-ATTRIBUTES)

Documentation

Copy or rename a remote file.

OP must be copy or rename and indicates the operation to perform. FILENAME specifies the file to copy or rename, NEWNAME is the name of the new file (for copy) or the new name of the file (for rename). OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already. KEEP-DATE means to make sure that NEWNAME has the same timestamp as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep the uid and gid if both files are on the same host. PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.

This function is invoked by tramp-sh-handle-copy-file and tramp-sh-handle-rename-file. It is an error if OP is neither of copy and rename. FILENAME and NEWNAME must be absolute file names.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp-sh.el.gz
(defun tramp-do-copy-or-rename-file
  (op filename newname &optional ok-if-already-exists keep-date
   preserve-uid-gid preserve-extended-attributes)
  "Copy or rename a remote file.
OP must be `copy' or `rename' and indicates the operation to perform.
FILENAME specifies the file to copy or rename, NEWNAME is the name of
the new file (for copy) or the new name of the file (for rename).
OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
KEEP-DATE means to make sure that NEWNAME has the same timestamp
as FILENAME.  PRESERVE-UID-GID, when non-nil, instructs to keep
the uid and gid if both files are on the same host.
PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.

This function is invoked by `tramp-sh-handle-copy-file' and
`tramp-sh-handle-rename-file'.  It is an error if OP is neither
of `copy' and `rename'.  FILENAME and NEWNAME must be absolute
file names."
  (unless (memq op '(copy rename))
    (error "Unknown operation `%s', must be `copy' or `rename'" op))

  (if (and
       (file-directory-p filename)
       (not (tramp-equal-remote filename newname)))
      (progn
	(copy-directory filename newname keep-date t)
	(when (eq op 'rename) (delete-directory filename 'recursive)))

    (let ((t1 (tramp-tramp-file-p filename))
	  (t2 (tramp-tramp-file-p newname))
	  (length (tramp-compat-file-attribute-size
		   (file-attributes (file-truename filename))))
	  (attributes (and preserve-extended-attributes
			   (file-extended-attributes filename)))
	  (msg-operation (if (eq op 'copy) "Copying" "Renaming")))

      (with-parsed-tramp-file-name (if t1 filename newname) nil
	(unless (file-exists-p filename)
	  (tramp-compat-file-missing v filename))
	(when (and (not ok-if-already-exists) (file-exists-p newname))
	  (tramp-error v 'file-already-exists newname))
	(when (and (file-directory-p newname)
		   (not (directory-name-p newname)))
	  (tramp-error v 'file-error "File is a directory %s" newname))

	(with-tramp-progress-reporter
	    v 0 (format "%s %s to %s" msg-operation filename newname)

	  (cond
	   ;; Both are Tramp files.
	   ((and t1 t2)
	    (with-parsed-tramp-file-name filename v1
	      (with-parsed-tramp-file-name newname v2
		(cond
		 ;; Shortcut: if method, host, user are the same for
		 ;; both files, we invoke `cp' or `mv' on the remote
		 ;; host directly.
		 ((tramp-equal-remote filename newname)
		  (tramp-do-copy-or-rename-file-directly
		   op filename newname
		   ok-if-already-exists keep-date preserve-uid-gid))

		 ;; Try out-of-band operation.
		 ((and
		   (tramp-method-out-of-band-p v1 length)
		   (tramp-method-out-of-band-p v2 length))
		  (tramp-do-copy-or-rename-file-out-of-band
		   op filename newname ok-if-already-exists keep-date))

		 ;; No shortcut was possible.  So we copy the file
		 ;; first.  If the operation was `rename', we go back
		 ;; and delete the original file (if the copy was
		 ;; successful).  The approach is simple-minded: we
		 ;; create a new buffer, insert the contents of the
		 ;; source file into it, then write out the buffer to
		 ;; the target file.  The advantage is that it doesn't
		 ;; matter which file name handlers are used for the
		 ;; source and target file.
		 (t
		  (tramp-do-copy-or-rename-file-via-buffer
		   op filename newname ok-if-already-exists keep-date))))))

	   ;; One file is a Tramp file, the other one is local.
	   ((or t1 t2)
	    (cond
	     ;; Fast track on local machine.
	     ((tramp-local-host-p v)
	      (tramp-do-copy-or-rename-file-directly
	       op filename newname
	       ok-if-already-exists keep-date preserve-uid-gid))

	     ;; If the Tramp file has an out-of-band method, the
	     ;; corresponding copy-program can be invoked.
	     ((tramp-method-out-of-band-p v length)
	      (tramp-do-copy-or-rename-file-out-of-band
	       op filename newname ok-if-already-exists keep-date))

	     ;; Use the inline method via a Tramp buffer.
	     (t (tramp-do-copy-or-rename-file-via-buffer
		 op filename newname ok-if-already-exists keep-date))))

	   (t
	    ;; One of them must be a Tramp file.
	    (error "Tramp implementation says this cannot happen")))

	  ;; Handle `preserve-extended-attributes'.  We ignore possible
	  ;; errors, because ACL strings could be incompatible.
	  (when attributes
	    (ignore-errors
	      (set-file-extended-attributes newname attributes)))

	  ;; In case of `rename', we must flush the cache of the source file.
	  (when (and t1 (eq op 'rename))
	    (with-parsed-tramp-file-name filename v1
	      (tramp-flush-file-properties v1 v1-localname)))

	  ;; When newname did exist, we have wrong cached values.
	  (when t2
	    (with-parsed-tramp-file-name newname v2
	      (tramp-flush-file-properties v2 v2-localname))))))))