wez             Tue Mar 18 18:37:54 2003 EDT

  Modified files:              
    /php4/ext/standard  php_fopen_wrapper.c 
    /php4/main  php_streams.h 
    /php4/main/streams  php_stream_plain_wrapper.h plain_wrapper.c 
  Log:
  Implement persistent plain file streams.
  
  Usage:
  
  php_stream *stream = php_stream_fopen("/path/to/file", "r+b", NULL,
        STREAM_OPEN_PERSISTENT | ENFORCE_SAFE_MODE | REPORT_ERRORS);
  
  the filename and mode are combined to form the hash key for the persistent
  list; they must be identical for this same stream to be returned again in the
  next request.
  
  Calling php_stream_close() on a persistent stream *will* close it, as is
  usual with all persistent resources in PHP/ZE.
  
  This is deliberately *not* exposed to user-space PHP at this time.
  
  
Index: php4/ext/standard/php_fopen_wrapper.c
diff -u php4/ext/standard/php_fopen_wrapper.c:1.36 
php4/ext/standard/php_fopen_wrapper.c:1.37
--- php4/ext/standard/php_fopen_wrapper.c:1.36  Tue Mar 18 11:40:28 2003
+++ php4/ext/standard/php_fopen_wrapper.c       Tue Mar 18 18:37:53 2003
@@ -17,7 +17,7 @@
    |          Hartmut Holzgraefe <[EMAIL PROTECTED]>                       |
    +----------------------------------------------------------------------+
  */
-/* $Id: php_fopen_wrapper.c,v 1.36 2003/03/18 16:40:28 wez Exp $ */
+/* $Id: php_fopen_wrapper.c,v 1.37 2003/03/18 23:37:53 wez Exp $ */
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -218,7 +218,7 @@
                return NULL;
        }
        
-       stream = php_stream_fopen_from_fd(fd, mode);
+       stream = php_stream_fopen_from_fd(fd, mode, NULL);
        if (stream == NULL) {
                close(fd);
        }
Index: php4/main/php_streams.h
diff -u php4/main/php_streams.h:1.76 php4/main/php_streams.h:1.77
--- php4/main/php_streams.h:1.76        Thu Feb 27 14:10:23 2003
+++ php4/main/php_streams.h     Tue Mar 18 18:37:54 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_streams.h,v 1.76 2003/02/27 19:10:23 wez Exp $ */
+/* $Id: php_streams.h,v 1.77 2003/03/18 23:37:54 wez Exp $ */
 
 #ifndef PHP_STREAMS_H
 #define PHP_STREAMS_H
@@ -64,7 +64,7 @@
 
 #define php_stream_fopen_with_path_rel(filename, mode, path, opened, options) 
_php_stream_fopen_with_path((filename), (mode), (path), (opened), (options) 
STREAMS_REL_CC TSRMLS_CC)
 
-#define php_stream_fopen_from_fd_rel(fd, mode)  _php_stream_fopen_from_fd((fd), 
(mode) STREAMS_REL_CC TSRMLS_CC)
+#define php_stream_fopen_from_fd_rel(fd, mode, persistent_id)   
_php_stream_fopen_from_fd((fd), (mode), (persistent_id) STREAMS_REL_CC TSRMLS_CC)
 #define php_stream_fopen_from_file_rel(file, mode)      
_php_stream_fopen_from_file((file), (mode) STREAMS_REL_CC TSRMLS_CC)
        
 #define php_stream_fopen_from_pipe_rel(file, mode)      
_php_stream_fopen_from_pipe((file), (mode) STREAMS_REL_CC TSRMLS_CC)
@@ -436,6 +436,9 @@
 
 /* don't apply open_basedir checks */
 #define STREAM_DISABLE_OPEN_BASEDIR    1024
+
+/* get (or create) a persistent version of the stream */
+#define STREAM_OPEN_PERSISTENT 2048
 
 /* Antique - no longer has meaning */
 #define IGNORE_URL_WIN 0
Index: php4/main/streams/php_stream_plain_wrapper.h
diff -u php4/main/streams/php_stream_plain_wrapper.h:1.2 
php4/main/streams/php_stream_plain_wrapper.h:1.3
--- php4/main/streams/php_stream_plain_wrapper.h:1.2    Wed Feb 19 03:40:19 2003
+++ php4/main/streams/php_stream_plain_wrapper.h        Tue Mar 18 18:37:54 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_stream_plain_wrapper.h,v 1.2 2003/02/19 08:40:19 sniper Exp $ */
+/* $Id: php_stream_plain_wrapper.h,v 1.3 2003/03/18 23:37:54 wez Exp $ */
 
 /* definitions for the plain files wrapper */
 
@@ -33,8 +33,8 @@
 PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode 
STREAMS_DC TSRMLS_DC);
 #define php_stream_fopen_from_file(file, mode) _php_stream_fopen_from_file((file), 
(mode) STREAMS_CC TSRMLS_CC)
 
-PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode STREAMS_DC 
TSRMLS_DC);
-#define php_stream_fopen_from_fd(fd, mode)     _php_stream_fopen_from_fd((fd), (mode) 
STREAMS_CC TSRMLS_CC)
+PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char 
*persistent_id STREAMS_DC TSRMLS_DC);
+#define php_stream_fopen_from_fd(fd, mode, persistent_id)      
_php_stream_fopen_from_fd((fd), (mode), (persistent_id) STREAMS_CC TSRMLS_CC)
 
 PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode 
STREAMS_DC TSRMLS_DC);
 #define php_stream_fopen_from_pipe(file, mode) _php_stream_fopen_from_pipe((file), 
(mode) STREAMS_CC TSRMLS_CC)
Index: php4/main/streams/plain_wrapper.c
diff -u php4/main/streams/plain_wrapper.c:1.11 php4/main/streams/plain_wrapper.c:1.12
--- php4/main/streams/plain_wrapper.c:1.11      Tue Mar 18 11:40:29 2003
+++ php4/main/streams/plain_wrapper.c   Tue Mar 18 18:37:54 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: plain_wrapper.c,v 1.11 2003/03/18 16:40:29 wez Exp $ */
+/* $Id: plain_wrapper.c,v 1.12 2003/03/18 23:37:54 wez Exp $ */
 
 #include "php.h"
 #include "php_globals.h"
@@ -88,6 +88,8 @@
        int open_flags;
        int fd;
        php_stream *ret;
+       int persistent = options & STREAM_OPEN_PERSISTENT;
+       char *persistent_id = NULL;
 
        if (FAILURE == php_stream_parse_fopen_modes(mode, &open_flags)) {
                if (options & REPORT_ERRORS) {
@@ -98,6 +100,25 @@
        
        realpath = expand_filepath(filename, NULL TSRMLS_CC);
 
+       if (persistent) {
+               spprintf(&persistent_id, 0, "streams_stdio_%d_%s", open_flags, 
realpath);
+               switch (php_stream_from_persistent_id(persistent_id, &ret TSRMLS_CC)) {
+                       case PHP_STREAM_PERSISTENT_SUCCESS:
+                               if (opened_path) {
+                                       *opened_path = realpath;
+                                       realpath = NULL;
+                               }
+                               if (realpath) {
+                                       efree(realpath);
+                               }
+                               /* fall through */
+
+                       case PHP_STREAM_PERSISTENT_FAILURE:
+                               efree(persistent_id);;
+                               return ret;
+               }
+       }
+       
        fd = open(realpath, open_flags, 0666);
 
        if (fd != -1)   {
@@ -111,22 +132,28 @@
                                goto err;
                } 
        
-               ret = php_stream_fopen_from_fd_rel(fd, mode);
+               ret = php_stream_fopen_from_fd_rel(fd, mode, persistent_id);
 
                if (ret)        {
-                       if (opened_path)        {
+                       if (opened_path) {
                                *opened_path = realpath;
                                realpath = NULL;
                        }
-                       if (realpath)
+                       if (realpath) {
                                efree(realpath);
-
+                       }
+                       if (persistent_id) {
+                               efree(persistent_id);
+                       }
                        return ret;
                }
 err:
                close(fd);
        }
        efree(realpath);
+       if (persistent_id) {
+               efree(persistent_id);
+       }
        return NULL;
 }
 /* }}} */
@@ -160,7 +187,7 @@
        int fd = php_open_temporary_fd(dir, pfx, opened_path TSRMLS_CC);
 
        if (fd != -1)   {
-               php_stream *stream = php_stream_fopen_from_fd_rel(fd, "r+b");
+               php_stream *stream = php_stream_fopen_from_fd_rel(fd, "r+b", NULL);
                if (stream) {
                        return stream;
                }
@@ -179,7 +206,7 @@
        int fd = php_open_temporary_fd(NULL, "php", &opened_path TSRMLS_CC);
 
        if (fd != -1)   {
-               php_stream *stream = php_stream_fopen_from_fd_rel(fd, "r+b");
+               php_stream *stream = php_stream_fopen_from_fd_rel(fd, "r+b", NULL);
                if (stream) {
                        php_stdio_stream_data *self = 
(php_stdio_stream_data*)stream->abstract;
 
@@ -197,12 +224,12 @@
        return NULL;
 }
 
-PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode STREAMS_DC 
TSRMLS_DC)
+PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char 
*persistent_id STREAMS_DC TSRMLS_DC)
 {
        php_stdio_stream_data *self;
        php_stream *stream;
        
-       self = emalloc_rel_orig(sizeof(*self));
+       self = pemalloc_rel_orig(sizeof(*self), persistent_id);
        memset(self, 0, sizeof(*self));
        self->file = NULL;
        self->is_pipe = 0;
@@ -228,7 +255,7 @@
        }
 #endif
        
-       stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
+       stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, persistent_id, 
mode);
 
        if (stream) {
                if (self->is_pipe) {
@@ -406,6 +433,7 @@
                }
                if (data->temp_file_name) {
                        unlink(data->temp_file_name);
+                       /* temporary streams are never persistent */
                        efree(data->temp_file_name);
                        data->temp_file_name = NULL;
                }
@@ -415,8 +443,7 @@
                data->fd = -1;
        }
 
-       /* STDIO streams are never persistent! */
-       efree(data);
+       pefree(data, stream->is_persistent);
 
        return ret;
 }



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to