]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiPruneMailSync.pm
get rid of unnecessary bytes::length usage
[public-inbox.git] / lib / PublicInbox / LeiPruneMailSync.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 # "lei prune-mail-sync" drops dangling sync information
5 package PublicInbox::LeiPruneMailSync;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9 use PublicInbox::LeiExportKw;
10 use PublicInbox::InboxWritable qw(eml_from_path);
11
12 sub eml_match ($$) {
13         my ($eml, $oidbin) = @_;
14         $oidbin eq git_sha(length($oidbin) == 20 ? 1 : 256, $eml)->digest;
15 }
16
17 sub prune_mdir { # lms->each_src callback
18         my ($oidbin, $id, $self, $mdir) = @_;
19         my @try = $$id =~ /:2,[a-zA-Z]*\z/ ? qw(cur new) : qw(new cur);
20         for my $d (@try) {
21                 my $src = "$mdir/$d/$$id";
22                 if ($self->{verify}) {
23                         my $eml = eml_from_path($src) or next;
24                         return if eml_match($eml, $oidbin);
25                 } elsif (-f $src) {
26                         return;
27                 }
28         }
29         # both tries failed
30         $self->{lei}->qerr("# maildir:$mdir $$id gone");
31         $self->{lei}->{sto}->ipc_do('lms_clear_src', "maildir:$mdir", $id);
32 }
33
34 sub prune_imap { # lms->each_src callback
35         my ($oidbin, $uid, $self, $uids, $url) = @_;
36         return if exists $uids->{$uid};
37         $self->{lei}->qerr("# $url $uid gone");
38         $self->{lei}->{sto}->ipc_do('lms_clear_src', $url, $uid);
39 }
40
41 sub input_path_url { # overrides PublicInbox::LeiInput::input_path_url
42         my ($self, $input, @args) = @_;
43         my $lms = $self->{-lms_ro} //= $self->{lse}->lms;
44         if ($input =~ /\Amaildir:(.+)/i) {
45                 my $mdir = $1;
46                 $lms->each_src($input, \&prune_mdir, $self, $mdir);
47         } elsif ($input =~ m!\Aimaps?://!i) {
48                 my $uri = PublicInbox::URIimap->new($input);
49                 my $mic = $self->{lei}->{net}->mic_for_folder($uri);
50                 my $uids = $mic->search('UID 1:*');
51                 $uids = +{ map { $_ => undef } @$uids };
52                 $lms->each_src($$uri, \&prune_imap, $self, $uids, $$uri);
53         } else { die "BUG: $input not supported" }
54         my $wait = $self->{lei}->{sto}->ipc_do('done');
55 }
56
57 sub lei_prune_mail_sync {
58         my ($lei, @folders) = @_;
59         my $sto = $lei->_lei_store or return $lei->fail(<<EOM);
60 lei/store uninitialized, see lei-import(1)
61 EOM
62         my $lse = $sto->search;
63         my $lms = $lse->lms or return $lei->fail(<<EOM);
64 lei mail_sync uninitialized, see lei-import(1)
65 EOM
66         if (defined(my $all = $lei->{opt}->{all})) {
67                 $lms->group2folders($lei, $all, \@folders) or return;
68         } else {
69                 my $err = $lms->arg2folder($lei, \@folders);
70                 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
71                 return $lei->fail($err->{fail}) if $err->{fail};
72         }
73         delete $lms->{dbh};
74         $sto->write_prepare($lei);
75         my $self = bless { lse => $lse }, __PACKAGE__;
76         $lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
77         $self->prepare_inputs($lei, \@folders) or return;
78         my $j = $lei->{opt}->{jobs} || scalar(@{$self->{inputs}}) || 1;
79         undef $lms; # for fork
80         my $ops = {};
81         $sto->write_prepare($lei);
82         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
83         $self->{-wq_nr_workers} = $j // 1; # locked
84         (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
85         $lei->{wq1} = $self;
86         $lei->{-err_type} = 'non-fatal';
87         net_merge_all_done($self) unless $lei->{auth};
88         $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
89 }
90
91 no warnings 'once';
92 *_complete_prune_mail_sync = \&PublicInbox::LeiExportKw::_complete_export_kw;
93 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
94 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
95 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
96
97 1;