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