
Raymond Toy pushed to branch issue-373-handle-temp-files at cmucl / cmucl Commits: 921b2043 by Raymond Toy at 2025-02-20T19:01:54-08:00 Add some comments for os_temp_path. - - - - - 221fa0ae by Raymond Toy at 2025-02-20T19:02:31-08:00 Fix error in creating default template name; clarify docstrings If the prefix arg is no supplied, we choose a default value for the template. It was previously incorrectly computed so make it right. Clarify some docstrings. - - - - - 6698b2c1 by Raymond Toy at 2025-02-20T19:47:44-08:00 Add function to create the template from the prefix or a default name. All of the temporary file/stream/directory macros had common code to create the actual template. Move this into a common function so that we do it the same for everyone. - - - - - 2 changed files: - src/code/extensions.lisp - src/lisp/os-common.c Changes: ===================================== src/code/extensions.lisp ===================================== @@ -632,6 +632,19 @@ (unless (alien:null-alien path) (alien:free-alien path))))) +;; Create a template suitable for mkstemp and mkdtemp. PREFIX is +;; string (or NIL) provided by the macros and is used as is as the +;; template prefix. If PREFIX is NIL, the prefix is obtained by +;; appending DEFAULT-NAME to the OS-dependent temporary path. In all +;; cases, we append exactly 6 X's to create the finale template. +(defun create-template (prefix default-name) + (concatenate 'string + (or prefix + (concatenate 'string + (get-os-temp-path) + default-name)) + "XXXXXX")) + ;;; WITH-TEMPORARY-STREAM -- Public ;;; (defmacro with-temporary-stream ((s &key @@ -641,7 +654,7 @@ decoding-error encoding-error) &parse-body (forms decls)) - "Return a stream to a temporary file that is automatically created." + _N"Return a stream to a temporary file that is automatically created." (let ((fd (gensym "FD-")) (filename (gensym "FILENAME-")) (dir (gensym "DIRECTION-")) @@ -652,10 +665,7 @@ (unless (member ,direction '(:output :io)) (error ":direction must be one of :output or :io, not ~S" ,direction)) - (let ((,file-template (concatenate 'string - (get-os-temp-path) - "cmucl-temp-stream-" - "XXXXXX")) + (let ((,file-template (create-template nil "cmucl-temp-stream-")) ,fd ,filename ,s) (unwind-protect (progn @@ -689,13 +699,13 @@ ;;; WITH-TEMPORARY-FILE -- Public (defmacro with-temporary-file ((filename &key prefix) &parse-body (forms decls)) + _N"Creates a temporary file with a name bound to Filename which a + namestring. If Prefix is not provided, the temporary file is created + in a OS-dependent location. Otherwise the prefix is used as a prefix + for the name. On completion, the file is automatically removed." (let ((fd (gensym "FD-")) (file-template (gensym "TEMP-PATH-"))) - `(let ((,file-template (concatenate 'string - (or ,prefix - (get-os-temp-path) - "cmucl-temp-file-") - "XXXXXX")) + `(let ((,file-template (create-template ,prefix "cmucl-temp-file-")) ,filename) (unwind-protect (let (,fd) @@ -715,17 +725,14 @@ ;;; WITH-TEMPORARY-DIRECTORY -- Public (defmacro with-temporary-directory ((dirname &key prefix) &parse-body (forms decls)) - "Return a pathname to a temporary directory. TEMPLATE is a string that - is used as a prefix for the name of the temporary directory. The - directory and all its contents are automatically removed afterward." + _N"Return a namestring to a temporary directory. If Prefix is not + provided, the directory is created in an OS-dependent location. + Otherwise, the Prefix is a string that is used as a prefix for the + name of the temporary directory. The directory and all its contents + are automatically removed afterward." (let ((err (gensym "ERR-")) - (dir-path (gensym "DIR-PATH")) (dir-template (gensym "DIR-TEMPLATE-"))) - `(let ((,dir-template (concatenate 'string - (or ,prefix - (get-os-temp-path) - "cmucl-temp-dir-") - "XXXXXX")) + `(let ((,dir-template (create-template ,prefix "cmucl-temp-dir-")) ,dirname ,err) (unwind-protect (progn ===================================== src/lisp/os-common.c ===================================== @@ -939,12 +939,22 @@ os_get_user_homedir(const char* name, int *status) return NULL; } +/* + * Return a new string containing the path to an OS-dependent location + * where temporary files/directories can be stored. If NULL is + * returned, such a location could not be found or some other error + * happened. + * + * Caller must call free(0 on the string returned. + */ char * os_temp_path() { #if defined(DARWIN) - // macosx has a secure per-user temporary directory. - // Don't cache the result as this is only called once. + /* + * macosx has a secure per-user temporary directory. + * Don't cache the result as this is only called once. + */ char path[PATH_MAX]; int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, path, PATH_MAX); @@ -954,7 +964,10 @@ os_temp_path() return strdup(path); #else - char *result; + /* + * If the TMP envvar is set, use that as the temporary directory. + * Otherwise, just assume "/tmp" will work. + */ char *tmp_path = getenv("TMP"); if (tmp_path == NULL) { View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/1a051ff335a2efdbbfe7b82... -- View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/1a051ff335a2efdbbfe7b82... You're receiving this email because of your account on gitlab.common-lisp.net.