]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Import.pm
cleanup: use '$ibx' consistently when referring to Inbox refs
[public-inbox.git] / lib / PublicInbox / Import.pm
index 73290eed5a04d37a8328eb39bd217cf2258fd6a7..fc4e72dc93ea0ce978a93f5224c74e5b8178acc3 100644 (file)
@@ -1,9 +1,10 @@
-# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # git fast-import-based ssoma-mda MDA replacement
-# This is only ever run by public-inbox-mda and public-inbox-learn,
-# not the WWW or NNTP code which only requires read-only access.
+# This is only ever run by public-inbox-mda, public-inbox-learn
+# and public-inbox-watch. Not the WWW or NNTP code which only
+# requires read-only access.
 package PublicInbox::Import;
 use strict;
 use warnings;
@@ -14,6 +15,7 @@ use PublicInbox::Address;
 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
 use PublicInbox::ContentId qw(content_digest);
 use PublicInbox::MDA;
+use POSIX qw(strftime);
 
 sub new {
        my ($class, $git, $name, $email, $ibx) = @_;
@@ -95,19 +97,13 @@ sub _check_path ($$$$) {
        $info =~ /\Amissing / ? undef : $info;
 }
 
-sub check_remove_v1 {
-       my ($r, $w, $tip, $path, $mime) = @_;
-
-       my $info = _check_path($r, $w, $tip, $path) or return ('MISSING',undef);
-       $info =~ m!\A100644 blob ([a-f0-9]{40})\t!s or die "not blob: $info";
-       my $blob = $1;
-
-       print $w "cat-blob $blob\n" or wfail;
+sub _cat_blob ($$$) {
+       my ($r, $w, $oid) = @_;
+       print $w "cat-blob $oid\n" or wfail;
        local $/ = "\n";
-       $info = <$r>;
+       my $info = <$r>;
        defined $info or die "EOF from fast-import / cat-blob: $!";
-       $info =~ /\A[a-f0-9]{40} blob (\d+)\n\z/ or
-                               die "unexpected cat-blob response: $info";
+       $info =~ /\A[a-f0-9]{40} blob (\d+)\n\z/ or return;
        my $left = $1;
        my $offset = 0;
        my $buf = '';
@@ -122,7 +118,26 @@ sub check_remove_v1 {
        $n = read($r, my $lf, 1);
        defined($n) or die "read final byte of cat-blob failed: $!";
        die "bad read on final byte: <$lf>" if $lf ne "\n";
-       my $cur = PublicInbox::MIME->new(\$buf);
+
+       # fixup some bugginess in old versions:
+       $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+       \$buf;
+}
+
+sub cat_blob {
+       my ($self, $oid) = @_;
+       my ($r, $w) = $self->gfi_start;
+       _cat_blob($r, $w, $oid);
+}
+
+sub check_remove_v1 {
+       my ($r, $w, $tip, $path, $mime) = @_;
+
+       my $info = _check_path($r, $w, $tip, $path) or return ('MISSING',undef);
+       $info =~ m!\A100644 blob ([a-f0-9]{40})\t!s or die "not blob: $info";
+       my $oid = $1;
+       my $msg = _cat_blob($r, $w, $oid) or die "BUG: cat-blob $1 failed";
+       my $cur = PublicInbox::MIME->new($msg);
        my $cur_s = $cur->header('Subject');
        $cur_s = '' unless defined $cur_s;
        my $cur_m = $mime->header('Subject');
@@ -163,8 +178,8 @@ sub _update_git_info ($$) {
        run_die([@cmd, 'update-server-info'], undef);
        ($self->{path_type} eq '2/38') and eval {
                require PublicInbox::SearchIdx;
-               my $inbox = $self->{inbox} || $git_dir;
-               my $s = PublicInbox::SearchIdx->new($inbox);
+               my $ibx = $self->{inbox} || $git_dir;
+               my $s = PublicInbox::SearchIdx->new($ibx);
                $s->index_sync({ ref => $self->{ref} });
        };
        eval { run_die([@cmd, qw(gc --auto)], undef) } if $do_gc;
@@ -192,6 +207,7 @@ sub get_mark {
        my ($r, $w) = $self->gfi_start;
        print $w "get-mark $mark\n" or wfail;
        defined(my $oid = <$r>) or die "get-mark failed, need git 2.6.0+\n";
+       chomp($oid);
        $oid;
 }
 
@@ -263,10 +279,12 @@ sub extract_author_info ($) {
 
        my $sender = '';
        my $from = $mime->header('From');
+       $from ||= '';
        my ($email) = PublicInbox::Address::emails($from);
        my ($name) = PublicInbox::Address::names($from);
        if (!defined($name) || !defined($email)) {
                $sender = $mime->header('Sender');
+               $sender ||= '';
                if (!defined($name)) {
                        ($name) = PublicInbox::Address::names($sender);
                }
@@ -319,7 +337,7 @@ sub v1_mid0 ($) {
        my $mids = mids($hdr);
 
        if (!scalar(@$mids)) { # spam often has no Message-Id
-               my $mid0 = digest2mid(content_digest($mime));
+               my $mid0 = digest2mid(content_digest($mime), $hdr);
                append_mid($hdr, $mid0);
                return $mid0;
        }
@@ -379,7 +397,7 @@ sub add {
 
        # v2: we need this for Xapian
        if ($self->{want_object_info}) {
-               chomp(my $oid = $self->get_mark(":$blob"));
+               my $oid = $self->get_mark(":$blob");
                $self->{last_object} = [ $oid, $n, \$str ];
        }
        my $ref = $self->{ref};
@@ -390,6 +408,8 @@ sub add {
                print $w "reset $ref\n" or wfail;
        }
 
+       # Mime decoding can create nulls replace them with spaces to protect git
+       $subject =~ tr/\0/ /;
        utf8::encode($subject);
        print $w "commit $ref\nmark :$commit\n",
                "author $name <$email> $author_time_raw\n",
@@ -425,27 +445,31 @@ sub done {
        _update_git_info($self, 1) if delete $self->{nchg};
 
        $self->lock_release;
+
+       $self->{git}->cleanup;
 }
 
 sub atfork_child {
        my ($self) = @_;
        foreach my $f (qw(in out)) {
+               next unless defined($self->{$f});
                close $self->{$f} or die "failed to close import[$f]: $!\n";
        }
 }
 
-sub digest2mid ($) {
-       my ($dig) = @_;
+sub digest2mid ($$) {
+       my ($dig, $hdr) = @_;
        my $b64 = $dig->clone->b64digest;
        # Make our own URLs nicer:
        # See "Base 64 Encoding with URL and Filename Safe Alphabet" in RFC4648
        $b64 =~ tr!+/=!-_!d;
 
-       # We can make this more meaningful with a date prefix or other things,
-       # but this is only needed for crap that fails to generate a Message-ID
-       # or reuses one.  In other words, it's usually spammers who hit this
-       # so they don't deserve nice Message-IDs :P
-       $b64 . '@localhost';
+       # Add a date prefix to prevent a leading '-' in case that trips
+       # up some tools (e.g. if a Message-ID were a expected as a
+       # command-line arg)
+       my $dt = msg_datestamp($hdr);
+       $dt = POSIX::strftime('%Y%m%d%H%M%S', gmtime($dt));
+       "$dt.$b64" . '@z';
 }
 
 sub clean_purge_buffer {
@@ -471,7 +495,7 @@ sub purge_oids {
        my $old = $self->{'ref'};
        my $git = $self->{git};
        my @export = (qw(fast-export --no-data --use-done-feature), $old);
-       my ($rd, $pid) = $git->popen(@export);
+       my $rd = $git->popen(@export);
        my ($r, $w) = $self->gfi_start;
        my @buf;
        my $npurge = 0;
@@ -496,11 +520,12 @@ sub purge_oids {
                        push @buf, $buf;
                } elsif (/^M 100644 ([a-f0-9]+) (\w+)/) {
                        my ($oid, $path) = ($1, $2);
+                       $tree->{$path} = 1;
                        if ($purge->{$oid}) {
                                push @oids, $oid;
-                               delete $tree->{$path};
+                               my $cmd = "M 100644 inline $path\ndata 0\n\n";
+                               push @buf, $cmd;
                        } else {
-                               $tree->{$path} = 1;
                                push @buf, $_;
                        }
                } elsif (/^D (\w+)/) {
@@ -525,6 +550,7 @@ sub purge_oids {
                        push @buf, $_;
                }
        }
+       close $rd or die "close fast-export failed: $?";
        if (@buf) {
                $w->print(@buf) or wfail;
        }
@@ -611,8 +637,8 @@ version 1.0
 =head1 DESCRIPTION
 
 An importer and remover for public-inboxes which takes L<Email::MIME>
-messages as input and stores them in a ssoma repository as
-documented in L<https://ssoma.public-inbox.org/ssoma_repository.txt>,
+messages as input and stores them in a git repository as
+documented in L<https://public-inbox.org/public-inbox-v1-format.txt>,
 except it does not allow duplicate Message-IDs.
 
 It requires L<git(1)> and L<git-fast-import(1)> to be installed.