Skip to content

Commit 567d8b5

Browse files
committed
streams: Refactor rmdir stream op to use zend_string
1 parent d8f33a2 commit 567d8b5

File tree

8 files changed

+38
-37
lines changed

8 files changed

+38
-37
lines changed

ext/phar/dirstream.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
526526
/**
527527
* Remove a directory within a phar archive
528528
*/
529-
int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) /* {{{ */
529+
bool phar_wrapper_rmdir(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context) /* {{{ */
530530
{
531531
phar_entry_info *entry;
532532
phar_archive_data *phar = NULL;
@@ -539,8 +539,8 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
539539
uint32_t path_len;
540540

541541
/* pre-readonly check, we need to know if this is a data phar */
542-
if (FAILURE == phar_split_fname(url, strlen(url), &arch, &arch_len, &entry2, &entry_len, 2, 2)) {
543-
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\", no phar archive specified, or phar archive does not exist", url);
542+
if (FAILURE == phar_split_fname(ZSTR_VAL(url), ZSTR_LEN(url), &arch, &arch_len, &entry2, &entry_len, 2, 2)) {
543+
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\", no phar archive specified, or phar archive does not exist", ZSTR_VAL(url));
544544
return 0;
545545
}
546546

@@ -552,24 +552,24 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
552552
efree(entry2);
553553

554554
if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
555-
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot rmdir directory \"%s\", write operations disabled", url);
555+
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot rmdir directory \"%s\", write operations disabled", ZSTR_VAL(url));
556556
return 0;
557557
}
558558

559-
if ((resource = phar_parse_url(wrapper, url, "w", options)) == NULL) {
559+
if ((resource = phar_parse_url(wrapper, ZSTR_VAL(url), "w", options)) == NULL) {
560560
return 0;
561561
}
562562

563563
/* we must have at the very least phar://alias.phar/internalfile.php */
564564
if (!resource->scheme || !resource->host || !resource->path) {
565565
php_url_free(resource);
566-
php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url \"%s\"", url);
566+
php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url \"%s\"", ZSTR_VAL(url));
567567
return 0;
568568
}
569569

570570
if (!zend_string_equals_literal_ci(resource->scheme, "phar")) {
571571
php_url_free(resource);
572-
php_stream_wrapper_log_error(wrapper, options, "phar error: not a phar stream url \"%s\"", url);
572+
php_stream_wrapper_log_error(wrapper, options, "phar error: not a phar stream url \"%s\"", ZSTR_VAL(url));
573573
return 0;
574574
}
575575

ext/phar/dirstream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
BEGIN_EXTERN_C()
2121
int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mode, int options, php_stream_context *context);
22-
int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
22+
bool phar_wrapper_rmdir(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
2323

2424
#ifdef PHAR_DIRSTREAM
2525
php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options);

