mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
ext/phar: Use zend_string instead of char* len pair
This commit is contained in:
parent
51bb8cfcb5
commit
d55074ede4
1 changed files with 105 additions and 106 deletions
|
@ -2600,12 +2600,11 @@ PHP_METHOD(Phar, isWritable)
|
||||||
/* {{{ Deletes a named file within the archive. */
|
/* {{{ Deletes a named file within the archive. */
|
||||||
PHP_METHOD(Phar, delete)
|
PHP_METHOD(Phar, delete)
|
||||||
{
|
{
|
||||||
char *fname;
|
zend_string *file_name;
|
||||||
size_t fname_len;
|
|
||||||
char *error;
|
char *error;
|
||||||
phar_entry_info *entry;
|
phar_entry_info *entry;
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2621,7 +2620,7 @@ PHP_METHOD(Phar, delete)
|
||||||
zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
|
zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len))) {
|
if (NULL != (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name))) {
|
||||||
if (entry->is_deleted) {
|
if (entry->is_deleted) {
|
||||||
/* entry is deleted, but has not been flushed to disk yet */
|
/* entry is deleted, but has not been flushed to disk yet */
|
||||||
RETURN_TRUE;
|
RETURN_TRUE;
|
||||||
|
@ -2631,7 +2630,7 @@ PHP_METHOD(Phar, delete)
|
||||||
phar_obj->archive->is_modified = 1;
|
phar_obj->archive->is_modified = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be deleted", fname);
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be deleted", ZSTR_VAL(file_name));
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2679,12 +2678,13 @@ PHP_METHOD(Phar, getPath)
|
||||||
*/
|
*/
|
||||||
PHP_METHOD(Phar, setAlias)
|
PHP_METHOD(Phar, setAlias)
|
||||||
{
|
{
|
||||||
char *alias, *error, *oldalias;
|
zend_string *new_alias = NULL;
|
||||||
|
char *error, *oldalias;
|
||||||
phar_archive_data *fd_ptr;
|
phar_archive_data *fd_ptr;
|
||||||
size_t alias_len, oldalias_len;
|
size_t oldalias_len;
|
||||||
int old_temp, readd = 0;
|
int old_temp, readd = 0;
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &alias, &alias_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &new_alias) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2711,12 +2711,12 @@ PHP_METHOD(Phar, setAlias)
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (alias_len == phar_obj->archive->alias_len && memcmp(phar_obj->archive->alias, alias, alias_len) == 0) {
|
if (zend_string_equals_cstr(new_alias, phar_obj->archive->alias, phar_obj->archive->alias_len)) {
|
||||||
RETURN_TRUE;
|
RETURN_TRUE;
|
||||||
}
|
}
|
||||||
if (alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
|
if (NULL != (fd_ptr = zend_hash_find_ptr(&(PHAR_G(phar_alias_map)), new_alias))) {
|
||||||
spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", alias, fd_ptr->fname);
|
spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", ZSTR_VAL(new_alias), fd_ptr->fname);
|
||||||
if (SUCCESS == phar_free_alias(fd_ptr, alias, alias_len)) {
|
if (SUCCESS == phar_free_alias(fd_ptr, ZSTR_VAL(new_alias), ZSTR_LEN(new_alias))) {
|
||||||
efree(error);
|
efree(error);
|
||||||
goto valid_alias;
|
goto valid_alias;
|
||||||
}
|
}
|
||||||
|
@ -2724,9 +2724,9 @@ PHP_METHOD(Phar, setAlias)
|
||||||
efree(error);
|
efree(error);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
if (!phar_validate_alias(alias, alias_len)) {
|
if (!phar_validate_alias(ZSTR_VAL(new_alias), ZSTR_LEN(new_alias))) {
|
||||||
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
||||||
"Invalid alias \"%s\" specified for phar \"%s\"", alias, phar_obj->archive->fname);
|
"Invalid alias \"%s\" specified for phar \"%s\"", ZSTR_VAL(new_alias), phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
valid_alias:
|
valid_alias:
|
||||||
|
@ -2743,13 +2743,8 @@ valid_alias:
|
||||||
oldalias_len = phar_obj->archive->alias_len;
|
oldalias_len = phar_obj->archive->alias_len;
|
||||||
old_temp = phar_obj->archive->is_temporary_alias;
|
old_temp = phar_obj->archive->is_temporary_alias;
|
||||||
|
|
||||||
if (alias_len) {
|
phar_obj->archive->alias = estrndup(ZSTR_VAL(new_alias), ZSTR_LEN(new_alias));
|
||||||
phar_obj->archive->alias = estrndup(alias, alias_len);
|
phar_obj->archive->alias_len = ZSTR_LEN(new_alias);
|
||||||
} else {
|
|
||||||
phar_obj->archive->alias = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
phar_obj->archive->alias_len = alias_len;
|
|
||||||
phar_obj->archive->is_temporary_alias = 0;
|
phar_obj->archive->is_temporary_alias = 0;
|
||||||
phar_flush(phar_obj->archive, NULL, 0, 0, &error);
|
phar_flush(phar_obj->archive, NULL, 0, 0, &error);
|
||||||
|
|
||||||
|
@ -2765,7 +2760,7 @@ valid_alias:
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, phar_obj->archive);
|
zend_hash_add_ptr(&(PHAR_G(phar_alias_map)), new_alias, phar_obj->archive);
|
||||||
|
|
||||||
if (oldalias) {
|
if (oldalias) {
|
||||||
efree(oldalias);
|
efree(oldalias);
|
||||||
|
@ -3408,13 +3403,13 @@ PHP_METHOD(Phar, decompressFiles)
|
||||||
/* {{{ copy a file internal to the phar archive to another new file within the phar */
|
/* {{{ copy a file internal to the phar archive to another new file within the phar */
|
||||||
PHP_METHOD(Phar, copy)
|
PHP_METHOD(Phar, copy)
|
||||||
{
|
{
|
||||||
char *oldfile, *newfile, *error;
|
char *error;
|
||||||
const char *pcr_error;
|
const char *pcr_error;
|
||||||
size_t oldfile_len, newfile_len;
|
|
||||||
phar_entry_info *oldentry, newentry = {0}, *temp;
|
phar_entry_info *oldentry, newentry = {0}, *temp;
|
||||||
size_t tmp_len = 0;
|
zend_string *new_file = NULL;
|
||||||
|
zend_string *old_file = NULL;
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &oldfile, &oldfile_len, &newfile, &newfile_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "PP", &old_file, &new_file) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3422,43 +3417,43 @@ PHP_METHOD(Phar, copy)
|
||||||
|
|
||||||
if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
|
if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
|
||||||
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
||||||
"Cannot copy \"%s\" to \"%s\", phar is read-only", oldfile, newfile);
|
"Cannot copy \"%s\" to \"%s\", phar is read-only", ZSTR_VAL(old_file), ZSTR_VAL(new_file));
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldfile_len >= sizeof(".phar")-1 && !memcmp(oldfile, ".phar", sizeof(".phar")-1)) {
|
if (zend_string_starts_with_literal(old_file, ".phar")) {
|
||||||
/* can't copy a meta file */
|
/* can't copy a meta file */
|
||||||
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
||||||
"file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
|
"file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newfile_len >= sizeof(".phar")-1 && !memcmp(newfile, ".phar", sizeof(".phar")-1)) {
|
if (zend_string_starts_with_literal(new_file, ".phar")) {
|
||||||
/* can't copy a meta file */
|
/* can't copy a meta file */
|
||||||
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
||||||
"file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
|
"file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (NULL == (oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, oldfile_len)) || oldentry->is_deleted) {
|
if (NULL == (oldentry = zend_hash_find_ptr(&phar_obj->archive->manifest, old_file)) || oldentry->is_deleted) {
|
||||||
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
||||||
"file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", oldfile, newfile, phar_obj->archive->fname);
|
"file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (NULL != (temp = zend_hash_str_find_ptr(&phar_obj->archive->manifest, newfile, newfile_len)) && !temp->is_deleted) {
|
if (NULL != (temp = zend_hash_find_ptr(&phar_obj->archive->manifest, new_file)) && !temp->is_deleted) {
|
||||||
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
||||||
"file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", oldfile, newfile, phar_obj->archive->fname);
|
"file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp_len = newfile_len;
|
size_t tmp_len = ZSTR_LEN(new_file);
|
||||||
if (phar_path_check(&newfile, &tmp_len, &pcr_error) > pcr_is_ok) {
|
char *tmp_new_file = ZSTR_VAL(new_file);
|
||||||
|
if (phar_path_check(&tmp_new_file, &tmp_len, &pcr_error) > pcr_is_ok) {
|
||||||
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
|
||||||
"file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", newfile, pcr_error, oldfile, phar_obj->archive->fname);
|
"file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", ZSTR_VAL(new_file), pcr_error, ZSTR_VAL(old_file), phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
newfile_len = tmp_len;
|
|
||||||
|
|
||||||
if (phar_obj->archive->is_persistent) {
|
if (phar_obj->archive->is_persistent) {
|
||||||
if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
|
if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
|
||||||
|
@ -3466,15 +3461,15 @@ PHP_METHOD(Phar, copy)
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
/* re-populate with copied-on-write entry */
|
/* re-populate with copied-on-write entry */
|
||||||
oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, oldfile_len);
|
oldentry = zend_hash_find_ptr(&phar_obj->archive->manifest, old_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy((void *) &newentry, oldentry, sizeof(phar_entry_info));
|
memcpy((void *) &newentry, oldentry, sizeof(phar_entry_info));
|
||||||
|
|
||||||
phar_metadata_tracker_clone(&newentry.metadata_tracker);
|
phar_metadata_tracker_clone(&newentry.metadata_tracker);
|
||||||
|
|
||||||
newentry.filename = estrndup(newfile, newfile_len);
|
newentry.filename = estrndup(tmp_new_file, tmp_len);
|
||||||
newentry.filename_len = newfile_len;
|
newentry.filename_len = tmp_len;
|
||||||
newentry.fp_refcount = 0;
|
newentry.fp_refcount = 0;
|
||||||
|
|
||||||
if (oldentry->fp_type != PHAR_FP) {
|
if (oldentry->fp_type != PHAR_FP) {
|
||||||
|
@ -3487,7 +3482,7 @@ PHP_METHOD(Phar, copy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
zend_hash_str_add_mem(&oldentry->phar->manifest, newfile, newfile_len, &newentry, sizeof(phar_entry_info));
|
zend_hash_str_add_mem(&oldentry->phar->manifest, ZSTR_VAL(new_file), tmp_len, &newentry, sizeof(phar_entry_info));
|
||||||
phar_obj->archive->is_modified = 1;
|
phar_obj->archive->is_modified = 1;
|
||||||
phar_flush(phar_obj->archive, 0, 0, 0, &error);
|
phar_flush(phar_obj->archive, 0, 0, 0, &error);
|
||||||
|
|
||||||
|
@ -3503,31 +3498,30 @@ PHP_METHOD(Phar, copy)
|
||||||
/* {{{ determines whether a file exists in the phar */
|
/* {{{ determines whether a file exists in the phar */
|
||||||
PHP_METHOD(Phar, offsetExists)
|
PHP_METHOD(Phar, offsetExists)
|
||||||
{
|
{
|
||||||
char *fname;
|
zend_string *file_name;
|
||||||
size_t fname_len;
|
|
||||||
phar_entry_info *entry;
|
phar_entry_info *entry;
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
PHAR_ARCHIVE_OBJECT();
|
PHAR_ARCHIVE_OBJECT();
|
||||||
|
|
||||||
if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, fname_len)) {
|
if (zend_hash_exists(&phar_obj->archive->manifest, file_name)) {
|
||||||
if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len))) {
|
if (NULL != (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name))) {
|
||||||
if (entry->is_deleted) {
|
if (entry->is_deleted) {
|
||||||
/* entry is deleted, but has not been flushed to disk yet */
|
/* entry is deleted, but has not been flushed to disk yet */
|
||||||
RETURN_FALSE;
|
RETURN_FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
|
if (zend_string_starts_with_literal(file_name, ".phar")) {
|
||||||
/* none of these are real files, so they don't exist */
|
/* none of these are real files, so they don't exist */
|
||||||
RETURN_FALSE;
|
RETURN_FALSE;
|
||||||
}
|
}
|
||||||
RETURN_TRUE;
|
RETURN_TRUE;
|
||||||
} else {
|
} else {
|
||||||
RETURN_BOOL(zend_hash_str_exists(&phar_obj->archive->virtual_dirs, fname, fname_len));
|
RETURN_BOOL(zend_hash_exists(&phar_obj->archive->virtual_dirs, file_name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
@ -3535,31 +3529,31 @@ PHP_METHOD(Phar, offsetExists)
|
||||||
/* {{{ get a PharFileInfo object for a specific file */
|
/* {{{ get a PharFileInfo object for a specific file */
|
||||||
PHP_METHOD(Phar, offsetGet)
|
PHP_METHOD(Phar, offsetGet)
|
||||||
{
|
{
|
||||||
char *fname, *error;
|
char *error;
|
||||||
size_t fname_len;
|
|
||||||
phar_entry_info *entry;
|
phar_entry_info *entry;
|
||||||
|
zend_string *file_name = NULL;
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
PHAR_ARCHIVE_OBJECT();
|
PHAR_ARCHIVE_OBJECT();
|
||||||
|
|
||||||
/* security is 0 here so that we can get a better error message than "entry doesn't exist" */
|
/* security is 0 here so that we can get a better error message than "entry doesn't exist" */
|
||||||
if (!(entry = phar_get_entry_info_dir(phar_obj->archive, fname, fname_len, 1, &error, 0))) {
|
if (!(entry = phar_get_entry_info_dir(phar_obj->archive, ZSTR_VAL(file_name), ZSTR_LEN(file_name), 1, &error, 0))) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist%s%s", fname, error?", ":"", error?error:"");
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist%s%s", ZSTR_VAL(file_name), error?", ":"", error?error:"");
|
||||||
} else {
|
} else {
|
||||||
if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
|
if (zend_string_equals_literal(file_name, ".phar/stub.php")) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get stub \".phar/stub.php\" directly in phar \"%s\", use getStub", phar_obj->archive->fname);
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get stub \".phar/stub.php\" directly in phar \"%s\", use getStub", phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
|
if (zend_string_equals_literal(file_name, ".phar/alias.txt")) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get alias \".phar/alias.txt\" directly in phar \"%s\", use getAlias", phar_obj->archive->fname);
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get alias \".phar/alias.txt\" directly in phar \"%s\", use getAlias", phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
|
if (zend_string_starts_with_literal(file_name, ".phar")) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot directly get any files or directories in magic \".phar\" directory");
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot directly get any files or directories in magic \".phar\" directory");
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
@ -3569,7 +3563,7 @@ PHP_METHOD(Phar, offsetGet)
|
||||||
efree(entry);
|
efree(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
zend_string *sfname = strpprintf(0, "phar://%s/%s", phar_obj->archive->fname, fname);
|
zend_string *sfname = strpprintf(0, "phar://%s/%s", phar_obj->archive->fname, ZSTR_VAL(file_name));
|
||||||
zval zfname;
|
zval zfname;
|
||||||
ZVAL_NEW_STR(&zfname, sfname);
|
ZVAL_NEW_STR(&zfname, sfname);
|
||||||
|
|
||||||
|
@ -3584,11 +3578,9 @@ PHP_METHOD(Phar, offsetGet)
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
/* {{{ add a file within the phar archive from a string or resource */
|
/* {{{ add a file within the phar archive from a string or resource */
|
||||||
static void phar_add_file(phar_archive_data **pphar, char *filename, size_t filename_len, char *cont_str, size_t cont_len, zval *zresource)
|
static void phar_add_file(phar_archive_data **pphar, zend_string *file_name, const zend_string *content, zval *zresource)
|
||||||
{
|
{
|
||||||
size_t start_pos=0;
|
|
||||||
char *error;
|
char *error;
|
||||||
size_t contents_len;
|
|
||||||
phar_entry_data *data;
|
phar_entry_data *data;
|
||||||
php_stream *contents_file = NULL;
|
php_stream *contents_file = NULL;
|
||||||
php_stream_statbuf ssb;
|
php_stream_statbuf ssb;
|
||||||
|
@ -3597,14 +3589,21 @@ static void phar_add_file(phar_archive_data **pphar, char *filename, size_t file
|
||||||
ALLOCA_FLAG(filename_use_heap)
|
ALLOCA_FLAG(filename_use_heap)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (filename_len >= sizeof(".phar")-1) {
|
if (
|
||||||
start_pos = '/' == filename[0]; /* account for any leading slash: multiple-leads handled elsewhere */
|
zend_string_starts_with_literal(file_name, ".phar")
|
||||||
if (!memcmp(&filename[start_pos], ".phar", sizeof(".phar")-1) && (filename[start_pos+5] == '/' || filename[start_pos+5] == '\\' || filename[start_pos+5] == '\0')) {
|
|| zend_string_starts_with_literal(file_name, "/.phar")
|
||||||
|
) {
|
||||||
|
size_t prefix_len = (ZSTR_VAL(file_name)[0] == '/') + sizeof(".phar")-1;
|
||||||
|
char next_char = ZSTR_VAL(file_name)[prefix_len];
|
||||||
|
if (next_char == '/' || next_char == '\\' || next_char == '\0') {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory");
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* TODO How to handle Windows path normalisation with zend_string ? */
|
||||||
|
char *filename = ZSTR_VAL(file_name);
|
||||||
|
size_t filename_len = ZSTR_LEN(file_name);
|
||||||
#ifdef PHP_WIN32
|
#ifdef PHP_WIN32
|
||||||
save_filename = filename;
|
save_filename = filename;
|
||||||
if (memchr(filename, '\\', filename_len)) {
|
if (memchr(filename, '\\', filename_len)) {
|
||||||
|
@ -3628,9 +3627,11 @@ static void phar_add_file(phar_archive_data **pphar, char *filename, size_t file
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data->internal_file->is_dir) {
|
if (!data->internal_file->is_dir) {
|
||||||
if (cont_str) {
|
size_t contents_len = 0;
|
||||||
contents_len = php_stream_write(data->fp, cont_str, cont_len);
|
if (content) {
|
||||||
if (contents_len != cont_len) {
|
contents_len = ZSTR_LEN(content);
|
||||||
|
size_t written_len = php_stream_write(data->fp, ZSTR_VAL(content), ZSTR_LEN(content));
|
||||||
|
if (written_len != contents_len) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s could not be written to", filename);
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s could not be written to", filename);
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
@ -3679,17 +3680,17 @@ finish: ;
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
/* {{{ create a directory within the phar archive */
|
/* {{{ create a directory within the phar archive */
|
||||||
static void phar_mkdir(phar_archive_data **pphar, char *dirname, size_t dirname_len)
|
static void phar_mkdir(phar_archive_data **pphar, zend_string *dir_name)
|
||||||
{
|
{
|
||||||
char *error;
|
char *error;
|
||||||
phar_entry_data *data;
|
phar_entry_data *data;
|
||||||
|
|
||||||
if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, dirname, dirname_len, "w+b", 2, &error, 1))) {
|
if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, ZSTR_VAL(dir_name), ZSTR_LEN(dir_name), "w+b", 2, &error, 1))) {
|
||||||
if (error) {
|
if (error) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created: %s", dirname, error);
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created: %s", ZSTR_VAL(dir_name), error);
|
||||||
efree(error);
|
efree(error);
|
||||||
} else {
|
} else {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created", dirname);
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created", ZSTR_VAL(dir_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -3716,12 +3717,12 @@ static void phar_mkdir(phar_archive_data **pphar, char *dirname, size_t dirname_
|
||||||
/* {{{ set the contents of an internal file to those of an external file */
|
/* {{{ set the contents of an internal file to those of an external file */
|
||||||
PHP_METHOD(Phar, offsetSet)
|
PHP_METHOD(Phar, offsetSet)
|
||||||
{
|
{
|
||||||
char *fname, *cont_str = NULL;
|
|
||||||
size_t fname_len, cont_len;
|
|
||||||
zval *zresource = NULL;
|
zval *zresource = NULL;
|
||||||
|
zend_string *file_name = NULL;
|
||||||
|
zend_string *file_content = NULL;
|
||||||
|
|
||||||
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "pr", &fname, &fname_len, &zresource) == FAILURE
|
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "Pr", &file_name, &zresource) == FAILURE
|
||||||
&& zend_parse_parameters(ZEND_NUM_ARGS(), "ps", &fname, &fname_len, &cont_str, &cont_len) == FAILURE) {
|
&& zend_parse_parameters(ZEND_NUM_ARGS(), "PS", &file_name, &file_content) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3732,33 +3733,33 @@ PHP_METHOD(Phar, offsetSet)
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
|
if (zend_string_equals_literal(file_name, ".phar/stub.php")) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set stub \".phar/stub.php\" directly in phar \"%s\", use setStub", phar_obj->archive->fname);
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set stub \".phar/stub.php\" directly in phar \"%s\", use setStub", phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
|
if (zend_string_equals_literal(file_name, ".phar/alias.txt")) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set alias \".phar/alias.txt\" directly in phar \"%s\", use setAlias", phar_obj->archive->fname);
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set alias \".phar/alias.txt\" directly in phar \"%s\", use setAlias", phar_obj->archive->fname);
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
|
if (zend_string_starts_with_literal(file_name, ".phar")) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set any files or directories in magic \".phar\" directory");
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set any files or directories in magic \".phar\" directory");
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
phar_add_file(&(phar_obj->archive), fname, fname_len, cont_str, cont_len, zresource);
|
phar_add_file(&(phar_obj->archive), file_name, file_content, zresource);
|
||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
/* {{{ remove a file from a phar */
|
/* {{{ remove a file from a phar */
|
||||||
PHP_METHOD(Phar, offsetUnset)
|
PHP_METHOD(Phar, offsetUnset)
|
||||||
{
|
{
|
||||||
char *fname, *error;
|
char *error;
|
||||||
size_t fname_len;
|
zend_string *file_name;
|
||||||
phar_entry_info *entry;
|
phar_entry_info *entry;
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3769,8 +3770,8 @@ PHP_METHOD(Phar, offsetUnset)
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, fname_len)) {
|
if (zend_hash_exists(&phar_obj->archive->manifest, file_name)) {
|
||||||
if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len))) {
|
if (NULL != (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name))) {
|
||||||
if (entry->is_deleted) {
|
if (entry->is_deleted) {
|
||||||
/* entry is deleted, but has not been flushed to disk yet */
|
/* entry is deleted, but has not been flushed to disk yet */
|
||||||
return;
|
return;
|
||||||
|
@ -3782,7 +3783,7 @@ PHP_METHOD(Phar, offsetUnset)
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
/* re-populate entry after copy on write */
|
/* re-populate entry after copy on write */
|
||||||
entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len);
|
entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name);
|
||||||
}
|
}
|
||||||
entry->is_modified = 0;
|
entry->is_modified = 0;
|
||||||
entry->is_deleted = 1;
|
entry->is_deleted = 1;
|
||||||
|
@ -3801,55 +3802,53 @@ PHP_METHOD(Phar, offsetUnset)
|
||||||
/* {{{ Adds an empty directory to the phar archive */
|
/* {{{ Adds an empty directory to the phar archive */
|
||||||
PHP_METHOD(Phar, addEmptyDir)
|
PHP_METHOD(Phar, addEmptyDir)
|
||||||
{
|
{
|
||||||
char *dirname;
|
zend_string *dir_name;
|
||||||
size_t dirname_len;
|
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &dirname, &dirname_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &dir_name) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
PHAR_ARCHIVE_OBJECT();
|
PHAR_ARCHIVE_OBJECT();
|
||||||
|
|
||||||
if (dirname_len >= sizeof(".phar")-1 && !memcmp(dirname, ".phar", sizeof(".phar")-1)) {
|
if (zend_string_starts_with_literal(dir_name, ".phar")) {
|
||||||
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create a directory in magic \".phar\" directory");
|
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create a directory in magic \".phar\" directory");
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
phar_mkdir(&phar_obj->archive, dirname, dirname_len);
|
phar_mkdir(&phar_obj->archive, dir_name);
|
||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
/* {{{ Adds a file to the archive using the filename, or the second parameter as the name within the archive */
|
/* {{{ Adds a file to the archive using the filename, or the second parameter as the name within the archive */
|
||||||
PHP_METHOD(Phar, addFile)
|
PHP_METHOD(Phar, addFile)
|
||||||
{
|
{
|
||||||
char *fname, *localname = NULL;
|
zend_string *file_name;
|
||||||
size_t fname_len, localname_len = 0;
|
zend_string *local_name = NULL;
|
||||||
php_stream *resource;
|
php_stream *resource;
|
||||||
zval zresource;
|
zval zresource;
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s!", &fname, &fname_len, &localname, &localname_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|S!", &file_name, &local_name) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
PHAR_ARCHIVE_OBJECT();
|
PHAR_ARCHIVE_OBJECT();
|
||||||
|
|
||||||
if (!strstr(fname, "://") && php_check_open_basedir(fname)) {
|
if (!strstr(ZSTR_VAL(file_name), "://") && php_check_open_basedir(ZSTR_VAL(file_name))) {
|
||||||
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive, open_basedir restrictions prevent this", fname);
|
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive, open_basedir restrictions prevent this", ZSTR_VAL(file_name));
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(resource = php_stream_open_wrapper(fname, "rb", 0, NULL))) {
|
if (!(resource = php_stream_open_wrapper(ZSTR_VAL(file_name), "rb", 0, NULL))) {
|
||||||
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive", fname);
|
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive", ZSTR_VAL(file_name));
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (localname) {
|
if (local_name) {
|
||||||
fname = localname;
|
file_name = local_name;
|
||||||
fname_len = localname_len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
php_stream_to_zval(resource, &zresource);
|
php_stream_to_zval(resource, &zresource);
|
||||||
phar_add_file(&(phar_obj->archive), fname, fname_len, NULL, 0, &zresource);
|
phar_add_file(&(phar_obj->archive), file_name, NULL, &zresource);
|
||||||
zval_ptr_dtor(&zresource);
|
zval_ptr_dtor(&zresource);
|
||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
@ -3857,16 +3856,16 @@ PHP_METHOD(Phar, addFile)
|
||||||
/* {{{ Adds a file to the archive using its contents as a string */
|
/* {{{ Adds a file to the archive using its contents as a string */
|
||||||
PHP_METHOD(Phar, addFromString)
|
PHP_METHOD(Phar, addFromString)
|
||||||
{
|
{
|
||||||
char *localname, *cont_str;
|
zend_string *local_name = NULL;
|
||||||
size_t localname_len, cont_len;
|
zend_string *file_content = NULL;
|
||||||
|
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ps", &localname, &localname_len, &cont_str, &cont_len) == FAILURE) {
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "PS", &local_name, &file_content) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
PHAR_ARCHIVE_OBJECT();
|
PHAR_ARCHIVE_OBJECT();
|
||||||
|
|
||||||
phar_add_file(&(phar_obj->archive), localname, localname_len, cont_str, cont_len, NULL);
|
phar_add_file(&(phar_obj->archive), local_name, file_content, NULL);
|
||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue