diff options
author | 2012-04-14 19:25:37 +0100 | |
---|---|---|
committer | 2012-04-14 19:25:37 +0100 | |
commit | 18c2a993d873b6d8e92988ffc33d7659398e207a (patch) | |
tree | d025cd7e70856432dc97c915236d1372da0aac6c | |
parent | c9e0c08a43cd63fe3a6315c687499116d90b2864 (diff) | |
download | paludis-18c2a993d873b6d8e92988ffc33d7659398e207a.tar.gz paludis-18c2a993d873b6d8e92988ffc33d7659398e207a.tar.xz |
Strip trailing garbage in tar files to avoid SIGPIPEs
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | paludis/repositories/e/ebuild/utils/Makefile.am | 4 | ||||
-rw-r--r-- | paludis/repositories/e/ebuild/utils/strip_tar_corruption.cc | 108 | ||||
-rwxr-xr-x | paludis/repositories/e/ebuild/utils/unpack | 11 |
4 files changed, 118 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore index 516076547..f17dfc9c1 100644 --- a/.gitignore +++ b/.gitignore @@ -328,6 +328,7 @@ paludis-*.*.*.tar.bz2 /paludis/repositories/e/ebuild/utils/prepman /paludis/repositories/e/ebuild/utils/prepstrip /paludis/repositories/e/ebuild/utils/print_exports +/paludis/repositories/e/ebuild/utils/strip_tar_corruption /paludis/repositories/e/ebuild/utils/unpaxinate /paludis/repositories/e/exndbam_repository_TEST /paludis/repositories/e/fetch_visitor_TEST diff --git a/paludis/repositories/e/ebuild/utils/Makefile.am b/paludis/repositories/e/ebuild/utils/Makefile.am index 934337acd..53c3135c7 100644 --- a/paludis/repositories/e/ebuild/utils/Makefile.am +++ b/paludis/repositories/e/ebuild/utils/Makefile.am @@ -59,7 +59,8 @@ libexecprog_SCRIPTS = \ libexecbindir = $(libexecdir)/paludis/utils libexecbin_PROGRAMS = \ print_exports \ - locked_pipe_command + locked_pipe_command \ + strip_tar_corruption if ENABLE_PBINS libexecbin_PROGRAMS += \ @@ -73,6 +74,7 @@ endif print_exports_SOURCES = print_exports.cc locked_pipe_command_SOURCES = locked_pipe_command.cc +strip_tar_corruption_SOURCES = strip_tar_corruption.cc AM_CXXFLAGS = -I$(top_srcdir) @PALUDIS_CXXFLAGS@ diff --git a/paludis/repositories/e/ebuild/utils/strip_tar_corruption.cc b/paludis/repositories/e/ebuild/utils/strip_tar_corruption.cc new file mode 100644 index 000000000..46ac176d5 --- /dev/null +++ b/paludis/repositories/e/ebuild/utils/strip_tar_corruption.cc @@ -0,0 +1,108 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2012 Ciaran McCreesh + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <iostream> +#include <sstream> +#include <string> +#include <cstdlib> + +int main(int, char * argv[]) +{ + char buf[512]; + uint64_t records_to_skip(0); + int zero_records(0); + while (std::cin.read(buf, 512)) + { + int bufn(std::cin.gcount()); + if (bufn < 512) + { + std::cout.write(buf, bufn); + std::cerr << argv[0] << ": Unexpectedly short tar file" << std::endl; + return EXIT_FAILURE; + } + + if (records_to_skip > 0) + { + --records_to_skip; + std::cout.write(buf, 512); + continue; + } + + if (std::string::npos == std::string(buf, 512).find_first_not_of(std::string("\0", 1))) + { + if (zero_records < 2) + std::cout.write(buf, 512); + ++zero_records; + continue; + } + + if (zero_records > 0) + { + if (zero_records >= 2) + { + if (std::string(buf, 8) == std::string("\xa6\x4c\x32\x2e\xd6\x14\xae\xdb")) + std::cerr << argv[0] << ": Found trailing corruption from pixz after " + << zero_records << " zero records, discarding it" << std::endl; + else + std::cerr << argv[0] << ": Found trailing corruption in an unrecognised format after " + << zero_records << " zero records, discarding it" << std::endl; + } + else + std::cerr << argv[0] << ": Found trailing corruption after a single zero record, discarding it" << std::endl; + + while (std::cin.read(buf, 512)) + { + /* nothing */ + } + + return EXIT_SUCCESS; + } + + zero_records = 0; + + switch (buf[156]) + { + case '2': case '3': case '4': case '5': case '6': + records_to_skip = 0; + break; + + default: + { + std::string s(&buf[124], 12); + std::stringstream ss(s); + if (! (ss >> std::oct >> records_to_skip)) + { + std::cerr << argv[0] << ": Unable to determine how many records to skip from '" << s << "'" << std::endl; + return EXIT_FAILURE; + } + + records_to_skip = (records_to_skip + 511) / 512; + } + } + + std::cout.write(buf, 512); + } + + if (zero_records < 2) + std::cerr << argv[0] << ": tar file does not include an EOF marker" << std::endl; + + return EXIT_SUCCESS; +} + + diff --git a/paludis/repositories/e/ebuild/utils/unpack b/paludis/repositories/e/ebuild/utils/unpack index 45ff5904e..aebe57cee 100755 --- a/paludis/repositories/e/ebuild/utils/unpack +++ b/paludis/repositories/e/ebuild/utils/unpack @@ -1,7 +1,7 @@ #!/usr/bin/env bash # vim: set sw=4 sts=4 et : -# Copyright (c) 2006, 2007, 2008 Ciaran McCreesh +# Copyright (c) 2006, 2007, 2008, 2011 Ciaran McCreesh # # Based in part upon ebuild.sh from Portage, which is Copyright 1995-2005 # Gentoo Foundation and distributed under the terms of the GNU General @@ -30,6 +30,7 @@ die() fi exit 123 } + assert() { local _pipestatus="${PIPESTATUS[*]}" @@ -82,8 +83,8 @@ unpack_a() { } unpack_tar.lzma() { - echo lzma -dc "${1}" \| tar xf - --no-same-owner "${TAR_OPTIONS[@]}" - lzma -dc "${1}" | tar xf - --no-same-owner "${TAR_OPTIONS[@]}" + echo lzma -dc "${1}" \| strip_tar_corruption \| tar xf - --no-same-owner "${TAR_OPTIONS[@]}" + lzma -dc "${1}" | strip_tar_corruption | tar xf - --no-same-owner "${TAR_OPTIONS[@]}" assert "Couldn't unpack ${1}" } @@ -98,8 +99,8 @@ unpack_7z() { } unpack_tar.xz() { - echo xz -dc "${1}" \| tar xf - --no-same-owner "${TAR_OPTIONS[@]}" - xz -dc "${1}" | tar xf - --no-same-owner "${TAR_OPTIONS[@]}" + echo xz -dc "${1}" \| strip_tar_corruption \| tar xf - --no-same-owner "${TAR_OPTIONS[@]}" + xz -dc "${1}" | strip_tar_corruption | tar xf - --no-same-owner "${TAR_OPTIONS[@]}" assert "Couldn't unpack ${1}" } |