Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH v2 4/5] vcs-snapshot.eclass: Detect and report invalid directory structure
Date: Thu, 25 Jul 2019 07:52:12
Message-Id: 05490291503fc340940ca618e4b9c329fb36e218.camel@gentoo.org
In Reply to: [gentoo-dev] [PATCH v2 4/5] vcs-snapshot.eclass: Detect and report invalid directory structure by "Michał Górny"
1 On Thu, 2019-07-25 at 09:37 +0200, Michał Górny wrote:
2 > Detect when the archive does not contain a single top-level directory,
3 > and abort in that case. Otherwise, --strip-components would result
4 > in unpredictable mess.
5 >
6 > Signed-off-by: Michał Górny <mgorny@g.o>
7 > ---
8 > eclass/vcs-snapshot.eclass | 17 +++++++++++++++--
9 > 1 file changed, 15 insertions(+), 2 deletions(-)
10 >
11 > diff --git a/eclass/vcs-snapshot.eclass b/eclass/vcs-snapshot.eclass
12 > index 312e9a4611e1..33abd9a7c190 100644
13 > --- a/eclass/vcs-snapshot.eclass
14 > +++ b/eclass/vcs-snapshot.eclass
15 > @@ -1,87 +1,100 @@
16 > # Copyright 1999-2019 Gentoo Authors
17 > # Distributed under the terms of the GNU General Public License v2
18 >
19 > # @ECLASS: vcs-snapshot.eclass
20 > # @MAINTAINER:
21 > # mgorny@g.o
22 > # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6 7
23 > # @BLURB: support eclass for unpacking VCS snapshot tarballs
24 > # @DESCRIPTION:
25 > # THIS ECLASS IS NOT NECESSARY FOR MODERN GITHUB AND GITLAB SNAPSHOTS.
26 > # THEIR DIRECTORY STRUCTURE IS ENTIRELY PREDICTABLE, SO UPDATE YOUR
27 > # EBUILD TO USE /ARCHIVE/ URI AND SET S IF NECESSARY.
28 > #
29 > # This eclass provides a convenience src_unpack() which does unpack all
30 > # the tarballs in SRC_URI to locations matching their (local) names,
31 > # discarding the original parent directory.
32 > #
33 > # The typical use case are VCS tag snapshots coming from BitBucket
34 > # (but not GitHub or GitLab). They have hash appended to the directory
35 > # name which makes extracting them a painful experience. But if you are
36 > # using a SRC_URI arrow to rename them (which quite likely you have to
37 > # do anyway), vcs-snapshot will just extract them into matching
38 > # directories.
39 > #
40 > # Please note that this eclass handles only tarballs (.tar, .tar.gz,
41 > # .tar.bz2 & .tar.xz). For any other file format (or suffix) it will
42 > # fall back to regular unpack. Support for additional formats may be
43 > # added in the future if necessary.
44 > #
45 > # @EXAMPLE:
46 > #
47 > # @CODE
48 > # EAPI=7
49 > # inherit vcs-snapshot
50 > #
51 > # SRC_URI="
52 > # https://bitbucket.org/foo/bar/get/${PV}.tar.bz2 -> ${P}.tar.bz2
53 > # https://bitbucket.org/foo/bar-otherstuff/get/${PV}.tar.bz2
54 > # -> ${P}-otherstuff.tar.bz2"
55 > # @CODE
56 > #
57 > # and however the tarballs were originally packed, all files will appear
58 > # in ${WORKDIR}/${P} and ${WORKDIR}/${P}-otherstuff respectively.
59 >
60 > case ${EAPI:-0} in
61 > 0|1|2|3|4|5|6|7) ;;
62 > *) die "vcs-snapshot.eclass API in EAPI ${EAPI} not yet established."
63 > esac
64 >
65 > EXPORT_FUNCTIONS src_unpack
66 >
67 > # @FUNCTION: vcs-snapshot_src_unpack
68 > # @DESCRIPTION:
69 > # Extract all the archives from ${A}. The .tar, .tar.gz, .tar.bz2
70 > # and .tar.xz archives will be unpacked to directories matching their
71 > # local names. Other archive types will be passed down to regular
72 > # unpack.
73 > vcs-snapshot_src_unpack() {
74 > debug-print-function ${FUNCNAME} "${@}"
75 >
76 > local f
77 >
78 > for f in ${A}
79 > do
80 > case "${f}" in
81 > *.tar|*.tar.gz|*.tar.bz2|*.tar.xz)
82 > local destdir=${WORKDIR}/${f%.tar*}
83 >
84 > debug-print "${FUNCNAME}: unpacking ${f} to ${destdir}"
85 >
86 > - # XXX: check whether the directory structure inside is
87 > - # fine? i.e. if the tarball has actually a parent dir.
88 > + local l topdirs=()
89 > + while read l; do
90
91 Oh, and please imagine you see '-r' here.
92
93 > + topdirs+=( "${l}" )
94 > + done < <(tar -t -f "${DISTDIR}/${f}" | cut -d/ -f1 | sort -u)
95 > + if [[ ${#topdirs[@]} -gt 1 ]]; then
96 > + eerror "The archive ${f} contains multiple or no top directory."
97 > + eerror "It is impossible for vcs-snapshot to unpack this correctly."
98 > + eerror "Top directories found:"
99 > + local d
100 > + for d in "${topdirs[@]}"; do
101 > + eerror " ${d}"
102 > + done
103 > + die "${FUNCNAME}: Invalid directory structure in archive ${f}"
104 > + fi
105 > +
106 > mkdir "${destdir}" || die
107 > # -o (--no-same-owner) to avoid restoring original owner
108 > einfo "Unpacking ${f}"
109 > tar -C "${destdir}" -x -o --strip-components 1 \
110 > -f "${DISTDIR}/${f}" || die
111 > ;;
112 > *)
113 > debug-print "${FUNCNAME}: falling back to unpack for ${f}"
114 >
115 > # fall back to the default method
116 > unpack "${f}"
117 > ;;
118 > esac
119 > done
120 > }
121
122 --
123 Best regards,
124 Michał Górny

Attachments

File name MIME type
signature.asc application/pgp-signature