xref: /PHP-7.4/ext/phar/tests/tar/refcount1.phpt (revision 26dfce7f)
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===DONE===
50--CLEAN--
51<?php unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.tar'); ?>
52--EXPECTF--
53===CLOSE===
54object(PharFileInfo)#%d (2) {
55  [%spathName":%sSplFileInfo":private]=>
56  string(%d) "phar://%srefcount1.phar.tar/b/c.php"
57  [%sfileName":%sSplFileInfo":private]=>
58  string(%d) "c.php"
59}
60string(5) "extra"
61===UNLINK===
62
63Warning: unlink(): phar error: "b/c.php" in phar "%srefcount1.phar.tar", has open file pointers, cannot unlink in %srefcount1.php on line %d
64object(PharFileInfo)#%d (2) {
65  [%spathName":%sSplFileInfo":private]=>
66  string(%d) "phar://%srefcount1.phar.tar/b/c.php"
67  [%sfileName":%sSplFileInfo":private]=>
68  string(%s) "c.php"
69}
70string(5) "extra"
71extra
72===DONE===
73