Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5194bd9

Browse files
committed
streams: Refactor unlink stream op to use zend_string
1 parent 947c402 commit 5194bd9

File tree

7 files changed

+41
-41
lines changed

7 files changed

+41
-41
lines changed

‎ext/phar/stream.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ static int phar_wrapper_stat(php_stream_wrapper *wrapper, const char *url, int f
667667
/**
668668
* Unlink a file within a phar archive
669669
*/
670-
static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) /* {{{ */
670+
static bool phar_wrapper_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context) /* {{{ */
671671
{
672672
php_url *resource;
673673
char *internal_file, *error;
@@ -676,22 +676,22 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
676676
phar_archive_data *pphar;
677677
uint32_t host_len;
678678

679-
if ((resource = phar_parse_url(wrapper, url, "rb", options)) == NULL) {
679+
if ((resource = phar_parse_url(wrapper, ZSTR_VAL(url), "rb", options)) == NULL) {
680680
php_stream_wrapper_log_error(wrapper, options, "phar error: unlink failed");
681-
return 0;
681+
return false;
682682
}
683683

684684
/* we must have at the very least phar://alias.phar/internalfile.php */
685685
if (!resource->scheme || !resource->host || !resource->path) {
686686
php_url_free(resource);
687-
php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url \"%s\"", url);
688-
return 0;
687+
php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url \"%s\"", ZSTR_VAL(url));
688+
return false;
689689
}
690690

691691
if (!zend_string_equals_literal_ci(resource->scheme, "phar")) {
692692
php_url_free(resource);
693-
php_stream_wrapper_log_error(wrapper, options, "phar error: not a phar stream url \"%s\"", url);
694-
return 0;
693+
php_stream_wrapper_log_error(wrapper, options, "phar error: not a phar stream url \"%s\"", ZSTR_VAL(url));
694+
return false;
695695
}
696696

697697
host_len = ZSTR_LEN(resource->host);
@@ -710,14 +710,14 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
710710
if (FAILURE == phar_get_entry_data(&idata, ZSTR_VAL(resource->host), host_len, internal_file, internal_file_len, "r", 0, &error, 1)) {
711711
/* constraints of fp refcount were not met */
712712
if (error) {
713-
php_stream_wrapper_log_error(wrapper, options, "unlink of \"%s\" failed: %s", url, error);
713+
php_stream_wrapper_log_error(wrapper, options, "unlink of \"%s\" failed: %s", ZSTR_VAL(url), error);
714714
efree(error);
715715
} else {
716-
php_stream_wrapper_log_error(wrapper, options, "unlink of \"%s\" failed, file does not exist", url);
716+
php_stream_wrapper_log_error(wrapper, options, "unlink of \"%s\" failed, file does not exist", ZSTR_VAL(url));
717717
}
718718
efree(internal_file);
719719
php_url_free(resource);
720-
return 0;
720+
return false;
721721
}
722722
if (error) {
723723
efree(error);
@@ -728,7 +728,7 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
728728
efree(internal_file);
729729
php_url_free(resource);
730730
phar_entry_delref(idata);
731-
return 0;
731+
return false;
732732
}
733733
php_url_free(resource);
734734
efree(internal_file);
@@ -737,7 +737,7 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
737737
php_stream_wrapper_log_error(wrapper, options, "%s", error);
738738
efree(error);
739739
}
740-
return 1;
740+
return true;
741741
}
742742
/* }}} */
743743

‎ext/phar/stream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void phar_entry_remove(phar_entry_data *idata, char **error);
2424

2525
static php_stream* phar_wrapper_open_url(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC);
2626
static bool phar_wrapper_rename(php_stream_wrapper *wrapper, const zend_string *url_from, const zend_string *url_to, int options, php_stream_context *context);
27-
static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
27+
static bool phar_wrapper_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
2828
static int phar_wrapper_stat(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context);
2929

3030
/* file/stream handlers */

