Raymond Toy pushed to branch issue-364-add-mkstemp-mkdtemp at cmucl / cmucl
Commits: 1beae8cb by Raymond Toy at 2025-01-03T12:00:26-08:00 unix-mkstemp returns the file descriptor and the file name
Previously, we only returned the file descriptor. Now we return the descriptor and the file name associated with the file. This is available to us because `mkstemp` modifies the template with the actual file name.
Also, we use %name->file to encode the Lisp string into a set of octets to pass to `mkstemp`. This means we need to convert updated template to a Lisp string.
Tested this using the template "/tmp/α-XXXXXX" (alpha character followed by "-XXXXXX"). The resulting file name was "/tmp/α-L2egJE" whith the X's appropriately replaced.
- - - - -
1 changed file:
- src/code/unix.lisp
Changes:
===================================== src/code/unix.lisp ===================================== @@ -2902,13 +2902,30 @@ c-string))
(defun unix-mkstemp (template) - _N"Generates a unique temporary file name from TEMPLATE, creates and - opens the file and returns a file stream for the file. + _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"." - (syscall ("mkstemp" c-call:c-string) - result - (copy-seq template))) + ;; Hope this buffer is large enough! + (let ((octets (%name->file template))) + (unless (< (length octets) 8192) + (error "Internal buffer is too small for encoded file name of ~D octets" + (length octets))) + (with-alien ((buf (array c-call:unsigned-char 8192))) + ;; Convert the Lisp string and copy it to the alien buffer, being + ;; sure to zero-terminate the buffer. + (loop for k from 0 + for c across octets + do + (setf (deref buf k) (char-code c)) + finally (setf (deref buf k) 0)) + + (syscall ("mkstemp" (* c-call:unsigned-char)) + (values result + ;; Convert the file name back to a Lisp string. + (%file->name (cast buf c-call:c-string))) + (cast buf (* c-call:unsigned-char))))))
(defun unix-mkdtemp (template) _N"Generate a uniquely named temporary directory from Template,
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/1beae8cbd0a126b59d7fab9d...