]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiMirror.pm
lei_mirror: simplify clone_v2_prep
[public-inbox.git] / lib / PublicInbox / LeiMirror.pm
index acf086654089f4d2cbf8f0d7c25eddd01271086b..de4cdc2293835e30be430176761b3633ccf9145c 100644 (file)
@@ -31,9 +31,9 @@ sub _wq_done_wait { # dwaitpid callback (via wq_eof)
                warn("unlink($f): $!\n") unless $!{ENOENT};
        } else {
                if (!$mrr->{dry_run} && $lei->{cmd} ne 'public-inbox-clone') {
-                       # calls _finish_add_external
-                       $lei->lazy_cb('add-external', '_finish_'
-                                       )->($lei, $mrr->{dst});
+                       require PublicInbox::LeiAddExternal;
+                       PublicInbox::LeiAddExternal::_finish_add_external(
+                                                       $lei, $mrr->{dst});
                }
                $lei->qerr("# mirrored $mrr->{src} => $mrr->{dst}");
        }
@@ -113,12 +113,11 @@ sub clone_cmd {
 
 sub ft_rename ($$$) {
        my ($ft, $dst, $open_mode) = @_;
-       my $fn = $ft->filename;
        my @st = stat($dst);
        my $mode = @st ? ($st[2] & 07777) : ($open_mode & ~umask);
-       chmod($mode, $ft) or croak "E: chmod $fn: $!";
+       chmod($mode, $ft) or croak "E: chmod($ft): $!";
        require File::Copy;
-       File::Copy::mv($fn, $dst) or croak "E: mv($fn => $ft): $!";
+       File::Copy::mv($ft->filename, $dst) or croak "E: mv($ft => $dst): $!";
        $ft->unlink_on_destroy(0);
 }
 
@@ -387,7 +386,8 @@ sub v1_done { # called via OnDestroy
                        die "rename($f, $o/info/alternates): $!";
                $f->unlink_on_destroy(0);
        }
-       return if $self->{-is_epoch};
+       return if ($self->{-is_epoch} ||
+               $self->{lei}->{opt}->{'inbox-config'} ne 'always');
        write_makefile($dst, 1);
        index_cloned_inbox($self, 1);
 }