‎ext/standard/file.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,21 +1294,20 @@ PHP_FUNCTION(rename)
12941294
/* {{{ Delete a file */
12951295
PHP_FUNCTION(unlink)
12961296
{
1297-
char *filename;
1298-
size_t filename_len;
1297+
zend_string *filename;
12991298
php_stream_wrapper *wrapper;
13001299
zval *zcontext = NULL;
13011300
php_stream_context *context = NULL;
13021301

13031302
ZEND_PARSE_PARAMETERS_START(1, 2)
1304-
Z_PARAM_PATH(filename, filename_len)
1303+
Z_PARAM_PATH_STR(filename)
13051304
Z_PARAM_OPTIONAL
13061305
Z_PARAM_RESOURCE_OR_NULL(zcontext)
13071306
ZEND_PARSE_PARAMETERS_END();
13081307

13091308
context = php_stream_context_from_zval(zcontext, 0);
13101309

1311-
wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
1310+
wrapper = php_stream_locate_url_wrapper(ZSTR_VAL(filename), NULL, 0);
13121311

13131312
if (!wrapper || !wrapper->wops) {
13141313
php_error_docref(NULL, E_WARNING, "Unable to locate stream wrapper");

‎ext/standard/ftp_fopen_wrapper.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -892,24 +892,24 @@ static int php_stream_ftp_url_stat(php_stream_wrapper *wrapper, const char *url,
892892
/* }}} */
893893

894894
/* {{{ php_stream_ftp_unlink */
895-
static int php_stream_ftp_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
895+
static bool php_stream_ftp_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context)
896896
{
897897
php_stream *stream = NULL;
898898
php_url *resource = NULL;
899899
int result;
900900
char tmp_line[512];
901901

902-
stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL);
902+
stream = php_ftp_fopen_connect(wrapper, ZSTR_VAL(url), "r", 0, NULL, context, NULL, &resource, NULL, NULL);
903903
if (!stream) {
904904
if (options & REPORT_ERRORS) {
905-
php_error_docref(NULL, E_WARNING, "Unable to connect to %s", url);
905+
php_error_docref(NULL, E_WARNING, "Unable to connect to %s", ZSTR_VAL(url));
906906
}
907907
goto unlink_errexit;
908908
}
909909

910910
if (resource->path == NULL) {
911911
if (options & REPORT_ERRORS) {
912-
php_error_docref(NULL, E_WARNING, "Invalid path provided in %s", url);
912+
php_error_docref(NULL, E_WARNING, "Invalid path provided in %s", ZSTR_VAL(url));
913913
}
914914
goto unlink_errexit;
915915
}
@@ -927,7 +927,7 @@ static int php_stream_ftp_unlink(php_stream_wrapper *wrapper, const char *url, i
927927

928928
php_url_free(resource);
929929
php_stream_close(stream);
930-
return 1;
930+
return true;
931931

932932
unlink_errexit:
933933
if (resource) {
@@ -936,7 +936,7 @@ static int php_stream_ftp_unlink(php_stream_wrapper *wrapper, const char *url, i
936936
if (stream) {
937937
php_stream_close(stream);
938938
}
939-
return 0;
939+
return false;
940940
}
941941
/* }}} */
942942

‎main/php_streams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ typedef struct _php_stream_wrapper_ops {
145145
const char *label;
146146

147147
/* delete a file */
148-
int (*unlink)(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
148+
bool (*unlink)(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
149149

150150
/* rename a file */
151151
bool (*rename)(php_stream_wrapper *wrapper, const zend_string *url_from, const zend_string *url_to, int options, php_stream_context *context);

‎main/streams/plain_wrapper.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,29 +1240,31 @@ static int php_plain_files_url_stater(php_stream_wrapper *wrapper, const char *u
12401240
return VCWD_STAT(url, &ssb->sb);
12411241
}
12421242

1243-
static int php_plain_files_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
1243+
static bool php_plain_files_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context)
12441244
{
1245-
if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
1246-
url += sizeof("file://") - 1;
1245+
const char *url_ptr = ZSTR_VAL(url);
1246+
size_t url_len = ZSTR_LEN(url);
1247+
if (zend_string_starts_with_literal_ci(url, "file://")) {
1248+
url_ptr += strlen("file://");
1249+
url_len -= strlen("file://");
12471250
}
12481251

1249-
if (php_check_open_basedir(url)) {
1250-
return 0;
1252+
if (php_check_open_basedir(ZSTR_VAL(url))) {
1253+
return false;
12511254
}
12521255

1253-
size_t url_len = strlen(url);
1254-
zend_result ret = VCWD_UNLINK(url, url_len);
1256+
zend_result ret = VCWD_UNLINK(url_ptr, url_len);
12551257
if (ret == FAILURE) {
12561258
if (options & REPORT_ERRORS) {
1257-
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
1259+
php_error_docref1(NULL, ZSTR_VAL(url), E_WARNING, "%s", strerror(errno));
12581260
}
1259-
return 0;
1261+
return false;
12601262
}
12611263

12621264
/* Clear stat cache (and realpath cache) */
12631265
php_clear_stat_cache(1, NULL, 0);
12641266

1265-
return 1;
1267+
return true;
12661268
}
12671269

12681270
static bool php_plain_files_rename(php_stream_wrapper *wrapper, const zend_string *url_from, const zend_string *url_to, int options, php_stream_context *context)

‎main/streams/userspace.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct php_user_stream_wrapper {
4545
static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC);
4646
static int user_wrapper_close(php_stream_wrapper *wrapper, php_stream *stream);
4747
static int user_wrapper_stat_url(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context);
48-
static int user_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
48+
static bool user_wrapper_unlink(php_stream_wrapper *wrapper, const zend_string *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);
5151
static bool user_wrapper_rmdir(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
@@ -1029,28 +1029,27 @@ static int php_userstreamop_set_option(php_stream *stream, int option, int value
10291029
}
10301030

10311031

1032-
static int user_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
1032+
static bool user_wrapper_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context)
10331033
{
10341034
struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
10351035
zval zfuncname, zretval;
10361036
zval args[1];
1037-
int call_result;
10381037
zval object;
1039-
int ret = 0;
10401038

10411039
/* create an instance of our class */
10421040
user_stream_create_object(uwrap, context, &object);
10431041
if (Z_TYPE(object) == IS_UNDEF) {
1044-
return ret;
1042+
return false;
10451043
}
10461044

10471045
/* call the unlink method */
1048-
ZVAL_STRING(&args[0], url);
1046+
ZVAL_STRINGL(&args[0], ZSTR_VAL(url), ZSTR_LEN(url));
10491047

10501048
ZVAL_STRING(&zfuncname, USERSTREAM_UNLINK);
10511049

1052-
call_result = call_method_if_exists(&object, &zfuncname, &zretval, 1, args);
1050+
zend_resultcall_result = call_method_if_exists(&object, &zfuncname, &zretval, 1, args);
10531051

1052+
bool ret = false;
10541053
if (call_result == SUCCESS && (Z_TYPE(zretval) == IS_FALSE || Z_TYPE(zretval) == IS_TRUE)) {
10551054
ret = (Z_TYPE(zretval) == IS_TRUE);
10561055
} else if (call_result == FAILURE) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /