From 4a8de5c704c30c9b67c8e0cab55c193dc8e92341 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 18 Mar 2003 23:39:31 +0000 Subject: [PATCH] Manual merge of the persistent plain file streams code. See http://news.php.net/article.php?group=php.cvs&article=19680 for more information. --- main/php_streams.h | 9 ++++++--- main/streams.c | 50 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/main/php_streams.h b/main/php_streams.h index 1c9ff46156..37ff5f3e05 100755 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -65,7 +65,7 @@ PHPAPI int php_file_le_pstream(void); #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_file_rel(file, mode) _php_stream_fopen_from_file((file), (mode) 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_pipe_rel(file, mode) _php_stream_fopen_from_pipe((file), (mode) STREAMS_REL_CC TSRMLS_CC) @@ -440,8 +440,8 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path, int options STREAMS_DC TSRMLS_DC); #define php_stream_fopen_with_path(filename, mode, path, opened) _php_stream_fopen_with_path((filename), (mode), (path), (opened) 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_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) @@ -511,6 +511,9 @@ PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show /* if set, skip 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 diff --git a/main/streams.c b/main/streams.c index 4e1517115a..50b6f65047 100755 --- a/main/streams.c +++ b/main/streams.c @@ -1295,7 +1295,7 @@ PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char 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; } @@ -1314,7 +1314,7 @@ PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC) 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; @@ -1465,6 +1465,7 @@ static int php_stdiop_close(php_stream *stream, int close_handle TSRMLS_DC) } if (data->temp_file_name) { unlink(data->temp_file_name); + /* temporary streams are never persistent */ efree(data->temp_file_name); } } else { @@ -1472,8 +1473,7 @@ static int php_stdiop_close(php_stream *stream, int close_handle TSRMLS_DC) data->file = NULL; } - /* STDIO streams are never persistent! */ - efree(data); + pefree(data, stream->is_persistent); return ret; } @@ -1875,7 +1875,9 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha struct stat st; int open_flags; int fd; - php_stream *ret; + php_stream *ret = NULL; + int persistent = options & STREAM_OPEN_PERSISTENT; + char *persistent_id = NULL; if (FAILURE == php_stream_parse_fopen_modes(mode, &open_flags)) { if (options & REPORT_ERRORS) { @@ -1886,6 +1888,25 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha 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) { @@ -1899,15 +1920,19 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha 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; } @@ -1915,16 +1940,19 @@ err: close(fd); } efree(realpath); + if (persistent_id) { + efree(persistent_id); + } 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; @@ -1949,7 +1977,7 @@ PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode STREAMS_DC } #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) { -- 2.40.0