Function: tramp-sh-handle-make-symbolic-link

tramp-sh-handle-make-symbolic-link is a byte-compiled function defined in tramp-sh.el.gz.

Signature

(tramp-sh-handle-make-symbolic-link TARGET LINKNAME &optional OK-IF-ALREADY-EXISTS)

Documentation

Like make-symbolic-link for Tramp files.

If TARGET is a non-Tramp file, it is used verbatim as the target of the symlink. If TARGET is a Tramp file, only the localname component is used as the target of the symlink.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp-sh.el.gz
;;; File Name Handler Functions:

(defun tramp-sh-handle-make-symbolic-link
    (target linkname &optional ok-if-already-exists)
  "Like `make-symbolic-link' for Tramp files.
If TARGET is a non-Tramp file, it is used verbatim as the target
of the symlink.  If TARGET is a Tramp file, only the localname
component is used as the target of the symlink."
  (with-parsed-tramp-file-name (expand-file-name linkname) nil
    ;; If TARGET is a Tramp name, use just the localname component.
    ;; Don't check for a proper method.
    (let ((non-essential t))
      (when (and (tramp-tramp-file-p target)
		 (tramp-file-name-equal-p v (tramp-dissect-file-name target)))
	(setq target (tramp-file-local-name (expand-file-name target))))
      ;; There could be a cyclic link.
      (tramp-flush-file-properties
       v (expand-file-name target (tramp-file-local-name default-directory))))

    ;; If TARGET is still remote, quote it.
    (if (tramp-tramp-file-p target)
	(make-symbolic-link
	 (tramp-compat-file-name-quote target 'top)
	 linkname ok-if-already-exists)

      (let ((ln (tramp-get-remote-ln v))
	    (cwd (tramp-run-real-handler
		  #'file-name-directory (list localname))))
	(unless ln
	  (tramp-error
	   v 'file-error
	   (concat "Making a symbolic link: "
		   "ln(1) does not exist on the remote host")))

	;; Do the 'confirm if exists' thing.
	(when (file-exists-p linkname)
	  ;; What to do?
	  (if (or (null ok-if-already-exists) ; not allowed to exist
		  (and (numberp ok-if-already-exists)
		       (not
			(yes-or-no-p
			 (format
			  "File %s already exists; make it a link anyway?"
			  localname)))))
	      (tramp-error v 'file-already-exists localname)
	    (delete-file linkname)))

	(tramp-flush-file-properties v localname)

	;; Right, they are on the same host, regardless of user,
	;; method, etc.  We now make the link on the remote machine.
	;; This will occur as the user that TARGET belongs to.
	(and (tramp-send-command-and-check
	      v (format "cd %s" (tramp-shell-quote-argument cwd)))
             (tramp-send-command-and-check
	      v (format
		 "%s -sf %s %s" ln
		 (tramp-shell-quote-argument target)
		 ;; The command could exceed PATH_MAX, so we use
		 ;; relative file names.  However, relative file names
		 ;; could start with "-".
		 ;; `tramp-shell-quote-argument' does not handle this,
		 ;; we must do it ourselves.
		 (tramp-shell-quote-argument
                  (concat "./" (file-name-nondirectory localname))))))))))