PageRenderTime 133ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/ext/phar/stream.c

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