@@ -433,7 +433,8 @@ sub clone_v2_prep ($$;$) {
        my $want = parse_epochs($lei->{opt}->{epoch}, $v2_epochs);
        my $task = $m ? bless { %$self }, __PACKAGE__ : $self;
        delete $task->{todo}; # $self->{todo} still exists
-       my (@src_edst, @skip, $desc, @entv);
+       my (@skip, $desc);
+       my $fini = PublicInbox::OnDestroy->new($$, \&v2_done, $task);
        for my $nr (sort { $a <=> $b } keys %$v2_epochs) {
                my ($uri, $key) = @{$v2_epochs->{$nr}};
                my $src = $uri->as_string;
@@ -453,8 +454,13 @@ failed to extract epoch number from $src
                        }
                }
                if (!$want || $want->{$nr}) {
-                       push @src_edst, $src, $edst;
-                       push @entv, $edst, $ent;
+                       my $etask = bless { %$task }, __PACKAGE__;
+                       $etask->{-ent} = $ent; # may have {reference}
+                       $etask->{cur_src} = $src;
+                       $etask->{cur_dst} = $edst;
+                       $etask->{-is_epoch} = $fini;
+                       my $ref = $ent->{reference} // '';
+                       push @{$self->{todo}->{$ref}}, $etask;
                        $self->{any_want}->{$key} = 1;
                } else { # create a placeholder so users only need to chmod +w
                        init_placeholder($src, $edst, $ent);
@@ -467,24 +473,11 @@ failed to extract epoch number from $src
 
        (!$self->{dry_run} && !-d $dst) and File::Path::mkpath($dst);
 
-       my $fini = PublicInbox::OnDestroy->new($$, \&v2_done, $task);
-
        $lei->{opt}->{'inbox-config'} =~ /\A(?:always|v2)\z/s and
                _get_txt_start($task, '_/text/config/raw', $fini);
 
        defined($desc) ? ($task->{'txt.description'} = $desc) :
                _get_txt_start($task, 'description', $fini);
-       while (@entv) {
-               my ($edst, $ent) = splice(@entv, 0, 2);
-               my $etask = bless { %$task }, __PACKAGE__;
-               $etask->{-ent} = $ent; # may have {reference}
-               $etask->{cur_src} = shift @src_edst // die 'BUG: no cur_src';
-               $etask->{cur_dst} = shift @src_edst // die 'BUG: no cur_dst';
-               $etask->{cur_dst} eq $edst or
-                       die "BUG: `$etask->{cur_dst}' != `$edst'";
-               $etask->{-is_epoch} = $fini;
-               push @{$self->{todo}->{($ent->{reference} // '')}}, $etask;
-       }
 }
 
 sub decode_manifest ($$$) {
@@ -595,6 +588,20 @@ EOM
        reap_live() while keys(%$LIVE);
 }
 
+sub dump_manifest ($$) {
+       my ($m, $ft) = @_;
+       # 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);
+       my $mtime = (stat($ft))[9];
+       seek($ft, SEEK_SET, 0) or die "seek($ft): $!";
+       truncate($ft, 0) or die "truncate($ft): $!";
+       gzip(\$json => $ft) or die "gzip($ft): $GzipError";
+       $ft->flush or die "flush($ft): $!";
+       utime($mtime, $mtime, "$ft") or die "utime(..., $ft): $!";
+}
+
 # FIXME: this gets confused by single inbox instance w/ global manifest.js.gz
 sub try_manifest {
        my ($self) = @_;
@@ -606,15 +613,14 @@ sub try_manifest {
        $uri->path($path . '/manifest.js.gz');
        my $ft = File::Temp->new(TEMPLATE => '.manifest-XXXX',
                                UNLINK => 1, TMPDIR => 1, SUFFIX => '.tmp');
-       my $fn = $ft->filename;
-       my $cmd = $curl->for_uri($lei, $uri, qw(-f -R -o), $fn);
+       my $cmd = $curl->for_uri($lei, $uri, qw(-f -R -o), $ft->filename);
        my %opt = map { $_ => $lei->{$_} } (0..2);
        my $cerr = run_reap($lei, $cmd, \%opt);
        if ($cerr) {
                return try_scrape($self) if ($cerr >> 8) == 22; # 404 missing
                return $lei->child_error($cerr, "@$cmd failed");
        }
-       my $m = eval { decode_manifest($ft, $fn, $uri) };
+       my $m = eval { decode_manifest($ft, $ft, $uri) };
        if ($@) {
                warn $@;
                return try_scrape($self);
@@ -676,15 +682,7 @@ EOM
        return if $self->{lei}->{child_error} || $self->{dry_run};
 
        # set by clone_v2_prep/-I/--exclude
-       if (delete $self->{-culled_manifest}) {
-               # 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);
-               my $mtime = (stat($fn))[9];
-               gzip(\$json => $fn) or die "gzip: $GzipError";
-               utime($mtime, $mtime, $fn) or die "utime(..., $fn): $!";
-       }
+       dump_manifest($m => $ft) if delete $self->{-culled_manifest};
        ft_rename($ft, "$self->{dst}/manifest.js.gz", 0666);
        open my $x, '>', "$self->{dst}/mirror.done"; # for _wq_done_wait
 }
@@ -762,6 +760,7 @@ help :
        @echo Rarely needed targets:
        @echo '    make reindex      - may be needed for new features/bugfixes'
        @echo '    make compact      - rewrite Xapian storage to save space'
+       @echo '    make index        - initial index after clone
 
 fetch :
        public-inbox-fetch
@@ -778,12 +777,14 @@ update :
                echo 'public-inbox index not initialized'; \
                echo 'see public-inbox-index(1) man page'; \
        fi
+index :
+       public-inbox-index
 reindex :
        public-inbox-index --reindex
 compact :
        public-inbox-compact
 
-.PHONY : help fetch update reindex compact
+.PHONY : help fetch update index reindex compact
 EOM
                close $fh or die "close($f): $!";
        } else {