]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Import.pm
import: initial module + test case
[public-inbox.git] / lib / PublicInbox / Import.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # git fast-import-based ssoma-mda MDA replacement
5 # This is only ever run by public-inbox-mda and public-inbox-learn,
6 # not the WWW or NNTP code which only requires read-only access.
7 package PublicInbox::Import;
8 use strict;
9 use warnings;
10 use Fcntl qw(:flock :DEFAULT);
11 use Email::Address;
12 use PublicInbox::Spawn qw(spawn);
13 use PublicInbox::MID qw(mid_mime mid2path);
14
15 sub new {
16         my ($class, $git, $name, $email) = @_;
17         bless {
18                 git => $git,
19                 ident => "$name <$email>",
20                 mark => 1,
21                 ref => 'refs/heads/master',
22         }, $class
23 }
24
25 # idempotent start function
26 sub gfi_start {
27         my ($self) = @_;
28
29         return ($self->{in}, $self->{out}) if $self->{pid};
30
31         my ($in_r, $in_w, $out_r, $out_w);
32         pipe($in_r, $in_w) or die "pipe failed: $!";
33         pipe($out_r, $out_w) or die "pipe failed: $!";
34         my $git = $self->{git};
35         my $git_dir = $git->{git_dir};
36         my $lockpath = "$git_dir/ssoma.lock";
37         sysopen(my $lockfh, $lockpath, O_WRONLY|O_CREAT) or
38                 die "failed to open lock $lockpath: $!";
39
40         # wait for other processes to be done
41         flock($lockfh, LOCK_EX) or die "lock failed: $!\n";
42         chomp($self->{tip} = $git->qx(qw(rev-parse --revs-only), $self->{ref}));
43
44         my @cmd = ('git', "--git-dir=$git_dir", qw(fast-import
45                         --quiet --done --date-format=rfc2822));
46         my $rdr = { 0 => fileno($out_r), 1 => fileno($in_w) };
47         my $pid = spawn(\@cmd, undef, $rdr);
48         die "spawn failed: $!" unless defined $pid;
49         $out_w->autoflush(1);
50         $self->{in} = $in_r;
51         $self->{out} = $out_w;
52         $self->{lockfh} = $lockfh;
53         $self->{pid} = $pid;
54         ($in_r, $out_w);
55 }
56
57 sub wfail () { die "write to fast-import failed: $!" }
58
59 sub now2822 () {
60         my @t = gmtime(time);
61         my $day = qw(Sun Mon Tue Wed Thu Fri Sat)[$t[6]];
62         my $mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$t[4]];
63
64         sprintf('%s, %2d %s %d %02d:%02d:%02d +0000',
65                 $day, $t[3], $mon, $t[5] + 1900, $t[2], $t[1], $t[0]);
66 }
67
68 # returns undef on non-existent
69 # (-1, msg) on mismatch
70 # (:MARK, msg) on success
71 sub remove {
72         my ($self, $mime) = @_; # mime = Email::MIME
73
74         my $mid = mid_mime($mime);
75         my $path = mid2path($mid);
76
77         my ($r, $w) = $self->gfi_start;
78         my $tip = $self->{tip};
79         return if $tip eq '';
80
81         print $w "ls $tip $path\n" or wfail;
82         local $/ = "\n";
83         my $check = <$r>;
84         defined $check or die "EOF from fast-import / ls: $!";
85         return if $check =~ /\Amissing /;
86         $check =~ m!\A100644 blob ([a-f0-9]{40})\t!s or die "not blob: $check";
87         my $blob = $1;
88         print $w "cat-blob $blob\n" or wfail;
89         $check = <$r>;
90         defined $check or die "EOF from fast-import / cat-blob: $!";
91         $check =~ /\A[a-f0-9]{40} blob (\d+)\n\z/ or
92                                 die "unexpected cat-blob response: $check";
93         my $left = $1;
94         my $offset = 0;
95         my $buf = '';
96         while ($left > 0) {
97                 my $n = read($r, $buf, $left, $offset);
98                 defined($n) or die "read cat-blob failed: $!";
99                 $n == 0 and die 'fast-export (cat-blob) died';
100                 $left -= $n;
101                 $offset += $n;
102         }
103         read($r, my $lf, 1);
104         die "bad read on final byte: <$lf>" if $lf ne "\n";
105         my $cur = Email::MIME->new($buf);
106         if ($cur->header('Subject') ne $mime->header('Subject') ||
107                         $cur->body ne $mime->body) {
108                 return (-1, $cur);
109         }
110
111         my $ref = $self->{ref};
112         my $commit = $self->{mark}++;
113         my $parent = $tip =~ /\A:/ ? $tip : undef;
114         unless ($parent) {
115                 print $w "reset $ref\n" or wfail;
116         }
117         my $ident = $self->{ident};
118         my $now = now2822();
119         print $w "commit $ref\nmark :$commit\n",
120                 "author $ident $now\n",
121                 "committer $ident $now\n",
122                 "data 3\nrm\n\n",
123                 'from ', ($parent ? $parent : $tip), "\n" or wfail;
124         print $w "D $path\n\n" or wfail;
125         (($self->{tip} = ":$commit"), $cur);
126 }
127
128 # returns undef on duplicate
129 sub add {
130         my ($self, $mime) = @_; # mime = Email::MIME
131
132         my $from = $mime->header('From');
133         my @from = Email::Address->parse($from);
134         my $name = $from[0]->name;
135         my $email = $from[0]->address;
136         my $date = $mime->header('Date');
137         my $subject = $mime->header('Subject');
138         $subject = '(no subject)' unless defined $subject;
139         my $mid = mid_mime($mime);
140         my $path = mid2path($mid);
141
142         my ($r, $w) = $self->gfi_start;
143         my $tip = $self->{tip};
144         if ($tip ne '') {
145                 print $w "ls $tip $path\n" or wfail;
146                 local $/ = "\n";
147                 my $check = <$r>;
148                 defined $check or die "EOF from fast-import: $!";
149                 return unless $check =~ /\Amissing /;
150         }
151
152         # kill potentially confusing/misleading headers
153         $mime->header_set($_) for qw(bytes lines content-length status);
154         $mime = $mime->as_string;
155         my $blob = $self->{mark}++;
156         print $w "blob\nmark :$blob\ndata ", length($mime), "\n" or wfail;
157         print $w $mime, "\n" or wfail;
158         my $ref = $self->{ref};
159         my $commit = $self->{mark}++;
160         my $parent = $tip =~ /\A:/ ? $tip : undef;
161
162         unless ($parent) {
163                 print $w "reset $ref\n" or wfail;
164         }
165         print $w "commit $ref\nmark :$commit\n",
166                 "author $name <$email> $date\n",
167                 "committer $self->{ident} ", now2822(), "\n",
168                 "data ", (length($subject) + 1), "\n",
169                 $subject, "\n\n" or wfail;
170         if ($tip ne '') {
171                 print $w 'from ', ($parent ? $parent : $tip), "\n" or wfail;
172         }
173         print $w "M 100644 :$blob $path\n\n" or wfail;
174         $self->{tip} = ":$commit";
175 }
176
177 sub done {
178         my ($self) = @_;
179         my $w = delete $self->{out} or return;
180         my $r = delete $self->{in} or die 'BUG: missing {in} when done';
181         print $w "done\n" or wfail;
182         my $pid = delete $self->{pid} or die 'BUG: missing {pid} when done';
183         waitpid($pid, 0) == $pid or die 'fast-import did not finish';
184         $? == 0 or die "fast-import failed: $?";
185         my $lockfh = delete $self->{lockfh} or die "BUG: not locked: $!";
186         flock($lockfh, LOCK_UN) or die "unlock failed: $!";
187         close $lockfh or die "close lock failed: $!";
188 }
189
190 1;