strings-helper 3.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/perl -w
# strings -- lintian collection script

# Copyright (C) 2012 Niels Thykier <niels@thykier.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

22 23
no lib '.';

24 25
use strict;
use warnings;
26
use autodie;
27 28 29

use File::Basename qw(dirname);
use IO::Handle;
30
use POSIX qw(WNOHANG);
31 32 33 34 35

my ($strdir) = @ARGV;
my $curfname = '';
my $curdir = '';

36 37
my (%seen, $out);
my @active_processes;
38

39
while (my $line = <STDIN>) {
40 41 42 43
    my ($fname, $string);
    chomp $line;
    ($fname, $string) = ($line =~ m/^([^:]++): (.++)$/o);
    if ($curfname ne $fname) {
44
        my $pid;
45 46
        # new file, prepare for it.
        if ($out) {
47
            close($out);
48
            undef(%seen);
49
            reap_pids(WNOHANG);
50
        }
51
        my $dir = $strdir . '/' . dirname($fname);
52
        if ($dir ne $curdir) {
53 54
            system('mkdir', '-p', $dir) == 0
              or die "mkdir -p $dir failed: " . (($? >> 8) & 256), "\n";
55 56
            $curdir = $dir;
        }
57 58 59
        ($out, $pid) = fork_gzip("$strdir/${fname}.gz");
        $out->blocking(1);
        push(@active_processes, $pid);
60 61
        $curfname = $fname;
    }
62
    next if $seen{$string}++;
63
    print {$out} "$string\n";
64 65 66
}

if ($out) {
67
    close($out);
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 109 110 111 112 113
reap_pids(0);

sub reap_pids {
    my ($waitpid_flags) = @_;
    my $i = 0;
    for my $pid (@active_processes) {
        my $res = waitpid($pid, $waitpid_flags);
        if ($res != $pid) {
            # If we were given WNOHANG, then it we try to reap as much
            # as possible and stop at the first failure (to keep it
            # simple).
            die("waitpid($pid, 0) failed: $!\n") if not $waitpid_flags;
            last;
        }
        $i++;
        if ($?) {
            my $res = ($? >> 8) & 0xff;
            my $sig = $? & 0xff;
            if ($res) {
                die("gzip failed exit code $res\n");
            } elsif ($sig) {
                die("gzip killed by signal $sig\n");
            }
        }
    }
    splice(@active_processes, 0, $i) if $i;
    return;
}

sub fork_gzip {
    my ($out) = @_;
    my ($gzip_stdin, $to_gzip);
    pipe($gzip_stdin, $to_gzip);
    my $pid = fork;
    if (not $pid) {
        # child
        close($to_gzip);
        open(STDIN, '<&', $gzip_stdin);
        open(STDOUT, '>', $out);
        exec('gzip', '-9nc');
    }
    close($gzip_stdin);
    return ($to_gzip, $pid);
}
114
exit 0;