@@ -226,6 +226,21 @@ void phar_destroy_phar_data(phar_archive_data *phar) /* {{{ */
226226
227227 phar_metadata_tracker_free (& phar -> metadata_tracker , phar -> is_persistent );
228228
229+ if (phar -> fp_refs ) {
230+ zend_ulong key ;
231+
232+ ZEND_HASH_FOREACH_NUM_KEY (phar -> fp_refs , key ) {
233+ php_stream * stream = (php_stream * ) (uintptr_t ) key ;
234+
235+ if (stream != phar -> fp && stream != phar -> ufp ) {
236+ php_stream_close (stream );
237+ }
238+ } ZEND_HASH_FOREACH_END ();
239+ zend_hash_destroy (phar -> fp_refs );
240+ FREE_HASHTABLE (phar -> fp_refs );
241+ phar -> fp_refs = NULL ;
242+ }
243+
229244 if (phar -> fp ) {
230245 php_stream_close (phar -> fp );
231246 phar -> fp = 0 ;
@@ -395,16 +410,67 @@ void destroy_phar_manifest_entry(zval *zv) /* {{{ */
395410}
396411/* }}} */
397412
413+ void phar_fp_addref (phar_archive_data * phar , php_stream * fp ) /* {{{ */
414+ {
415+ zval * count , tmp ;
416+
417+ if (!phar -> fp_refs ) {
418+ ALLOC_HASHTABLE (phar -> fp_refs );
419+ zend_hash_init (phar -> fp_refs , 2 , NULL , NULL , 0 );
420+ }
421+
422+ count = zend_hash_index_find (phar -> fp_refs , (zend_ulong ) (uintptr_t ) fp );
423+ if (count ) {
424+ Z_LVAL_P (count )++ ;
425+ } else {
426+ ZVAL_LONG (& tmp , 1 );
427+ zend_hash_index_add_new (phar -> fp_refs , (zend_ulong ) (uintptr_t ) fp , & tmp );
428+ }
429+ }
430+ /* }}} */
431+
432+ bool phar_fp_in_use (const phar_archive_data * phar , const php_stream * fp ) /* {{{ */
433+ {
434+ return phar -> fp_refs && zend_hash_index_exists (phar -> fp_refs , (zend_ulong ) (uintptr_t ) fp );
435+ }
436+ /* }}} */
437+
438+ static void phar_entry_data_release_fp (phar_entry_data * idata ) /* {{{ */
439+ {
440+ phar_archive_data * phar = idata -> phar ;
441+ php_stream * fp = idata -> fp ;
442+ zval * count ;
443+
444+ if (!fp ) {
445+ return ;
446+ }
447+
448+ count = phar -> fp_refs ? zend_hash_index_find (phar -> fp_refs , (zend_ulong ) (uintptr_t ) fp ) : NULL ;
449+ if (count ) {
450+ if (-- Z_LVAL_P (count ) > 0 ) {
451+ return ;
452+ }
453+ zend_hash_index_del (phar -> fp_refs , (zend_ulong ) (uintptr_t ) fp );
454+ if (fp != phar -> fp && fp != phar -> ufp ) {
455+ php_stream_close (fp );
456+ }
457+ return ;
458+ }
459+
460+ if (fp != phar -> fp && fp != phar -> ufp && fp != idata -> internal_file -> fp ) {
461+ php_stream_close (fp );
462+ }
463+ }
464+ /* }}} */
465+
398466void phar_entry_delref (phar_entry_data * idata ) /* {{{ */
399467{
400468 if (idata -> internal_file && !idata -> internal_file -> is_persistent ) {
401469 if (-- idata -> internal_file -> fp_refcount < 0 ) {
402470 idata -> internal_file -> fp_refcount = 0 ;
403471 }
404472
405- if (idata -> fp && idata -> fp != idata -> phar -> fp && idata -> fp != idata -> phar -> ufp && idata -> fp != idata -> internal_file -> fp ) {
406- php_stream_close (idata -> fp );
407- }
473+ phar_entry_data_release_fp (idata );
408474 /* if phar_get_or_create_entry_data returns a sub-directory, we have to free it */
409475 if (idata -> internal_file -> is_temp_dir ) {
410476 destroy_phar_manifest_entry_int (idata -> internal_file );
@@ -427,9 +493,7 @@ void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */
427493 phar = idata -> phar ;
428494
429495 if (idata -> internal_file -> fp_refcount < 2 && idata -> internal_file -> fileinfo_lock_count == 0 ) {
430- if (idata -> fp && idata -> fp != idata -> phar -> fp && idata -> fp != idata -> phar -> ufp && idata -> fp != idata -> internal_file -> fp ) {
431- php_stream_close (idata -> fp );
432- }
496+ phar_entry_data_release_fp (idata );
433497 zend_hash_str_del (& idata -> phar -> manifest , idata -> internal_file -> filename , idata -> internal_file -> filename_len );
434498 idata -> phar -> refcount -- ;
435499 efree (idata );
@@ -2534,8 +2598,6 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
25342598 php_stream_filter * filter ;
25352599 php_serialize_data_t metadata_hash ;
25362600 smart_str main_metadata_str = {0 };
2537- bool free_fp = true;
2538- bool free_ufp = true;
25392601 bool manifest_hack = false;
25402602 php_stream * shared_cfp = NULL ;
25412603
@@ -2681,18 +2743,6 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
26812743 /* remove this from the new phar */
26822744 continue ;
26832745 }
2684- if (entry -> fp_refcount ) {
2685- /* open file pointers refer to this fp, do not free the stream */
2686- switch (entry -> fp_type ) {
2687- case PHAR_FP :
2688- free_fp = false;
2689- break ;
2690- case PHAR_UFP :
2691- free_ufp = false;
2692- default :
2693- break ;
2694- }
2695- }
26962746 /* after excluding deleted files, calculate manifest size in bytes and number of entries */
26972747 ++ new_manifest_count ;
26982748 phar_add_virtual_dirs (phar , entry -> filename , entry -> filename_len );
@@ -3116,12 +3166,12 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
31163166 /* finally, close the temp file, rename the original phar,
31173167 move the temp to the old phar, unlink the old phar, and reload it into memory
31183168 */
3119- if (phar -> fp && free_fp ) {
3169+ if (phar -> fp && ! phar_fp_in_use ( phar , phar -> fp ) ) {
31203170 php_stream_close (phar -> fp );
31213171 }
31223172
31233173 if (phar -> ufp ) {
3124- if (free_ufp ) {
3174+ if (! phar_fp_in_use ( phar , phar -> ufp ) ) {
31253175 php_stream_close (phar -> ufp );
31263176 }
31273177 phar -> ufp = NULL ;
0 commit comments