diff options
| author | Paul Eggert | 2017-08-05 21:27:45 -0700 |
|---|---|---|
| committer | Paul Eggert | 2017-08-05 21:28:09 -0700 |
| commit | 785a4a1d52fd7da3f3169fda26841341667c1661 (patch) | |
| tree | 456a79a178ba18701ad5f0b5d5b9993414f1e27d | |
| parent | c0df64db08b58cdac37cb38c16f2ba2f097fae92 (diff) | |
| download | emacs-785a4a1d52fd7da3f3169fda26841341667c1661.tar.gz emacs-785a4a1d52fd7da3f3169fda26841341667c1661.zip | |
Fix a couple of make-temp-file races
* lisp/emacs-lisp/autoload.el (autoload--save-buffer):
* lisp/emacs-lisp/bytecomp.el (byte-compile-file):
Use make-temp-file, not make-temp-name, to avoid an unlikely race
that could lose data. Remove the deletion hook as quickly as
possible after the file is renamed; though a race still remains
here, it is smaller than before.
| -rw-r--r-- | lisp/emacs-lisp/autoload.el | 10 | ||||
| -rw-r--r-- | lisp/emacs-lisp/bytecomp.el | 40 |
2 files changed, 26 insertions, 24 deletions
diff --git a/lisp/emacs-lisp/autoload.el b/lisp/emacs-lisp/autoload.el index 8fe94013700..4a9bd6d06b3 100644 --- a/lisp/emacs-lisp/autoload.el +++ b/lisp/emacs-lisp/autoload.el | |||
| @@ -875,16 +875,16 @@ FILE's modification time." | |||
| 875 | "Save current buffer to its file, atomically." | 875 | "Save current buffer to its file, atomically." |
| 876 | ;; Copied from byte-compile-file. | 876 | ;; Copied from byte-compile-file. |
| 877 | (let* ((version-control 'never) | 877 | (let* ((version-control 'never) |
| 878 | (tempfile (make-temp-name buffer-file-name)) | 878 | (tempfile (make-temp-file buffer-file-name)) |
| 879 | (kill-emacs-hook | 879 | (kill-emacs-hook |
| 880 | (cons (lambda () (ignore-errors (delete-file tempfile))) | 880 | (cons (lambda () (ignore-errors (delete-file tempfile))) |
| 881 | kill-emacs-hook))) | 881 | kill-emacs-hook))) |
| 882 | (write-region (point-min) (point-max) tempfile nil 1) | 882 | (write-region (point-min) (point-max) tempfile nil 1) |
| 883 | (backup-buffer) | 883 | (backup-buffer) |
| 884 | (rename-file tempfile buffer-file-name t) | 884 | (rename-file tempfile buffer-file-name t)) |
| 885 | (set-buffer-modified-p nil) | 885 | (set-buffer-modified-p nil) |
| 886 | (set-visited-file-modtime) | 886 | (set-visited-file-modtime) |
| 887 | (or noninteractive (message "Wrote %s" buffer-file-name)))) | 887 | (or noninteractive (message "Wrote %s" buffer-file-name))) |
| 888 | 888 | ||
| 889 | (defun autoload-save-buffers () | 889 | (defun autoload-save-buffers () |
| 890 | (while autoload-modified-buffers | 890 | (while autoload-modified-buffers |
diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el index fdd4276e4e7..5fa7389e431 100644 --- a/lisp/emacs-lisp/bytecomp.el +++ b/lisp/emacs-lisp/bytecomp.el | |||
| @@ -1888,25 +1888,27 @@ The value is non-nil if there were no errors, nil if errors." | |||
| 1888 | (insert "\n") ; aaah, unix. | 1888 | (insert "\n") ; aaah, unix. |
| 1889 | (if (file-writable-p target-file) | 1889 | (if (file-writable-p target-file) |
| 1890 | ;; We must disable any code conversion here. | 1890 | ;; We must disable any code conversion here. |
| 1891 | (let* ((coding-system-for-write 'no-conversion) | 1891 | (progn |
| 1892 | ;; Write to a tempfile so that if another Emacs | 1892 | (let* ((coding-system-for-write 'no-conversion) |
| 1893 | ;; process is trying to load target-file (eg in a | 1893 | ;; Write to a tempfile so that if another Emacs |
| 1894 | ;; parallel bootstrap), it does not risk getting a | 1894 | ;; process is trying to load target-file (eg in a |
| 1895 | ;; half-finished file. (Bug#4196) | 1895 | ;; parallel bootstrap), it does not risk getting a |
| 1896 | (tempfile (make-temp-name target-file)) | 1896 | ;; half-finished file. (Bug#4196) |
| 1897 | (kill-emacs-hook | 1897 | (tempfile (make-temp-file target-file)) |
| 1898 | (cons (lambda () (ignore-errors (delete-file tempfile))) | 1898 | (kill-emacs-hook |
| 1899 | kill-emacs-hook))) | 1899 | (cons (lambda () (ignore-errors |
| 1900 | (write-region (point-min) (point-max) tempfile nil 1) | 1900 | (delete-file tempfile))) |
| 1901 | ;; This has the intentional side effect that any | 1901 | kill-emacs-hook))) |
| 1902 | ;; hard-links to target-file continue to | 1902 | (write-region (point-min) (point-max) tempfile nil 1) |
| 1903 | ;; point to the old file (this makes it possible | 1903 | ;; This has the intentional side effect that any |
| 1904 | ;; for installed files to share disk space with | 1904 | ;; hard-links to target-file continue to |
| 1905 | ;; the build tree, without causing problems when | 1905 | ;; point to the old file (this makes it possible |
| 1906 | ;; emacs-lisp files in the build tree are | 1906 | ;; for installed files to share disk space with |
| 1907 | ;; recompiled). Previously this was accomplished by | 1907 | ;; the build tree, without causing problems when |
| 1908 | ;; deleting target-file before writing it. | 1908 | ;; emacs-lisp files in the build tree are |
| 1909 | (rename-file tempfile target-file t) | 1909 | ;; recompiled). Previously this was accomplished by |
| 1910 | ;; deleting target-file before writing it. | ||
| 1911 | (rename-file tempfile target-file t)) | ||
| 1910 | (or noninteractive (message "Wrote %s" target-file))) | 1912 | (or noninteractive (message "Wrote %s" target-file))) |
| 1911 | ;; This is just to give a better error message than write-region | 1913 | ;; This is just to give a better error message than write-region |
| 1912 | (let ((exists (file-exists-p target-file))) | 1914 | (let ((exists (file-exists-p target-file))) |