vcs-snapshot.eclass: Detect and report invalid directory structure
authorMichał Górny <mgorny@gentoo.org>
Thu, 25 Jul 2019 06:16:42 +0000 (08:16 +0200)
committerMichał Górny <mgorny@gentoo.org>
Sat, 27 Jul 2019 09:49:49 +0000 (11:49 +0200)
Detect when the archive does not contain a single top-level directory,
and abort in that case.  Otherwise, --strip-components would result
in unpredictable mess.

Signed-off-by: Michał Górny <mgorny@gentoo.org>
eclass/vcs-snapshot.eclass

index 312e9a4611e15ee8c622d3585e4f5114e4c7f997..d3f7025fbb35bd7b478f1b759cb145d3fe31c8a4 100644 (file)
@@ -68,8 +68,21 @@ vcs-snapshot_src_unpack() {
 
                                debug-print "${FUNCNAME}: unpacking ${f} to ${destdir}"
 
-                               # XXX: check whether the directory structure inside is
-                               # fine? i.e. if the tarball has actually a parent dir.
+                               local l topdirs=()
+                               while read -r l; do
+                                       topdirs+=( "${l}" )
+                               done < <(tar -t -f "${DISTDIR}/${f}" | cut -d/ -f1 | sort -u)
+                               if [[ ${#topdirs[@]} -gt 1 ]]; then
+                                       eerror "The archive ${f} contains multiple or no top directory."
+                                       eerror "It is impossible for vcs-snapshot to unpack this correctly."
+                                       eerror "Top directories found:"
+                                       local d
+                                       for d in "${topdirs[@]}"; do
+                                               eerror "    ${d}"
+                                       done
+                                       die "${FUNCNAME}: Invalid directory structure in archive ${f}"
+                               fi
+
                                mkdir "${destdir}" || die
                                # -o (--no-same-owner) to avoid restoring original owner
                                einfo "Unpacking ${f}"