Function: tramp-file-name-for-operation

tramp-file-name-for-operation is a byte-compiled function defined in tramp.el.gz.

Signature

(tramp-file-name-for-operation OPERATION &rest ARGS)

Documentation

Return file name related to OPERATION file primitive.

ARGS are the arguments OPERATION has been called with.

It does not always return a Tramp file name, for example if the first argument of expand-file-name is absolute and not remote. Must be handled by the callers.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp.el.gz
;; We handle here all file primitives.  Most of them have the file
;; name as first parameter; nevertheless we check for them explicitly
;; in order to be signaled if a new primitive appears.  This
;; scenario is needed because there isn't a way to decide by
;; syntactical means whether a foreign method must be called.  It would
;; ease the life if `file-name-handler-alist' would support a decision
;; function as well but regexp only.
;; Operations added by external packages are kept in
;; `tramp-file-name-for-operation-external'.
(defun tramp-file-name-for-operation (operation &rest args)
  "Return file name related to OPERATION file primitive.
ARGS are the arguments OPERATION has been called with.

It does not always return a Tramp file name, for example if the
first argument of `expand-file-name' is absolute and not remote.
Must be handled by the callers."
  (cond
   ;; FILE resp DIRECTORY.
   ((or
     (memq operation
	   '(access-file byte-compiler-base-file-name delete-directory
	     delete-file diff-latest-backup-file directory-file-name
	     directory-files directory-files-and-attributes dired-compress-file
	     dired-uncache file-acl file-accessible-directory-p file-attributes
	     file-directory-p file-executable-p file-exists-p file-local-copy
	     file-locked-p file-modes file-name-as-directory
	     file-name-case-insensitive-p file-name-directory
	     file-name-nondirectory file-name-sans-versions
	     file-notify-add-watch file-ownership-preserved-p file-readable-p
	     file-regular-p file-remote-p file-selinux-context file-symlink-p
	     file-system-info file-truename file-writable-p
	     find-backup-file-name get-file-buffer
	     insert-directory insert-file-contents load lock-file make-directory
	     make-lock-file-name set-file-acl set-file-modes
	     set-file-selinux-context set-file-times substitute-in-file-name
	     unhandled-file-name-directory unlock-file vc-registered
	     ;; Emacs 28- only.
	     make-directory-internal
	     ;; Emacs 29+ only.
	     abbreviate-file-name
	     ;; Tramp internal magic file name function.
	     tramp-set-file-uid-gid))
     (eq (alist-get operation tramp-file-name-for-operation-external) 'file))
    (if (and (stringp (nth 0 args)) (file-name-absolute-p (nth 0 args)))
	(nth 0 args)
      default-directory))

   ;; STRING FILE.
   ((eq operation 'make-symbolic-link) (nth 1 args))

   ;; FILE DIRECTORY resp FILE1 FILE2.
   ((memq operation
	  '(add-name-to-file copy-directory copy-file
	    file-equal-p file-in-directory-p
	    file-name-all-completions file-name-completion
	    file-newer-than-file-p rename-file))
    (cond
     ((tramp-tramp-file-p (nth 0 args)) (nth 0 args))
     ((file-name-absolute-p (nth 1 args)) (nth 1 args))
     (t default-directory)))

   ;; FILE DIRECTORY resp FILE1 FILE2.
   ((eq operation 'expand-file-name)
    (cond
     ((file-name-absolute-p (nth 0 args)) (nth 0 args))
     ((tramp-tramp-file-p (nth 1 args)) (nth 1 args))
     (t default-directory)))

   ;; START END FILE.
   ((eq operation 'write-region)
    (if (file-name-absolute-p (nth 2 args))
	(nth 2 args)
      default-directory))

   ;; BUFFER.
   ((memq operation
	  '(make-auto-save-file-name
	    set-visited-file-modtime verify-visited-file-modtime))
    (buffer-file-name
     (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))

   ;; COMMAND.
   ((or
     (memq operation
	   '(exec-path make-nearby-temp-file make-process process-file
	     shell-command start-file-process temporary-file-directory
	     ;; Emacs 29+ only.
             list-system-processes memory-info process-attributes
             ;; Emacs 30+ only.
	     file-group-gid file-user-uid))
     (eq (alist-get operation tramp-file-name-for-operation-external)
	 'default-directory))
    default-directory)

   ;; PROC or BUFFER.
   ((or
     (memq operation '(file-notify-rm-watch file-notify-valid-p))
     (eq (alist-get operation tramp-file-name-for-operation-external) 'process))
    (or (when-let* (((processp (nth 0 args)))
		    (vec (process-get (nth 0 args) 'tramp-vector)))
	  (tramp-make-tramp-file-name vec))
	(when-let*
	    ((buf (cond
		   ((processp (nth 0 args)) (process-buffer (nth 0 args)))
		   ((bufferp (nth 0 args)) (get-buffer (nth 0 args)))
		   ((stringp (nth 0 args))
		    ;; Process or buffer name.
		    (or (get-process (nth 0 args)) (get-buffer (nth 0 args)))))))
	  (tramp-get-default-directory buf))
	""))

   ;; VEC.
   ((or
     (memq operation
	   '(tramp-get-home-directory tramp-get-remote-gid
	     tramp-get-remote-groups tramp-get-remote-uid))
     (eq (alist-get operation tramp-file-name-for-operation-external)
	 'tramp-file-name))
    (or
     (and (tramp-file-name-p (nth 0 args))
	  (tramp-make-tramp-file-name (nth 0 args)))
     ""))

   ;; A function.
   ((functionp (alist-get operation tramp-file-name-for-operation-external))
    (apply
     (alist-get operation tramp-file-name-for-operation-external)
     operation args))

   ;; Unknown file primitive.
   (t (unless (memq 'remote-file-error debug-ignored-errors)
	(tramp-error
	 nil 'remote-file-error "Unknown file I/O primitive: %s" operation)))))