Skip to content

Commit 21888a9

Browse files
Jonathan Tomeralbinus
authored andcommitted
Don't rewrite buffer contents after saving by rename (Bug#35497)
* lisp/files.el (basic-save-buffer-2): Don't rewrite file contents after saving-by-renaming. (Bug#35497) * test/lisp/files-tests.el (files-tests-dont-rewrite-precious-files): * test/lisp/net/tramp-tests.el (tramp-test10-write-region-file-precious-flag): Regression tests for this change.
1 parent cd8a1d6 commit 21888a9

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

lisp/files.el

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5258,7 +5258,7 @@ Before and after saving the buffer, this function runs
52585258
(set-file-extended-attributes buffer-file-name
52595259
(nth 1 setmodes)))
52605260
(set-file-modes buffer-file-name
5261-
(logior (car setmodes) 128))))))
5261+
(logior (car setmodes) 128)))))
52625262
(let (success)
52635263
(unwind-protect
52645264
(progn
@@ -5274,7 +5274,7 @@ Before and after saving the buffer, this function runs
52745274
(and setmodes (not success)
52755275
(progn
52765276
(rename-file (nth 2 setmodes) buffer-file-name t)
5277-
(setq buffer-backed-up nil))))))
5277+
(setq buffer-backed-up nil)))))))
52785278
setmodes))
52795279

52805280
(declare-function diff-no-select "diff"

test/lisp/files-tests.el

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,5 +1244,20 @@ See <https://debbugs.gnu.org/35241>."
12441244
(executable-find (file-name-nondirectory tmpfile))))))
12451245
(delete-file tmpfile))))
12461246

1247+
(ert-deftest files-tests-dont-rewrite-precious-files ()
1248+
"Test that `file-precious-flag' forces files to be saved by
1249+
renaming only, rather than modified in-place."
1250+
(let* ((temp-file-name (make-temp-file "files-tests"))
1251+
(advice (lambda (_start _end filename &rest _r)
1252+
(should-not (string= filename temp-file-name)))))
1253+
(unwind-protect
1254+
(with-current-buffer (find-file-noselect temp-file-name)
1255+
(advice-add #'write-region :before advice)
1256+
(setq-local file-precious-flag t)
1257+
(insert "foobar")
1258+
(should (null (save-buffer))))
1259+
(ignore-errors (advice-remove #'write-region advice))
1260+
(ignore-errors (delete-file temp-file-name)))))
1261+
12471262
(provide 'files-tests)
12481263
;;; files-tests.el ends here

test/lisp/net/tramp-tests.el

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
;;; Code:
4343

44+
(require 'cl-seq)
4445
(require 'dired)
4546
(require 'ert)
4647
(require 'ert-x)
@@ -2270,6 +2271,34 @@ This checks also `file-name-as-directory', `file-name-directory',
22702271
;; Cleanup.
22712272
(ignore-errors (delete-file tmp-name))))))
22722273

2274+
(ert-deftest tramp-test10-write-region-file-precious-flag ()
2275+
"Check that `file-precious-flag' is respected with Tramp in use."
2276+
(skip-unless (tramp--test-enabled))
2277+
(skip-unless (tramp--test-sh-p))
2278+
2279+
(let* ((tmp-name (tramp--test-make-temp-name))
2280+
written-files
2281+
(advice (lambda (_start _end filename &rest _r)
2282+
(push filename written-files))))
2283+
2284+
(unwind-protect
2285+
(with-current-buffer (find-file-noselect tmp-name)
2286+
;; Write initial contents. Adapt `visited-file-modtime'
2287+
;; in order to suppress confirmation.
2288+
(insert "foo")
2289+
(write-region nil nil tmp-name)
2290+
(set-visited-file-modtime)
2291+
;; Run the test.
2292+
(advice-add 'write-region :before advice)
2293+
(setq-local file-precious-flag t)
2294+
(insert "bar")
2295+
(should (null (save-buffer)))
2296+
(should-not (cl-member tmp-name written-files :test #'string=)))
2297+
2298+
;; Cleanup.
2299+
(ignore-errors (advice-remove 'write-region advice))
2300+
(ignore-errors (delete-file tmp-name)))))
2301+
22732302
(ert-deftest tramp-test11-copy-file ()
22742303
"Check `copy-file'."
22752304
(skip-unless (tramp--test-enabled))

0 commit comments

Comments
 (0)