Fix #4993: shorten long file names for caches

Emacs generates long file names for auto-save and backup files.
undo-fu-session and tramp are two more big offenders. This fix SHA1s
their file names so they never exceed 40 characters.

Will also affect any package that uses make-backup-file-name-1
directly (like undo-tree).
This commit is contained in:
Henrik Lissner 2021-05-06 14:33:08 -04:00
parent a98c2ece63
commit 9831642a02
2 changed files with 35 additions and 0 deletions

View file

@ -130,6 +130,36 @@ or file path may exist now."
(eq buffer (window-buffer (selected-window))) ; only visible buffers (eq buffer (window-buffer (selected-window))) ; only visible buffers
(set-auto-mode)))))) (set-auto-mode))))))
;; HACK Emacs generates long file paths for its auto-save files; long =
;; `auto-save-list-file-prefix' + `buffer-file-name'. If too long, the
;; filesystem will murder your family. To appease it, I compress
;; `buffer-file-name' to a stable 40 characters.
;; TODO PR this upstream; should be a universal issue!
(defadvice! doom-make-hashed-auto-save-file-name-a (orig-fn)
"Compress the auto-save file name so paths don't get too long."
:around #'make-auto-save-file-name
(let ((buffer-file-name
(if (or
;; Don't do anything for non-file-visiting buffers. Names
;; generated for those are short enough already.
(null buffer-file-name)
;; If an alternate handler exists for this path, bow out. Most of
;; them end up calling `make-auto-save-file-name' again anyway, so
;; we still achieve this advice's ultimate goal.
(find-file-name-handler buffer-file-name
'make-auto-save-file-name))
buffer-file-name
(sha1 buffer-file-name))))
(funcall orig-fn)))
;; HACK Does the same for Emacs backup files, but also packages that use
;; `make-backup-file-name-1' directly (like undo-tree).
(defadvice! doom-make-hashed-backup-file-name-a (args)
"A few places use the backup file name so paths don't get too long."
:filter-args #'make-backup-file-name-1
(setcar args (sha1 (car args)))
args)
;; ;;
;;; Formatting ;;; Formatting

View file

@ -31,6 +31,11 @@
(setq undo-fu-session-directory (concat doom-cache-dir "undo-fu-session/") (setq undo-fu-session-directory (concat doom-cache-dir "undo-fu-session/")
undo-fu-session-incompatible-files '("\\.gpg$" "/COMMIT_EDITMSG\\'" "/git-rebase-todo\\'")) undo-fu-session-incompatible-files '("\\.gpg$" "/COMMIT_EDITMSG\\'" "/git-rebase-todo\\'"))
;; HACK Fix #4993: prevent file names that are too long for the filesystem.
;; TODO PR this upstream; should be a universal issue
(advice-add #'undo-fu-session--make-file-name
:filter-args #'doom-make-hashed-backup-file-name-a)
;; HACK We avoid `:config' here because `use-package's `:after' complicates ;; HACK We avoid `:config' here because `use-package's `:after' complicates
;; the load order of a package's `:config' block and makes it impossible ;; the load order of a package's `:config' block and makes it impossible
;; for the user to override its settings with merely `after!' (or ;; for the user to override its settings with merely `after!' (or