aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/ebuild/utils/strip_tar_corruption.cc
blob: 46ac176d5202be990d3a0870b93aefb18175e2b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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;
}