]> git.eshelyaron.com Git - emacs.git/commitdiff
Don't signal error when locking a file from non file-visiting buffer
authorEli Zaretskii <eliz@gnu.org>
Mon, 24 Jul 2023 15:08:40 +0000 (18:08 +0300)
committerEli Zaretskii <eliz@gnu.org>
Mon, 24 Jul 2023 15:08:40 +0000 (18:08 +0300)
* lisp/userlock.el (userlock--check-content-unchanged): Support
the case where a file is locked before being written to from a
non file-visiting buffer.  (Bug#64821)

lisp/userlock.el

index 562bc0a0a9f41ad2dce8b52a4788abcc38324d97..96de17d54fd6a729846825316ab035cf599b9671 100644 (file)
@@ -110,10 +110,11 @@ You can <\\`q'>uit; don't modify this file."))
 
 (defun userlock--check-content-unchanged (filename)
   (with-demoted-errors "Unchanged content check: %S"
-    ;; Even tho we receive `filename', we know that `filename' refers to the current
-    ;; buffer's file.
-    (cl-assert (equal (expand-file-name filename)
-                      (expand-file-name buffer-file-truename)))
+    ;; Even tho we receive `filename', we know that `filename' refers
+    ;; to the current buffer's file.
+    (cl-assert (or (null buffer-file-truename) ; temporary buffer
+                   (equal (expand-file-name filename)
+                          (expand-file-name buffer-file-truename))))
     ;; Note: rather than read the file and compare to the buffer, we could save
     ;; the buffer and compare to the file, but for encrypted data this
     ;; wouldn't work well (and would risk exposing the data).
@@ -135,7 +136,13 @@ You can <\\`q'>uit; don't modify this file."))
                          (compare-buffer-substrings
                           buf start end
                           (current-buffer) (point-min) (point-max))))))
-          (set-visited-file-modtime)
+          ;; We know that some buffer visits FILENAME, because our
+          ;; caller (see lock_file) verified that.  Thus, we set the
+          ;; modtime in that buffer, to cater to use case where the
+          ;; file is about to be written to from some buffer that
+          ;; doesn't visit any file, like a temporary buffer.
+          (with-current-buffer (get-file-buffer (file-truename filename))
+            (set-visited-file-modtime))
           'unchanged)))))
 
 ;;;###autoload