From d863efe443893b0c2f1344cf956f8809aba21d43 Mon Sep 17 00:00:00 2001 From: Dieter Baron Date: Mon, 22 Apr 2024 16:23:01 +0200 Subject: [PATCH] Fix listing directories with trailing / in argument. --- regress/CMakeLists.txt | 2 +- regress/zipcmp_zip_dir_slash.test | 5 ++--- src/zipcmp.c | 14 +++++++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/regress/CMakeLists.txt b/regress/CMakeLists.txt index 27819748b..c3af1ea16 100644 --- a/regress/CMakeLists.txt +++ b/regress/CMakeLists.txt @@ -98,7 +98,7 @@ foreach(FULL_CASE IN LISTS EXTRA_TESTS) endforeach() set(XFAIL_TESTS - zipcmp_zip_dir_slash.test +# zipcmp_zip_dir_slash.test ) foreach(CASE ${XFAIL_TESTS}) diff --git a/regress/zipcmp_zip_dir_slash.test b/regress/zipcmp_zip_dir_slash.test index 2784f7635..5b8f8d664 100644 --- a/regress/zipcmp_zip_dir_slash.test +++ b/regress/zipcmp_zip_dir_slash.test @@ -9,10 +9,9 @@ file zipcmp_zip_dir.zip zipcmp_zip_dir.zip return 1 stdout --- zipcmp_zip_dir.zip -+++ a ++++ a/ - directory '00-empty-dir/' - file 'dir-with-file/a', size 1, crc e8b7be43, mtime 0 -- directory 'empty-dir/' -+ directory 'dir-with-file/' + directory 'empty-dir-in-dir/' +- directory 'empty-dir/' end-of-inline-data diff --git a/src/zipcmp.c b/src/zipcmp.c index 928d32fe2..21e3fcaac 100644 --- a/src/zipcmp.c +++ b/src/zipcmp.c @@ -410,7 +410,19 @@ list_directory(const char *name, struct archive *a) { zip_uint64_t nalloc; size_t prefix_length; - char *const names[2] = {(char *)name, NULL}; + char* normalized_name = strdup(name); + prefix_length = strlen(name) + 1; + while (prefix_length > 0 && normalized_name[prefix_length-1] == '/') { + prefix_length -= 1; + } + if (prefix_length > 0) { + normalized_name[prefix_length] = '\0'; + } + else { + normalized_name[1] = '\0'; + } + + char *const names[2] = {(char *)normalized_name, NULL}; if ((fts = fts_open(names, FTS_NOCHDIR | FTS_LOGICAL, NULL)) == NULL) {