]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiALE.pm
lei: All Local Externals: bare git dir for alternates
[public-inbox.git] / lib / PublicInbox / LeiALE.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # All Locals Ever: track lei/store + externals ever used as
5 # long as they're on an accessible FS.  Includes "lei q" --include
6 # and --only targets that haven't been through "lei add-external".
7 # Typically: ~/.cache/lei/all_locals_ever.git
8 package PublicInbox::LeiALE;
9 use strict;
10 use v5.10.1;
11 use parent qw(PublicInbox::LeiSearch PublicInbox::Lock);
12 use PublicInbox::Git;
13 use PublicInbox::Import;
14 use Fcntl qw(SEEK_SET);
15
16 sub new {
17         my ($cls, $d) = @_;
18         PublicInbox::Import::init_bare($d, 'ale');
19         bless {
20                 git => PublicInbox::Git->new($d),
21                 lock_path => "$d/lei_ale.state", # dual-duty lock + state
22                 ibxish => [], # Inbox and ExtSearch (and LeiSearch) objects
23         }, $cls;
24 }
25
26 sub over {} # undef for xoids_for
27
28 sub overs_all { # for xoids_for (called only in lei workers?)
29         my ($self) = @_;
30         my $pid = $$;
31         if (($self->{owner_pid} // $pid) != $pid) {
32                 delete($_->{over}) for @{$self->{ibxish}};
33         }
34         $self->{owner_pid} = $pid;
35         grep(defined, map { $_->over } @{$self->{ibxish}});
36 }
37
38 sub refresh_externals {
39         my ($self, $lxs) = @_;
40         $self->git->cleanup;
41         my $lk = $self->lock_for_scope;
42         my $cur_lxs = ref($lxs)->new;
43         my $orig = do {
44                 local $/;
45                 readline($self->{lockfh}) //
46                                 die "readline($self->{lock_path}): $!";
47         };
48         my $new = '';
49         my $old = '';
50         my $gone = 0;
51         my %seen_ibxish; # $dir => any-defined value
52         for my $dir (split(/\n/, $orig)) {
53                 if (-d $dir && -r _ && $cur_lxs->prepare_external($dir)) {
54                         $seen_ibxish{$dir} //= length($old .= "$dir\n");
55                 } else {
56                         ++$gone;
57                 }
58         }
59         my @ibxish = $cur_lxs->locals;
60         for my $x ($lxs->locals) {
61                 my $d = File::Spec->canonpath($x->{inboxdir} // $x->{topdir});
62                 $seen_ibxish{$d} //= do {
63                         $new .= "$d\n";
64                         push @ibxish, $x;
65                 };
66         }
67         if ($new ne '' || $gone) {
68                 $self->{lockfh}->autoflush(1);
69                 if ($gone) {
70                         seek($self->{lockfh}, 0, SEEK_SET) or die "seek: $!";
71                         truncate($self->{lockfh}, 0) or die "truncate: $!";
72                 } else {
73                         $old = '';
74                 }
75                 print { $self->{lockfh} } $old, $new or die "print: $!";
76         }
77         $new = $old = '';
78         my $f = $self->git->{git_dir}.'/objects/info/alternates';
79         if (open my $fh, '<', $f) {
80                 local $/;
81                 $old = <$fh> // die "readline($f): $!";
82         }
83         for my $x (@ibxish) {
84                 $new .= File::Spec->canonpath($x->git->{git_dir})."/objects\n";
85         }
86         $self->{ibxish} = \@ibxish;
87         return if $old eq $new;
88
89         # this needs to be atomic since child processes may start
90         # git-cat-file at any time
91         my $tmp = "$f.$$.tmp";
92         open my $fh, '>', $tmp or die "open($tmp): $!";
93         print $fh $new or die "print($tmp): $!";
94         close $fh or die "close($tmp): $!";
95         rename($tmp, $f) or die "rename($tmp, $f): $!";
96 }
97
98 1;