]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiMirror.pm
www: _/text/config/raw Last-Modified: is mm->created_at
[public-inbox.git] / lib / PublicInbox / LeiMirror.pm
index fe81b967fd4ae990c97d1db6769a7515e5e5f53e..4be8f70ae4015b63c6cd8622715dc67f69a045ea 100644 (file)
@@ -6,7 +6,9 @@ package PublicInbox::LeiMirror;
 use strict;
 use v5.10.1;
 use parent qw(PublicInbox::IPC);
+use PublicInbox::Config;
 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
+use IO::Compress::Gzip qw(gzip $GzipError);
 use PublicInbox::Spawn qw(popen_rd spawn run_die);
 use File::Temp ();
 use Fcntl qw(SEEK_SET O_CREAT O_EXCL O_WRONLY);
@@ -21,7 +23,8 @@ sub _wq_done_wait { # dwaitpid callback (via wq_eof)
                $lei->err("unlink($f): $!") unless $!{ENOENT};
        } else {
                if ($lei->{cmd} ne 'public-inbox-clone') {
-                       $lei->add_external_finish($mrr->{dst});
+                       $lei->lazy_cb('add-external', '_finish_'
+                                       )->($lei, $mrr->{dst});
                }
                $lei->qerr("# mirrored $mrr->{src} => $mrr->{dst}");
        }
@@ -93,15 +96,15 @@ sub _get_txt { # non-fatal
        my $path = $uri->path;
        chop($path) eq '/' or die "BUG: $uri not canonicalized";
        $uri->path("$path/$endpoint");
-       my $cmd = $self->{curl}->for_uri($lei, $uri, '--compressed');
-       my $ce = "$self->{dst}/$file";
-       my $ft = File::Temp->new(TEMPLATE => "$file-XXXX",
-                               UNLINK => 1, DIR => $self->{dst});
-       my $opt = { 0 => $lei->{0}, 1 => $ft, 2 => $lei->{2} };
+       my $ft = File::Temp->new(TEMPLATE => "$file-XXXX", DIR => $self->{dst});
+       my $f = $ft->filename;
+       my $opt = { 0 => $lei->{0}, 1 => $lei->{1}, 2 => $lei->{2} };
+       my $cmd = $self->{curl}->for_uri($lei, $uri,
+                                       qw(--compressed -R -o), $f);
        my $cerr = run_reap($lei, $cmd, $opt);
        return "$uri missing" if ($cerr >> 8) == 22;
        return "# @$cmd failed (non-fatal)" if $cerr;
-       my $f = $ft->filename;
+       my $ce = "$self->{dst}/$file";
        rename($f, $ce) or return "rename($f, $ce): $! (non-fatal)";
        $ft->unlink_on_destroy(0);
        undef; # success
@@ -119,6 +122,7 @@ sub _try_config {
        my $err = _get_txt($self, qw(_/text/config/raw inbox.config.example));
        return $self->{lei}->err($err) if $err;
        my $f = "$self->{dst}/inbox.config.example";
+       chmod((stat($f))[2] & 0444, $f) or die "chmod(a-w, $f): $!";
        my $cfg = PublicInbox::Config->git_config_dump($f, $self->{lei}->{2});
        my $ibx = $self->{ibx} = {};
        for my $sec (grep(/\Apublicinbox\./, @{$cfg->{-section_order}})) {
@@ -267,14 +271,14 @@ EOM
        close $fh or die "close:($f): $!";
 }
 
-sub clone_v2 ($$) {
-       my ($self, $v2_epochs) = @_;
+sub clone_v2 ($$;$) {
+       my ($self, $v2_epochs, $m) = @_; # $m => manifest.js.gz hashref
        my $lei = $self->{lei};
        my $curl = $self->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
        my $pfx = $curl->torsocks($lei, (values %$v2_epochs)[0]) or return;
        my $dst = $self->{dst};
        my $want = parse_epochs($lei->{opt}->{epoch}, $v2_epochs);
-       my (@src_edst, @read_only);
+       my (@src_edst, @read_only, @skip_nr);
        for my $nr (sort { $a <=> $b } keys %$v2_epochs) {
                my $uri = $v2_epochs->{$nr};
                my $src = $uri->as_string;
@@ -289,8 +293,15 @@ failed to extract epoch number from $src
                } else { # create a placeholder so users only need to chmod +w
                        init_placeholder($src, $edst);
                        push @read_only, $edst;
+                       push @skip_nr, $nr;
                }
        }
+       if (@skip_nr) { # filter out the epochs we skipped
+               my $re = join('|', @skip_nr);
+               my @del = grep(m!/git/$re\.git\z!, keys %$m);
+               delete @$m{@del};
+               $self->{-culled_manifest} = 1;
+       }
        my $lk = bless { lock_path => "$dst/inbox.lock" }, 'PublicInbox::Lock';
        _try_config($self);
        my $on_destroy = $lk->lock_for_scope($$);
@@ -379,13 +390,20 @@ EOM
                        my ($n) = ("$uri" =~ m!/([0-9]+)\.git\z!);
                        $n => $uri->clone
                } @v2_epochs;
-               clone_v2($self, \%v2_epochs);
+               clone_v2($self, \%v2_epochs, $m);
        } elsif (defined $v1_path) {
                clone_v1($self);
        } else {
                die "E: confused by <$uri>, possible matches:\n\t",
                        join(', ', sort keys %$m), "\n";
        }
+       if (delete $self->{-culled_manifest}) { # set by clone_v2
+               # write the smaller manifest if epochs were skipped so
+               # users won't have to delete manifest if they +w an
+               # epoch they no longer want to skip
+               my $json = PublicInbox::Config->json->encode($m);
+               gzip(\$json => $fn) or die "gzip: $GzipError";
+       }
        my $fin = "$self->{dst}/manifest.js.gz";
        rename($fn, $fin) or die "E: rename($fn, $fin): $!";
        $ft->unlink_on_destroy(0);