]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxWritable.pm
inboxwritable: drop {-importer} cyclic reference
[public-inbox.git] / lib / PublicInbox / InboxWritable.pm
1 # Copyright (C) 2018-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Extends read-only Inbox for writing
5 package PublicInbox::InboxWritable;
6 use strict;
7 use warnings;
8 use base qw(PublicInbox::Inbox);
9 use PublicInbox::Import;
10 use PublicInbox::Filter::Base;
11 *REJECT = *PublicInbox::Filter::Base::REJECT;
12
13 use constant {
14         PERM_UMASK => 0,
15         OLD_PERM_GROUP => 1,
16         OLD_PERM_EVERYBODY => 2,
17         PERM_GROUP => 0660,
18         PERM_EVERYBODY => 0664,
19 };
20
21 sub new {
22         my ($class, $ibx, $creat_opt) = @_;
23         my $self = bless $ibx, $class;
24
25         # TODO: maybe stop supporting this
26         if ($creat_opt) { # for { nproc => $N }
27                 $self->{-creat_opt} = $creat_opt;
28                 init_inbox($self) if ($self->{version} || 1) == 1;
29         }
30         $self;
31 }
32
33 sub assert_usable_dir {
34         my ($self) = @_;
35         my $dir = $self->{inboxdir};
36         return $dir if defined($dir) && $dir ne '';
37         die "no inboxdir defined for $self->{name}\n";
38 }
39
40 sub init_inbox {
41         my ($self, $shards, $skip_epoch, $skip_artnum) = @_;
42         # TODO: honor skip_artnum
43         my $v = $self->{version} || 1;
44         if ($v == 1) {
45                 my $dir = assert_usable_dir($self);
46                 PublicInbox::Import::init_bare($dir);
47         } else {
48                 my $v2w = importer($self);
49                 $v2w->init_inbox($shards, $skip_epoch, $skip_artnum);
50         }
51 }
52
53 sub importer {
54         my ($self, $parallel) = @_;
55         my $v = $self->{version} || 1;
56         if ($v == 2) {
57                 eval { require PublicInbox::V2Writable };
58                 die "v2 not supported: $@\n" if $@;
59                 my $opt = $self->{-creat_opt};
60                 my $v2w = PublicInbox::V2Writable->new($self, $opt);
61                 $v2w->{parallel} = $parallel;
62                 $v2w;
63         } elsif ($v == 1) {
64                 my @arg = (undef, undef, undef, $self);
65                 PublicInbox::Import->new(@arg);
66         } else {
67                 $! = 78; # EX_CONFIG 5.3.5 local configuration error
68                 die "unsupported inbox version: $v\n";
69         }
70 }
71
72 sub filter {
73         my ($self, $im) = @_;
74         my $f = $self->{filter};
75         if ($f && $f =~ /::/) {
76                 # v2 keeps msgmap open, which causes conflicts for filters
77                 # such as PublicInbox::Filter::RubyLang which overload msgmap
78                 # for a predictable serial number.
79                 if ($im && ($self->{version} || 1) >= 2 && $self->{altid}) {
80                         $im->done;
81                 }
82
83                 my @args = (-inbox => $self);
84                 # basic line splitting, only
85                 # Perhaps we can have proper quote splitting one day...
86                 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
87
88                 eval "require $f";
89                 if ($@) {
90                         warn $@;
91                 } else {
92                         # e.g: PublicInbox::Filter::Vger->new(@args)
93                         return $f->new(@args);
94                 }
95         }
96         undef;
97 }
98
99 sub is_maildir_basename ($) {
100         my ($bn) = @_;
101         return 0 if $bn !~ /\A[a-zA-Z0-9][\-\w:,=\.]+\z/;
102         if ($bn =~ /:2,([A-Z]+)\z/i) {
103                 my $flags = $1;
104                 return 0 if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
105         }
106         1;
107 }
108
109 sub is_maildir_path ($) {
110         my ($path) = @_;
111         my @p = split(m!/+!, $path);
112         (is_maildir_basename($p[-1]) && -f $path) ? 1 : 0;
113 }
114
115 sub maildir_path_load ($) {
116         my ($path) = @_;
117         if (open my $fh, '<', $path) {
118                 local $/;
119                 my $str = <$fh>;
120                 $str or return;
121                 return PublicInbox::MIME->new(\$str);
122         } elsif ($!{ENOENT}) {
123                 # common with Maildir
124                 return;
125         } else {
126                 warn "failed to open $path: $!\n";
127                 return;
128         }
129 }
130
131 sub import_maildir {
132         my ($self, $dir) = @_;
133         my $im = $self->importer(1);
134
135         foreach my $sub (qw(cur new tmp)) {
136                 -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
137         }
138         foreach my $sub (qw(cur new)) {
139                 opendir my $dh, "$dir/$sub" or die "opendir $dir/$sub: $!\n";
140                 while (defined(my $fn = readdir($dh))) {
141                         next unless is_maildir_basename($fn);
142                         my $mime = maildir_path_load("$dir/$fn") or next;
143
144                         if (my $filter = $self->filter($im)) {
145                                 my $ret = $filter->scrub($mime) or return;
146                                 return if $ret == REJECT();
147                                 $mime = $ret;
148                         }
149                         $im->add($mime);
150                 }
151         }
152         $im->done;
153 }
154
155 # asctime: From example@example.com Fri Jun 23 02:56:55 2000
156 my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
157
158 sub mb_add ($$$$) {
159         my ($im, $variant, $filter, $msg) = @_;
160         $$msg =~ s/(\r?\n)+\z/$1/s;
161         my $mime = PublicInbox::MIME->new($msg);
162         if ($variant eq 'mboxrd') {
163                 $$msg =~ s/^>(>*From )/$1/sm;
164         } elsif ($variant eq 'mboxo') {
165                 $$msg =~ s/^>From /From /sm;
166         }
167         if ($filter) {
168                 my $ret = $filter->scrub($mime) or return;
169                 return if $ret == REJECT();
170                 $mime = $ret;
171         }
172         $im->add($mime)
173 }
174
175 sub import_mbox {
176         my ($self, $fh, $variant) = @_;
177         if ($variant !~ /\A(?:mboxrd|mboxo)\z/) {
178                 die "variant must be 'mboxrd' or 'mboxo'\n";
179         }
180         my $im = $self->importer(1);
181         my $prev = undef;
182         my $msg = '';
183         my $filter = $self->filter;
184         while (defined(my $l = <$fh>)) {
185                 if ($l =~ /$from_strict/o) {
186                         if (!defined($prev) || $prev =~ /^\r?$/) {
187                                 mb_add($im, $variant, $filter, \$msg) if $msg;
188                                 $msg = '';
189                                 $prev = $l;
190                                 next;
191                         }
192                         warn "W[$.] $l\n";
193                 }
194                 $prev = $l;
195                 $msg .= $l;
196         }
197         mb_add($im, $variant, $filter, \$msg) if $msg;
198         $im->done;
199 }
200
201 sub _read_git_config_perm {
202         my ($self) = @_;
203         chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
204         $perm;
205 }
206
207 sub _git_config_perm {
208         my $self = shift;
209         my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
210         return PERM_UMASK if (!defined($perm) || $perm eq '');
211         return PERM_UMASK if ($perm eq 'umask');
212         return PERM_GROUP if ($perm eq 'group');
213         if ($perm =~ /\A(?:all|world|everybody)\z/) {
214                 return PERM_EVERYBODY;
215         }
216         return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
217         return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
218
219         my $i = oct($perm);
220         return PERM_UMASK if ($i == PERM_UMASK);
221         return PERM_GROUP if ($i == OLD_PERM_GROUP);
222         return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
223
224         if (($i & 0600) != 0600) {
225                 die "core.sharedRepository mode invalid: ".
226                     sprintf('%.3o', $i) . "\nOwner must have permissions\n";
227         }
228         ($i & 0666);
229 }
230
231 sub _umask_for {
232         my ($perm) = @_; # _git_config_perm return value
233         my $rv = $perm;
234         return umask if $rv == 0;
235
236         # set +x bit if +r or +w were set
237         $rv |= 0100 if ($rv & 0600);
238         $rv |= 0010 if ($rv & 0060);
239         $rv |= 0001 if ($rv & 0006);
240         (~$rv & 0777);
241 }
242
243 sub with_umask {
244         my ($self, $cb) = @_;
245         my $old = umask $self->{umask};
246         my $rv = eval { $cb->() };
247         my $err = $@;
248         umask $old;
249         die $err if $err;
250         $rv;
251 }
252
253 sub umask_prepare {
254         my ($self) = @_;
255         my $perm = _git_config_perm($self);
256         my $umask = _umask_for($perm);
257         $self->{umask} = $umask;
258 }
259
260 1;