xref: /PHP-8.0/ext/phar/tests/tar/refcount1.phpt (revision f8d79582)
1--TEST--
2Phar: test that refcounting avoids problems with deleting a file tar-based
3--SKIPIF--
4<?php if (!extension_loaded("phar")) die("skip"); ?>
5--INI--
6phar.readonly=0
7phar.require_hash=0
8--FILE--
9<?php
10include __DIR__ . '/files/tarmaker.php.inc';
11$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar.tar';
12$alias = 'phar://' . $fname;
13
14$tar = new tarmaker($fname, 'none');
15$tar->init();
16$tar->addFile('.phar/stub.php', "<?php __HALT_COMPILER(); ?>");
17
18$files = array();
19
20$files['a.php'] = '<?php echo "This is a\n"; ?>';
21$files['b.php'] = '<?php echo "This is b\n"; ?>';
22$files['b/c.php'] = '<?php echo "This is b/c\n"; ?>';
23$files['.phar/alias.txt'] = 'hio';
24
25foreach ($files as $n => $file) {
26    $tar->addFile($n, $file);
27}
28
29$tar->close();
30
31$fp = fopen($alias . '/b/c.php', 'wb');
32fwrite($fp, "extra");
33fclose($fp);
34echo "===CLOSE===\n";
35$phar = new Phar($fname);
36$b = fopen($alias . '/b/c.php', 'rb');
37$a = $phar['b/c.php'];
38var_dump($a);
39var_dump(fread($b, 20));
40rewind($b);
41echo "===UNLINK===\n";
42unlink($alias . '/b/c.php');
43var_dump($a);
44var_dump(fread($b, 20));
45include $alias . '/b/c.php';
46
47?>
48
49--CLEAN--
50<?php unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.tar'); ?>
51--EXPECTF--
52===CLOSE===
53object(PharFileInfo)#%d (2) {
54  [%spathName":%sSplFileInfo":private]=>
55  string(%d) "phar://%srefcount1.phar.tar/b/c.php"
56  [%sfileName":%sSplFileInfo":private]=>
57  string(%d) "c.php"
58}
59string(5) "extra"
60===UNLINK===
61
62Warning: unlink(): phar error: "b/c.php" in phar "%srefcount1.phar.tar", has open file pointers, cannot unlink in %srefcount1.php on line %d
63object(PharFileInfo)#%d (2) {
64  [%spathName":%sSplFileInfo":private]=>
65  string(%d) "phar://%srefcount1.phar.tar/b/c.php"
66  [%sfileName":%sSplFileInfo":private]=>
67  string(%s) "c.php"
68}
69string(5) "extra"
70extra
71