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 lib ".";
20 70b47196 2023-03-29 op use GotMArc qw(parse);
21 70b47196 2023-03-29 op
22 27363023 2022-08-25 op my $jobs = $ENV{'MAKE_JOBS'} // 1;
23 27363023 2022-08-25 op
24 70b47196 2023-03-29 op my $csumdir = $ENV{'CSUMDIR'};
25 70b47196 2023-03-29 op die '$CSUMDIR undefined' unless defined $csumdir;
26 70b47196 2023-03-29 op
27 27363023 2022-08-25 op my $poll = IO::Poll->new();
28 27363023 2022-08-25 op for (1..$jobs) {
29 f43c847e 2023-05-05 op open(my $kid, '|-', 'mexp') or die "can't exec mexp: $!";
30 27363023 2022-08-25 op $poll->mask($kid => POLLOUT);
31 27363023 2022-08-25 op }
32 27363023 2022-08-25 op
33 70b47196 2023-03-29 op # get current thread checksum
34 70b47196 2023-03-29 op sub thrsum {
35 70b47196 2023-03-29 op my $sha = Digest::SHA->new(256);
36 70b47196 2023-03-29 op $sha->add(encode('UTF-8', $_)) for @_;
37 70b47196 2023-03-29 op return $sha->hexdigest;
38 70b47196 2023-03-29 op }
39 70b47196 2023-03-29 op
40 70b47196 2023-03-29 op # get stored thread checksum
41 70b47196 2023-03-29 op sub oldsum {
42 70b47196 2023-03-29 op my $tid = shift;
43 70b47196 2023-03-29 op open my $fh, '<', "$csumdir/$tid" or return "";
44 70b47196 2023-03-29 op my $sum = <$fh>;
45 70b47196 2023-03-29 op chomp $sum;
46 70b47196 2023-03-29 op return $sum;
47 70b47196 2023-03-29 op }
48 70b47196 2023-03-29 op
49 70b47196 2023-03-29 op # save thread checksum
50 70b47196 2023-03-29 op sub savesum {
51 70b47196 2023-03-29 op my ($tid, $sum) = @_;
52 70b47196 2023-03-29 op open my $fh, '>', "$csumdir/$tid"
53 70b47196 2023-03-29 op or die "can't open checksum file $csumdir/$tid: $!";
54 70b47196 2023-03-29 op say $fh $sum;
55 70b47196 2023-03-29 op close $fh;
56 70b47196 2023-03-29 op }
57 70b47196 2023-03-29 op
58 27363023 2022-08-25 op sub process {
59 70b47196 2023-03-29 op my @entries = @_;
60 70b47196 2023-03-29 op
61 70b47196 2023-03-29 op return unless @entries;
62 70b47196 2023-03-29 op
63 6b36ff28 2023-04-01 op my $mail = parse $entries[0];
64 bbdbef1a 2023-04-01 op die "wtf?" if $mail->{level} != 0;
65 70b47196 2023-03-29 op
66 bbdbef1a 2023-04-01 op my $tid = $mail->{mid};
67 70b47196 2023-03-29 op my $thrsum = thrsum @_;
68 70b47196 2023-03-29 op my $oldsum = oldsum $tid;
69 70b47196 2023-03-29 op return if $thrsum eq $oldsum;
70 70b47196 2023-03-29 op
71 27363023 2022-08-25 op die "poll: $!" if $poll->poll() == -1;
72 27363023 2022-08-25 op my @handles = $poll->handles(POLLOUT) or die "no procs ready?";
73 27363023 2022-08-25 op my $handle = $handles[int(rand(@handles))];
74 70b47196 2023-03-29 op print $handle $_ foreach @entries;
75 70b47196 2023-03-29 op
76 70b47196 2023-03-29 op savesum($tid, $thrsum);
77 27363023 2022-08-25 op }
78 27363023 2022-08-25 op
79 70b47196 2023-03-29 op unveil($csumdir, "rwc") or die "unveil $csumdir: $!";
80 70b47196 2023-03-29 op pledge("stdio rpath wpath cpath") or die "pledge: $!";
81 054f3fd4 2022-08-25 op
82 27363023 2022-08-25 op my @thread;
83 27363023 2022-08-25 op while (<>) {
84 27363023 2022-08-25 op print; # continue the pipeline
85 27363023 2022-08-25 op
86 de557185 2022-08-26 op my $new_thread = m/^-/;
87 de557185 2022-08-26 op if ($new_thread && @thread) {
88 27363023 2022-08-25 op process @thread;
89 27363023 2022-08-25 op @thread = ();
90 27363023 2022-08-25 op }
91 27363023 2022-08-25 op
92 27363023 2022-08-25 op push @thread, $_;
93 27363023 2022-08-25 op }
94 27363023 2022-08-25 op
95 27363023 2022-08-25 op process @thread if @thread;