]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
wwwstream: always show multi-line cloning instructions
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index 66f8a8a3f181948332ac28b396b1d4990108094d..152d90ab3ea595fd56c8b7643831fd28fbecbdbb 100644 (file)
@@ -15,7 +15,8 @@ use PublicInbox::ContentId qw(content_id content_digest);
 use PublicInbox::Inbox;
 use PublicInbox::OverIdx;
 use PublicInbox::Msgmap;
-use PublicInbox::Spawn;
+use PublicInbox::Spawn qw(spawn);
+use PublicInbox::SearchIdx;
 use IO::Handle;
 
 # an estimate of the post-packed size to the raw uncompressed size
@@ -62,6 +63,7 @@ sub new {
        }
 
        $v2ibx = PublicInbox::InboxWritable->new($v2ibx);
+       $v2ibx->umask_prepare;
 
        my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
        my $self = {
@@ -81,11 +83,14 @@ sub new {
 }
 
 sub init_inbox {
-       my ($self, $parallel) = @_;
+       my ($self, $parallel, $skip) = @_;
        $self->{parallel} = $parallel;
        $self->idx_init;
        my $epoch_max = -1;
        git_dir_latest($self, \$epoch_max);
+       if (defined $skip && $epoch_max == -1) {
+               $epoch_max = $skip;
+       }
        $self->git_init($epoch_max >= 0 ? $epoch_max : 0);
        $self->done;
 }
@@ -94,6 +99,13 @@ sub init_inbox {
 # mimics Import::add and wraps it for v2
 sub add {
        my ($self, $mime, $check_cb) = @_;
+       $self->{-inbox}->with_umask(sub {
+               _add($self, $mime, $check_cb)
+       });
+}
+
+sub _add {
+       my ($self, $mime, $check_cb) = @_;
 
        # spam check:
        if ($check_cb) {
@@ -174,19 +186,19 @@ sub num_for_harder {
 
        my $hdr = $mime->header_obj;
        my $dig = content_digest($mime);
-       $$mid0 = PublicInbox::Import::digest2mid($dig);
+       $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
        my $num = $self->{mm}->mid_insert($$mid0);
        unless (defined $num) {
                # it's hard to spoof the last Received: header
                my @recvd = $hdr->header_raw('Received');
                $dig->add("Received: $_") foreach (@recvd);
-               $$mid0 = PublicInbox::Import::digest2mid($dig);
+               $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
                $num = $self->{mm}->mid_insert($$mid0);
 
                # fall back to a random Message-ID and give up determinism:
                until (defined($num)) {
                        $dig->add(rand);
-                       $$mid0 = PublicInbox::Import::digest2mid($dig);
+                       $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
                        warn "using random Message-ID <$$mid0> as fallback\n";
                        $num = $self->{mm}->mid_insert($$mid0);
                }
@@ -259,12 +271,32 @@ sub purge_oids {
        $purges;
 }
 
+sub content_ids ($) {
+       my ($mime) = @_;
+       my @cids = ( content_id($mime) );
+
+       # Email::MIME->as_string doesn't always round-trip, so we may
+       # use a second content_id
+       my $rt = content_id(PublicInbox::MIME->new(\($mime->as_string)));
+       push @cids, $rt if $cids[0] ne $rt;
+       \@cids;
+}
+
+sub content_matches ($$) {
+       my ($cids, $existing) = @_;
+       my $cid = content_id($existing);
+       foreach (@$cids) {
+               return 1 if $_ eq $cid
+       }
+       0
+}
+
 sub remove_internal {
        my ($self, $mime, $cmt_msg, $purge) = @_;
        $self->idx_init;
        my $im = $self->importer unless $purge;
        my $over = $self->{over};
-       my $cid = content_id($mime);
+       my $cids = content_ids($mime);
        my $parts = $self->{idx_parts};
        my $mm = $self->{mm};
        my $removed;
@@ -287,7 +319,7 @@ sub remove_internal {
                        }
                        my $orig = $$msg;
                        my $cur = PublicInbox::MIME->new($msg);
-                       if (content_id($cur) eq $cid) {
+                       if (content_matches($cids, $cur)) {
                                $smsg->{mime} = $cur;
                                $gone{$smsg->{num}} = [ $smsg, \$orig ];
                        }
@@ -327,12 +359,16 @@ sub remove_internal {
 
 sub remove {
        my ($self, $mime, $cmt_msg) = @_;
-       remove_internal($self, $mime, $cmt_msg);
+       $self->{-inbox}->with_umask(sub {
+               remove_internal($self, $mime, $cmt_msg);
+       });
 }
 
 sub purge {
        my ($self, $mime) = @_;
-       my $purges = remove_internal($self, $mime, undef, {});
+       my $purges = $self->{-inbox}->with_umask(sub {
+               remove_internal($self, $mime, undef, {});
+       });
        $self->idx_init if @$purges; # ->done is called on purges
        for my $i (0..$#$purges) {
                defined(my $cmt = $purges->[$i]) or next;
@@ -462,15 +498,21 @@ sub git_init {
        PublicInbox::Import::run_die(\@cmd);
 
        my $alt = "$all/objects/info/alternates";
-       my $new_obj_dir = "../../git/$epoch.git/objects";
        my %alts;
+       my @add;
        if (-e $alt) {
                open(my $fh, '<', $alt) or die "open < $alt: $!\n";
                %alts = map { chomp; $_ => 1 } (<$fh>);
        }
-       return $git_dir if $alts{$new_obj_dir};
+       foreach my $i (0..$epoch) {
+               my $dir = "../../git/$i.git/objects";
+               push @add, $dir if !$alts{$dir} && -d "$pfx/$i.git";
+       }
+       return $git_dir unless @add;
        open my $fh, '>>', $alt or die "open >> $alt: $!\n";
-       print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
+       foreach my $dir (@add) {
+               print $fh "$dir\n" or die "print >> $alt: $!\n";
+       }
        close $fh or die "close $alt: $!\n";
        $git_dir
 }
@@ -541,7 +583,6 @@ sub import_init {
 sub diff ($$$) {
        my ($mid, $cur, $new) = @_;
        use File::Temp qw(tempfile);
-       use PublicInbox::Spawn qw(spawn);
 
        my ($ah, $an) = tempfile('email-cur-XXXXXXXX', TMPDIR => 1);
        print $ah $cur->as_string or die "print: $!";
@@ -572,8 +613,7 @@ sub get_blob ($$) {
 sub lookup_content {
        my ($self, $mime, $mid) = @_;
        my $over = $self->{over};
-       my $cid = content_id($mime);
-       my $found;
+       my $cids = content_ids($mime);
        my ($id, $prev);
        while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
                my $msg = get_blob($self, $smsg);
@@ -582,16 +622,16 @@ sub lookup_content {
                        next;
                }
                my $cur = PublicInbox::MIME->new($msg);
-               if (content_id($cur) eq $cid) {
+               if (content_matches($cids, $cur)) {
                        $smsg->{mime} = $cur;
-                       $found = $smsg;
-                       last;
+                       return $smsg;
                }
 
+
                # XXX DEBUG_DIFF is experimental and may be removed
                diff($mid, $cur, $mime) if $ENV{DEBUG_DIFF};
        }
-       $found;
+       undef;
 }
 
 sub atfork_child {
@@ -616,12 +656,12 @@ sub mark_deleted {
        my $mids = mids($mime->header_obj);
        my $cid = content_id($mime);
        foreach my $mid (@$mids) {
-               $D->{"$mid\0$cid"} = 1;
+               $D->{"$mid\0$cid"} = $oid;
        }
 }
 
 sub reindex_oid {
-       my ($self, $mm_tmp, $D, $git, $oid, $regen) = @_;
+       my ($self, $mm_tmp, $D, $git, $oid, $regen, $reindex) = @_;
        my $len;
        my $msgref = $git->cat_file($oid, \$len);
        my $mime = PublicInbox::MIME->new($$msgref);
@@ -634,11 +674,12 @@ sub reindex_oid {
        my $num = -1;
        my $del = 0;
        foreach my $mid (@$mids) {
-               $del += (delete $D->{"$mid\0$cid"} || 0);
+               $del += delete($D->{"$mid\0$cid"}) ? 1 : 0;
                my $n = $mm_tmp->num_for($mid);
                if (defined $n && $n > $num) {
                        $mid0 = $mid;
                        $num = $n;
+                       $self->{mm}->mid_set($num, $mid0);
                }
        }
        if (!defined($mid0) && $regen && !$del) {
@@ -663,7 +704,8 @@ sub reindex_oid {
 
        if (!defined($mid0) || $del) {
                if (!defined($mid0) && $del) { # expected for deletes
-                       $$regen--;
+                       $num = $$regen--;
+                       $self->{mm}->num_highwater($num) unless $reindex;
                        return
                }
 
@@ -719,16 +761,7 @@ sub last_commits {
        $heads;
 }
 
-sub is_ancestor ($$$) {
-       my ($git, $cur, $tip) = @_;
-       return 0 unless $git->check($cur);
-       my $cmd = [ 'git', "--git-dir=$git->{git_dir}",
-               qw(merge-base --is-ancestor), $cur, $tip ];
-       my $pid = spawn($cmd);
-       defined $pid or die "spawning ".join(' ', @$cmd)." failed: $!";
-       waitpid($pid, 0) == $pid or die join(' ', @$cmd) .' did not finish';
-       $? == 0;
-}
+*is_ancestor = *PublicInbox::SearchIdx::is_ancestor;
 
 sub index_prepare {
        my ($self, $opts, $epoch_max, $ranges) = @_;
@@ -778,7 +811,7 @@ $range
                $ranges->[$i] = $range;
 
                # can't use 'rev-list --count' if we use --diff-filter
-               my $fh = $git->popen(qw(log --pretty=tformat:%h
+               my $fh = $git->popen(qw(log --pretty=tformat:%H
                                --no-notes --no-color --no-renames
                                --diff-filter=AM), $range, '--', 'm');
                ++$regen_max while <$fh>;
@@ -812,7 +845,10 @@ sub unindex_oid {
                        warn "BUG: multiple articles linked to $oid\n",
                                join(',',sort keys %gone), "\n";
                }
-               $self->{unindexed}->{$_}++ foreach keys %gone;
+               foreach my $num (keys %gone) {
+                       $self->{unindexed}->{$_}++;
+                       $self->{mm}->num_delete($num);
+               }
                $self->unindex_oid_remote($oid, $mid);
        }
 }
@@ -849,13 +885,14 @@ sub index_sync {
        return unless defined $latest;
        $self->idx_init; # acquire lock
        my $mm_tmp = $self->{mm}->tmp_clone;
-       my $ranges = $opts->{reindex} ? [] : $self->last_commits($epoch_max);
+       my $reindex = $opts->{reindex};
+       my $ranges = $reindex ? [] : $self->last_commits($epoch_max);
 
-       my ($min, $max) = $mm_tmp->minmax;
+       my $high = $self->{mm}->num_highwater();
        my $regen = $self->index_prepare($opts, $epoch_max, $ranges);
-       $$regen += $max if $max;
-       my $D = {};
-       my @cmd = qw(log --raw -r --pretty=tformat:%h
+       $$regen += $high if $high;
+       my $D = {}; # "$mid\0$cid" => $oid
+       my @cmd = qw(log --raw -r --pretty=tformat:%H
                        --no-notes --no-color --no-abbrev --no-renames);
 
        # work backwards through history
@@ -875,7 +912,7 @@ sub index_sync {
                                chomp($cmt = $_);
                        } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
                                $self->reindex_oid($mm_tmp, $D, $git, $1,
-                                               $regen);
+                                               $regen, $reindex);
                        } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\td$/o) {
                                $self->mark_deleted($D, $git, $1);
                        }
@@ -884,13 +921,13 @@ sub index_sync {
                delete $self->{reindex_pipe};
                $self->update_last_commit($git, $i, $cmt) if defined $cmt;
        }
-       my @d = sort keys %$D;
-       if (@d) {
-               warn "BUG: ", scalar(@d)," unseen deleted messages marked\n";
-               foreach (@d) {
-                       my ($mid, undef) = split(/\0/, $_, 2);
-                       warn "<$mid>\n";
-               }
+
+       # unindex is required for leftovers if "deletes" affect messages
+       # in a previous fetch+index window:
+       if (scalar keys %$D) {
+               my $git = $self->{-inbox}->git;
+               $self->unindex_oid($git, $_) for values %$D;
+               $git->cleanup;
        }
        $self->done;
 }