aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/files.el19
-rw-r--r--src/fileio.c9
-rw-r--r--test/lisp/files-tests.el14
3 files changed, 26 insertions, 16 deletions
diff --git a/lisp/files.el b/lisp/files.el
index 19573cdf7b2..b05d453b0e7 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1407,14 +1407,17 @@ You can then use `write-region' to write new data into the file.
1407If DIR-FLAG is non-nil, create a new empty directory instead of a file. 1407If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1408 1408
1409If SUFFIX is non-nil, add that at the end of the file name." 1409If SUFFIX is non-nil, add that at the end of the file name."
1410 (let ((absolute-prefix (expand-file-name prefix temporary-file-directory))) 1410 (let ((absolute-prefix
1411 (if (or (zerop (length prefix)) (member prefix '("." "..")))
1412 (concat (file-name-as-directory temporary-file-directory) prefix)
1413 (expand-file-name prefix temporary-file-directory))))
1411 (if (find-file-name-handler absolute-prefix 'write-region) 1414 (if (find-file-name-handler absolute-prefix 'write-region)
1412 (files--make-magic-temp-file prefix dir-flag suffix) 1415 (files--make-magic-temp-file absolute-prefix dir-flag suffix)
1413 (make-temp-file-internal absolute-prefix 1416 (make-temp-file-internal absolute-prefix
1414 (if dir-flag t) (or suffix ""))))) 1417 (if dir-flag t) (or suffix "")))))
1415 1418
1416(defun files--make-magic-temp-file (prefix &optional dir-flag suffix) 1419(defun files--make-magic-temp-file (absolute-prefix &optional dir-flag suffix)
1417 "Implement (make-temp-file PREFIX DIR-FLAG SUFFIX). 1420 "Implement (make-temp-file ABSOLUTE-PREFIX DIR-FLAG SUFFIX).
1418This implementation works on magic file names." 1421This implementation works on magic file names."
1419 ;; Create temp files with strict access rights. It's easy to 1422 ;; Create temp files with strict access rights. It's easy to
1420 ;; loosen them later, whereas it's impossible to close the 1423 ;; loosen them later, whereas it's impossible to close the
@@ -1423,13 +1426,7 @@ This implementation works on magic file names."
1423 (let (file) 1426 (let (file)
1424 (while (condition-case () 1427 (while (condition-case ()
1425 (progn 1428 (progn
1426 (setq file 1429 (setq file (make-temp-name absolute-prefix))
1427 (make-temp-name
1428 (if (zerop (length prefix))
1429 (file-name-as-directory
1430 temporary-file-directory)
1431 (expand-file-name prefix
1432 temporary-file-directory))))
1433 (if suffix 1430 (if suffix
1434 (setq file (concat file suffix))) 1431 (setq file (concat file suffix)))
1435 (if dir-flag 1432 (if dir-flag
diff --git a/src/fileio.c b/src/fileio.c
index b7e3b71a475..69079c6ae49 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -662,17 +662,16 @@ DEFUN ("make-temp-file-internal", Fmake_temp_file_internal,
662Return the name of the generated file. If DIR-FLAG is zero, do not 662Return the name of the generated file. If DIR-FLAG is zero, do not
663create the file, just its name. Otherwise, if DIR-FLAG is non-nil, 663create the file, just its name. Otherwise, if DIR-FLAG is non-nil,
664create an empty directory. The file name should end in SUFFIX. 664create an empty directory. The file name should end in SUFFIX.
665Do not expand PREFIX; a non-absolute PREFIX is relative to the Emacs
666working directory.
665 667
666Signal an error if the file could not be created. 668Signal an error if the file could not be created.
667 669
668This function does not grok magic file names. */) 670This function does not grok magic file names. */)
669 (Lisp_Object prefix, Lisp_Object dir_flag, Lisp_Object suffix) 671 (Lisp_Object prefix, Lisp_Object dir_flag, Lisp_Object suffix)
670{ 672{
671 bool make_temp_name = EQ (dir_flag, make_number (0)); 673 CHECK_STRING (prefix);
672 CHECK_STRING (suffix); 674 CHECK_STRING (suffix);
673 if (!make_temp_name)
674 prefix = Fexpand_file_name (prefix, Vtemporary_file_directory);
675
676 Lisp_Object encoded_prefix = ENCODE_FILE (prefix); 675 Lisp_Object encoded_prefix = ENCODE_FILE (prefix);
677 Lisp_Object encoded_suffix = ENCODE_FILE (suffix); 676 Lisp_Object encoded_suffix = ENCODE_FILE (suffix);
678 ptrdiff_t prefix_len = SBYTES (encoded_prefix); 677 ptrdiff_t prefix_len = SBYTES (encoded_prefix);
@@ -686,7 +685,7 @@ This function does not grok magic file names. */)
686 memset (data + prefix_len, 'X', nX); 685 memset (data + prefix_len, 'X', nX);
687 memcpy (data + prefix_len + nX, SSDATA (encoded_suffix), suffix_len); 686 memcpy (data + prefix_len + nX, SSDATA (encoded_suffix), suffix_len);
688 int kind = (NILP (dir_flag) ? GT_FILE 687 int kind = (NILP (dir_flag) ? GT_FILE
689 : make_temp_name ? GT_NOCREATE 688 : EQ (dir_flag, make_number (0)) ? GT_NOCREATE
690 : GT_DIR); 689 : GT_DIR);
691 int fd = gen_tempname (data, suffix_len, O_BINARY | O_CLOEXEC, kind); 690 int fd = gen_tempname (data, suffix_len, O_BINARY | O_CLOEXEC, kind);
692 if (fd < 0 || (NILP (dir_flag) && emacs_close (fd) != 0)) 691 if (fd < 0 || (NILP (dir_flag) && emacs_close (fd) != 0))
diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el
index 7bfdca53e08..4a17e0d4697 100644
--- a/test/lisp/files-tests.el
+++ b/test/lisp/files-tests.el
@@ -166,6 +166,20 @@ form.")
166 (should (eq buffer-file-coding-system 'iso-2022-7bit-unix)))) 166 (should (eq buffer-file-coding-system 'iso-2022-7bit-unix))))
167 (delete-file tempfile)))) 167 (delete-file tempfile))))
168 168
169(ert-deftest files-test-make-temp-file-empty-prefix ()
170 "Test make-temp-file with an empty prefix."
171 (let ((tempfile (make-temp-file ""))
172 (tempdir (make-temp-file "" t))
173 (tempfile-. (make-temp-file "."))
174 (tempdir-. (make-temp-file "." t))
175 (tempfile-.. (make-temp-file ".."))
176 (tempdir-.. (make-temp-file ".." t)))
177 (dolist (file (list tempfile tempfile-. tempfile-..))
178 (should file)
179 (delete-file file))
180 (dolist (dir (list tempdir tempdir-. tempdir-..))
181 (should dir)
182 (delete-directory dir))))
169 183
170;; Stop the above "Local Var..." confusing Emacs. 184;; Stop the above "Local Var..." confusing Emacs.
171 185