ext/standard/file.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,13 +1160,12 @@ PHP_FUNCTION(mkdir)
11601160
/* {{{ Remove a directory */
11611161
PHP_FUNCTION(rmdir)
11621162
{
1163-
char *dir;
1164-
size_t dir_len;
1163+
zend_string *dir;
11651164
zval *zcontext = NULL;
11661165
php_stream_context *context;
11671166

11681167
ZEND_PARSE_PARAMETERS_START(1, 2)
1169-
Z_PARAM_PATH(dir, dir_len)
1168+
Z_PARAM_PATH_STR(dir)
11701169
Z_PARAM_OPTIONAL
11711170
Z_PARAM_RESOURCE_OR_NULL(zcontext)
11721171
ZEND_PARSE_PARAMETERS_END();

ext/standard/ftp_fopen_wrapper.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,24 +1111,24 @@ static int php_stream_ftp_mkdir(php_stream_wrapper *wrapper, const char *url, in
11111111
/* }}} */
11121112

11131113
/* {{{ php_stream_ftp_rmdir */
1114-
static int php_stream_ftp_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
1114+
static bool php_stream_ftp_rmdir(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context)
11151115
{
11161116
php_stream *stream = NULL;
11171117
php_url *resource = NULL;
11181118
int result;
11191119
char tmp_line[512];
11201120

1121-
stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL);
1121+
stream = php_ftp_fopen_connect(wrapper, ZSTR_VAL(url), "r", 0, NULL, context, NULL, &resource, NULL, NULL);
11221122
if (!stream) {
11231123
if (options & REPORT_ERRORS) {
1124-
php_error_docref(NULL, E_WARNING, "Unable to connect to %s", url);
1124+
php_error_docref(NULL, E_WARNING, "Unable to connect to %s", ZSTR_VAL(url));
11251125
}
11261126
goto rmdir_errexit;
11271127
}
11281128

11291129
if (resource->path == NULL) {
11301130
if (options & REPORT_ERRORS) {
1131-
php_error_docref(NULL, E_WARNING, "Invalid path provided in %s", url);
1131+
php_error_docref(NULL, E_WARNING, "Invalid path provided in %s", ZSTR_VAL(url));
11321132
}
11331133
goto rmdir_errexit;
11341134
}

main/php_streams.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ typedef struct _php_stream_wrapper_ops {
152152

153153
/* Create/Remove directory */
154154
int (*stream_mkdir)(php_stream_wrapper *wrapper, const char *url, int mode, int options, php_stream_context *context);
155-
int (*stream_rmdir)(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
155+
bool (*stream_rmdir)(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
156156
/* Metadata handling */
157157
int (*stream_metadata)(php_stream_wrapper *wrapper, const char *url, int options, void *value, php_stream_context *context);
158158
} php_stream_wrapper_ops;
@@ -370,7 +370,7 @@ PHPAPI int _php_stream_stat_path(const char *path, int flags, php_stream_statbuf
370370
PHPAPI int _php_stream_mkdir(const char *path, int mode, int options, php_stream_context *context);
371371
#define php_stream_mkdir(path, mode, options, context) _php_stream_mkdir(path, mode, options, context)
372372

373-
PHPAPI int _php_stream_rmdir(const char *path, int options, php_stream_context *context);
373+
PHPAPI bool _php_stream_rmdir(const zend_string *path, int options, php_stream_context *context);
374374
#define php_stream_rmdir(path, options, context) _php_stream_rmdir(path, options, context)
375375

376376
PHPAPI php_stream *_php_stream_opendir(const char *path, int options, php_stream_context *context STREAMS_DC);

main/streams/plain_wrapper.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,33 +1461,35 @@ static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, i
14611461
}
14621462
}
14631463

1464-
static int php_plain_files_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
1464+
static bool php_plain_files_rmdir(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context)
14651465
{
1466-
if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
1467-
url += sizeof("file://") - 1;
1466+
const char *url_ptr = ZSTR_VAL(url);
1467+
size_t url_len = ZSTR_LEN(url);
1468+
if (zend_string_starts_with_literal_ci(url, "file://")) {
1469+
url_ptr += strlen("file://");
1470+
url_len -= strlen("file://");
14681471
}
14691472

1470-
if (php_check_open_basedir(url)) {
1471-
return 0;
1473+
if (php_check_open_basedir(url_ptr)) {
1474+
return false;
14721475
}
14731476

1474-
size_t url_len = strlen(url);
14751477
#ifdef PHP_WIN32
1476-
if (!php_win32_check_trailing_space(url, url_len)) {
1477-
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
1478-
return 0;
1478+
if (!php_win32_check_trailing_space(url_ptr, url_len)) {
1479+
php_error_docref1(NULL, ZSTR_VAL(url), E_WARNING, "%s", strerror(ENOENT));
1480+
return false;
14791481
}
14801482
#endif
14811483

1482-
if (VCWD_RMDIR(url, url_len) < 0) {
1483-
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
1484-
return 0;
1484+
if (VCWD_RMDIR(url_ptr, url_len) < 0) {
1485+
php_error_docref1(NULL, ZSTR_VAL(url), E_WARNING, "%s", strerror(errno));
1486+
return false;
14851487
}
14861488

14871489
/* Clear stat cache (and realpath cache) */
1488-
php_clear_stat_cache(1, NULL, 0);
1490+
php_clear_stat_cache(true, NULL, 0);
14891491

1490-
return 1;
1492+
return true;
14911493
}
14921494

14931495
static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url, int option, void *value, php_stream_context *context)

main/streams/streams.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,13 +2096,13 @@ PHPAPI int _php_stream_mkdir(const char *path, int mode, int options, php_stream
20962096
/* }}} */
20972097

20982098
/* {{{ _php_stream_rmdir */
2099-
PHPAPI int _php_stream_rmdir(const char *path, int options, php_stream_context *context)
2099+
PHPAPI bool _php_stream_rmdir(const zend_string *path, int options, php_stream_context *context)
21002100
{
21012101
php_stream_wrapper *wrapper = NULL;
21022102

2103-
wrapper = php_stream_locate_url_wrapper(path, NULL, 0);
2103+
wrapper = php_stream_locate_url_wrapper(ZSTR_VAL(path), NULL, 0);
21042104
if (!wrapper || !wrapper->wops || !wrapper->wops->stream_rmdir) {
2105-
return 0;
2105+
return false;
21062106
}
21072107

21082108
return wrapper->wops->stream_rmdir(wrapper, path, options, context);

main/streams/userspace.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static int user_wrapper_stat_url(php_stream_wrapper *wrapper, const char *url, i
4848
static int user_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
4949
static bool user_wrapper_rename(php_stream_wrapper *wrapper, const zend_string *url_from, const zend_string *url_to, int options, php_stream_context *context);
5050
static int user_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url, int mode, int options, php_stream_context *context);
51-
static int user_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
51+
static bool user_wrapper_rmdir(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
5252
static int user_wrapper_metadata(php_stream_wrapper *wrapper, const char *url, int option, void *value, php_stream_context *context);
5353
static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, const char *filename, const char *mode,
5454
int options, zend_string **opened_path, php_stream_context *context STREAMS_DC);
@@ -1151,7 +1151,7 @@ static int user_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url, int
11511151
return ret;
11521152
}
11531153

1154-
static int user_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url,
1154+
static bool user_wrapper_rmdir(php_stream_wrapper *wrapper, const zend_string *url,
11551155
int options, php_stream_context *context)
11561156
{
11571157
struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
@@ -1168,7 +1168,7 @@ static int user_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url,
11681168
}
11691169

11701170
/* call the rmdir method */
1171-
ZVAL_STRING(&args[0], url);
1171+
ZVAL_STRINGL(&args[0], ZSTR_VAL(url), ZSTR_LEN(url));
11721172
ZVAL_LONG(&args[1], options);
11731173

11741174
ZVAL_STRING(&zfuncname, USERSTREAM_RMDIR);

0 commit comments

Comments
 (0)