pollita Fri Dec 12 18:05:20 2003 EDT Modified files: /php-src/ext/standard file.c ftp_fopen_wrapper.c http_fopen_wrapper.c php_fopen_wrapper.c /php-src/main php_streams.h /php-src/main/streams plain_wrapper.c userspace.c /php-src/ext/zlib zlib_fopen_wrapper.c /php-src/ext/bz2 bz2.c /php-src/ext/curl streams.c Log: Route rename() via wrapper ops. Move current rename() code to main/streams/plain_wrapper.c Implement ftp/rename() Implement userstreams/rename()
Index: php-src/ext/standard/file.c diff -u php-src/ext/standard/file.c:1.368 php-src/ext/standard/file.c:1.369 --- php-src/ext/standard/file.c:1.368 Wed Dec 10 01:08:39 2003 +++ php-src/ext/standard/file.c Fri Dec 12 18:05:15 2003 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.368 2003/12/10 06:08:39 moriyoshi Exp $ */ +/* $Id: file.c,v 1.369 2003/12/12 23:05:15 pollita Exp $ */ /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */ @@ -1437,48 +1437,40 @@ } /* }}} */ -/* {{{ proto bool rename(string old_name, string new_name) +/* {{{ proto bool rename(string old_name, string new_name[, resource context]) Rename a file */ PHP_FUNCTION(rename) { - zval **old_arg, **new_arg; char *old_name, *new_name; - int ret; + int old_name_len, new_name_len; + zval *zcontext = NULL; + php_stream_wrapper *wrapper; + php_stream_context *context; - if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &old_arg, &new_arg) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|r", &old_name, &old_name_len, &new_name, &new_name_len, &zcontext) == FAILURE) { + RETURN_FALSE; } - convert_to_string_ex(old_arg); - convert_to_string_ex(new_arg); - - old_name = Z_STRVAL_PP(old_arg); - new_name = Z_STRVAL_PP(new_arg); + wrapper = php_stream_locate_url_wrapper(old_name, NULL, 0 TSRMLS_CC); - if (PG(safe_mode) &&(!php_checkuid(old_name, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { + if (!wrapper || !wrapper->wops) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate stream wrapper."); RETURN_FALSE; } - if (php_check_open_basedir(old_name TSRMLS_CC)) { + if (!wrapper->wops->rename) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s wrapper does not support renaming.", wrapper->wops->label ? wrapper->wops->label : "Source"); RETURN_FALSE; } - ret = VCWD_RENAME(old_name, new_name); - - if (ret == -1) { -#ifdef EXDEV - if (errno == EXDEV) { - if (php_copy_file(old_name, new_name TSRMLS_CC) == SUCCESS) { - VCWD_UNLINK(old_name); - RETURN_TRUE; - } - } -#endif - php_error_docref2(NULL TSRMLS_CC, old_name, new_name, E_WARNING, "%s", strerror(errno)); + if (wrapper != php_stream_locate_url_wrapper(new_name, NULL, 0 TSRMLS_CC)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot rename a file across wrapper types."); RETURN_FALSE; } - RETURN_TRUE; + context = php_stream_context_from_zval(zcontext, 0); + + RETURN_BOOL(wrapper->wops->rename(wrapper, old_name, new_name, 0, context TSRMLS_CC)); } /* }}} */ Index: php-src/ext/standard/ftp_fopen_wrapper.c diff -u php-src/ext/standard/ftp_fopen_wrapper.c:1.68 php-src/ext/standard/ftp_fopen_wrapper.c:1.69 --- php-src/ext/standard/ftp_fopen_wrapper.c:1.68 Wed Dec 10 16:23:34 2003 +++ php-src/ext/standard/ftp_fopen_wrapper.c Fri Dec 12 18:05:15 2003 @@ -18,7 +18,7 @@ | Sara Golemon <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: ftp_fopen_wrapper.c,v 1.68 2003/12/10 21:23:34 iliaa Exp $ */ +/* $Id: ftp_fopen_wrapper.c,v 1.69 2003/12/12 23:05:15 pollita Exp $ */ #include "php.h" #include "php_globals.h" @@ -842,6 +842,89 @@ } /* }}} */ +/* {{{ php_stream_ftp_rename + */ +static int php_stream_ftp_rename(php_stream_wrapper *wrapper, char *url_from, char *url_to, int options, php_stream_context *context TSRMLS_DC) +{ + php_stream *stream = NULL; + php_url *resource_from = NULL, *resource_to = NULL; + int result; + char tmp_line[512]; + + resource_from = php_url_parse(url_from); + resource_to = php_url_parse(url_to); + /* Must be same scheme (ftp/ftp or ftps/ftps), same host, and same port + (or a 21/0 0/21 combination which is also "same") + Also require paths to/from */ + if (!resource_from || + !resource_to || + !resource_from->scheme || + !resource_to->scheme || + strcmp(resource_from->scheme, resource_to->scheme) || + !resource_from->host || + !resource_to->host || + strcmp(resource_from->host, resource_to->host) || + (resource_from->port != resource_to->port && + resource_from->port * resource_to->port != 0 && + resource_from->port + resource_to->port != 21) || + !resource_from->path || + !resource_to->path) { + goto rename_errexit; + } + + stream = php_ftp_fopen_connect(wrapper, url_from, "r", 0, NULL, NULL, NULL, NULL, NULL, NULL TSRMLS_CC); + if (!stream) { + if (options & REPORT_ERRORS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to %s", resource_from->host); + } + goto rename_errexit; + } + + /* Rename FROM */ + php_stream_write_string(stream, "RNFR "); + php_stream_write_string(stream, resource_from->path); + php_stream_write_string(stream, "\r\n"); + + result = GET_FTP_RESULT(stream); + if (result < 300 || result > 399) { + if (options & REPORT_ERRORS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error Renaming file: %s", tmp_line); + } + goto rename_errexit; + } + + /* Rename TO */ + php_stream_write_string(stream, "RNTO "); + php_stream_write_string(stream, resource_to->path); + php_stream_write_string(stream, "\r\n"); + + result = GET_FTP_RESULT(stream); + if (result < 200 || result > 299) { + if (options & REPORT_ERRORS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error Renaming file: %s", tmp_line); + } + goto rename_errexit; + } + + php_url_free(resource_from); + php_url_free(resource_to); + php_stream_close(stream); + return 1; + + rename_errexit: + if (resource_from) { + php_url_free(resource_from); + } + if (resource_to) { + php_url_free(resource_to); + } + if (stream) { + php_stream_close(stream); + } + return 0; +} +/* }}} */ + static php_stream_wrapper_ops ftp_stream_wops = { php_stream_url_wrap_ftp, php_stream_ftp_stream_close, /* stream_close */ @@ -849,7 +932,8 @@ php_stream_ftp_url_stat, /* stat_url */ php_stream_ftp_opendir, /* opendir */ "FTP", - php_stream_ftp_unlink /* unlink */ + php_stream_ftp_unlink, /* unlink */ + php_stream_ftp_rename /* rename */ }; PHPAPI php_stream_wrapper php_stream_ftp_wrapper = { Index: php-src/ext/standard/http_fopen_wrapper.c diff -u php-src/ext/standard/http_fopen_wrapper.c:1.77 php-src/ext/standard/http_fopen_wrapper.c:1.78 --- php-src/ext/standard/http_fopen_wrapper.c:1.77 Wed Dec 3 00:30:15 2003 +++ php-src/ext/standard/http_fopen_wrapper.c Fri Dec 12 18:05:15 2003 @@ -18,7 +18,7 @@ | Wez Furlong <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: http_fopen_wrapper.c,v 1.77 2003/12/03 05:30:15 pollita Exp $ */ +/* $Id: http_fopen_wrapper.c,v 1.78 2003/12/12 23:05:15 pollita Exp $ */ #include "php.h" #include "php_globals.h" @@ -542,7 +542,8 @@ NULL, /* stat_url */ NULL, /* opendir */ "HTTP", - NULL /* unlink */ + NULL, /* unlink */ + NULL, /* rename */ }; PHPAPI php_stream_wrapper php_stream_http_wrapper = { Index: php-src/ext/standard/php_fopen_wrapper.c diff -u php-src/ext/standard/php_fopen_wrapper.c:1.40 php-src/ext/standard/php_fopen_wrapper.c:1.41 --- php-src/ext/standard/php_fopen_wrapper.c:1.40 Tue Jun 10 16:03:38 2003 +++ php-src/ext/standard/php_fopen_wrapper.c Fri Dec 12 18:05:15 2003 @@ -17,7 +17,7 @@ | Hartmut Holzgraefe <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: php_fopen_wrapper.c,v 1.40 2003/06/10 20:03:38 imajes Exp $ */ +/* $Id: php_fopen_wrapper.c,v 1.41 2003/12/12 23:05:15 pollita Exp $ */ #include <stdio.h> #include <stdlib.h> @@ -233,7 +233,8 @@ NULL, /* stat */ NULL, /* opendir */ "PHP", - NULL /* unlink */ + NULL, /* unlink */ + NULL /* rename */ }; php_stream_wrapper php_stream_php_wrapper = { Index: php-src/main/php_streams.h diff -u php-src/main/php_streams.h:1.87 php-src/main/php_streams.h:1.88 --- php-src/main/php_streams.h:1.87 Mon Dec 1 14:47:03 2003 +++ php-src/main/php_streams.h Fri Dec 12 18:05:16 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_streams.h,v 1.87 2003/12/01 19:47:03 pollita Exp $ */ +/* $Id: php_streams.h,v 1.88 2003/12/12 23:05:16 pollita Exp $ */ #ifndef PHP_STREAMS_H #define PHP_STREAMS_H @@ -148,6 +148,9 @@ /* delete a file */ int (*unlink)(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC); + + /* rename a file */ + int (*rename)(php_stream_wrapper *wrapper, char *url_from, char *url_to, int options, php_stream_context *context TSRMLS_DC); } php_stream_wrapper_ops; struct _php_stream_wrapper { Index: php-src/main/streams/plain_wrapper.c diff -u php-src/main/streams/plain_wrapper.c:1.30 php-src/main/streams/plain_wrapper.c:1.31 --- php-src/main/streams/plain_wrapper.c:1.30 Mon Dec 1 20:04:14 2003 +++ php-src/main/streams/plain_wrapper.c Fri Dec 12 18:05:17 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: plain_wrapper.c,v 1.30 2003/12/02 01:04:14 pollita Exp $ */ +/* $Id: plain_wrapper.c,v 1.31 2003/12/12 23:05:17 pollita Exp $ */ #include "php.h" #include "php_globals.h" @@ -956,6 +956,49 @@ return 1; } +static int php_plain_files_rename(php_stream_wrapper *wrapper, char *url_from, char *url_to, int options, php_stream_context *context TSRMLS_DC) +{ + char *p; + int ret; + + if (!url_from || !url_to) { + return 0; + } + + if ((p = strstr(url_from, "://")) != NULL) { + url_from = p + 3; + } + + if ((p = strstr(url_to, "://")) != NULL) { + url_to = p + 3; + } + + if (PG(safe_mode) &&(!php_checkuid(url_from, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { + return 0; + } + + if (php_check_open_basedir(url_from TSRMLS_CC)) { + return 0; + } + + ret = VCWD_RENAME(url_from, url_to); + + if (ret == -1) { +#ifdef EXDEV + if (errno == EXDEV) { + if (php_copy_file(url_from, url_to TSRMLS_CC) == SUCCESS) { + VCWD_UNLINK(url_from); + return 1; + } + } +#endif + php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno)); + return 0; + } + + return 1; +} + static php_stream_wrapper_ops php_plain_files_wrapper_ops = { php_plain_files_stream_opener, NULL, @@ -963,7 +1006,8 @@ php_plain_files_url_stater, php_plain_files_dir_opener, "plainfile", - php_plain_files_unlink + php_plain_files_unlink, + php_plain_files_rename }; php_stream_wrapper php_plain_files_wrapper = { Index: php-src/main/streams/userspace.c diff -u php-src/main/streams/userspace.c:1.13 php-src/main/streams/userspace.c:1.14 --- php-src/main/streams/userspace.c:1.13 Tue Dec 2 15:06:40 2003 +++ php-src/main/streams/userspace.c Fri Dec 12 18:05:17 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: userspace.c,v 1.13 2003/12/02 20:06:40 pollita Exp $ */ +/* $Id: userspace.c,v 1.14 2003/12/12 23:05:17 pollita Exp $ */ #include "php.h" #include "php_globals.h" @@ -34,6 +34,7 @@ static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, char *filename, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC); static int user_wrapper_stat_url(php_stream_wrapper *wrapper, char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC); static int user_wrapper_unlink(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC); +static int user_wrapper_rename(php_stream_wrapper *wrapper, char *url_from, char *url_to, int options, php_stream_context *context TSRMLS_DC); static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, char *filename, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC); @@ -44,7 +45,8 @@ user_wrapper_stat_url, user_wrapper_opendir, "user-space", - user_wrapper_unlink + user_wrapper_unlink, + user_wrapper_rename }; @@ -95,6 +97,7 @@ #define USERSTREAM_STAT "stream_stat" #define USERSTREAM_STATURL "url_stat" #define USERSTREAM_UNLINK "unlink" +#define USERSTREAM_RENAME "rename" #define USERSTREAM_DIR_OPEN "dir_opendir" #define USERSTREAM_DIR_READ "dir_readdir" #define USERSTREAM_DIR_REWIND "dir_rewinddir" @@ -158,6 +161,11 @@ return true / false; } + function rename(string $from, string $to) + { + return true / false; + } + function dir_opendir(string $url, int $options) { return true / false; @@ -791,6 +799,69 @@ return ret; } +static int user_wrapper_rename(php_stream_wrapper *wrapper, char *url_from, char *url_to, int options, php_stream_context *context TSRMLS_DC) +{ + struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract; + zval *zold_name, *znew_name, *zfuncname, *zretval, *zcontext; + zval **args[2]; + int call_result; + zval *object; + int ret = 0; + + /* create an instance of our class */ + ALLOC_ZVAL(object); + object_init_ex(object, uwrap->ce); + ZVAL_REFCOUNT(object) = 1; + PZVAL_IS_REF(object) = 1; + + if (context) { + MAKE_STD_ZVAL(zcontext); + php_stream_context_to_zval(context, zcontext); + add_property_zval(object, "context", zcontext); + /* The object property should be the only reference, + 'get rid' of our local reference. */ + zval_ptr_dtor(&zcontext); + } else { + add_property_null(object, "context"); + } + + /* call the rename method */ + MAKE_STD_ZVAL(zold_name); + ZVAL_STRING(zold_name, url_from, 1); + args[0] = &zold_name; + + MAKE_STD_ZVAL(znew_name); + ZVAL_STRING(znew_name, url_to, 1); + args[1] = &znew_name; + + MAKE_STD_ZVAL(zfuncname); + ZVAL_STRING(zfuncname, USERSTREAM_RENAME, 1); + + call_result = call_user_function_ex(NULL, + &object, + zfuncname, + &zretval, + 2, args, + 0, NULL TSRMLS_CC); + + if (call_result == SUCCESS && zretval && Z_TYPE_P(zretval) == IS_BOOL) { + ret = Z_LVAL_P(zretval); + } else if (call_result == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s::" USERSTREAM_RENAME " is not implemented!", uwrap->classname); + } + + /* clean up */ + zval_ptr_dtor(&object); + if (zretval) + zval_ptr_dtor(&zretval); + + zval_ptr_dtor(&zfuncname); + zval_ptr_dtor(&zold_name); + zval_ptr_dtor(&znew_name); + + return ret; +} + static int user_wrapper_stat_url(php_stream_wrapper *wrapper, char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC) { struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract; Index: php-src/ext/zlib/zlib_fopen_wrapper.c diff -u php-src/ext/zlib/zlib_fopen_wrapper.c:1.41 php-src/ext/zlib/zlib_fopen_wrapper.c:1.42 --- php-src/ext/zlib/zlib_fopen_wrapper.c:1.41 Wed Jul 23 20:03:42 2003 +++ php-src/ext/zlib/zlib_fopen_wrapper.c Fri Dec 12 18:05:18 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zlib_fopen_wrapper.c,v 1.41 2003/07/24 00:03:42 iliaa Exp $ */ +/* $Id: zlib_fopen_wrapper.c,v 1.42 2003/12/12 23:05:18 pollita Exp $ */ #define _GNU_SOURCE @@ -160,7 +160,8 @@ NULL, /* stat_url */ NULL, /* opendir */ "ZLIB", - NULL /* unlink */ + NULL, /* unlink */ + NULL /* rename */ }; php_stream_wrapper php_stream_gzip_wrapper = { Index: php-src/ext/bz2/bz2.c diff -u php-src/ext/bz2/bz2.c:1.3 php-src/ext/bz2/bz2.c:1.4 --- php-src/ext/bz2/bz2.c:1.3 Tue Dec 9 11:29:54 2003 +++ php-src/ext/bz2/bz2.c Fri Dec 12 18:05:18 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: bz2.c,v 1.3 2003/12/09 16:29:54 sniper Exp $ */ +/* $Id: bz2.c,v 1.4 2003/12/12 23:05:18 pollita Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -226,7 +226,8 @@ NULL, /* stat */ NULL, /* opendir */ "BZip2", - NULL /* unlink */ + NULL, /* unlink */ + NULL /* rename */ }; php_stream_wrapper php_stream_bzip2_wrapper = { Index: php-src/ext/curl/streams.c diff -u php-src/ext/curl/streams.c:1.9 php-src/ext/curl/streams.c:1.10 --- php-src/ext/curl/streams.c:1.9 Sun Nov 16 23:54:27 2003 +++ php-src/ext/curl/streams.c Fri Dec 12 18:05:19 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: streams.c,v 1.9 2003/11/17 04:54:27 iliaa Exp $ */ +/* $Id: streams.c,v 1.10 2003/12/12 23:05:19 pollita Exp $ */ /* This file implements cURL based wrappers. * NOTE: If you are implementing your own streams that are intended to @@ -362,7 +362,8 @@ NULL, /* stat url */ NULL, /* opendir */ NULL, /* label */ - NULL /* unlink */ + NULL, /* unlink */ + NULL /* rename */ }; php_stream_wrapper php_curl_wrapper = {
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php