summaryrefslogtreecommitdiff
path: root/main/streams/plain_wrapper.c
diff options
context:
space:
mode:
authorDmitry Stogov <dmitry@zend.com>2015-03-04 02:05:28 +0300
committerDmitry Stogov <dmitry@zend.com>2015-03-04 02:05:28 +0300
commit2fa8d67a5ce59ba9ba6192481e3c2522c3ff5542 (patch)
tree9d9d57215f756c387722e74d7d1e1c2de3276a1c /main/streams/plain_wrapper.c
parent2841aa95db84f3563c94c90f84bf7f47ba159a2d (diff)
downloadphp-git-2fa8d67a5ce59ba9ba6192481e3c2522c3ff5542.tar.gz
Use zend_string* instead of char* for opened_patch handling. Avoid reallocations and improve string reuse.
Diffstat (limited to 'main/streams/plain_wrapper.c')
-rw-r--r--main/streams/plain_wrapper.c54
1 files changed, 23 insertions, 31 deletions
diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c
index fe256b767e..83bd059c34 100644
--- a/main/streams/plain_wrapper.c
+++ b/main/streams/plain_wrapper.c
@@ -124,7 +124,7 @@ typedef struct {
unsigned _reserved:29;
int lock_flag; /* stores the lock state */
- char *temp_file_name; /* if non-null, this is the path to a temporary file that
+ zend_string *temp_name; /* if non-null, this is the path to a temporary file that
* is to be deleted when the stream is closed */
#if HAVE_FLUSHIO
char last_op;
@@ -168,7 +168,7 @@ static php_stream *_php_stream_fopen_from_fd_int(int fd, const char *mode, const
self->is_pipe = 0;
self->lock_flag = LOCK_UN;
self->is_process_pipe = 0;
- self->temp_file_name = NULL;
+ self->temp_name = NULL;
self->fd = fd;
return php_stream_alloc_rel(&php_stream_stdio_ops, self, persistent_id, mode);
@@ -184,15 +184,15 @@ static php_stream *_php_stream_fopen_from_file_int(FILE *file, const char *mode
self->is_pipe = 0;
self->lock_flag = LOCK_UN;
self->is_process_pipe = 0;
- self->temp_file_name = NULL;
+ self->temp_name = NULL;
self->fd = fileno(file);
return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
}
-PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path_ptr STREAMS_DC)
+PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_ptr STREAMS_DC)
{
- char *opened_path = NULL;
+ zend_string *opened_path = NULL;
int fd;
fd = php_open_temporary_fd(dir, pfx, &opened_path);
@@ -207,9 +207,9 @@ PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char
if (stream) {
php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
stream->wrapper = &php_plain_files_wrapper;
- stream->orig_path = estrdup(opened_path);
+ stream->orig_path = estrndup(opened_path->val, opened_path->len);
- self->temp_file_name = opened_path;
+ self->temp_name = opened_path;
self->lock_flag = LOCK_UN;
return stream;
@@ -311,7 +311,7 @@ PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STRE
self->lock_flag = LOCK_UN;
self->is_process_pipe = 1;
self->fd = fileno(file);
- self->temp_file_name = NULL;
+ self->temp_name = NULL;
stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
@@ -450,11 +450,11 @@ static int php_stdiop_close(php_stream *stream, int close_handle)
} else {
return 0; /* everything should be closed already -> success */
}
- if (data->temp_file_name) {
- unlink(data->temp_file_name);
+ if (data->temp_name) {
+ unlink(data->temp_name->val);
/* temporary streams are never persistent */
- efree(data->temp_file_name);
- data->temp_file_name = NULL;
+ zend_string_release(data->temp_name);
+ data->temp_name = NULL;
}
} else {
ret = 0;
@@ -890,7 +890,7 @@ static php_stream_ops php_plain_files_dirstream_ops = {
};
static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, const char *path, const char *mode,
- int options, char **opened_path, php_stream_context *context STREAMS_DC)
+ int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
{
DIR *dir = NULL;
php_stream *stream = NULL;
@@ -928,9 +928,9 @@ static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, const
/* }}} */
/* {{{ php_stream_fopen */
-PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path, int options STREAMS_DC)
+PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, zend_string **opened_path, int options STREAMS_DC)
{
- char *realpath = NULL;
+ char realpath[MAXPATHLEN];
int open_flags;
int fd;
php_stream *ret;
@@ -945,9 +945,9 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
}
if (options & STREAM_ASSUME_REALPATH) {
- realpath = estrdup(filename);
+ strlcpy(realpath, filename, sizeof(realpath));
} else {
- if ((realpath = expand_filepath(filename, NULL)) == NULL) {
+ if (expand_filepath(filename, realpath) == NULL) {
return NULL;
}
}
@@ -957,15 +957,12 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
switch (php_stream_from_persistent_id(persistent_id, &ret)) {
case PHP_STREAM_PERSISTENT_SUCCESS:
if (opened_path) {
- *opened_path = realpath;
- realpath = NULL;
+ //TODO: avoid reallocation???
+ *opened_path = zend_string_init(realpath, strlen(realpath), 0);
}
/* fall through */
case PHP_STREAM_PERSISTENT_FAILURE:
- if (realpath) {
- efree(realpath);
- }
efree(persistent_id);;
return ret;
}
@@ -983,11 +980,7 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
if (ret) {
if (opened_path) {
- *opened_path = realpath;
- realpath = NULL;
- }
- if (realpath) {
- efree(realpath);
+ *opened_path = zend_string_init(realpath, strlen(realpath), 0);
}
if (persistent_id) {
efree(persistent_id);
@@ -1005,7 +998,7 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
r = do_fstat(self, 0);
if ((r == 0 && !S_ISREG(self->sb.st_mode))) {
if (opened_path) {
- efree(*opened_path);
+ zend_string_release(*opened_path);
*opened_path = NULL;
}
php_stream_close(ret);
@@ -1018,7 +1011,6 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
}
close(fd);
}
- efree(realpath);
if (persistent_id) {
efree(persistent_id);
}
@@ -1028,7 +1020,7 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, const char *path, const char *mode,
- int options, char **opened_path, php_stream_context *context STREAMS_DC)
+ int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
{
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path)) {
return NULL;
@@ -1393,7 +1385,7 @@ PHPAPI php_stream_wrapper php_plain_files_wrapper = {
};
/* {{{ php_stream_fopen_with_path */
-PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path, int options STREAMS_DC)
+PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path, int options STREAMS_DC)
{
/* code ripped off from fopen_wrappers.c */
char *pathbuf, *end;