1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-24 07:20:37 +00:00

Improve `file-has-changed-p'

* doc/lispref/files.texi (File Attributes): Be precise when
filename does not exist in `file-has-changed-p'.

* lisp/files.el (file-has-changed-p): Suppress remote file caches.
Handle the case FILE does not exists.
This commit is contained in:
Michael Albinus 2021-11-05 20:46:35 +01:00
parent 133026c362
commit a08a9a9aea
2 changed files with 17 additions and 13 deletions

View File

@ -1318,14 +1318,14 @@ on the 19th, @file{aug-20} was written on the 20th, and the file
This function returns non-@code{nil} if the time stamp of
@var{filename} has changed since the last call. When called for the
first time for some @var{filename}, it records the last modification
time and size of the file, and returns non-@code{nil}. Thereafter,
when called for the same @var{filename}, it compares the current time
stamp and size with the recorded ones, and returns non-@code{nil} only
if either the time stamp or the size (or both) are different. This is
useful when a Lisp program wants to re-read a file whenever it
changes. With an optional argument @var{tag}, which must be a symbol,
the size and modification time comparisons are limited to calls with
the same tag.
time and size of the file, and returns non-@code{nil} when
@var{filename} exists. Thereafter, when called for the same
@var{filename}, it compares the current time stamp and size with the
recorded ones, and returns non-@code{nil} only if either the time
stamp or the size (or both) are different. This is useful when a Lisp
program wants to re-read a file whenever it changes. With an optional
argument @var{tag}, which must be a symbol, the size and modification
time comparisons are limited to calls with the same tag.
@end defun
@defun file-attributes filename &optional id-format

View File

@ -6187,15 +6187,19 @@ Return nil if DIR is not an existing directory."
(defun file-has-changed-p (file &optional tag)
"Return non-nil if FILE has changed.
The size and modification time of FILE are compared to the size
and modification time of tghe same FILE during a previous
and modification time of the same FILE during a previous
invocation of `file-has-changed-p'. Thus, the first invocation
of `file-has-changed-p' always returns non-nil.
of `file-has-changed-p' always returns non-nil when FILE exists.
The optional argument TAG, which must be a symbol, can be used to
limit the comparison to invocations with identical tags; it can be
the symbol of the calling function, for example."
(let* ((fileattr (file-attributes file 'integer))
(attr (cons (file-attribute-size fileattr)
(file-attribute-modification-time fileattr)))
(let* (;; FIXME: Shall we use `file-truename'?
(file (directory-file-name file))
(remote-file-name-inhibit-cache t)
(fileattr (file-attributes file 'integer))
(attr (and fileattr
(cons (file-attribute-size fileattr)
(file-attribute-modification-time fileattr))))
(sym (concat (symbol-name tag) "@" file))
(cachedattr (gethash sym file-has-changed-p--hash-table)))
(when (not (equal attr cachedattr))