]> Sergey Matveev's repositories - public-inbox.git/commitdiff
v2writable: hoist out write_alternates
authorEric Wong <e@80x24.org>
Tue, 27 Oct 2020 07:54:11 +0000 (07:54 +0000)
committerEric Wong <e@80x24.org>
Sat, 7 Nov 2020 10:18:39 +0000 (10:18 +0000)
We'll be reusing this for external indices and possibly
other places.

lib/PublicInbox/V2Writable.pm

index eecc702b8a62f7148fb1ad42bfb0b261950e4def..aa812a6b3b3b4284329bcac6d447e8a5adbe6192 100644 (file)
@@ -681,6 +681,18 @@ sub done {
        die $err if $err;
 }
 
+sub write_alternates ($$$) {
+       my ($info_dir, $mode, $out) = @_;
+       my $fh = File::Temp->new(TEMPLATE => 'alt-XXXXXXXX', DIR => $info_dir);
+       my $tmp = $fh->filename;
+       print $fh @$out or die "print $tmp: $!\n";
+       chmod($mode, $fh) or die "fchmod $tmp: $!\n";
+       close $fh or die "close $tmp $!\n";
+       my $alt = "$info_dir/alternates";
+       rename($tmp, $alt) or die "rename $tmp => $alt: $!\n";
+       $fh->unlink_on_destroy(0);
+}
+
 sub fill_alternates ($$) {
        my ($self, $epoch) = @_;
 
@@ -719,15 +731,8 @@ sub fill_alternates ($$) {
                }
        }
        return unless $new;
-
-       my $fh = File::Temp->new(TEMPLATE => 'alt-XXXXXXXX', DIR => $info_dir);
-       my $tmp = $fh->filename;
-       print $fh join("\n", sort { $alt{$b} <=> $alt{$a} } keys %alt), "\n"
-               or die "print $tmp: $!\n";
-       chmod($mode, $fh) or die "fchmod $tmp: $!\n";
-       close $fh or die "close $tmp $!\n";
-       rename($tmp, $alt) or die "rename $tmp => $alt: $!\n";
-       $fh->unlink_on_destroy(0);
+       write_alternates($info_dir, $mode,
+               [join("\n", sort { $alt{$b} <=> $alt{$a} } keys %alt), "\n"]);
 }
 
 sub git_init {