stream.c 33 KB

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