Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH 10/15] unpacker.eclass: Add on-the-fly .gpkg.tar unpacking support
Date: Sun, 25 Sep 2022 18:26:16
Message-Id: 20220925182317.1559529-11-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH 00/15] unpacker.eclass: Tests, bugfixes and GPKG support by "Michał Górny"
1 Signed-off-by: Michał Górny <mgorny@g.o>
2 ---
3 eclass/tests/unpacker.sh | 47 ++++++++++++++++++++++++++++++++++++++++
4 eclass/unpacker.eclass | 38 ++++++++++++++++++++++++++++++++
5 2 files changed, 85 insertions(+)
6
7 diff --git a/eclass/tests/unpacker.sh b/eclass/tests/unpacker.sh
8 index 60b651759a52..bbbfa32623ab 100755
9 --- a/eclass/tests/unpacker.sh
10 +++ b/eclass/tests/unpacker.sh
11 @@ -136,6 +136,43 @@ test_deb() {
12 "create_deb '${suffix}' '${tool_cmd}' \${archive} \${TESTFILE}"
13 }
14
15 +create_gpkg() {
16 + local suffix=${1}
17 + local tool=${2}
18 + local archive=${3}
19 + local infile=${4}
20 + local gpkg_dir=${archive%.gpkg.tar}
21 +
22 + mkdir image metadata "${gpkg_dir}" || die
23 + cp "${infile}" image/ || die
24 + tar -c metadata | ${tool} > "${gpkg_dir}/metadata.tar${suffix}"
25 + assert "packing metadata.tar${suffix} failed"
26 + : > "${gpkg_dir}/metadata.tar${suffix}.sig" || die
27 + tar -c image | ${tool} > "${gpkg_dir}/image.tar${suffix}"
28 + assert "packing image.tar${suffix} failed"
29 + : > "${gpkg_dir}/image.tar${suffix}.sig" || die
30 + : > "${gpkg_dir}"/gpkg-1 || die
31 + tar -cf "${archive}" --format=ustar \
32 + "${gpkg_dir}"/{gpkg-1,{metadata,image}.tar"${suffix}"} || die
33 + rm -r image metadata "${gpkg_dir}" || die
34 +}
35 +
36 +test_gpkg() {
37 + local suffix=${1}
38 + local tool=${2}
39 + local tool_cmd
40 +
41 + if [[ -n ${tool} ]]; then
42 + tool_cmd="${tool} -c"
43 + else
44 + tool_cmd=cat
45 + fi
46 +
47 + test_unpack "test-${tool}-1.2.3-1.gpkg.tar" \
48 + "test-${tool}-1.2.3-1/image/test.in" "tar ${tool}" \
49 + "create_gpkg '${suffix}' '${tool_cmd}' \${archive} \${TESTFILE}"
50 +}
51 +
52 test_reject_junk() {
53 local suffix=${1}
54 local archive=test${1}
55 @@ -209,6 +246,16 @@ test_deb .xz xz
56 test_deb .bz2 bzip2
57 test_deb .lzma lzma
58
59 +test_gpkg
60 +test_gpkg .gz gzip
61 +test_gpkg .bz2 bzip2
62 +test_gpkg .lz4 lz4
63 +test_gpkg .lz lzip
64 +test_gpkg .lzma lzma
65 +test_gpkg .lzo lzop
66 +test_gpkg .xz xz
67 +test_gpkg .zst zstd
68 +
69 test_unpack test.zip test.in zip 'zip -q ${archive} ${TESTFILE}'
70 # test handling non-adjusted zip with junk prepended
71 test_unpack test.zip test.in zip \
72 diff --git a/eclass/unpacker.eclass b/eclass/unpacker.eclass
73 index a64c5eae18aa..70a46ac19709 100644
74 --- a/eclass/unpacker.eclass
75 +++ b/eclass/unpacker.eclass
76 @@ -408,6 +408,42 @@ _unpacker_get_decompressor() {
77 esac
78 }
79
80 +# @FUNCTION: unpack_gpkg
81 +# @USAGE: <gpkg file>
82 +# @DESCRIPTION:
83 +# Unpack the image subarchive of a GPKG package on-the-fly, preserving
84 +# the original directory structure (i.e. into <gpkg-dir>/image).
85 +unpack_gpkg() {
86 + [[ $# -eq 1 ]] || die "Usage: ${FUNCNAME} <file>"
87 +
88 + local gpkg=$(find_unpackable_file "$1")
89 + unpack_banner "${gpkg}"
90 +
91 + local l images=()
92 + while read -r l; do
93 + case ${l} in
94 + */image.tar*.sig)
95 + ;;
96 + */image.tar*)
97 + images+=( "${l}" )
98 + ;;
99 + esac
100 + done < <(tar -tf "${gpkg}" || die "unable to list ${gpkg}")
101 +
102 + if [[ ${#images[@]} -eq 0 ]]; then
103 + die "No image.tar found in ${gpkg}"
104 + elif [[ ${#images[@]} -gt 1 ]]; then
105 + die "More than one image.tar found in ${gpkg}"
106 + fi
107 +
108 + local decomp=$(_unpacker_get_decompressor "${images[0]}")
109 + local dirname=${images[0]%/*}
110 + mkdir -p "${dirname}" || die
111 + tar -xOf "${gpkg}" "${images[0]}" | ${decomp:-cat} |
112 + tar --no-same-owner -xC "${dirname}"
113 + assert "Unpacking ${gpkg} failed"
114 +}
115 +
116 # @FUNCTION: _unpacker
117 # @USAGE: <one archive to unpack>
118 # @INTERNAL
119 @@ -427,6 +463,8 @@ _unpacker() {
120 # then figure out if there are any archiving aspects
121 local arch=""
122 case ${m} in
123 + *.gpkg.tar)
124 + arch="unpack_gpkg" ;;
125 *.tgz|*.tbz|*.tbz2|*.txz|*.tar.*|*.tar)
126 arch="tar --no-same-owner -xof" ;;
127 *.cpio.*|*.cpio)
128 --
129 2.37.3