Optionally ignore unsupported types instead of panicking

This commit is contained in:
apio 2022-12-30 11:13:21 +01:00
parent 56d43f98ac
commit d018d128a0
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 11 additions and 1 deletions

View File

@ -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)

View File

@ -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`

View File

@ -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);
}