Raymond Toy pushed to branch issue-373-handle-temp-files at cmucl / cmucl

Commits:

2 changed files:

Changes:

  • src/code/extensions.lisp
    ... ... @@ -632,6 +632,19 @@
    632 632
           (unless (alien:null-alien path)
    
    633 633
     	(alien:free-alien path)))))
    
    634 634
     
    
    635
    +;; Create a template suitable for mkstemp and mkdtemp.  PREFIX is
    
    636
    +;; string (or NIL) provided by the macros and is used as is as the
    
    637
    +;; template prefix.  If PREFIX is NIL, the prefix is obtained by
    
    638
    +;; appending DEFAULT-NAME to the OS-dependent temporary path.  In all
    
    639
    +;; cases, we append exactly 6 X's to create the finale template.
    
    640
    +(defun create-template (prefix default-name)
    
    641
    +  (concatenate 'string
    
    642
    +	       (or prefix
    
    643
    +		   (concatenate 'string
    
    644
    +				(get-os-temp-path)
    
    645
    +				default-name))
    
    646
    +	       "XXXXXX"))
    
    647
    +
    
    635 648
     ;;; WITH-TEMPORARY-STREAM  -- Public
    
    636 649
     ;;;
    
    637 650
     (defmacro with-temporary-stream ((s &key
    
    ... ... @@ -641,7 +654,7 @@
    641 654
     				      decoding-error
    
    642 655
     				      encoding-error)
    
    643 656
     				 &parse-body (forms decls))
    
    644
    -  "Return a stream to a temporary file that is automatically created."
    
    657
    +  _N"Return a stream to a temporary file that is automatically created."
    
    645 658
       (let ((fd (gensym "FD-"))
    
    646 659
     	(filename (gensym "FILENAME-"))
    
    647 660
     	(dir (gensym "DIRECTION-"))
    
    ... ... @@ -652,10 +665,7 @@
    652 665
            (unless (member ,direction '(:output :io))
    
    653 666
     	 (error ":direction must be one of :output or :io, not ~S"
    
    654 667
     		,direction))
    
    655
    -       (let ((,file-template (concatenate 'string
    
    656
    -					  (get-os-temp-path)
    
    657
    -					  "cmucl-temp-stream-"
    
    658
    -					  "XXXXXX"))
    
    668
    +       (let ((,file-template (create-template nil "cmucl-temp-stream-"))
    
    659 669
     	     ,fd ,filename ,s)
    
    660 670
     	 (unwind-protect
    
    661 671
     	      (progn
    
    ... ... @@ -689,13 +699,13 @@
    689 699
     ;;; WITH-TEMPORARY-FILE  -- Public
    
    690 700
     (defmacro with-temporary-file ((filename &key prefix)
    
    691 701
     			       &parse-body (forms decls))
    
    702
    +  _N"Creates a temporary file with a name bound to Filename which a
    
    703
    + namestring.  If Prefix is not provided, the temporary file is created
    
    704
    + in a OS-dependent location.  Otherwise the prefix is used as a prefix
    
    705
    + for the name.  On completion, the file is automatically removed."
    
    692 706
       (let ((fd (gensym "FD-"))
    
    693 707
     	(file-template (gensym "TEMP-PATH-")))
    
    694
    -    `(let ((,file-template (concatenate 'string
    
    695
    -					(or ,prefix
    
    696
    -					    (get-os-temp-path)
    
    697
    -					    "cmucl-temp-file-")
    
    698
    -					"XXXXXX"))
    
    708
    +    `(let ((,file-template (create-template ,prefix "cmucl-temp-file-"))
    
    699 709
     	   ,filename)
    
    700 710
            (unwind-protect
    
    701 711
     	    (let (,fd)
    
    ... ... @@ -715,17 +725,14 @@
    715 725
     ;;; WITH-TEMPORARY-DIRECTORY  -- Public
    
    716 726
     (defmacro with-temporary-directory ((dirname &key prefix)
    
    717 727
     				    &parse-body (forms decls))
    
    718
    -  "Return a pathname to a temporary directory.  TEMPLATE is a string that
    
    719
    -  is used as a prefix for the name of the temporary directory.  The
    
    720
    -  directory and all its contents are automatically removed afterward."
    
    728
    + _N"Return a namestring to a temporary directory.  If Prefix is not
    
    729
    + provided, the directory is created in an OS-dependent location.
    
    730
    + Otherwise, the Prefix is a string that is used as a prefix for the
    
    731
    + name of the temporary directory.  The directory and all its contents
    
    732
    + are automatically removed afterward."
    
    721 733
       (let ((err (gensym "ERR-"))
    
    722
    -	(dir-path (gensym "DIR-PATH"))
    
    723 734
     	(dir-template (gensym "DIR-TEMPLATE-")))
    
    724
    -    `(let ((,dir-template (concatenate 'string
    
    725
    -				       (or ,prefix
    
    726
    -					   (get-os-temp-path)
    
    727
    -					   "cmucl-temp-dir-")
    
    728
    -				       "XXXXXX"))
    
    735
    +    `(let ((,dir-template (create-template ,prefix "cmucl-temp-dir-"))
    
    729 736
     	   ,dirname ,err)
    
    730 737
            (unwind-protect
    
    731 738
     	    (progn
    

  • src/lisp/os-common.c
    ... ... @@ -939,12 +939,22 @@ os_get_user_homedir(const char* name, int *status)
    939 939
         return NULL;
    
    940 940
     }
    
    941 941
         
    
    942
    +/*
    
    943
    + * Return a new string containing the path to an OS-dependent location
    
    944
    + * where temporary files/directories can be stored.  If NULL is
    
    945
    + * returned, such a location could not be found or some other error
    
    946
    + * happened.
    
    947
    + *
    
    948
    + * Caller must call free(0 on the string returned.
    
    949
    + */
    
    942 950
     char *
    
    943 951
     os_temp_path()
    
    944 952
     {
    
    945 953
     #if defined(DARWIN)
    
    946
    -    // macosx has a secure per-user temporary directory.
    
    947
    -    // Don't cache the result as this is only called once.
    
    954
    +    /*
    
    955
    +     * macosx has a secure per-user temporary directory.
    
    956
    +     * Don't cache the result as this is only called once.
    
    957
    +     */
    
    948 958
         char path[PATH_MAX];
    
    949 959
     
    
    950 960
         int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, path, PATH_MAX);
    
    ... ... @@ -954,7 +964,10 @@ os_temp_path()
    954 964
         
    
    955 965
         return strdup(path);
    
    956 966
     #else
    
    957
    -    char *result;
    
    967
    +    /*
    
    968
    +     * If the TMP envvar is set, use that as the temporary directory.
    
    969
    +     * Otherwise, just assume "/tmp" will work.
    
    970
    +     */
    
    958 971
         char *tmp_path = getenv("TMP");
    
    959 972
     
    
    960 973
         if (tmp_path == NULL) {