Function: tramp-adb-handle-write-region

tramp-adb-handle-write-region is a byte-compiled function defined in tramp-adb.el.gz.

Signature

(tramp-adb-handle-write-region START END FILENAME &optional APPEND VISIT LOCKNAME MUSTBENEW)

Documentation

Like write-region for Tramp files.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp-adb.el.gz
(defun tramp-adb-handle-write-region
  (start end filename &optional append visit lockname mustbenew)
  "Like `write-region' for Tramp files."
  (setq filename (expand-file-name filename)
	lockname (file-truename (or lockname filename)))
  (with-parsed-tramp-file-name filename nil
    (when (and mustbenew (file-exists-p filename)
	       (or (eq mustbenew 'excl)
		   (not
		    (y-or-n-p
		     (format "File %s exists; overwrite anyway?" filename)))))
      (tramp-error v 'file-already-exists filename))

    (let ((file-locked (eq (file-locked-p lockname) t))
	  (curbuf (current-buffer))
	  (tmpfile (tramp-compat-make-temp-file filename)))

      ;; Lock file.
      (when (and (not (auto-save-file-name-p (file-name-nondirectory filename)))
		 (file-remote-p lockname)
		 (not file-locked))
	(setq file-locked t)
	;; `lock-file' exists since Emacs 28.1.
	(tramp-compat-funcall 'lock-file lockname))

      (when (and append (file-exists-p filename))
	(copy-file filename tmpfile 'ok)
	(set-file-modes tmpfile (logior (or (file-modes tmpfile) 0) #o0600)))
      (let (create-lockfiles)
        (write-region start end tmpfile append 'no-message))
      (with-tramp-progress-reporter
	  v 3 (format-message
	       "Moving tmp file `%s' to `%s'" tmpfile filename)
	(unwind-protect
	    (unless (tramp-adb-execute-adb-command
		     v "push" tmpfile (tramp-compat-file-name-unquote localname))
	      (tramp-error v 'file-error "Cannot write: `%s'" filename))
	  (delete-file tmpfile)))

      ;; We must also flush the cache of the directory, because
      ;; `file-attributes' reads the values from there.
      (tramp-flush-file-properties v localname)

      (unless (equal curbuf (current-buffer))
	(tramp-error
	 v 'file-error
	 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))

      ;; Set file modification time.
      (when (or (eq visit t) (stringp visit))
	(set-visited-file-modtime
	 (or (tramp-compat-file-attribute-modification-time
	      (file-attributes filename))
	     (current-time))))

      ;; Unlock file.
      (when file-locked
	;; `unlock-file' exists since Emacs 28.1.
	(tramp-compat-funcall 'unlock-file lockname))

      ;; The end.
      (when (and (null noninteractive)
		 (or (eq visit t) (string-or-null-p visit)))
	(tramp-message v 0 "Wrote %s" filename))
      (run-hooks 'tramp-handle-write-region-hook))))