php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55195 PharData::buildFromDirectory does not seem to close the file after archiving
Submitted: 2011-07-12 22:37 UTC Modified: 2012-06-26 23:44 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: kedomingo at gmail dot com Assigned:
Status: Closed Package: PHAR related
PHP Version: 5.3.6 OS: Windows 7 Professional 32bit
Private report: No CVE-ID: None
 [2011-07-12 22:37 UTC] kedomingo at gmail dot com
Description:
------------
While I can create a file, write to it, close it, and delete it via unlink, I 
can't delete a file created by the buildFromDirectory function of PharData. I 
suspect that the file has been left open.

Test script:
---------------
<?php

$file = uniqid().".txt"; /// Demo: Script can create and delete a file (if properly closed) normally
if( !($f = fopen($file, 'w')) ) echo "Could not open $file <br />\n";
if( !fwrite($f, 'test') )       echo "Could not write to $file <br />\n";
if( !fclose($f) )               echo "Could not close $file <br />\n";
if( !unlink($file) )            echo "Could not delete $file <br />\n";

if( !is_dir( $dir = dirname(__FILE__) . '/phartest' ) ) { /// Create test directory for PharData
    mkdir( dirname(__FILE__) . '/phartest' );
    for($i = 0; $i < 3; $i++) { $f = fopen($dir . "/file$i.txt", 'w'); fwrite($f, $i); fclose($f); };
}

$file2 = uniqid().".zip"; /// Create test archive in the same directory as the demo file
$phar = new PharData($file2);
if( $phar->buildFromDirectory($dir) )
    if(!unlink($file2)) /// Did phar close the file? Can we delete it?
        echo "Could not delete $file2!";
?>

Expected result:
----------------
I expect that the call to unlink($file2) will delete the newly created archive 
(with filename $file2)

Actual result:
--------------
Warning: unlink(<$file2>): Permission denied in C:\htdocs\phartest.php on line 17 
Could not delete <$file2>!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-21 20:23 UTC] michaelmotes at email dot com
I worked around this bug by simply destroying the phar object, unset($phar).
 [2012-06-26 14:49 UTC] ralph at ralphschindler dot com
Why should you be able to delete the currently open phar?

It seems to me that $file2 is still open for modification in PHP, for example, 
you can call setStub(), or add new files or metadata to the $phar you have open.  
Since this is the case,

Even unlinkArchive() seems to imply in its documentation that the $phar needs to 
have the reference removed before you can unlink it:

http://www.php.net/manual/en/phar.unlinkarchive.php

I am inclined to say that this is not an issue.
 [2012-06-26 23:44 UTC] kedomingo at gmail dot com
-Status: Open +Status: Closed
 [2012-06-26 23:44 UTC] kedomingo at gmail dot com
ralph, you are right. It also happens that this was submitted almost a year ago, 
and I've realized the scenario that you were saying months after this ticket's 
submission.

At the time, I was thinking that buildFromDirectory is a one-off procedure and 
should automatically close the archive. Perhaps this should be clarified in the 
docs.

Ticket closed. Thanks guys!

Kyle
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 17:01:29 2024 UTC