Function: tramp-handle-lock-file

tramp-handle-lock-file is a byte-compiled function defined in tramp.el.gz.

Signature

(tramp-handle-lock-file FILE)

Documentation

Like lock-file for Tramp files.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp.el.gz
(defun tramp-handle-lock-file (file)
  "Like `lock-file' for Tramp files."
  ;; See if this file is visited and has changed on disk since it
  ;; was visited.
  (catch 'dont-lock
    (unless (eq (file-locked-p file) t) ;; Locked by me.
      (when (and buffer-file-truename
		 (not (verify-visited-file-modtime))
		 (file-exists-p file))
	;; In filelock.c, `userlock--ask-user-about-supersession-threat'
	;; is called, which also checks file contents.  This is unwise
	;; for remote files.
	(ask-user-about-supersession-threat file))

      (when-let ((info (tramp-get-lock-file file))
		 (match (string-match tramp-lock-file-info-regexp info)))
	(unless (ask-user-about-lock
		 file (format
		       "%s@%s (pid %s)" (match-string 1 info)
		       (match-string 2 info) (match-string 3 info)))
	  (throw 'dont-lock nil)))

      (when-let ((lockname (tramp-compat-make-lock-file-name file))
                 ;; USER@HOST.PID[:BOOT_TIME]
                 (info
                  (format
                   "%s@%s.%s" (user-login-name) tramp-system-name
                   (tramp-get-lock-pid file))))

	;; Protect against security hole.
	(with-parsed-tramp-file-name file nil
	  (when (and (not tramp-allow-unsafe-temporary-files)
		     create-lockfiles
		     (file-in-directory-p lockname temporary-file-directory)
		     (zerop (or (tramp-compat-file-attribute-user-id
				 (file-attributes file 'integer))
				tramp-unknown-id-integer))
		     (not (with-tramp-connection-property
			      (tramp-get-process v) "unsafe-temporary-file"
			    (yes-or-no-p
			     (concat
			      "Lock file on local temporary directory, "
			      "do you want to continue?")))))
	    (tramp-error v 'file-error "Unsafe lock file name")))

	;; Do the lock.
        (let ((tramp-verbose 0)
              create-lockfiles signal-hook-function)
	  (condition-case nil
	      (make-symbolic-link info lockname 'ok-if-already-exists)
	    (error
	     (with-file-modes #o0644
               (write-region info nil lockname)))))))))