%bcond_with check

Name:           libarchive
Version:        3.5.1
Release:        2
Summary:        Multi-format archive and compression library

License:        BSD
URL:            https://www.libarchive.org/
Source0:        https://www.libarchive.org/downloads/%{name}-%{version}.tar.gz

BuildRequires:  gcc bison sharutils zlib-devel bzip2-devel xz-devel
BuildRequires:  lzo-devel e2fsprogs-devel libacl-devel libattr-devel
BuildRequires:  openssl-devel libxml2-devel lz4-devel automake libzstd-devel

Provides:       bsdtar bsdcpio bsdcat
Obsoletes:      bsdtar bsdcpio bsdcat

Patch6001:      libarchive-uninitialized-value.patch
Patch6002:      backport-CVE-2021-36976.patch

%description
%{name} is an open-source BSD-licensed C programming library that 
provides streaming access to a variety of different archive formats,
including tar, cpio, pax, zip, and ISO9660 images. The distribution 
also includes bsdtar and bsdcpio, full-featured implementations of 
tar and cpio that use %{name}.

%package 	devel
Summary:        Development files for %{name}
Requires:       %{name} = %{version}-%{release}

%description 	devel
%{name}-devel contains the header files for developing
applications that want to make use of %{name}.

%package_help

%prep
%autosetup -n %{name}-%{version} -p1

%build
%configure --disable-rpath
%disable_rpath

%make_build

%install
%make_install 
%delete_la

replace ()
{
    filename=$1
    file=`basename "$filename"`
    binary=${file%%.*}
    pattern=${binary##bsd}

    awk "
        /^.Dt ${pattern^^} 1/ {
            print \".Dt ${binary^^} 1\";
            next;
        }
        !stop && /^.Nm $pattern/ {
            print \".Nm $binary\" ;
            stop = 1 ;
            next;
        }
        1;
    " "$filename" > "$filename.new"
    mv "$filename".new "$filename"
}

for manpage in bsdtar.1 bsdcpio.1
do
    installed_manpage=`find "$RPM_BUILD_ROOT" -name "$manpage"`
    replace "$installed_manpage"
done


%check
%if %{with check}
logfiles ()
{
    find -name '*_test.log' -or -name test-suite.log
}

tempdirs ()
{
    cat `logfiles` \
        | awk "match(\$0, /[^[:space:]]*`date -I`[^[:space:]]*/) { print substr(\$0, RSTART, RLENGTH); }" \
        | sort | uniq
}

cat_logs ()
{
    for i in `logfiles`
    do
        echo "=== $i ==="
        cat "$i"
    done
}

run_testsuite ()
{
    rc=0
    LD_LIBRARY_PATH=`pwd`/.libs make %{?_smp_mflags} check -j1 || {
        cat_logs

        for i in `tempdirs`; do
            if test -d "$i" ; then
                find $i -printf "%p\n    ~> a: %a\n    ~> c: %c\n    ~> t: %t\n    ~> %s B\n"
                cat $i/*.log
            fi
        done
        return 1
    }
    cat_logs
}

run_testsuite

%endif

%ldconfig_scriptlets

%files
%defattr(-,root,root)
%{!?_licensedir:%global license %%doc}
%license COPYING
%{_libdir}/%{name}.so.13*
%{_bindir}/bsdtar
%{_bindir}/bsdcpio
%{_bindir}/bsdcat

%files devel
%defattr(-,root,root)
%{_includedir}/*.h
%{_libdir}/%{name}.so
%{_libdir}/pkgconfig/%{name}.pc
%{_libdir}/*.a

%files help
%defattr(-,root,root)
%doc NEWS README.md
%{_mandir}/man1/*
%{_mandir}/man3/*
%{_mandir}/man5/*

%changelog
* Thu Oct 14 2021 yangcheng <yagcheng87@huawei.com> - 3.5.1-2
- Type:CVE
- ID:CVE-2021-36976
- SUG:NA
- DESC:fix CVE-2021-36976

* Fri Jan 29 2021 zhanzhimin <zhanzhimin@huawei.com> - 3.5.1-1
- Upgrade to version 3.5.1

* Fri Aug 21 2020 yanan <yanan@huawei.com> - 3.4.3-2
- Type:bugfix
- ID:NA
- SUG:NA
- DESC:Avoid stack overflow in read_data_compressed

* Tue Jul 28 2020 zhangnaru <zhangnaru@huawei.com> - 3.4.3-1
- Type:enhancement
- ID:NA
- SUG:NA
- DESC:Upgrade to version 3.4.3

* Mon Apr 20 2020 openEuler Buildteam <buildteam@openeuler.org> - 3.4.1-3
- Type:cves
- ID:CVE-2020-9308
- SUG:NA
- DESC:fix CVE-2020-9308

* Tue Mar 10 2020 songnannan <songnannan2@huawei.com> - 3.4.1-2
- bugfix about uninitialized value

* Wed Jan 8 2020 openEuler Buildteam <buildteam@openeuler.org> - 3.4.1-1
- Type:bugfix
- ID:NA
- SUG:NA
- DESC: update to 3.4.1

* Fri Jan 3 2020 openEuler Buildteam <buildteam@openeuler.org> - 3.4.0-3
- Type:bugfix
- ID:NA
- SUG:NA
- DESC:bugfix about CVE-2019-19221.patch

* Wed Oct 9 2019 openEuler Buildteam <buildteam@openeuler.org> - 3.4.0-2
- Type:bugfix
- Id:NA
- SUG:NA
- DESC:delete the comment for patch0

* Mon Sep 16 2019 openEuler Buildteam <buildteam@openeuler.org> - 3.4.0-1
- Package init
