Skip to content
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
Next Next commit
streams: Refactor rmdir stream op to use zend_string
  • Loading branch information
Girgias committed Aug 23, 2024
commit 567d8b53ebe0893a413a7fc7fabeb52a2c21aca5
14 changes: 7 additions & 7 deletions ext/phar/dirstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
/**
* Remove a directory within a phar archive
*/
int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) /* {{{ */
bool phar_wrapper_rmdir(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context) /* {{{ */
{
phar_entry_info *entry;
phar_archive_data *phar = NULL;
Expand All @@ -539,8 +539,8 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
uint32_t path_len;

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

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

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

if ((resource = phar_parse_url(wrapper, url, "w", options)) == NULL) {
if ((resource = phar_parse_url(wrapper, ZSTR_VAL(url), "w", options)) == NULL) {
return 0;
}

/* 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);
php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url \"%s\"", ZSTR_VAL(url));
return 0;
}

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);
php_stream_wrapper_log_error(wrapper, options, "phar error: not a phar stream url \"%s\"", ZSTR_VAL(url));
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/phar/dirstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

BEGIN_EXTERN_C()
int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mode, int options, php_stream_context *context);
int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
bool phar_wrapper_rmdir(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);

#ifdef PHAR_DIRSTREAM
php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options);
Expand Down
5 changes: 2 additions & 3 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1160,13 +1160,12 @@ PHP_FUNCTION(mkdir)
/* {{{ Remove a directory */
PHP_FUNCTION(rmdir)
{
char *dir;
size_t dir_len;
zend_string *dir;
zval *zcontext = NULL;
php_stream_context *context;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_PATH(dir, dir_len)
Z_PARAM_PATH_STR(dir)
Z_PARAM_OPTIONAL
Z_PARAM_RESOURCE_OR_NULL(zcontext)
ZEND_PARSE_PARAMETERS_END();
Expand Down
8 changes: 4 additions & 4 deletions ext/standard/ftp_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,24 +1111,24 @@ static int php_stream_ftp_mkdir(php_stream_wrapper *wrapper, const char *url, in
/* }}} */

/* {{{ php_stream_ftp_rmdir */
static int php_stream_ftp_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
static bool php_stream_ftp_rmdir(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 rmdir_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 rmdir_errexit;
}
Expand Down
4 changes: 2 additions & 2 deletions main/php_streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ typedef struct _php_stream_wrapper_ops {

/* Create/Remove directory */
int (*stream_mkdir)(php_stream_wrapper *wrapper, const char *url, int mode, int options, php_stream_context *context);
int (*stream_rmdir)(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
bool (*stream_rmdir)(php_stream_wrapper *wrapper, const zend_string *url, int options, php_stream_context *context);
/* Metadata handling */
int (*stream_metadata)(php_stream_wrapper *wrapper, const char *url, int options, void *value, php_stream_context *context);
} php_stream_wrapper_ops;
Expand Down Expand Up @@ -370,7 +370,7 @@ PHPAPI int _php_stream_stat_path(const char *path, int flags, php_stream_statbuf
PHPAPI int _php_stream_mkdir(const char *path, int mode, int options, php_stream_context *context);
#define php_stream_mkdir(path, mode, options, context) _php_stream_mkdir(path, mode, options, context)

PHPAPI int _php_stream_rmdir(const char *path, int options, php_stream_context *context);
PHPAPI bool _php_stream_rmdir(const zend_string *path, int options, php_stream_context *context);
#define php_stream_rmdir(path, options, context) _php_stream_rmdir(path, options, context)

PHPAPI php_stream *_php_stream_opendir(const char *path, int options, php_stream_context *context STREAMS_DC);
Expand Down
30 changes: 16 additions & 14 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1461,33 +1461,35 @@ static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, i
}
}

static int php_plain_files_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
static bool php_plain_files_rmdir(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(url_ptr)) {
return false;
}

size_t url_len = strlen(url);
#ifdef PHP_WIN32
if (!php_win32_check_trailing_space(url, url_len)) {
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
return 0;
if (!php_win32_check_trailing_space(url_ptr, url_len)) {
php_error_docref1(NULL, ZSTR_VAL(url), E_WARNING, "%s", strerror(ENOENT));
return false;
}
#endif

if (VCWD_RMDIR(url, url_len) < 0) {
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
return 0;
if (VCWD_RMDIR(url_ptr, url_len) < 0) {
php_error_docref1(NULL, ZSTR_VAL(url), E_WARNING, "%s", strerror(errno));
return false;
}

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

return 1;
return true;
}

static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url, int option, void *value, php_stream_context *context)
Expand Down
6 changes: 3 additions & 3 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -2096,13 +2096,13 @@ PHPAPI int _php_stream_mkdir(const char *path, int mode, int options, php_stream
/* }}} */

/* {{{ _php_stream_rmdir */
PHPAPI int _php_stream_rmdir(const char *path, int options, php_stream_context *context)
PHPAPI bool _php_stream_rmdir(const zend_string *path, int options, php_stream_context *context)
{
php_stream_wrapper *wrapper = NULL;

wrapper = php_stream_locate_url_wrapper(path, NULL, 0);
wrapper = php_stream_locate_url_wrapper(ZSTR_VAL(path), NULL, 0);
if (!wrapper || !wrapper->wops || !wrapper->wops->stream_rmdir) {
return 0;
return false;
}

return wrapper->wops->stream_rmdir(wrapper, path, options, context);
Expand Down
6 changes: 3 additions & 3 deletions main/streams/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static int user_wrapper_stat_url(php_stream_wrapper *wrapper, const char *url, i
static int user_wrapper_unlink(php_stream_wrapper *wrapper, const char *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 int user_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, 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);
static int user_wrapper_metadata(php_stream_wrapper *wrapper, const char *url, int option, void *value, php_stream_context *context);
static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, const char *filename, const char *mode,
int options, zend_string **opened_path, php_stream_context *context STREAMS_DC);
Expand Down Expand Up @@ -1151,7 +1151,7 @@ static int user_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url, int
return ret;
}

static int user_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url,
static bool user_wrapper_rmdir(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;
Expand All @@ -1168,7 +1168,7 @@ static int user_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url,
}

/* call the rmdir method */
ZVAL_STRING(&args[0], url);
ZVAL_STRINGL(&args[0], ZSTR_VAL(url), ZSTR_LEN(url));
ZVAL_LONG(&args[1], options);

ZVAL_STRING(&zfuncname, USERSTREAM_RMDIR);
Expand Down