[Git][cmucl/cmucl][issue-364-add-mkstemp-mkdtemp] 2 commits: Yet another version of unix-mkstemp
Raymond Toy pushed to branch issue-364-add-mkstemp-mkdtemp at cmucl / cmucl Commits: 36f7ed64 by Raymond Toy at 2025-01-08T08:17:56-08:00 Yet another version of unix-mkstemp This is more in line with what we do for unix-mkdtemp. - - - - - 7bca4800 by Raymond Toy at 2025-01-08T08:21:43-08:00 Revert changes to exports.lisp and extensions.lisp We're not going to define macros for temporary files and directories in this MR. That's for another later MR. - - - - - 3 changed files: - src/code/exports.lisp - src/code/extensions.lisp - src/code/unix.lisp Changes: ===================================== src/code/exports.lisp ===================================== @@ -1717,11 +1717,7 @@ "DESCRIBE-EXTERNAL-FORMAT" "LIST-ALL-EXTERNAL-FORMATS" "STRING-ENCODE" "STRING-DECODE" - "SET-SYSTEM-EXTERNAL-FORMAT") - ;; Temporary files/directories - (:export - "WITH-TEMPORARY-FILE" - "WITH-TEMPORARY-DIRECTORY")) + "SET-SYSTEM-EXTERNAL-FORMAT")) (defpackage "STREAM" (:import-from "SYSTEM" "LISP-STREAM") ===================================== src/code/extensions.lisp ===================================== @@ -22,8 +22,7 @@ read-char-no-edit listen-skip-whitespace concat-pnames iterate once-only collect do-anonymous undefined-value required-argument define-hash-cache defun-cached - cache-hash-eq do-hash - with-temporary-file)) + cache-hash-eq do-hash)) (import 'lisp::whitespace-char-p) @@ -613,73 +612,3 @@ "Return an EQ hash of X. The value of this hash for any given object can (of course) change at arbitary times." `(lisp::pointer-hash ,x)) - -;;; WITH-TEMPORARY-FILE -- Public -(defmacro with-temporary-file ((var template-prefix - &key - (element-type 'base-char) - (external-format :default) - (buffering :full) - decoding-error - encoding-error) - &parse-body (forms decls)) - _N"A temporary file is opened using the Open-args and bound to the - variable Var. The name of the temporary file uses Template-prefix - for the name. If the temporary file cannot be opened, the forms are - not evaluated. The Forms are executed, and when they terminate, - normally or otherwise, the file is closed. - - Defined keywords: - :element-type - Type of object to read or write. Default BASE-CHAR - :external-format - An external format name - :buffering - Buffering to use for the file. Must be one of - :NONE, :LINE, :FULL - :decoding-error - How to handle decoding errors. See OPEN - :encoding-error - How to handle encoding errors. See OPEN" - (let ((abortp (gensym)) - (template (gensym "TEMPLATE-")) - (fd (gensym "FD-"))) - - `(let* ((,template (concatenate 'string - ,template-prefix - "XXXXXX")) - (,fd (unix::unix-mkstemp ,template))) - (unless ,fd - (error "Could not create temporary file using template ~A" - ,template)) - (let ((,var (lisp::make-fd-stream (unix::unix-mkstemp ,template) - :auto-close t - :file ,template - :output t - :input t - :element-type ',element-type - :external-format ,external-format - :decoding-error ,decoding-error - :encoding-error ,encoding-error - :buffering ,buffering)) - (,abortp t)) - ,@decls - (unwind-protect - (multiple-value-prog1 - (progn ,@forms) - (setq ,abortp nil)) - (when ,var - (close ,var :abort ,abortp))))))) - -;; WITH-TEMPORARY-DIRECTORY -- Public -(defmacro with-temporary-directory ((var template-prefix) - &parse-body (forms decls)) - _N"Create a temporary directory using Template-prefix as the name of the directory." - (let ((template (gensym "TEMPLATE-"))) - `(let ((,template (concatenate 'string ,template-prefix - "XXXXXX"))) - ,@decls - (let ((,var (unix::unix-mkdtemp ,template))) - (unless ,var - (error "Could not create temporary directory using template ~A" - ,template)) - (unwind-protect - (multiple-value-prog1 - (progn ,@forms))) - ;; Remove the directory - (unix:unix-rmdir ,var))))) ===================================== src/code/unix.lisp ===================================== @@ -2927,6 +2927,20 @@ (%file->name (cast buf c-call:c-string))) (cast buf (* c-call:unsigned-char)))))) +(defun unix-mkstemp (template) + _N"Generates a unique temporary file name from TEMPLATE, and creates + and opens the file. On success, the corresponding file descriptor + and name of the file is returned. + + The last six characters of the template must be \"XXXXXX\"." + ;; Hope this buffer is large enough! + (let ((octets (%name->file template))) + (syscall ("mkstemp" c-call:c-string) + (values result + ;; Convert the file name back to a Lisp string. + (%file->name octets)) + octets))) + (defun unix-mkdtemp (template) _N"Generate a uniquely named temporary directory from Template, which must have \"XXXXXX\" as the last six characters. The View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/3d4af597c0f721644199648... -- View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/3d4af597c0f721644199648... You're receiving this email because of your account on gitlab.common-lisp.net.
participants (1)
-
Raymond Toy (@rtoy)