Skip to content

Commit 0b21a89

Browse files
committed
add lastId property to ZipArchive
1 parent 33ef3d6 commit 0b21a89

File tree

9 files changed

+76
-75
lines changed

9 files changed

+76
-75
lines changed

ext/zip/php_zip.c

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
275275
}
276276
/* }}} */
277277

278-
static int php_zip_add_file(struct zip *za, const char *filename, size_t filename_len,
278+
static int php_zip_add_file(ze_zip_object *obj, const char *filename, size_t filename_len,
279279
char *entry_name, size_t entry_name_len, /* unused if replace >= 0 */
280280
zip_uint64_t offset_start, zip_uint64_t offset_len,
281281
zend_long replace, /* index to replace, add new file if < 0 */
@@ -300,25 +300,26 @@ static int php_zip_add_file(struct zip *za, const char *filename, size_t filenam
300300
return -1;
301301
}
302302

303-
zs = zip_source_file(za, resolved_path, offset_start, offset_len);
303+
zs = zip_source_file(obj->za, resolved_path, offset_start, offset_len);
304304
if (!zs) {
305305
return -1;
306306
}
307307
// Replace
308308
if (replace >= 0) {
309-
if (zip_file_replace(za, replace, zs, flags) < 0) {
309+
if (zip_file_replace(obj->za, replace, zs, flags) < 0) {
310310
zip_source_free(zs);
311311
return -1;
312312
}
313-
zip_error_clear(za);
313+
zip_error_clear(obj->za);
314314
return 1;
315315
}
316316
// Add
317-
if (zip_file_add(za, entry_name, zs, flags) < 0) {
317+
obj->last_id = zip_file_add(obj->za, entry_name, zs, flags);
318+
if (obj->last_id < 0) {
318319
zip_source_free(zs);
319320
return -1;
320321
}
321-
zip_error_clear(za);
322+
zip_error_clear(obj->za);
322323
return 1;
323324
}
324325
/* }}} */
@@ -433,67 +434,69 @@ static int php_zip_parse_options(zval *options, zend_long *remove_all_path,
433434
#endif
434435
/* }}} */
435436

436-
static zend_long php_zip_status(struct zip *za) /* {{{ */
437+
static zend_long php_zip_status(ze_zip_object *obj) /* {{{ */
437438
{
438439
#if LIBZIP_VERSION_MAJOR < 1
439440
int zep, syp;
440441

441-
zip_error_get(za, &zep, &syp);
442+
zip_error_get(obj->za, &zep, &syp);
442443
#else
443444
int zep;
444445
zip_error_t *err;
445446

446-
err = zip_get_error(za);
447+
err = zip_get_error(obj->za);
447448
zep = zip_error_code_zip(err);
448449
zip_error_fini(err);
449450
#endif
450451
return zep;
451452
}
452453
/* }}} */
453454

454-
static zend_long php_zip_status_sys(struct zip *za) /* {{{ */
455+
static zend_long php_zip_last_id(ze_zip_object *obj) /* {{{ */
456+
{
457+
return obj->last_id;
458+
}
459+
/* }}} */
460+
461+
static zend_long php_zip_status_sys(ze_zip_object *obj) /* {{{ */
455462
{
456463
#if LIBZIP_VERSION_MAJOR < 1
457464
int zep, syp;
458465

459-
zip_error_get(za, &zep, &syp);
466+
zip_error_get(obj->za, &zep, &syp);
460467
#else
461468
int syp;
462469
zip_error_t *err;
463470

464-
err = zip_get_error(za);
471+
err = zip_get_error(obj->za);
465472
syp = zip_error_code_system(err);
466473
zip_error_fini(err);
467474
#endif
468475
return syp;
469476
}
470477
/* }}} */
471478

472-
static zend_long php_zip_get_num_files(struct zip *za) /* {{{ */
479+
static zend_long php_zip_get_num_files(ze_zip_object *obj) /* {{{ */
473480
{
474-
zip_int64_t num = zip_get_num_entries(za, 0);
481+
zip_int64_t num = zip_get_num_entries(obj->za, 0);
475482
return MIN(num, ZEND_LONG_MAX);
476483
}
477484
/* }}} */
478485

479-
static char * php_zipobj_get_filename(ze_zip_object *obj) /* {{{ */
486+
static char * php_zipobj_get_filename(ze_zip_object *obj, int *len) /* {{{ */
480487
{
481-
482-
if (!obj) {
483-
return NULL;
484-
}
485-
486-
if (obj->filename) {
488+
if (obj && obj->filename) {
489+
*len = strlen(obj->filename);
487490
return obj->filename;
488491
}
489492
return NULL;
490493
}
491494
/* }}} */
492495

493-
static char * php_zipobj_get_zip_comment(struct zip *za, int *len) /* {{{ */
496+
static char * php_zipobj_get_zip_comment(ze_zip_object *obj, int *len) /* {{{ */
494497
{
495-
if (za) {
496-
return (char *)zip_get_archive_comment(za, len, 0);
498+
if (obj->za) {
499+
return (char *)zip_get_archive_comment(obj->za, len, 0);
497500
}
498501
return NULL;
499502
}
@@ -764,28 +767,25 @@ static zend_object_handlers zip_object_handlers;
764767

765768
static HashTable zip_prop_handlers;
766769

767-
typedef zend_long (*zip_read_int_t)(struct zip *za);
768-
typedef char *(*zip_read_const_char_t)(struct zip *za, int *len);
769-
typedef char *(*zip_read_const_char_from_ze_t)(ze_zip_object *obj);
770+
typedef zend_long (*zip_read_int_t)(ze_zip_object *obj);
771+
typedef char *(*zip_read_const_char_t)(ze_zip_object *obj, int *len);
770772

771773
typedef struct _zip_prop_handler {
772774
zip_read_int_t read_int_func;
773775
zip_read_const_char_t read_const_char_func;
774-
zip_read_const_char_from_ze_t read_const_char_from_obj_func;
775776

776777
int type;
777778
} zip_prop_handler;
778779
/* }}} */
779780

780-
static void php_zip_register_prop_handler(HashTable *prop_handler, char *name, zip_read_int_t read_int_func, zip_read_const_char_t read_char_func, zip_read_const_char_from_ze_t read_char_from_obj_func, int rettype) /* {{{ */
781+
static void php_zip_register_prop_handler(HashTable *prop_handler, char *name, zip_read_int_t read_int_func, zip_read_const_char_t read_char_func, int rettype) /* {{{ */
781782
{
782783
zip_prop_handler hnd;
783784
zend_string *str;
784785
zval tmp;
785786

786787
hnd.read_const_char_func = read_char_func;
787788
hnd.read_int_func = read_int_func;
788-
hnd.read_const_char_from_obj_func = read_char_from_obj_func;
789789
hnd.type = rettype;
790790
str = zend_string_init_interned(name, strlen(name), 1);
791791
zend_hash_add_mem(prop_handler, str, &hnd, sizeof(zip_prop_handler));
@@ -805,20 +805,9 @@ static zval *php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd,
805805

806806
if (obj && obj->za != NULL) {
807807
if (hnd->read_const_char_func) {
808-
retchar = hnd->read_const_char_func(obj->za, &len);
809-
} else {
810-
if (hnd->read_int_func) {
811-
retint = hnd->read_int_func(obj->za);
812-
if (retint == -1) {
813-
php_error_docref(NULL, E_WARNING, "Internal zip error returned");
814-
return NULL;
815-
}
816-
} else {
817-
if (hnd->read_const_char_from_obj_func) {
818-
retchar = hnd->read_const_char_from_obj_func(obj);
819-
len = strlen(retchar);
820-
}
821-
}
808+
retchar = hnd->read_const_char_func(obj, &len);
809+
} else if (hnd->read_int_func) {
810+
retint = hnd->read_int_func(obj);
822811
}
823812
}
824813

@@ -1037,6 +1026,7 @@ static zend_object *php_zip_object_new(zend_class_entry *class_type) /* {{{ */
10371026
zend_object_std_init(&intern->zo, class_type);
10381027
object_properties_init(&intern->zo, class_type);
10391028
intern->zo.handlers = &zip_object_handlers;
1029+
intern->last_id = -1;
10401030

10411031
return &intern->zo;
10421032
}
@@ -1579,8 +1569,6 @@ static ZIPARCHIVE_METHOD(addEmptyDir)
15791569
zval *self = ZEND_THIS;
15801570
char *dirname;
15811571
size_t dirname_len;
1582-
int idx;
1583-
struct zip_stat sb;
15841572
char *s;
15851573
zend_long flags = 0;
15861574

@@ -1604,16 +1592,11 @@ static ZIPARCHIVE_METHOD(addEmptyDir)
16041592
s = dirname;
16051593
}
16061594

1607-
idx = zip_stat(intern, s, 0, &sb);
1608-
if (idx >= 0) {
1595+
if ((Z_ZIP_P(self)->last_id = zip_dir_add(intern, (const char *)s, flags)) == -1) {
16091596
RETVAL_FALSE;
16101597
} else {
1611-
if (zip_dir_add(intern, (const char *)s, flags) == -1) {
1612-
RETVAL_FALSE;
1613-
} else {
1614-
zip_error_clear(intern);
1615-
RETVAL_TRUE;
1616-
}
1598+
zip_error_clear(intern);
1599+
RETVAL_TRUE;
16171600
}
16181601

16191602
if (s != dirname) {
@@ -1624,7 +1607,6 @@ static ZIPARCHIVE_METHOD(addEmptyDir)
16241607

16251608
static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
16261609
{
1627-
struct zip *intern;
16281610
zval *self = ZEND_THIS;
16291611
char *path = ".";
16301612
char *remove_path = NULL, *save_remove_path;
@@ -1637,7 +1619,6 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
16371619
int found;
16381620
zend_string *pattern;
16391621

1640-
ZIP_FROM_OBJECT(intern, self);
16411622
/* 1 == glob, 2 == pcre */
16421623
if (type == 1) {
16431624
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|la",
@@ -1717,7 +1698,7 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
17171698
basename = NULL;
17181699
}
17191700

1720-
if (php_zip_add_file(intern, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file),
1701+
if (php_zip_add_file(Z_ZIP_P(self), Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file),
17211702
entry_name, entry_name_len, 0, 0, -1, zip_flags) < 0) {
17221703
zend_array_destroy(Z_ARR_P(return_value));
17231704
RETURN_FALSE;
@@ -1751,16 +1732,13 @@ static ZIPARCHIVE_METHOD(addPattern)
17511732
Add a file in a Zip archive using its path and the name to use. */
17521733
static ZIPARCHIVE_METHOD(addFile)
17531734
{
1754-
struct zip *intern;
17551735
zval *self = ZEND_THIS;
17561736
char *entry_name = NULL;
17571737
size_t entry_name_len = 0;
17581738
zend_long offset_start = 0, offset_len = 0;
17591739
zend_string *filename;
17601740
zend_long flags = ZIP_FL_OVERWRITE;
17611741

1762-
ZIP_FROM_OBJECT(intern, self);
1763-
17641742
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|slll",
17651743
&filename, &entry_name, &entry_name_len, &offset_start, &offset_len, &flags) == FAILURE) {
17661744
RETURN_THROWS();
@@ -1776,7 +1754,7 @@ static ZIPARCHIVE_METHOD(addFile)
17761754
entry_name_len = ZSTR_LEN(filename);
17771755
}
17781756

1779-
if (php_zip_add_file(intern, ZSTR_VAL(filename), ZSTR_LEN(filename),
1757+
if (php_zip_add_file(Z_ZIP_P(self), ZSTR_VAL(filename), ZSTR_LEN(filename),
17801758
entry_name, entry_name_len, offset_start, offset_len, -1, flags) < 0) {
17811759
RETURN_FALSE;
17821760
} else {
@@ -1789,15 +1767,12 @@ static ZIPARCHIVE_METHOD(addFile)
17891767
Add a file in a Zip archive using its path and the name to use. */
17901768
static ZIPARCHIVE_METHOD(replaceFile)
17911769
{
1792-
struct zip *intern;
17931770
zval *self = ZEND_THIS;
17941771
zend_long index;
17951772
zend_long offset_start = 0, offset_len = 0;
17961773
zend_string *filename;
17971774
zend_long flags = 0;
17981775

1799-
ZIP_FROM_OBJECT(intern, self);
1800-
18011776
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Pl|lll",
18021777
&filename, &index, &offset_start, &offset_len, &flags) == FAILURE) {
18031778
RETURN_THROWS();
@@ -1813,7 +1788,7 @@ static ZIPARCHIVE_METHOD(replaceFile)
18131788
RETURN_FALSE;
18141789
}
18151790

1816-
if (php_zip_add_file(intern, ZSTR_VAL(filename), ZSTR_LEN(filename),
1791+
if (php_zip_add_file(Z_ZIP_P(self), ZSTR_VAL(filename), ZSTR_LEN(filename),
18171792
NULL, 0, offset_start, offset_len, index, flags) < 0) {
18181793
RETURN_FALSE;
18191794
} else {
@@ -1861,7 +1836,8 @@ static ZIPARCHIVE_METHOD(addFromString)
18611836
RETURN_FALSE;
18621837
}
18631838

1864-
if (zip_file_add(intern, name, zs, flags) == -1) {
1839+
ze_obj->last_id = zip_file_add(intern, name, zs, flags);
1840+
if (ze_obj->last_id == -1) {
18651841
zip_source_free(zs);
18661842
RETURN_FALSE;
18671843
} else {
@@ -3069,11 +3045,12 @@ static PHP_MINIT_FUNCTION(zip)
30693045
zip_class_entry = zend_register_internal_class(&ce);
30703046

30713047
zend_hash_init(&zip_prop_handlers, 0, NULL, php_zip_free_prop_handler, 1);
3072-
php_zip_register_prop_handler(&zip_prop_handlers, "status", php_zip_status, NULL, NULL, IS_LONG);
3073-
php_zip_register_prop_handler(&zip_prop_handlers, "statusSys", php_zip_status_sys, NULL, NULL, IS_LONG);
3074-
php_zip_register_prop_handler(&zip_prop_handlers, "numFiles", php_zip_get_num_files, NULL, NULL, IS_LONG);
3075-
php_zip_register_prop_handler(&zip_prop_handlers, "filename", NULL, NULL, php_zipobj_get_filename, IS_STRING);
3076-
php_zip_register_prop_handler(&zip_prop_handlers, "comment", NULL, php_zipobj_get_zip_comment, NULL, IS_STRING);
3048+
php_zip_register_prop_handler(&zip_prop_handlers, "lastId", php_zip_last_id, NULL, IS_LONG);
3049+
php_zip_register_prop_handler(&zip_prop_handlers, "status", php_zip_status, NULL, IS_LONG);
3050+
php_zip_register_prop_handler(&zip_prop_handlers, "statusSys", php_zip_status_sys, NULL, IS_LONG);
3051+
php_zip_register_prop_handler(&zip_prop_handlers, "numFiles", php_zip_get_num_files, NULL, IS_LONG);
3052+
php_zip_register_prop_handler(&zip_prop_handlers, "filename", NULL, php_zipobj_get_filename, IS_STRING);
3053+
php_zip_register_prop_handler(&zip_prop_handlers, "comment", NULL, php_zipobj_get_zip_comment, IS_STRING);
30773054
zend_class_implements(zip_class_entry, 1, zend_ce_countable);
30783055

30793056
REGISTER_ZIP_CLASS_CONST_LONG("CREATE", ZIP_CREATE);

ext/zip/php_zip.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ typedef struct _ze_zip_object {
5959
char *filename;
6060
int filename_len;
6161
int buffers_cnt;
62-
zend_object zo;
62+
zip_int64_t last_id;
6363
#ifdef HAVE_PROGRESS_CALLBACK
6464
zval progress_callback;
6565
#endif
6666
#ifdef HAVE_CANCEL_CALLBACK
6767
zval cancel_callback;
6868
#endif
69+
zend_object zo;
6970
} ze_zip_object;
7071

7172
static inline ze_zip_object *php_zip_fetch_object(zend_object *obj) {

ext/zip/tests/bug11216.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ unlink('__test.zip');
1818
bool(true)
1919
ZipArchive Object
2020
(
21+
[lastId] => 0
2122
[status] => 0
2223
[statusSys] => 0
2324
[numFiles] => 1

ext/zip/tests/bug38943.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ object(myZip)#1 (%d) {
3737
[0]=>
3838
int(1)
3939
}
40+
["lastId"]=>
41+
int(0)
4042
["status"]=>
4143
int(0)
4244
["statusSys"]=>

ext/zip/tests/bug38943_2.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ object(myZip)#1 (%d) {
2323
[0]=>
2424
int(1)
2525
}
26+
["lastId"]=>
27+
int(0)
2628
["status"]=>
2729
int(0)
2830
["statusSys"]=>

ext/zip/tests/bug38944.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ int(0)
2525
int(0)
2626
string(%d) "%s"
2727
string(0) ""
28-
object(ZipArchive)#%d (5) {
28+
object(ZipArchive)#%d (6) {
29+
["lastId"]=>
30+
int(-1)
2931
["status"]=>
3032
int(0)
3133
["statusSys"]=>

0 commit comments

Comments
 (0)