Mercurial > hg > xemacs-beta
diff lisp/files.el @ 4266:c5a2b80bc4fa
[xemacs-hg @ 2007-11-14 18:51:20 by aidan]
Import make-temp-name (the functionality of mkstemp(3)) from GNU.
author | aidan |
---|---|
date | Wed, 14 Nov 2007 18:51:31 +0000 |
parents | 346788f5aa64 |
children | 555e21a66d51 |
line wrap: on
line diff
--- a/lisp/files.el Wed Nov 14 18:20:42 2007 +0000 +++ b/lisp/files.el Wed Nov 14 18:51:31 2007 +0000 @@ -163,8 +163,8 @@ (defun normal-backup-enable-predicate (name) "Default `backup-enable-predicate' function. -Checks for files in `temporary-file-directory' or -`small-temporary-file-directory'." +Checks for files in the directory returned by `temp-directory' or specified +by `small-temporary-file-directory'." (let ((temporary-file-directory (temp-directory))) (not (or (let ((comp (compare-strings temporary-file-directory 0 nil name 0 nil))) @@ -330,9 +330,8 @@ When one transform applies, its result is final; no further transforms are tried. -The default value is set up to put the auto-save file into the -temporary directory (see the variable `temporary-file-directory') for -editing a remote file." +The default value is set up to put the auto-save file into the temporary +directory (see the function `temp-directory') for editing a remote file." :group 'auto-save :type '(repeat (list (string :tag "Regexp") (string :tag "Replacement"))) ;:version "21.1" @@ -715,6 +714,51 @@ (setq newname (expand-file-name tem (file-name-directory newname))) (setq count (1- count)))) newname)) + +(defun make-temp-file (prefix &optional dir-flag suffix) + "Create a temporary file. +The returned file name (created by appending some random characters at the +end of PREFIX, and expanding against the return value of `temp-directory' if +necessary), is guaranteed to point to a newly created empty file. You can +then use `write-region' to write new data into the file. + +If DIR-FLAG is non-nil, create a new empty directory instead of a file. + +If SUFFIX is non-nil, add that at the end of the file name. + +This function is analagous to mkstemp(3) under POSIX, avoiding the race +condition between testing for the existence of the generated filename (under +POSIX with mktemp(3), under Emacs Lisp with `make-temp-name') and creating +it." + (let ((umask (default-file-modes)) + (temporary-file-directory (temp-directory)) + file) + (unwind-protect + (progn + ;; Create temp files with strict access rights. It's easy to + ;; loosen them later, whereas it's impossible to close the + ;; time-window of loose permissions otherwise. + (set-default-file-modes #o700) + (while (condition-case () + (progn + (setq file + (make-temp-name + (expand-file-name prefix + temporary-file-directory))) + (if suffix + (setq file (concat file suffix))) + (if dir-flag + (make-directory file) + (write-region "" nil file nil 'silent nil 'excl)) + nil) + (file-already-exists t)) + ;; the file was somehow created by someone else between + ;; `make-temp-name' and `write-region', let's try again. + nil) + file) + ;; Reset the umask. + (set-default-file-modes umask)))) + (defun switch-to-other-buffer (arg) "Switch to the previous buffer. With a numeric arg, n, switch to the nth