1 27363023 2022-08-25 op #!/usr/bin/env perl
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
7 27363023 2022-08-25 op use open ":std", ":encoding(UTF-8)";
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);
16 054f3fd4 2022-08-25 op use OpenBSD::Pledge;
17 054f3fd4 2022-08-25 op use OpenBSD::Unveil;
19 70b47196 2023-03-29 op use GotMArc qw(parse);
21 27363023 2022-08-25 op my $jobs = $ENV{'MAKE_JOBS'} // 1;
23 70b47196 2023-03-29 op my $csumdir = $ENV{'CSUMDIR'};
24 70b47196 2023-03-29 op die '$CSUMDIR undefined' unless defined $csumdir;
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);
32 70b47196 2023-03-29 op # get current thread checksum
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;
39 70b47196 2023-03-29 op # get stored thread checksum
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>;
48 70b47196 2023-03-29 op # save thread checksum
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: $!";
58 70b47196 2023-03-29 op my @entries = @_;
60 70b47196 2023-03-29 op return unless @entries;
62 6b36ff28 2023-04-01 op my $mail = parse $entries[0];
63 bbdbef1a 2023-04-01 op die "wtf?" if $mail->{level} != 0;
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;
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;
75 70b47196 2023-03-29 op savesum($tid, $thrsum);
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: $!";
83 27363023 2022-08-25 op print; # continue the pipeline
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;
91 27363023 2022-08-25 op push @thread, $_;
94 27363023 2022-08-25 op process @thread if @thread;