Don't create directory in erc-truncate compat check

* lisp/erc/erc-log.el (erc-log--check-writable-nocreate-p): New variable.
(erc-logging-enabled): Use `erc-log--save-in-progress-p' flag to
conditionally avoid creating a directory when checking if the location
is writable.
(erc-log--call-when-logging-enabled-sans-module)
(erc-log--check-legacy-implicit-enabling-by-truncate): Rename former to
latter, and guard against creating a ~/log directory just to see if it's
writable when calling `erc-logging-enabled'.
(erc-truncate-mode): Explain legacy "implicit logging" behavior in doc
string.
* lisp/erc/erc-truncate.el (erc-truncate--warn-about-logging): Make more
concise, and defer to `erc-truncate-mode' doc string for particulars.
* lisp/erc/erc.el (erc-directory-writable-p): Add comment to rename on
next non-patch release.
This commit is contained in:
F. Jason Park 2024-09-30 02:10:02 -07:00
parent 3f1ce47fe7
commit 1de2c86317
3 changed files with 36 additions and 25 deletions

View file

@ -307,6 +307,10 @@ Return nil if BUFFER is a server buffer."
(erc-save-buffer-in-logs buffer)))
(defvar erc-log--save-in-progress-p nil)
;; The function `erc-directory-writable-p' may signal when HOME is not
;; writable, such as when running the test suite (/nonexistent). This
;; flag tells `erc-logging-enabled' to use `file-writable-p' instead.
(defvar erc-log--check-writable-nocreate-p nil)
;;;###autoload
(defun erc-logging-enabled (&optional buffer)
@ -319,7 +323,9 @@ is writable (it will be created as necessary) and
(and erc-log-channels-directory
(not erc-log--save-in-progress-p)
(or (functionp erc-log-channels-directory)
(erc-directory-writable-p erc-log-channels-directory))
(if erc-log--check-writable-nocreate-p
(file-writable-p erc-log-channels-directory)
(erc-directory-writable-p erc-log-channels-directory)))
(if (functionp erc-enable-logging)
(funcall erc-enable-logging buffer)
(buffer-local-value 'erc-enable-logging buffer))))
@ -452,14 +458,14 @@ You can save every individual message by putting this function on
(defun erc-log--save-on-clear (_ end)
(erc-save-buffer-in-logs end))
;; This is a kludge to avoid littering erc-truncate.el with forward
;; declarations needed only for a corner-case compatibility check.
(defun erc-log--call-when-logging-enabled-sans-module (fn)
(when (and (erc-logging-enabled)
(not (or erc-log-mode (memq 'log erc-modules))))
(let ((dirfile (and (stringp erc-log-channels-directory)
erc-log-channels-directory)))
(funcall fn dirfile))))
;; This exists to avoid littering erc-truncate.el with forward
;; declarations needed only for a compatibility check.
(defun erc-log--check-legacy-implicit-enabling-by-truncate ()
"Return non-nil when conditions for legacy \"implicit\" activation are met.
This only concerns the \\+`truncate' module."
(and (not (or erc-log-mode (memq 'log erc-modules)))
(let ((erc-log--check-writable-nocreate-p t))
(erc-logging-enabled))))
(provide 'erc-log)

View file

@ -52,7 +52,18 @@ plus `erc-max-buffer-size'."
"Truncate a query buffer if it gets too large.
This prevents the query buffer from getting too large, which can
bring any grown Emacs to its knees after a few days worth of
tracking heavy-traffic channels."
tracking heavy-traffic channels.
Before ERC 5.6, this module performed logging whenever the \\+`log'
module's library, \\+`erc-log', happened to be loaded, regardless of
whether the \\+`log' module itself was enabled. (Loading can of course
happen in any number of ways, such as when browsing options via
\\[customize-group] or completing autoloaded symbol names at the
\\[describe-variable] prompt.) Users of \\+`truncate' who prefer the
old behavior can add \\+`log' to `erc-modules' to get the same effect.
Those who don't want logging but need to load the \\+`erc-log' library
for other purposes should customize either `erc-enable-logging' or
`erc-log-channels-directory' to avoid the annoying warning."
;;enable
((add-hook 'erc-insert-done-hook #'erc-truncate-buffer)
(add-hook 'erc-connect-pre-hook #'erc-truncate--warn-about-logging)
@ -83,21 +94,13 @@ tracking heavy-traffic channels."
(defun erc-truncate--warn-about-logging (&rest _)
(when (and (not erc--target)
(fboundp 'erc-log--call-when-logging-enabled-sans-module))
;; We could also enable `erc-log-mode' here, but the risk of
;; lasting damage is nonzero.
(erc-log--call-when-logging-enabled-sans-module
(lambda (dirfile)
;; Emit a real Emacs warning because the message may be
;; truncated away before it can be read if merely inserted.
(erc-button--display-error-notice-with-keys-and-warn
"The `truncate' module no longer enables logging implicitly."
" If you want ERC to write logs before truncating, add `log' to"
" `erc-modules' using something like \\[customize-option]."
" To silence this message, don't `require' `erc-log'."
(and dirfile " Alternatively, change the value of")
(and dirfile " `erc-log-channels-directory', or move ")
dirfile (and dirfile " elsewhere."))))))
(fboundp 'erc-log--check-legacy-implicit-enabling-by-truncate)
(erc-log--check-legacy-implicit-enabling-by-truncate))
;; Emit a real Emacs warning because the message may be
;; truncated away before it can be read if merely inserted.
(erc-button--display-error-notice-with-keys-and-warn
"The `truncate' module no longer enables logging implicitly."
" See the doc string for `erc-truncate-mode' for details.")))
;;;###autoload
(defun erc-truncate-buffer-to-size (size &optional buffer)

View file

@ -9001,6 +9001,8 @@ If S is nil or an empty string then return general CLIENTINFO."
;; Hook functions
;; FIXME rename this to something like `erc-ensure-directory-writable'.
;; Functions suffixed with "-p" probably shouldn't have side effects.
(defun erc-directory-writable-p (dir)
"Determine whether DIR is a writable directory.
If it doesn't exist, create it."