1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
|
/*
+----------------------------------------------------------------------+
| phar:// stream wrapper support |
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Gregory Beaver <cellog@php.net> |
| Marcus Boerger <helly@php.net> |
+----------------------------------------------------------------------+
*/
#define PHAR_STREAM 1
#include "phar_internal.h"
#include "stream.h"
#include "dirstream.h"
static const php_stream_ops phar_ops = {
phar_stream_write, /* write */
phar_stream_read, /* read */
phar_stream_close, /* close */
phar_stream_flush, /* flush */
"phar stream",
phar_stream_seek, /* seek */
NULL, /* cast */
phar_stream_stat, /* stat */
NULL, /* set option */
};
static const php_stream_wrapper_ops phar_stream_wops = {
phar_wrapper_open_url,
NULL, /* phar_wrapper_close */
NULL, /* phar_wrapper_stat, */
phar_wrapper_stat, /* stat_url */
phar_wrapper_open_dir, /* opendir */
"phar",
phar_wrapper_unlink, /* unlink */
phar_wrapper_rename, /* rename */
phar_wrapper_mkdir, /* create directory */
phar_wrapper_rmdir, /* remove directory */
NULL
};
const php_stream_wrapper php_stream_phar_wrapper = {
&phar_stream_wops,
NULL,
0 /* is_url */
};
/**
* Open a phar file for streams API
*/
php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options) /* {{{ */
{
php_url *resource;
char *arch = NULL, *entry = NULL, *error;
size_t arch_len, entry_len;
if (strncasecmp(filename, "phar://", 7)) {
return NULL;
}
if (mode[0] == 'a') {
if (!(options & PHP_STREAM_URL_STAT_QUIET)) {
php_stream_wrapper_log_error(wrapper, options, "phar error: open mode append not supported");
}
return NULL;
}
if (phar_split_fname(filename, strlen(filename), &arch, &arch_len, &entry, &entry_len, 2, (mode[0] == 'w' ? 2 : 0)) == FAILURE) {
if (!(options & PHP_STREAM_URL_STAT_QUIET)) {
if (arch && !entry) {
php_stream_wrapper_log_error(wrapper, options, "phar error: no directory in \"%s\", must have at least phar://%s/ for root directory (always use full path to a new phar)", filename, arch);
arch = NULL;
} else {
php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url or non-existent phar \"%s\"", filename);
}
}
return NULL;
}
resource = ecalloc(1, sizeof(php_url));
resource->scheme = ZSTR_INIT_LITERAL("phar", 0);
resource->host = zend_string_init(arch, arch_len, 0);
efree(arch);
resource->path = zend_string_init(entry, entry_len, 0);
efree(entry);
#ifdef MBO_0
if (resource) {
fprintf(stderr, "Alias: %s\n", alias);
fprintf(stderr, "Scheme: %s\n", ZSTR_VAL(resource->scheme));
/* fprintf(stderr, "User: %s\n", resource->user);*/
/* fprintf(stderr, "Pass: %s\n", resource->pass ? "***" : NULL);*/
fprintf(stderr, "Host: %s\n", ZSTR_VAL(resource->host));
/* fprintf(stderr, "Port: %d\n", resource->port);*/
fprintf(stderr, "Path: %s\n", ZSTR_VAL(resource->path));
/* fprintf(stderr, "Query: %s\n", resource->query);*/
/* fprintf(stderr, "Fragment: %s\n", resource->fragment);*/
}
#endif
if (mode[0] == 'w' || (mode[0] == 'r' && mode[1] == '+')) {
phar_archive_data *pphar = NULL, *phar;
if (PHAR_G(request_init) && HT_IS_INITIALIZED(&PHAR_G(phar_fname_map)) && NULL == (pphar = zend_hash_find_ptr(&(PHAR_G(phar_fname_map)), resource->host))) {
pphar = NULL;
}
if (PHAR_G(readonly) && (!pphar || !pphar->is_data)) {
if (!(options & PHP_STREAM_URL_STAT_QUIET)) {
php_stream_wrapper_log_error(wrapper, options, "phar error: write operations disabled by the php.ini setting phar.readonly");
}
php_url_free(resource);
return NULL;
}
if (phar_open_or_create_filename(ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, 0, options, &phar, &error) == FAILURE)
{
if (error) {
if (!(options & PHP_STREAM_URL_STAT_QUIET)) {
php_stream_wrapper_log_error(wrapper, options, "%s", error);
}
efree(error);
}
php_url_free(resource);
return NULL;
}
if (phar->is_persistent && FAILURE == phar_copy_on_write(&phar)) {
if (error) {
spprintf(&error, 0, "Cannot open cached phar '%s' as writeable, copy on write failed", ZSTR_VAL(resource->host));
if (!(options & PHP_STREAM_URL_STAT_QUIET)) {
php_stream_wrapper_log_error(wrapper, options, "%s", error);
}
efree(error);
}
php_url_free(resource);
return NULL;
}
} else {
if (phar_open_from_filename(ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, options, NULL, &error) == FAILURE)
{
if (error) {
if (!(options & PHP_STREAM_URL_STAT_QUIET)) {
php_stream_wrapper_log_error(wrapper, options, "%s", error);
}
efree(error);
}
php_url_free(resource);
return NULL;
}
}
return resource;
}
/* }}} */
/**
* used for fopen('phar://...') and company
*/
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) /* {{{ */
{
phar_archive_data *phar;
phar_entry_data *idata;
char *internal_file;
char *error;
HashTable *pharcontext;
php_url *resource = NULL;
php_stream *fpf;
zval *pzoption, *metadata;
if ((resource = phar_parse_url(wrapper, path, mode, options)) == NULL) {
return NULL;
}
/* 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\"", path);
return NULL;
}
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\"", path);
return NULL;
}
size_t host_len = ZSTR_LEN(resource->host);
phar_request_initialize();
/* strip leading "/" */
internal_file = estrndup(ZSTR_VAL(resource->path) + 1, ZSTR_LEN(resource->path) - 1);
if (mode[0] == 'w' || (mode[0] == 'r' && mode[1] == '+')) {
if (NULL == (idata = phar_get_or_create_entry_data(ZSTR_VAL(resource->host), host_len, internal_file, strlen(internal_file), mode, 0, &error, 1))) {
if (error) {
php_stream_wrapper_log_error(wrapper, options, "%s", error);
efree(error);
} else {
php_stream_wrapper_log_error(wrapper, options, "phar error: file \"%s\" could not be created in phar \"%s\"", internal_file, ZSTR_VAL(resource->host));
}
efree(internal_file);
php_url_free(resource);
return NULL;
}
if (error) {
efree(error);
}
fpf = php_stream_alloc(&phar_ops, idata, NULL, mode);
php_url_free(resource);
efree(internal_file);
if (context && Z_TYPE(context->options) != IS_UNDEF && (pzoption = zend_hash_str_find(HASH_OF(&context->options), "phar", sizeof("phar")-1)) != NULL) {
pharcontext = HASH_OF(pzoption);
if (idata->internal_file->uncompressed_filesize == 0
&& idata->internal_file->compressed_filesize == 0
&& (pzoption = zend_hash_str_find(pharcontext, "compress", sizeof("compress")-1)) != NULL
&& Z_TYPE_P(pzoption) == IS_LONG
&& (Z_LVAL_P(pzoption) & ~PHAR_ENT_COMPRESSION_MASK) == 0
) {
idata->internal_file->flags &= ~PHAR_ENT_COMPRESSION_MASK;
idata->internal_file->flags |= Z_LVAL_P(pzoption);
}
if ((pzoption = zend_hash_str_find(pharcontext, "metadata", sizeof("metadata")-1)) != NULL) {
phar_metadata_tracker_free(&idata->internal_file->metadata_tracker, idata->internal_file->is_persistent);
metadata = pzoption;
ZVAL_COPY_DEREF(&idata->internal_file->metadata_tracker.val, metadata);
idata->phar->is_modified = 1;
}
}
if (opened_path) {
*opened_path = strpprintf(MAXPATHLEN, "phar://%s/%s", idata->phar->fname, idata->internal_file->filename);
}
return fpf;
} else {
if (!*internal_file && (options & STREAM_OPEN_FOR_INCLUDE)) {
/* retrieve the stub */
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), host_len, NULL, 0, NULL)) {
php_stream_wrapper_log_error(wrapper, options, "file %s is not a valid phar archive", ZSTR_VAL(resource->host));
efree(internal_file);
php_url_free(resource);
return NULL;
}
if (phar->is_tar || phar->is_zip) {
if ((FAILURE == phar_get_entry_data(&idata, ZSTR_VAL(resource->host), host_len, ".phar/stub.php", sizeof(".phar/stub.php")-1, "r", 0, &error, 0)) || !idata) {
goto idata_error;
}
efree(internal_file);
if (opened_path) {
*opened_path = strpprintf(MAXPATHLEN, "%s", phar->fname);
}
php_url_free(resource);
goto phar_stub;
} else {
php_stream *stream = phar_get_pharfp(phar);
if (stream == NULL) {
if (UNEXPECTED(FAILURE == phar_open_archive_fp(phar))) {
php_stream_wrapper_log_error(wrapper, options, "phar error: could not reopen phar \"%s\"", ZSTR_VAL(resource->host));
efree(internal_file);
php_url_free(resource);
return NULL;
}
stream = phar_get_pharfp(phar);
}
phar_entry_info *entry;
entry = (phar_entry_info *) ecalloc(1, sizeof(phar_entry_info));
entry->is_temp_dir = 1;
entry->filename = estrndup("", 0);
entry->filename_len = 0;
entry->phar = phar;
entry->offset = entry->offset_abs = 0;
entry->compressed_filesize = entry->uncompressed_filesize = phar->halt_offset;
entry->is_crc_checked = 1;
idata = (phar_entry_data *) ecalloc(1, sizeof(phar_entry_data));
idata->fp = stream;
idata->phar = phar;
idata->internal_file = entry;
if (!phar->is_persistent) {
++(entry->phar->refcount);
}
++(entry->fp_refcount);
php_url_free(resource);
if (opened_path) {
*opened_path = strpprintf(MAXPATHLEN, "%s", phar->fname);
}
efree(internal_file);
goto phar_stub;
}
}
/* read-only access is allowed to magic files in .phar directory */
if ((FAILURE == phar_get_entry_data(&idata, ZSTR_VAL(resource->host), host_len, internal_file, strlen(internal_file), "r", 0, &error, 0)) || !idata) {
idata_error:
if (error) {
php_stream_wrapper_log_error(wrapper, options, "%s", error);
efree(error);
} else {
php_stream_wrapper_log_error(wrapper, options, "phar error: \"%s\" is not a file in phar \"%s\"", internal_file, ZSTR_VAL(resource->host));
}
efree(internal_file);
php_url_free(resource);
return NULL;
}
}
php_url_free(resource);
#ifdef MBO_0
fprintf(stderr, "Pharname: %s\n", idata->phar->filename);
fprintf(stderr, "Filename: %s\n", internal_file);
fprintf(stderr, "Entry: %s\n", idata->internal_file->filename);
fprintf(stderr, "Size: %u\n", idata->internal_file->uncompressed_filesize);
fprintf(stderr, "Compressed: %u\n", idata->internal_file->flags);
fprintf(stderr, "Offset: %u\n", idata->internal_file->offset_within_phar);
fprintf(stderr, "Cached: %s\n", idata->internal_file->filedata ? "yes" : "no");
#endif
/* check length, crc32 */
if (!idata->internal_file->is_crc_checked && phar_postprocess_file(idata, idata->internal_file->crc32, &error, 2) != SUCCESS) {
php_stream_wrapper_log_error(wrapper, options, "%s", error);
efree(error);
phar_entry_delref(idata);
efree(internal_file);
return NULL;
}
if (!PHAR_G(cwd_init) && (options & STREAM_OPEN_FOR_INCLUDE)) {
char *entry = idata->internal_file->filename, *cwd;
PHAR_G(cwd_init) = 1;
if ((idata->phar->is_tar || idata->phar->is_zip) && idata->internal_file->filename_len == sizeof(".phar/stub.php")-1 && !strncmp(idata->internal_file->filename, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
/* we're executing the stub, which doesn't count as a file */
PHAR_G(cwd_init) = 0;
} else if ((cwd = strrchr(entry, '/'))) {
PHAR_G(cwd_len) = cwd - entry;
PHAR_G(cwd) = estrndup(entry, PHAR_G(cwd_len));
} else {
/* root directory */
PHAR_G(cwd_len) = 0;
PHAR_G(cwd) = NULL;
}
}
if (opened_path) {
*opened_path = strpprintf(MAXPATHLEN, "phar://%s/%s", idata->phar->fname, idata->internal_file->filename);
}
efree(internal_file);
phar_stub:
fpf = php_stream_alloc(&phar_ops, idata, NULL, mode);
return fpf;
}
/* }}} */
/**
* Used for fclose($fp) where $fp is a phar archive
*/
static int phar_stream_close(php_stream *stream, int close_handle) /* {{{ */
{
/* for some reasons phar needs to be flushed even if there is no write going on */
phar_stream_flush(stream);
phar_entry_delref((phar_entry_data *)stream->abstract);
return 0;
}
/* }}} */
/**
* used for fread($fp) and company on a fopen()ed phar file handle
*/
static ssize_t phar_stream_read(php_stream *stream, char *buf, size_t count) /* {{{ */
{
phar_entry_data *data = (phar_entry_data *)stream->abstract;
ssize_t got;
phar_entry_info *entry;
if (data->internal_file->link) {
entry = phar_get_link_source(data->internal_file);
} else {
entry = data->internal_file;
}
if (entry->is_deleted) {
stream->eof = 1;
return -1;
}
/* use our proxy position */
php_stream_seek(data->fp, data->position + data->zero, SEEK_SET);
got = php_stream_read(data->fp, buf, MIN(count, (size_t)(entry->uncompressed_filesize - data->position)));
data->position = php_stream_tell(data->fp) - data->zero;
stream->eof = (data->position == (zend_off_t) entry->uncompressed_filesize);
return got;
}
/* }}} */
/**
* Used for fseek($fp) on a phar file handle
*/
static int phar_stream_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset) /* {{{ */
{
phar_entry_data *data = (phar_entry_data *)stream->abstract;
phar_entry_info *entry;
int res;
zend_ulong temp;
if (data->internal_file->link) {
entry = phar_get_link_source(data->internal_file);
} else {
entry = data->internal_file;
}
switch (whence) {
case SEEK_END :
temp = (zend_ulong) data->zero + (zend_ulong) entry->uncompressed_filesize + (zend_ulong) offset;
break;
case SEEK_CUR :
temp = (zend_ulong) data->zero + (zend_ulong) data->position + (zend_ulong) offset;
break;
case SEEK_SET :
temp = (zend_ulong) data->zero + (zend_ulong) offset;
break;
default:
temp = 0;
}
zend_off_t temp_signed = (zend_off_t) temp;
if (temp_signed > data->zero + (zend_off_t) entry->uncompressed_filesize) {
*newoffset = -1; /* FIXME: this will invalidate the ZEND_ASSERT(stream->position >= 0); assertion in streams.c */
return -1;
}
if (temp_signed < data->zero) {
*newoffset = -1; /* FIXME: this will invalidate the ZEND_ASSERT(stream->position >= 0); assertion in streams.c */
return -1;
}
res = php_stream_seek(data->fp, temp_signed, SEEK_SET);
*newoffset = php_stream_tell(data->fp) - data->zero;
data->position = *newoffset;
return res;
}
/* }}} */
/**
* Used for writing to a phar file
*/
static ssize_t phar_stream_write(php_stream *stream, const char *buf, size_t count) /* {{{ */
{
phar_entry_data *data = (phar_entry_data *) stream->abstract;
php_stream_seek(data->fp, data->position + data->zero, SEEK_SET);
if (count != php_stream_write(data->fp, buf, count)) {
php_stream_wrapper_log_error(stream->wrapper, stream->flags, "phar error: Could not write %d characters to \"%s\" in phar \"%s\"", (int) count, data->internal_file->filename, data->phar->fname);
return -1;
}
data->position = php_stream_tell(data->fp) - data->zero;
if (data->position > (zend_off_t)data->internal_file->uncompressed_filesize) {
data->internal_file->uncompressed_filesize = data->position;
}
data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize;
data->internal_file->old_flags = data->internal_file->flags;
data->internal_file->is_modified = 1;
return count;
}
/* }}} */
/**
* Used to save work done on a writeable phar
*/
static int phar_stream_flush(php_stream *stream) /* {{{ */
{
char *error;
phar_entry_data *data = (phar_entry_data *) stream->abstract;
if (data->internal_file->is_modified) {
data->internal_file->timestamp = time(0);
phar_flush(data->phar, &error);
if (error) {
php_stream_wrapper_log_error(stream->wrapper, REPORT_ERRORS, "%s", error);
efree(error);
}
return EOF;
} else {
return EOF;
}
}
/* }}} */
/* {{{ phar_dostat */
/**
* stat an opened phar file handle stream, used by phar_stat()
*/
void phar_dostat(phar_archive_data *phar, phar_entry_info *data, php_stream_statbuf *ssb, bool is_temp_dir)
{
memset(ssb, 0, sizeof(php_stream_statbuf));
if (!is_temp_dir && !data->is_dir) {
ssb->sb.st_size = data->uncompressed_filesize;
ssb->sb.st_mode = data->flags & PHAR_ENT_PERM_MASK;
ssb->sb.st_mode |= S_IFREG; /* regular file */
/* timestamp is just the timestamp when this was added to the phar */
ssb->sb.st_mtime = data->timestamp;
ssb->sb.st_atime = data->timestamp;
ssb->sb.st_ctime = data->timestamp;
} else if (!is_temp_dir && data->is_dir) {
ssb->sb.st_size = 0;
ssb->sb.st_mode = data->flags & PHAR_ENT_PERM_MASK;
ssb->sb.st_mode |= S_IFDIR; /* regular directory */
/* timestamp is just the timestamp when this was added to the phar */
ssb->sb.st_mtime = data->timestamp;
ssb->sb.st_atime = data->timestamp;
ssb->sb.st_ctime = data->timestamp;
} else {
ssb->sb.st_size = 0;
ssb->sb.st_mode = 0777;
ssb->sb.st_mode |= S_IFDIR; /* regular directory */
ssb->sb.st_mtime = phar->max_timestamp;
ssb->sb.st_atime = phar->max_timestamp;
ssb->sb.st_ctime = phar->max_timestamp;
}
if (!phar->is_writeable) {
ssb->sb.st_mode = (ssb->sb.st_mode & 0555) | (ssb->sb.st_mode & ~0777);
}
ssb->sb.st_nlink = 1;
ssb->sb.st_rdev = -1;
/* this is only for APC, so use /dev/null device - no chance of conflict there! */
ssb->sb.st_dev = 0xc;
/* generate unique inode number for alias/filename, so no phars will conflict */
if (!is_temp_dir) {
ssb->sb.st_ino = data->inode;
}
#ifndef PHP_WIN32
ssb->sb.st_blksize = -1;
ssb->sb.st_blocks = -1;
#endif
}
/* }}}*/
/**
* Stat an opened phar file handle
*/
static int phar_stream_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
{
phar_entry_data *data = (phar_entry_data *)stream->abstract;
/* If ssb is NULL then someone is misbehaving */
if (!ssb) {
return -1;
}
phar_dostat(data->phar, data->internal_file, ssb, 0);
return 0;
}
/* }}} */
/**
* Stream wrapper stat implementation of stat()
*/
static int phar_wrapper_stat(php_stream_wrapper *wrapper, const char *url, int flags,
php_stream_statbuf *ssb, php_stream_context *context) /* {{{ */
{
php_url *resource = NULL;
char *internal_file, *error;
phar_archive_data *phar;
phar_entry_info *entry;
size_t internal_file_len;
if ((resource = phar_parse_url(wrapper, url, "r", flags|PHP_STREAM_URL_STAT_QUIET)) == NULL) {
return FAILURE;
}
/* we must have at the very least phar://alias.phar/internalfile.php */
if (!resource->scheme || !resource->host || !resource->path) {
php_url_free(resource);
return FAILURE;
}
if (!zend_string_equals_literal_ci(resource->scheme, "phar")) {
php_url_free(resource);
return FAILURE;
}
size_t host_len = ZSTR_LEN(resource->host);
phar_request_initialize();
internal_file = ZSTR_VAL(resource->path) + 1; /* strip leading "/" */
/* find the phar in our trusty global hash indexed by alias (host of phar://blah.phar/file.whatever) */
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), host_len, NULL, 0, &error)) {
php_url_free(resource);
if (error) {
efree(error);
}
return FAILURE;
}
if (error) {
efree(error);
}
if (*internal_file == '\0') {
/* root directory requested */
phar_dostat(phar, NULL, ssb, 1);
php_url_free(resource);
return SUCCESS;
}
if (!HT_IS_INITIALIZED(&phar->manifest)) {
php_url_free(resource);
return FAILURE;
}
internal_file_len = strlen(internal_file);
/* search through the manifest of files, and if we have an exact match, it's a file */
if (NULL != (entry = zend_hash_str_find_ptr(&phar->manifest, internal_file, internal_file_len))) {
phar_dostat(phar, entry, ssb, 0);
php_url_free(resource);
return SUCCESS;
}
if (zend_hash_str_exists(&(phar->virtual_dirs), internal_file, internal_file_len)) {
phar_dostat(phar, NULL, ssb, 1);
php_url_free(resource);
return SUCCESS;
}
/* check for mounted directories */
if (HT_IS_INITIALIZED(&phar->mounted_dirs) && zend_hash_num_elements(&phar->mounted_dirs)) {
zend_string *str_key;
ZEND_HASH_MAP_FOREACH_STR_KEY(&phar->mounted_dirs, str_key) {
if (ZSTR_LEN(str_key) >= internal_file_len || strncmp(ZSTR_VAL(str_key), internal_file, ZSTR_LEN(str_key))) {
continue;
} else {
char *test;
size_t test_len;
php_stream_statbuf ssbi;
if (NULL == (entry = zend_hash_find_ptr(&phar->manifest, str_key))) {
goto free_resource;
}
if (!entry->tmp || !entry->is_mounted) {
goto free_resource;
}
test_len = spprintf(&test, MAXPATHLEN, "%s%s", entry->tmp, internal_file + ZSTR_LEN(str_key));
if (SUCCESS != php_stream_stat_path(test, &ssbi)) {
efree(test);
continue;
}
/* mount the file/directory just in time */
if (SUCCESS != phar_mount_entry(phar, test, test_len, internal_file, internal_file_len)) {
efree(test);
goto free_resource;
}
efree(test);
if (NULL == (entry = zend_hash_str_find_ptr(&phar->manifest, internal_file, internal_file_len))) {
goto free_resource;
}
phar_dostat(phar, entry, ssb, 0);
php_url_free(resource);
return SUCCESS;
}
} ZEND_HASH_FOREACH_END();
}
free_resource:
php_url_free(resource);
return FAILURE;
}
/* }}} */
/**
* 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) /* {{{ */
{
php_url *resource;
char *internal_file, *error;
int internal_file_len;
phar_entry_data *idata;
phar_archive_data *pphar;
if ((resource = phar_parse_url(wrapper, url, "rb", options)) == NULL) {
php_stream_wrapper_log_error(wrapper, options, "phar error: unlink failed");
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);
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);
return 0;
}
size_t host_len = ZSTR_LEN(resource->host);
phar_request_initialize();
pphar = zend_hash_find_ptr(&(PHAR_G(phar_fname_map)), resource->host);
if (PHAR_G(readonly) && (!pphar || !pphar->is_data)) {
php_url_free(resource);
php_stream_wrapper_log_error(wrapper, options, "phar error: write operations disabled by the php.ini setting phar.readonly");
return 0;
}
/* need to copy to strip leading "/", will get touched again */
internal_file = estrndup(ZSTR_VAL(resource->path) + 1, ZSTR_LEN(resource->path) - 1);
internal_file_len = ZSTR_LEN(resource->path) - 1;
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);
efree(error);
} else {
php_stream_wrapper_log_error(wrapper, options, "unlink of \"%s\" failed, file does not exist", url);
}
efree(internal_file);
php_url_free(resource);
return 0;
}
if (error) {
efree(error);
}
if (idata->internal_file->fp_refcount > 1) {
/* more than just our fp resource is open for this file */
php_stream_wrapper_log_error(wrapper, options, "phar error: \"%s\" in phar \"%s\", has open file pointers, cannot unlink", internal_file, ZSTR_VAL(resource->host));
efree(internal_file);
php_url_free(resource);
phar_entry_delref(idata);
return 0;
}
php_url_free(resource);
efree(internal_file);
phar_entry_remove(idata, &error);
if (error) {
php_stream_wrapper_log_error(wrapper, options, "%s", error);
efree(error);
}
return 1;
}
/* }}} */
static int phar_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context) /* {{{ */
{
php_url *resource_from, *resource_to;
char *error;
phar_archive_data *phar, *pfrom, *pto;
phar_entry_info *entry;
int is_dir = 0;
int is_modified = 0;
error = NULL;
if ((resource_from = phar_parse_url(wrapper, url_from, "wb", options|PHP_STREAM_URL_STAT_QUIET)) == NULL) {
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": invalid or non-writable url \"%s\"", url_from, url_to, url_from);
return 0;
}
if (SUCCESS != phar_get_archive(&pfrom, ZSTR_VAL(resource_from->host), ZSTR_LEN(resource_from->host), NULL, 0, &error)) {
pfrom = NULL;
if (error) {
efree(error);
}
}
if (PHAR_G(readonly) && (!pfrom || !pfrom->is_data)) {
php_url_free(resource_from);
php_error_docref(NULL, E_WARNING, "phar error: Write operations disabled by the php.ini setting phar.readonly");
return 0;
}
if ((resource_to = phar_parse_url(wrapper, url_to, "wb", options|PHP_STREAM_URL_STAT_QUIET)) == NULL) {
php_url_free(resource_from);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": invalid or non-writable url \"%s\"", url_from, url_to, url_to);
return 0;
}
if (SUCCESS != phar_get_archive(&pto, ZSTR_VAL(resource_to->host), ZSTR_LEN(resource_to->host), NULL, 0, &error)) {
if (error) {
efree(error);
}
pto = NULL;
}
if (PHAR_G(readonly) && (!pto || !pto->is_data)) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: Write operations disabled by the php.ini setting phar.readonly");
return 0;
}
if (!zend_string_equals(resource_from->host, resource_to->host)) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\", not within the same phar archive", url_from, url_to);
return 0;
}
/* we must have at the very least phar://alias.phar/internalfile.php */
if (!resource_from->scheme || !resource_from->host || !resource_from->path) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": invalid url \"%s\"", url_from, url_to, url_from);
return 0;
}
if (!resource_to->scheme || !resource_to->host || !resource_to->path) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": invalid url \"%s\"", url_from, url_to, url_to);
return 0;
}
if (!zend_string_equals_literal_ci(resource_from->scheme, "phar")) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": not a phar stream url \"%s\"", url_from, url_to, url_from);
return 0;
}
if (!zend_string_equals_literal_ci(resource_to->scheme, "phar")) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": not a phar stream url \"%s\"", url_from, url_to, url_to);
return 0;
}
size_t host_len = ZSTR_LEN(resource_from->host);
if (SUCCESS != phar_get_archive(&phar, ZSTR_VAL(resource_from->host), host_len, NULL, 0, &error)) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": %s", url_from, url_to, error);
efree(error);
return 0;
}
if (phar->is_persistent && FAILURE == phar_copy_on_write(&phar)) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": could not make cached phar writeable", url_from, url_to);
return 0;
}
if (NULL != (entry = zend_hash_str_find_ptr(&(phar->manifest), ZSTR_VAL(resource_from->path)+1, ZSTR_LEN(resource_from->path)-1))) {
phar_entry_info new, *source;
/* perform rename magic */
if (entry->is_deleted) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\" from extracted phar archive, source has been deleted", url_from, url_to);
return 0;
}
/* transfer all data over to the new entry */
memcpy((void *) &new, (void *) entry, sizeof(phar_entry_info));
/* mark the old one for deletion */
entry->is_deleted = 1;
entry->fp = NULL;
ZVAL_UNDEF(&entry->metadata_tracker.val);
entry->link = entry->tmp = NULL;
source = entry;
/* add to the manifest, and then store the pointer to the new guy in entry
* if it already exists, we overwrite the destination like what copy('phar://...', 'phar://...') does. */
entry = zend_hash_str_update_mem(&(phar->manifest), ZSTR_VAL(resource_to->path)+1, ZSTR_LEN(resource_to->path)-1, (void **)&new, sizeof(phar_entry_info));
entry->filename = estrndup(ZSTR_VAL(resource_to->path)+1, ZSTR_LEN(resource_to->path)-1);
if (FAILURE == phar_copy_entry_fp(source, entry, &error)) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": %s", url_from, url_to, error);
efree(error);
zend_hash_str_del(&(phar->manifest), entry->filename, strlen(entry->filename));
return 0;
}
is_modified = 1;
entry->is_modified = 1;
entry->filename_len = strlen(entry->filename);
is_dir = entry->is_dir;
} else {
is_dir = zend_hash_str_exists(&(phar->virtual_dirs), ZSTR_VAL(resource_from->path)+1, ZSTR_LEN(resource_from->path)-1);
if (!is_dir) {
/* file does not exist */
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\" from extracted phar archive, source does not exist", url_from, url_to);
return 0;
}
}
/* Rename directory. Update all nested paths */
if (is_dir) {
Bucket *b;
zend_string *str_key;
zend_string *new_str_key;
size_t from_len = ZSTR_LEN(resource_from->path) - 1;
size_t to_len = ZSTR_LEN(resource_to->path) - 1;
ZEND_HASH_MAP_FOREACH_BUCKET(&phar->manifest, b) {
str_key = b->key;
entry = Z_PTR(b->val);
if (!entry->is_deleted &&
ZSTR_LEN(str_key) > from_len &&
memcmp(ZSTR_VAL(str_key), ZSTR_VAL(resource_from->path)+1, from_len) == 0 &&
IS_SLASH(ZSTR_VAL(str_key)[from_len])) {
new_str_key = zend_string_alloc(ZSTR_LEN(str_key) + to_len - from_len, 0);
memcpy(ZSTR_VAL(new_str_key), ZSTR_VAL(resource_to->path) + 1, to_len);
memcpy(ZSTR_VAL(new_str_key) + to_len, ZSTR_VAL(str_key) + from_len, ZSTR_LEN(str_key) - from_len);
ZSTR_VAL(new_str_key)[ZSTR_LEN(new_str_key)] = 0;
is_modified = 1;
entry->is_modified = 1;
efree(entry->filename);
// TODO: avoid reallocation (make entry->filename zend_string*)
entry->filename = estrndup(ZSTR_VAL(new_str_key), ZSTR_LEN(new_str_key));
entry->filename_len = ZSTR_LEN(new_str_key);
zend_string_release_ex(str_key, 0);
b->h = zend_string_hash_val(new_str_key);
b->key = new_str_key;
}
} ZEND_HASH_FOREACH_END();
zend_hash_rehash(&phar->manifest);
ZEND_HASH_MAP_FOREACH_BUCKET(&phar->virtual_dirs, b) {
str_key = b->key;
if (zend_string_starts_with_cstr(str_key, ZSTR_VAL(resource_from->path)+1, from_len) &&
(ZSTR_LEN(str_key) == from_len || IS_SLASH(ZSTR_VAL(str_key)[from_len]))) {
new_str_key = zend_string_alloc(ZSTR_LEN(str_key) + to_len - from_len, 0);
memcpy(ZSTR_VAL(new_str_key), ZSTR_VAL(resource_to->path) + 1, to_len);
memcpy(ZSTR_VAL(new_str_key) + to_len, ZSTR_VAL(str_key) + from_len, ZSTR_LEN(str_key) - from_len);
ZSTR_VAL(new_str_key)[ZSTR_LEN(new_str_key)] = 0;
zend_string_release_ex(str_key, 0);
b->h = zend_string_hash_val(new_str_key);
b->key = new_str_key;
}
} ZEND_HASH_FOREACH_END();
zend_hash_rehash(&phar->virtual_dirs);
ZEND_HASH_MAP_FOREACH_BUCKET(&phar->mounted_dirs, b) {
str_key = b->key;
if (zend_string_starts_with_cstr(str_key, ZSTR_VAL(resource_from->path)+1, from_len) &&
(ZSTR_LEN(str_key) == from_len || IS_SLASH(ZSTR_VAL(str_key)[from_len]))) {
new_str_key = zend_string_alloc(ZSTR_LEN(str_key) + to_len - from_len, 0);
memcpy(ZSTR_VAL(new_str_key), ZSTR_VAL(resource_to->path) + 1, to_len);
memcpy(ZSTR_VAL(new_str_key) + to_len, ZSTR_VAL(str_key) + from_len, ZSTR_LEN(str_key) - from_len);
ZSTR_VAL(new_str_key)[ZSTR_LEN(new_str_key)] = 0;
zend_string_release_ex(str_key, 0);
b->h = zend_string_hash_val(new_str_key);
b->key = new_str_key;
}
} ZEND_HASH_FOREACH_END();
zend_hash_rehash(&phar->mounted_dirs);
}
if (is_modified) {
phar_flush(phar, &error);
if (error) {
php_url_free(resource_from);
php_url_free(resource_to);
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": %s", url_from, url_to, error);
efree(error);
return 0;
}
}
php_url_free(resource_from);
php_url_free(resource_to);
return 1;
}
/* }}} */
|