Blame


1 27363023 2022-08-25 op #!/usr/bin/env perl
2 1bcb9899 2022-08-29 op #
3 1bcb9899 2022-08-29 op # pe was written by Omar Polo <op@openbsd.org> and is placed in the
4 1bcb9899 2022-08-29 op # public domain. The author hereby disclaims copyright to this source
5 1bcb9899 2022-08-29 op # code.
6 27363023 2022-08-25 op
7 27363023 2022-08-25 op use open ":std", ":encoding(UTF-8)";
8 27363023 2022-08-25 op use strict;
9 27363023 2022-08-25 op use warnings;
10 27363023 2022-08-25 op use v5.32;
11 70b47196 2023-03-29 op
12 70b47196 2023-03-29 op use Digest::SHA;
13 70b47196 2023-03-29 op use Encode qw(encode);
14 27363023 2022-08-25 op use IO::Poll qw(POLLOUT);
15 27363023 2022-08-25 op
16 054f3fd4 2022-08-25 op use OpenBSD::Pledge;
17 054f3fd4 2022-08-25 op use OpenBSD::Unveil;
18 054f3fd4 2022-08-25 op
19 70b47196 2023-03-29 op use GotMArc qw(parse);
20 70b47196 2023-03-29 op
21 27363023 2022-08-25 op my $jobs = $ENV{'MAKE_JOBS'} // 1;
22 27363023 2022-08-25 op
23 70b47196 2023-03-29 op my $csumdir = $ENV{'CSUMDIR'};
24 70b47196 2023-03-29 op die '$CSUMDIR undefined' unless defined $csumdir;
25 70b47196 2023-03-29 op
26 27363023 2022-08-25 op my $poll = IO::Poll->new();
27 27363023 2022-08-25 op for (1..$jobs) {
28 f43c847e 2023-05-05 op open(my $kid, '|-', 'mexp') or die "can't exec mexp: $!";
29 27363023 2022-08-25 op $poll->mask($kid => POLLOUT);
30 27363023 2022-08-25 op }
31 27363023 2022-08-25 op
32 70b47196 2023-03-29 op # get current thread checksum
33 70b47196 2023-03-29 op sub thrsum {
34 70b47196 2023-03-29 op my $sha = Digest::SHA->new(256);
35 70b47196 2023-03-29 op $sha->add(encode('UTF-8', $_)) for @_;
36 70b47196 2023-03-29 op return $sha->hexdigest;
37 70b47196 2023-03-29 op }
38 70b47196 2023-03-29 op
39 70b47196 2023-03-29 op # get stored thread checksum
40 70b47196 2023-03-29 op sub oldsum {
41 70b47196 2023-03-29 op my $tid = shift;
42 70b47196 2023-03-29 op open my $fh, '<', "$csumdir/$tid" or return "";
43 70b47196 2023-03-29 op my $sum = <$fh>;
44 70b47196 2023-03-29 op chomp $sum;
45 70b47196 2023-03-29 op return $sum;
46 70b47196 2023-03-29 op }
47 70b47196 2023-03-29 op
48 70b47196 2023-03-29 op # save thread checksum
49 70b47196 2023-03-29 op sub savesum {
50 70b47196 2023-03-29 op my ($tid, $sum) = @_;
51 70b47196 2023-03-29 op open my $fh, '>', "$csumdir/$tid"
52 70b47196 2023-03-29 op or die "can't open checksum file $csumdir/$tid: $!";
53 70b47196 2023-03-29 op say $fh $sum;
54 70b47196 2023-03-29 op close $fh;
55 70b47196 2023-03-29 op }
56 70b47196 2023-03-29 op
57 27363023 2022-08-25 op sub process {
58 70b47196 2023-03-29 op my @entries = @_;
59 70b47196 2023-03-29 op
60 70b47196 2023-03-29 op return unless @entries;
61 70b47196 2023-03-29 op
62 6b36ff28 2023-04-01 op my $mail = parse $entries[0];
63 bbdbef1a 2023-04-01 op die "wtf?" if $mail->{level} != 0;
64 70b47196 2023-03-29 op
65 bbdbef1a 2023-04-01 op my $tid = $mail->{mid};
66 70b47196 2023-03-29 op my $thrsum = thrsum @_;
67 70b47196 2023-03-29 op my $oldsum = oldsum $tid;
68 70b47196 2023-03-29 op return if $thrsum eq $oldsum;
69 70b47196 2023-03-29 op
70 27363023 2022-08-25 op die "poll: $!" if $poll->poll() == -1;
71 27363023 2022-08-25 op my @handles = $poll->handles(POLLOUT) or die "no procs ready?";
72 27363023 2022-08-25 op my $handle = $handles[int(rand(@handles))];
73 70b47196 2023-03-29 op print $handle $_ foreach @entries;
74 70b47196 2023-03-29 op
75 70b47196 2023-03-29 op savesum($tid, $thrsum);
76 27363023 2022-08-25 op }
77 27363023 2022-08-25 op
78 70b47196 2023-03-29 op unveil($csumdir, "rwc") or die "unveil $csumdir: $!";
79 70b47196 2023-03-29 op pledge("stdio rpath wpath cpath") or die "pledge: $!";
80 054f3fd4 2022-08-25 op
81 27363023 2022-08-25 op my @thread;
82 27363023 2022-08-25 op while (<>) {
83 27363023 2022-08-25 op print; # continue the pipeline
84 27363023 2022-08-25 op
85 de557185 2022-08-26 op my $new_thread = m/^-/;
86 de557185 2022-08-26 op if ($new_thread && @thread) {
87 27363023 2022-08-25 op process @thread;
88 27363023 2022-08-25 op @thread = ();
89 27363023 2022-08-25 op }
90 27363023 2022-08-25 op
91 27363023 2022-08-25 op push @thread, $_;
92 27363023 2022-08-25 op }
93 27363023 2022-08-25 op
94 27363023 2022-08-25 op process @thread if @thread;