apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mladen Turk" <mt...@mappingsoft.com>
Subject [ADDON] apr_file_open_temp
Date Fri, 14 Sep 2001 18:04:33 GMT
Hi,

Here is some add to open.c to enable the temporary file creation.
Don't know if this make sense in general, but its useful for me.
I've make a patch for WIN32 only, but it should be straightforward for
other platforms too.


--- include/apr_file_io.h.old	Fri Sep 14 10:59:22 2001
+++ include/apr_file_io.h	Fri Sep 14 10:59:09 2001
@@ -101,6 +101,7 @@
 #define APR_SHARELOCK  1024        /**< Platform dependent support for
higher
                                         level locked read/write access
to support
                                         writes across process/machines
*/
+#define APR_TEMP       2048        /**< Open temporary file */
  
 /** @} */
 
@@ -236,6 +237,15 @@
  * @ingroup apr_file_open
  */
 APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile,
+                                              apr_pool_t *cont);
+
+/**
+ * creates new temp file for R/W access. on close delete the file.
+ * @param new_file The opened file descriptor.
+ * @param cont The pool to allocate the file out of.
+ * @ingroup apr_file_open
+ */
+APR_DECLARE(apr_status_t) apr_file_open_temp(apr_file_t **new_file,
                                               apr_pool_t *cont);
 
 /**


--- file_io/win32/open.c.old	Fri Sep 14 10:59:31 2001
+++ file_io/win32/open.c	Fri Sep 14 10:58:22 2001
@@ -317,8 +317,10 @@
 
         if (file->buffered)
             apr_lock_destroy(file->mutex);
-
-        return APR_SUCCESS;
+        if (file->flags & APR_TEMP)
+            return apr_file_remove(file->fname, file->cntxt);
+        else
+            return APR_SUCCESS;
     }
     return stat;
 }
@@ -452,6 +454,56 @@
         return apr_get_os_error();
 
     return apr_os_file_put(thefile, &file_handle, cont);
+}
+
+
+APR_DECLARE(apr_status_t) apr_file_open_temp(apr_file_t **new,
apr_pool_t *cont)
+{
+    static const char letters[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+    static const int nletters = sizeof (letters) - 1;   
+    int count, pos;
+    unsigned int value;
+    char fname[APR_PATH_MAX];
+    char *tmp;
+    apr_finfo_t sbuf;
+    apr_status_t check;
+
+    if ((tmp = getenv("TMPDIR")) != NULL)
+        strcpy(fname, tmp);
+    else if ((tmp = getenv("TMP")) != NULL)
+        strcpy(fname, tmp);
+    else if ((tmp = getenv("TEMP")) != NULL)
+        strcpy(fname, tmp);
+#ifdef WIN32
+    else
+        strcpy(fname, "C:");
+    strcat(fname, "\\");
+#else
+    else
+        strcpy(fname, "/tmp");
+    strcat(fname, "/");
+#endif
+    pos = strlen(fname);
+    apr_generate_random_bytes((unsigned char*)&value, sizeof(unsigned
int));
+    strcpy(fname + pos + 6, ".tmp");
+    for (count = 0; count < 100; value += 7777, ++count)
+    {
+        unsigned int v = value;
+        int i;
+        for (i = 0; i < 6; i++)
+        {
+            fname[pos+i] = letters[v % nletters]; 
+            v /= nletters;
+        }
+        check = apr_stat(&sbuf, fname, APR_FINFO_TYPE, cont);
+        if (check || sbuf.filetype != APR_REG)
+            return apr_file_open(new, fname,
+                                 APR_READ | APR_WRITE | APR_CREATE |
APR_EXCL | APR_TEMP,
+                                 0600, cont);
+
+    }
+
+    return APR_EBADF;
 }
 
 APR_POOL_IMPLEMENT_ACCESSOR_X(file, cntxt);


Mladen Turk
MCSE/GIS Specialist
Sisiceva 10,
10000 Zagreb, Croatia
mailto:[ mturk@mappingsoft.com ]
http://apache.mappingsoft.com




Mime
View raw message