Skip to content

Virtual cwd refactoring #15524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
streams: Refactor unlink stream op to use zend_string
  • Loading branch information
Girgias committed Aug 23, 2024
commit 5194bd969d1cb41b4b73ac73e4dfa8df209c9181
24 changes: 12 additions & 12 deletions ext/phar/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ static int phar_wrapper_stat(php_stream_wrapper *wrapper, const char *url, int f
/**
* Unlink a file within a phar archive
*/
static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) /* {{{ */
static bool phar_wrapper_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context) /* {{{ */
{
php_url *resource;
char *internal_file, *error;
Expand All @@ -676,22 +676,22 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
phar_archive_data *pphar;
uint32_t host_len;

if ((resource = phar_parse_url(wrapper, url, "rb", options)) == NULL) {
if ((resource = phar_parse_url(wrapper, ZSTR_VAL(url), "rb", options)) == NULL) {
php_stream_wrapper_log_error(wrapper, options, "phar error: unlink failed");
return 0;
return false;
}

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

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

host_len = ZSTR_LEN(resource->host);
Expand All @@ -710,14 +710,14 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
if (FAILURE == phar_get_entry_data(&idata, ZSTR_VAL(resource->host), host_len, internal_file, internal_file_len, "r", 0, &error, 1)) {
/* constraints of fp refcount were not met */
if (error) {
php_stream_wrapper_log_error(wrapper, options, "unlink of \"%s\" failed: %s", url, error);
php_stream_wrapper_log_error(wrapper, options, "unlink of \"%s\" failed: %s", ZSTR_VAL(url), error);
efree(error);
} else {
php_stream_wrapper_log_error(wrapper, options, "unlink of \"%s\" failed, file does not exist", url);
php_stream_wrapper_log_error(wrapper, options, "unlink of \"%s\" failed, file does not exist", ZSTR_VAL(url));
}
efree(internal_file);
php_url_free(resource);
return 0;
return false;
}
if (error) {
efree(error);
Expand All @@ -728,7 +728,7 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
efree(internal_file);
php_url_free(resource);
phar_entry_delref(idata);
return 0;
return false;
}
php_url_free(resource);
efree(internal_file);
Expand All @@ -737,7 +737,7 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
php_stream_wrapper_log_error(wrapper, options, "%s", error);
efree(error);
}
return 1;
return true;
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion ext/phar/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void phar_entry_remove(phar_entry_data *idata, char **error);

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);
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);
static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
static bool phar_wrapper_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
static int phar_wrapper_stat(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context);

/* file/stream handlers */
Expand Down
7 changes: 3 additions & 4 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1294,21 +1294,20 @@ PHP_FUNCTION(rename)
/* {{{ Delete a file */
PHP_FUNCTION(unlink)
{
char *filename;
size_t filename_len;
zend_string *filename;
php_stream_wrapper *wrapper;
zval *zcontext = NULL;
php_stream_context *context = NULL;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_PATH(filename, filename_len)
Z_PARAM_PATH_STR(filename)
Z_PARAM_OPTIONAL
Z_PARAM_RESOURCE_OR_NULL(zcontext)
ZEND_PARSE_PARAMETERS_END();

context = php_stream_context_from_zval(zcontext, 0);

wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
wrapper = php_stream_locate_url_wrapper(ZSTR_VAL(filename), NULL, 0);

if (!wrapper || !wrapper->wops) {
php_error_docref(NULL, E_WARNING, "Unable to locate stream wrapper");
Expand Down
12 changes: 6 additions & 6 deletions ext/standard/ftp_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,24 +892,24 @@ static int php_stream_ftp_url_stat(php_stream_wrapper *wrapper, const char *url,
/* }}} */

/* {{{ php_stream_ftp_unlink */
static int php_stream_ftp_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
static bool php_stream_ftp_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context)
{
php_stream *stream = NULL;
php_url *resource = NULL;
int result;
char tmp_line[512];

stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL);
stream = php_ftp_fopen_connect(wrapper, ZSTR_VAL(url), "r", 0, NULL, context, NULL, &resource, NULL, NULL);
if (!stream) {
if (options & REPORT_ERRORS) {
php_error_docref(NULL, E_WARNING, "Unable to connect to %s", url);
php_error_docref(NULL, E_WARNING, "Unable to connect to %s", ZSTR_VAL(url));
}
goto unlink_errexit;
}

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

php_url_free(resource);
php_stream_close(stream);
return 1;
return true;

unlink_errexit:
if (resource) {
Expand All @@ -936,7 +936,7 @@ static int php_stream_ftp_unlink(php_stream_wrapper *wrapper, const char *url, i
if (stream) {
php_stream_close(stream);
}
return 0;
return false;
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion main/php_streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ typedef struct _php_stream_wrapper_ops {
const char *label;

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

/* rename a file */
bool (*rename)(php_stream_wrapper *wrapper, const zend_string *url_from, const zend_string *url_to, int options, php_stream_context *context);
Expand Down
22 changes: 12 additions & 10 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1240,29 +1240,31 @@ static int php_plain_files_url_stater(php_stream_wrapper *wrapper, const char *u
return VCWD_STAT(url, &ssb->sb);
}

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

if (php_check_open_basedir(url)) {
return 0;
if (php_check_open_basedir(ZSTR_VAL(url))) {
return false;
}

size_t url_len = strlen(url);
zend_result ret = VCWD_UNLINK(url, url_len);
zend_result ret = VCWD_UNLINK(url_ptr, url_len);
if (ret == FAILURE) {
if (options & REPORT_ERRORS) {
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
php_error_docref1(NULL, ZSTR_VAL(url), E_WARNING, "%s", strerror(errno));
}
return 0;
return false;
}

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

return 1;
return true;
}

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)
Expand Down
13 changes: 6 additions & 7 deletions main/streams/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct php_user_stream_wrapper {
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);
static int user_wrapper_close(php_stream_wrapper *wrapper, php_stream *stream);
static int user_wrapper_stat_url(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context);
static int user_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
static bool user_wrapper_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
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);
static int user_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url, int mode, int options, php_stream_context *context);
static bool user_wrapper_rmdir(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
Expand Down Expand Up @@ -1029,28 +1029,27 @@ static int php_userstreamop_set_option(php_stream *stream, int option, int value
}


static int user_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
static bool user_wrapper_unlink(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context)
{
struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
zval zfuncname, zretval;
zval args[1];
int call_result;
zval object;
int ret = 0;

/* create an instance of our class */
user_stream_create_object(uwrap, context, &object);
if (Z_TYPE(object) == IS_UNDEF) {
return ret;
return false;
}

/* call the unlink method */
ZVAL_STRING(&args[0], url);
ZVAL_STRINGL(&args[0], ZSTR_VAL(url), ZSTR_LEN(url));

ZVAL_STRING(&zfuncname, USERSTREAM_UNLINK);

call_result = call_method_if_exists(&object, &zfuncname, &zretval, 1, args);
zend_result call_result = call_method_if_exists(&object, &zfuncname, &zretval, 1, args);

bool ret = false;
if (call_result == SUCCESS && (Z_TYPE(zretval) == IS_FALSE || Z_TYPE(zretval) == IS_TRUE)) {
ret = (Z_TYPE(zretval) == IS_TRUE);
} else if (call_result == FAILURE) {
Expand Down
Loading