]> granicus.if.org Git - php/commitdiff
fix creation of new files within an existing phar, add test
authorGreg Beaver <cellog@php.net>
Fri, 5 Jan 2007 01:50:26 +0000 (01:50 +0000)
committerGreg Beaver <cellog@php.net>
Fri, 5 Jan 2007 01:50:26 +0000 (01:50 +0000)
ext/phar/phar.c
ext/phar/tests/open_for_write_existing.phpt
ext/phar/tests/open_for_write_newfile.phpt [new file with mode: 0644]

index bbbf069090318413664407061b9e0397f0ae30e8..912a5431ce1e7843c9893c8b55866e50480bf451 100644 (file)
@@ -374,10 +374,11 @@ static phar_entry_data *phar_get_or_create_entry_data(char *fname, int fname_len
                ret->internal_file->crc32 = 0;
                return ret;
        }
-       /* create an entry, this is a new file */
-       if ((entry = phar_get_entry_info(phar, path, path_len TSRMLS_CC)) != NULL) {
-               ret = (phar_entry_data *) emalloc(sizeof(phar_entry_data));
-               entry = (phar_entry_info *) emalloc(sizeof(phar_entry_info));
+       /* create a new phar data holder */
+       ret = (phar_entry_data *) emalloc(sizeof(phar_entry_data));
+       if ((entry = phar_get_entry_info(phar, path, path_len TSRMLS_CC)) == NULL) {
+               /* create an entry, this is a new file */
+               etemp.flags = 0;
                etemp.filename_len = path_len;
                etemp.filename = estrndup(path, path_len);
                etemp.uncompressed_filesize = 0;
@@ -388,8 +389,9 @@ static phar_entry_data *phar_get_or_create_entry_data(char *fname, int fname_len
                etemp.crc_checked = TRUE;
                etemp.fp = NULL;
                etemp.temp_file = 0;
-               memcpy((void *) entry, (void *) &etemp, sizeof(phar_entry_info));
-               zend_hash_add(&phar->manifest, etemp.filename, path_len, (void*)entry, sizeof(phar_entry_info), NULL);
+               zend_hash_add(&phar->manifest, etemp.filename, path_len, (void*)&etemp, sizeof(phar_entry_info), NULL);
+               /* retrieve the phar manifest copy */
+               entry = phar_get_entry_info(phar, path, path_len TSRMLS_CC);
        }
        ret->phar = phar;
        ret->internal_file = entry;
index 769f6e4640aee1cdd7d7512a9b66828e9891c2de..81a7217bffd40b448a98f357893017081509cf1f 100644 (file)
@@ -30,6 +30,7 @@ fwrite($fp, 'extra');
 fclose($fp);
 include 'phar://' . dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php/b/c.php';
 ?>
+
 ===DONE===
 --CLEAN--
 <?php unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php'); ?>
diff --git a/ext/phar/tests/open_for_write_newfile.phpt b/ext/phar/tests/open_for_write_newfile.phpt
new file mode 100644 (file)
index 0000000..ed017d7
--- /dev/null
@@ -0,0 +1,41 @@
+--TEST--
+Phar: fopen a .phar for writing (new file)
+--SKIPIF--
+<?php if (!extension_loaded("phar")) print "skip"; ?>
+--FILE--
+<?php
+$file = "<?php __HALT_COMPILER(); ?>";
+
+$files = array();
+$files['a.php'] = '<?php echo "This is a\n"; ?>';
+$files['b.php'] = '<?php echo "This is b\n"; ?>';
+$files['b/c.php'] = '<?php echo "This is b/c\n"; ?>';
+$manifest = '';
+foreach($files as $name => $cont) {
+       $len = strlen($cont);
+       $manifest .= pack('V', strlen($name)) . $name . pack('VVVVC', $len, time(), $len, crc32($cont), 0x00);
+}
+$alias = '';
+$manifest = pack('VnV', count($files), 0x0800, strlen($alias)) . $alias . $manifest;
+$file .= pack('V', strlen($manifest)) . $manifest;
+foreach($files as $cont)
+{
+       $file .= $cont;
+}
+
+file_put_contents(dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php', $file);
+
+$fp = fopen('phar://' . dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php/b/new.php', 'wb');
+fwrite($fp, 'extra');
+fclose($fp);
+include 'phar://' . dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php/b/c.php';
+include 'phar://' . dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php/b/new.php';
+?>
+
+===DONE===
+--CLEAN--
+<?php unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php'); ?>
+--EXPECT--
+This is b/c
+extra
+===DONE===