Function: ghostel--download-file
ghostel--download-file is a natively compiled function defined in
ghostel-module-install.el.
Signature
(ghostel--download-file URL DEST)
Documentation
Download URL to DEST atomically. Return the final URL on success, else nil.
The returned URL reflects any HTTP redirects followed during the
fetch, which lets callers resolve a latest alias to the actual
release tag. Writes to a sibling temp file in the same directory
and renames it into place once the download succeeds. Renaming
swaps the directory entry to a new inode, so any process (notably
a running Emacs) that has the previous DEST file mmap'd keeps a
valid mapping to the old file content. Writing to DEST directly
would truncate the existing inode and corrupt that mapping.
Source Code
;; Defined in /nix/store/1252krff0gb2kisjcpp45m50cpdskfrl-emacs-packages-deps/share/emacs/site-lisp/ghostel-module-install.el
(defun ghostel--download-file (url dest)
"Download URL to DEST atomically. Return the final URL on success, else nil.
The returned URL reflects any HTTP redirects followed during the
fetch, which lets callers resolve a `latest' alias to the actual
release tag. Writes to a sibling temp file in the same directory
and renames it into place once the download succeeds. Renaming
swaps the directory entry to a new inode, so any process (notably
a running Emacs) that has the previous DEST file mmap'd keeps a
valid mapping to the old file content. Writing to DEST directly
would truncate the existing inode and corrupt that mapping."
(let* ((url-request-method "GET")
(url-show-status nil)
(tmp (make-temp-name (concat dest ".tmp.")))
(final-url nil))
(unwind-protect
(let ((buf (url-retrieve-synchronously url t t 30)))
(when buf
(unwind-protect
(with-current-buffer buf
(set-buffer-multibyte nil)
(goto-char (point-min))
(when (re-search-forward "^HTTP/[0-9.]+ 200" nil t)
(when (re-search-forward "\r?\n\r?\n" nil t)
(let ((coding-system-for-write 'binary)
(start (point)))
(when (< start (point-max))
(write-region start (point-max) tmp nil 'silent)
(set-file-modes tmp #o755)
(rename-file tmp dest t)
;; `url-current-object' tracks the URL of the
;; final response after redirect-following, so
;; latest-asset URLs resolve to /download/vX.Y.Z/.
(setq final-url
(or (and (boundp 'url-current-object)
url-current-object
(url-recreate-url url-current-object))
url)))))))
(when (buffer-live-p buf)
(kill-buffer buf)))))
(unless final-url
(when (file-exists-p tmp)
(ignore-errors (delete-file tmp)))))
final-url))