apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Reid <dr...@jetnet.co.uk>
Subject [PATCH] apr_file_dup -> apr_file_dup/apr_file_dup2
Date Tue, 08 Jan 2002 19:30:30 GMT
This patch basically adds apr_file_dup2 to APR and changes the logic such
that unless we call apr_file_dup2 we only ever call dup, not dup2.

This should prevent some potential errors and also follow the
posix standards closer.

It doesn't make any attempt to protect any fd's, which is how I
think it should work, and so basically backs out wrowe's change from
last night.

Will has seen it and is going to look at the win32/os2 code.

david

? file_io/unix/dummy
? file_io/unix/ranlib
? include/diffs
Index: file_io/unix/filedup.c
===================================================================
RCS file: /home/cvs/apr/file_io/unix/filedup.c,v
retrieving revision 1.36
diff -u -r1.36 filedup.c
--- file_io/unix/filedup.c	6 Dec 2001 13:43:45 -0000	1.36
+++ file_io/unix/filedup.c	8 Jan 2002 17:39:08 -0000
@@ -58,21 +58,23 @@
 #include "apr_thread_mutex.h"
 #include "inherit.h"
 
-apr_status_t apr_file_dup(apr_file_t **new_file, apr_file_t *old_file, apr_pool_t *p)
+static apr_status_t _file_dup(apr_file_t **new_file, 
+                              apr_file_t *old_file, apr_pool_t *p,
+                              int which_dup)
 {
-    int have_file = 0;
-
-    if ((*new_file) == NULL) {
+    if ((*new_file) == NULL && which_dup == 1) {
         (*new_file) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
         if ((*new_file) == NULL) {
             return APR_ENOMEM;
         }
+        (*new_file)->cntxt = p;
     } else {
-        have_file = 1;
+        /* We can't dup2 unless we have a valid new_file */
+        return APR_EINVAL;
     }
-    
-    (*new_file)->cntxt = p; 
-    if (have_file) {
+
+
+    if (which_dup == 2) {
         dup2(old_file->filedes, (*new_file)->filedes);
     }
     else {
@@ -95,8 +97,31 @@
      * apr_file_set_inherit() again on the dupped handle, as necessary.
      */
     (*new_file)->flags = old_file->flags & ~APR_INHERIT;
-    apr_pool_cleanup_register((*new_file)->cntxt, (void *)(*new_file), 
-                              apr_unix_file_cleanup, apr_unix_file_cleanup);
+
     return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_file_dup(apr_file_t **new_file,
+                                       apr_file_t *old_file, apr_pool_t *p)
+{
+    apr_status_t rv;
+
+    rv = _file_dup(new_file, old_file, p, 1);
+    if (rv != APR_SUCCESS)
+        return rv;
+
+    /* we do this here as we don't want to double register an existing 
+     * apr_file_t for cleanup
+     */
+    apr_pool_cleanup_register((*new_file)->cntxt, (void *)(*new_file),
+                              apr_unix_file_cleanup, apr_unix_file_cleanup);
+    return rv;
+
+}
+
+APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t **new_file,
+                                        apr_file_t *old_file, apr_pool_t *p)
+{
+    return _file_dup(new_file, old_file, p, 2);
 }
 
Index: include/apr_file_io.h
===================================================================
RCS file: /home/cvs/apr/include/apr_file_io.h,v
retrieving revision 1.111
diff -u -r1.111 apr_file_io.h
--- include/apr_file_io.h	27 Sep 2001 16:44:13 -0000	1.111
+++ include/apr_file_io.h	8 Jan 2002 17:39:09 -0000
@@ -391,6 +391,18 @@
                                       apr_pool_t *p);
 
 /**
+ * duplicate the specified file descriptor and close the original
+ * @param new_file The structure to duplicate into
+ * @param old_file The file to duplicate
+ * @param p        The pool to use for the new file
+ *
+ * @remark *arg1 MUST point at a valid apr_file_t. It cannot point at NULL
+ */
+APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t **new_file,
+                                        apr_file_t *old_file,
+                                        apr_pool_t *p);
+
+/**
  * Move the read/write file offset to a specified byte within a file.
  * @param thefile The file descriptor
  * @param where How to move the pointer, one of:


Mime
View raw message