apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mladen Turk" <mt...@mappingsoft.com>
Subject [PROPOSAL] apr_file_temp()
Date Mon, 17 Sep 2001 20:52:44 GMT
Hi,
Here is the patch that enables apr to deal with temp files.


===================================================================
RCS file: /home/cvspublic/apr/include/apr_file_io.h,v
retrieving revision 1.109
diff -u -r1.109 apr_file_io.h
--- apr_file_io.h   2001/08/12 16:10:43 1.109
+++ apr_file_io.h   2001/09/17 11:38:34
@@ -179,6 +179,18 @@
                                    apr_pool_t *cont);
 
 /**
+ * Creates a unique temporary file.
+ * @param new_file The opened file descriptor.
+ * @param templ Template for the temp file /tmp/aprXXXXXX or NULL
+ * @remark If Template is NULL then the default template is used in the
form
+ *         $TMPDIR/aprXXXXXX. If template is not NULL then the last six
characters
+ *         of templ must be XXXXXX or the function will fail.
+ */
+
+APR_DECLARE(apr_status_t) apr_file_temp(apr_file_t **new_file, const
char *templ,
+                                   apr_pool_t *cont);
+
+/**
  * Close the specified file.
  * @param file The file descriptor to close.
  */

===================================================================
RCS file: /home/cvspublic/apr/file_io/unix/open.c,v
retrieving revision 1.84
diff -u -r1.84 open.c
--- open.c  2001/08/10 21:04:47 1.84
+++ open.c  2001/09/17 11:43:00
@@ -172,6 +172,59 @@
     return APR_SUCCESS;
 }
 
+APR_DECLARE(apr_status_t) apr_file_temp(apr_file_t **new, const char
*templ, apr_pool_t *cont)
+{
+   char fname[APR_PATH_MAX];
+
+    (*new) = (apr_file_t *)apr_pcalloc(cont, sizeof(apr_file_t));
+    (*new)->cntxt = cont;
+    (*new)->flags = APR_READ | APR_WRITE | APR_CREATE | APR_DELONCLOSE;
+    (*new)->filedes = -1;
+   
+   if (templ == NULL) {
+       char *tmp;
+       if ((tmp = getenv("TMPDIR")) != NULL)
+           strcpy(fname, tmp);
+       else
+           strcpy(fname, P_tmpdir);
+       strcat(fname, "/aprXXXXXX");                
+   }
+   else {
+       int pos;
+       pos = strlen (templ);
+       if (pos < 6 || strcmp (&templ[pos - 6], "XXXXXX"))
+           return APR_EINVAL;
+       strcpy(fname, templ);               
+   }
+   
+    (*new)->filedes = apr_mkstemp(fname);
+    
+    if ((*new)->filedes < 0) {
+       (*new)->filedes = -1;
+       (*new)->eof_hit = 1;
+       return errno;
+    }
+
+    (*new)->fname = apr_pstrdup(cont, fname);
+    (*new)->blocking = BLK_ON;
+    (*new)->buffer = NULL;
+   /* delete the file on close */
+    unlink(fname);
+
+    (*new)->pipe = 0;
+    (*new)->timeout = -1;
+    (*new)->ungetchar = -1;
+    (*new)->eof_hit = 0;
+    (*new)->filePtr = 0;
+    (*new)->bufpos = 0;
+    (*new)->dataRead = 0;
+    (*new)->direction = 0;
+    apr_pool_cleanup_register((*new)->cntxt, (void *)(*new), 
+                              apr_unix_file_cleanup,
apr_unix_file_cleanup);
+    return APR_SUCCESS;
+}
+
+
 APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
 {
     apr_status_t rv;

===================================================================
RCS file: /home/cvspublic/apr/file_io/win32/open.c,v
retrieving revision 1.82
diff -u -r1.82 open.c
--- open.c  2001/09/05 04:50:30 1.82
+++ open.c  2001/09/17 11:43:45
@@ -309,6 +309,59 @@
     return APR_SUCCESS;
 }
 
+APR_DECLARE(apr_status_t) apr_file_temp(apr_file_t **new, const char
*templ, apr_pool_t *cont)
+{
+   static const char letters[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+   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 (templ) {
+       pos = strlen (templ);
+       if (pos < 6 || strcmp (&templ[pos - 6], "XXXXXX"))
+           return APR_EINVAL;
+       else
+           pos -= 6;
+       strcpy(fname, templ);
+   }
+   else {
+       if ((tmp = getenv("TMP")) != NULL)
+           strcpy(fname, tmp);
+       else if ((tmp = getenv("TEMP")) != NULL)
+           strcpy(fname, tmp);
+       else
+           strcpy(fname, "C:");
+       strcat(fname, "\\apr");
+
+       pos = strlen(fname);
+   }
+
+   apr_generate_random_bytes((unsigned char*)&value, sizeof(unsigned
int));
+   for (count = 0; count < 100; value += 1048583, ++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_DELONCLOSE,
+                                APR_OS_DEFAULT, cont);
+
+   }
+
+   return APR_EBADF;
+}
+
+
 APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
 {
     apr_status_t stat;




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




Mime
View raw message