Raymond Toy pushed to branch issue-364-add-mkstemp-mkdtemp at cmucl / cmucl

Commits:

3 changed files:

Changes:

  • src/code/exports.lisp
    ... ... @@ -1717,11 +1717,7 @@
    1717 1717
     	   "DESCRIBE-EXTERNAL-FORMAT"
    
    1718 1718
     	   "LIST-ALL-EXTERNAL-FORMATS"
    
    1719 1719
     	   "STRING-ENCODE" "STRING-DECODE"
    
    1720
    -	   "SET-SYSTEM-EXTERNAL-FORMAT")
    
    1721
    -  ;; Temporary files/directories
    
    1722
    -  (:export
    
    1723
    -   "WITH-TEMPORARY-FILE"
    
    1724
    -   "WITH-TEMPORARY-DIRECTORY"))
    
    1720
    +	   "SET-SYSTEM-EXTERNAL-FORMAT"))
    
    1725 1721
     
    
    1726 1722
     (defpackage "STREAM"
    
    1727 1723
       (:import-from "SYSTEM" "LISP-STREAM")
    

  • src/code/extensions.lisp
    ... ... @@ -22,8 +22,7 @@
    22 22
     		read-char-no-edit listen-skip-whitespace concat-pnames
    
    23 23
     		iterate once-only collect do-anonymous undefined-value
    
    24 24
     		required-argument define-hash-cache defun-cached
    
    25
    -		cache-hash-eq do-hash
    
    26
    -	  with-temporary-file))
    
    25
    +		cache-hash-eq do-hash))
    
    27 26
     
    
    28 27
     (import 'lisp::whitespace-char-p)
    
    29 28
     
    
    ... ... @@ -613,73 +612,3 @@
    613 612
       "Return an EQ hash of X.  The value of this hash for any given object can (of
    
    614 613
       course) change at arbitary times."
    
    615 614
       `(lisp::pointer-hash ,x))
    616
    -
    
    617
    -;;; WITH-TEMPORARY-FILE -- Public
    
    618
    -(defmacro with-temporary-file ((var template-prefix
    
    619
    -				&key 
    
    620
    -				  (element-type 'base-char)
    
    621
    -				  (external-format :default)
    
    622
    -				  (buffering :full)
    
    623
    -				  decoding-error
    
    624
    -				  encoding-error)
    
    625
    -			       &parse-body (forms decls))
    
    626
    -  _N"A temporary file is opened using the Open-args and bound to the
    
    627
    - variable Var.  The name of the temporary file uses Template-prefix
    
    628
    - for the name.  If the temporary file cannot be opened, the forms are
    
    629
    - not evaluated.  The Forms are executed, and when they terminate,
    
    630
    - normally or otherwise, the file is closed.
    
    631
    -
    
    632
    - Defined keywords:
    
    633
    -  :element-type    - Type of object to read or write.  Default BASE-CHAR
    
    634
    -  :external-format - An external format name
    
    635
    -  :buffering       - Buffering to use for the file.  Must be one of
    
    636
    -                      :NONE, :LINE, :FULL
    
    637
    -  :decoding-error  - How to handle decoding errors.  See OPEN
    
    638
    -  :encoding-error  - How to handle encoding errors.  See OPEN"
    
    639
    -  (let ((abortp (gensym))
    
    640
    -	(template (gensym "TEMPLATE-"))
    
    641
    -	(fd (gensym "FD-")))
    
    642
    -    
    
    643
    -    `(let* ((,template (concatenate 'string
    
    644
    -				    ,template-prefix
    
    645
    -				    "XXXXXX"))
    
    646
    -	    (,fd (unix::unix-mkstemp ,template)))
    
    647
    -       (unless ,fd
    
    648
    -	 (error "Could not create temporary file using template ~A"
    
    649
    -		,template))
    
    650
    -       (let ((,var (lisp::make-fd-stream (unix::unix-mkstemp ,template)
    
    651
    -					 :auto-close t
    
    652
    -					 :file ,template
    
    653
    -					 :output t
    
    654
    -					 :input t
    
    655
    -					 :element-type ',element-type
    
    656
    -					 :external-format ,external-format
    
    657
    -					 :decoding-error ,decoding-error
    
    658
    -					 :encoding-error ,encoding-error
    
    659
    -					 :buffering ,buffering))
    
    660
    -	     (,abortp t))
    
    661
    -	 ,@decls
    
    662
    -	 (unwind-protect
    
    663
    -	      (multiple-value-prog1
    
    664
    -		  (progn ,@forms)
    
    665
    -		(setq ,abortp nil))
    
    666
    -	   (when ,var
    
    667
    -	     (close ,var :abort ,abortp)))))))
    
    668
    -
    
    669
    -;; WITH-TEMPORARY-DIRECTORY -- Public
    
    670
    -(defmacro with-temporary-directory ((var template-prefix)
    
    671
    -				    &parse-body (forms decls))
    
    672
    -  _N"Create a temporary directory using Template-prefix as the name of the directory."
    
    673
    -  (let ((template (gensym "TEMPLATE-")))
    
    674
    -    `(let ((,template (concatenate 'string ,template-prefix
    
    675
    -				   "XXXXXX")))
    
    676
    -       ,@decls
    
    677
    -       (let ((,var (unix::unix-mkdtemp ,template)))
    
    678
    -	 (unless ,var
    
    679
    -	   (error "Could not create temporary directory using template ~A"
    
    680
    -		  ,template))
    
    681
    -	 (unwind-protect
    
    682
    -	      (multiple-value-prog1
    
    683
    -		  (progn ,@forms)))
    
    684
    -	 ;; Remove the directory
    
    685
    -	 (unix:unix-rmdir ,var)))))

  • src/code/unix.lisp
    ... ... @@ -2927,6 +2927,20 @@
    2927 2927
     		       (%file->name (cast buf c-call:c-string)))
    
    2928 2928
     	       (cast buf (* c-call:unsigned-char))))))
    
    2929 2929
     
    
    2930
    +(defun unix-mkstemp (template)
    
    2931
    +  _N"Generates a unique temporary file name from TEMPLATE, and creates
    
    2932
    +  and opens the file.  On success, the corresponding file descriptor
    
    2933
    +  and name of the file is returned.
    
    2934
    +
    
    2935
    + The last six characters of the template must be \"XXXXXX\"."
    
    2936
    +  ;; Hope this buffer is large enough!
    
    2937
    +  (let ((octets (%name->file template)))
    
    2938
    +    (syscall ("mkstemp" c-call:c-string)
    
    2939
    +	       (values result
    
    2940
    +		       ;; Convert the file name back to a Lisp string.
    
    2941
    +		       (%file->name octets))
    
    2942
    +	       octets)))
    
    2943
    +
    
    2930 2944
     (defun unix-mkdtemp (template)
    
    2931 2945
       _N"Generate a uniquely named temporary directory from Template,
    
    2932 2946
       which must have \"XXXXXX\" as the last six characters.  The