>From 8563c4c7bf458477c4489c2e448e3a37f0e18098 Mon Sep 17 00:00:00 2001
From: James Vasile <james@hackervisions.org>
Date: Fri, 17 Dec 2010 23:36:58 -0500
Subject: [PATCH 2/2] Allow creation of temporary files without immediately unlinking them.

---
 src/osicat.lisp |   30 +++++++++++++++++-------------
 1 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/src/osicat.lisp b/src/osicat.lisp
index cd3d028..21e9eb7 100644
--- a/src/osicat.lisp
+++ b/src/osicat.lisp
@@ -265,13 +265,13 @@ PATHSPEC exists and is a symlink pointing to an existent file."
             (multiple-value-prog1 (locally ,@body) (setf ,errorp nil))
          (when ,errorp ,close-clause)))))
 
-(defun %open-temporary-file/fd-streams (filename element-type external-format)
+(defun %open-temporary-file/fd-streams (filename element-type external-format &key (no-unlink nil))
   (handler-case
       (multiple-value-bind (fd path)
           (nix:mkstemp filename)
         (%close-on-error
             (nix:close fd)
-          (nix:unlink path))
+          (unless no-unlink (nix:unlink path)))
         (make-fd-stream fd :direction :io
                         :element-type element-type
                         :external-format external-format
@@ -280,7 +280,7 @@ PATHSPEC exists and is a symlink pointing to an existent file."
     (nix:posix-error ()
       (error 'file-error :pathname filename))))
 
-(defun %open-temporary-file/no-fd-streams (filename element-type external-format)
+(defun %open-temporary-file/no-fd-streams (filename element-type external-format &key (no-unlink nil))
   (do ((counter 100 (1- counter)))
       ((zerop counter) (error 'file-error :pathname filename))
     (let* ((path (nix:mktemp filename))
@@ -291,19 +291,21 @@ PATHSPEC exists and is a symlink pointing to an existent file."
                          :if-does-not-exist :create)))
       (%close-on-error
           (close stream :abort t)
-        (nix:unlink path))
+        (unless no-unlink (nix:unlink path)))
       (return stream))))
 
 (defun open-temporary-file (&key (pathspec *temporary-directory*)
                             (element-type 'character)
-                            (external-format :default))
+                            (external-format :default)
+                            (no-unlink nil))
   "Creates a temporary file setup for input and output, and returns a
 stream connected to that file.
 
-PATHSPEC serves as template for the file to be created: a certain number
-of random characters will be concatenated to the file component of PATHSPEC.
-If PATHSPEC has no directory component, the file will be created inside
-*TEMPORARY-DIRECTORY*. The file itself is unlinked once it has been opened.
+PATHSPEC serves as template for the file to be created: a certain
+number of random characters will be concatenated to the file component
+of PATHSPEC.  If PATHSPEC has no directory component, the file will be
+created inside *TEMPORARY-DIRECTORY*. The file itself is unlinked once
+it has been opened unless NO-UNLINK is T.
 
 ELEMENT-TYPE specifies the unit of transaction of the stream.
 Consider using WITH-TEMPORARY-FILE instead of this function.
@@ -313,13 +315,14 @@ On failure, a FILE-ERROR may be signalled."
          (native-namestring
           (merge-pathnames (pathname pathspec) *temporary-directory*))))
     #+osicat-fd-streams
-    (%open-temporary-file/fd-streams filename element-type external-format)
+    (%open-temporary-file/fd-streams filename element-type external-format :no-unlink no-unlink)
     #-osicat-fd-streams
-    (%open-temporary-file/no-fd-streams filename element-type external-format)))
+    (%open-temporary-file/no-fd-streams filename element-type external-format :no-unlink no-unlink)))
 
 (defmacro with-temporary-file ((stream &key (pathspec *temporary-directory*)
                                        (element-type 'character)
-                                       (external-format :default))
+                                       (external-format :default)
+                                       (no-unlink nil))
                                &body body)
   "Within the lexical scope of the body, STREAM is connected to a
 temporary file as created by OPEN-TEMPORARY-FILE.  The file is
@@ -329,7 +332,8 @@ closed automatically once BODY exits."
                                      :element-type ,(if (eq element-type 'character)
                                                         (quote 'character)
                                                         element-type)
-                                     :external-format ,external-format))
+                                     :external-format ,external-format
+                                     :no-unlink ,no-unlink))
      ,@body))
 
 ;;;; Directory access
-- 
1.7.2.3

