diff --git a/CMakeLists.txt b/CMakeLists.txt index fadfa44..db40e6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.8..3.22) project(minitar LANGUAGES C VERSION 1.3.0) +option(MINITAR_IGNORE_UNSUPPORTED_TYPES "Skip past entries that have unsupported types instead of panicking" OFF) + set(SOURCES src/tar.c src/util.c @@ -9,6 +11,10 @@ set(SOURCES add_library(minitar STATIC ${SOURCES}) +if(MINITAR_IGNORE_UNSUPPORTED_TYPES) +target_compile_definitions(minitar PRIVATE MINITAR_IGNORE_UNSUPPORTED_TYPES) +endif() + target_include_directories(minitar PUBLIC ${CMAKE_CURRENT_LIST_DIR}) # for minitar.h set_target_properties(minitar PROPERTIES OUTPUT_NAME mtar) diff --git a/README.md b/README.md index 83840cd..ac8cd35 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ This enum lists all supported file types: `MTAR_DIRECTORY`: Directories -Other file types supported in tar archives, such as block/character devices, FIFOs, or symlinks, are not supported and minitar will throw an error when encountering one of them. +Other file types supported in tar archives, such as block/character devices, FIFOs, or symlinks, are not supported and minitar will throw an error when encountering one of them. This behavior can be controlled by passing `-DMINITAR_IGNORE_UNSUPPORTED_TYPES=ON` to CMake when configuring, which will make minitar silently ignore such entries instead of panicking. ### minitar_entry_metadata `struct minitar_entry_metadata` diff --git a/src/util.c b/src/util.c index 1978fc0..5437ad6 100644 --- a/src/util.c +++ b/src/util.c @@ -168,9 +168,13 @@ void minitar_parse_metadata_from_tar_header(const struct tar_header* hdr, struct int minitar_validate_header(const struct tar_header* hdr) { +#ifdef MINITAR_IGNORE_UNSUPPORTED_TYPES + if (hdr->typeflag != '\0' && hdr->typeflag != '0' && hdr->typeflag != '5') return 0; +#else if (hdr->typeflag != '\0' && hdr->typeflag != '0' && hdr->typeflag != '1' && hdr->typeflag != '2' && hdr->typeflag != '3' && hdr->typeflag != '4' && hdr->typeflag != '5' && hdr->typeflag != '6') return 0; +#endif return !strncmp(hdr->magic, "ustar", 5); }