]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxWritable.pm
inboxwritable: _init_v1: set created_at ASAP
[public-inbox.git] / lib / PublicInbox / InboxWritable.pm
1 # Copyright (C) 2018-2020 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 v5.10.1;
8 use parent qw(PublicInbox::Inbox Exporter);
9 use PublicInbox::Import;
10 use PublicInbox::Filter::Base qw(REJECT);
11 use Errno qw(ENOENT);
12 our @EXPORT_OK = qw(eml_from_path warn_ignore_cb);
13
14 use constant {
15         PERM_UMASK => 0,
16         OLD_PERM_GROUP => 1,
17         OLD_PERM_EVERYBODY => 2,
18         PERM_GROUP => 0660,
19         PERM_EVERYBODY => 0664,
20 };
21
22 sub new {
23         my ($class, $ibx, $creat_opt) = @_;
24         return $ibx if ref($ibx) eq $class;
25         my $self = bless $ibx, $class;
26
27         # TODO: maybe stop supporting this
28         if ($creat_opt) { # for { nproc => $N }
29                 $self->{-creat_opt} = $creat_opt;
30                 init_inbox($self) if $self->version == 1;
31         }
32         $self;
33 }
34
35 sub assert_usable_dir {
36         my ($self) = @_;
37         my $dir = $self->{inboxdir};
38         return $dir if defined($dir) && $dir ne '';
39         die "no inboxdir defined for $self->{name}\n";
40 }
41
42 sub _init_v1 {
43         my ($self, $skip_artnum) = @_;
44         if (defined($self->{indexlevel}) || defined($skip_artnum)) {
45                 require PublicInbox::SearchIdx;
46                 require PublicInbox::Msgmap;
47                 my $sidx = PublicInbox::SearchIdx->new($self, 1); # just create
48                 $sidx->begin_txn_lazy;
49                 my $mm = PublicInbox::Msgmap->new($self->{inboxdir}, 1);
50                 if (defined $skip_artnum) {
51                         $mm->{dbh}->begin_work;
52                         $mm->skip_artnum($skip_artnum);
53                         $mm->{dbh}->commit;
54                 }
55                 undef $mm; # ->created_at set
56                 $sidx->commit_txn_lazy;
57         } else {
58                 open my $fh, '>>', "$self->{inboxdir}/ssoma.lock" or
59                         die "$self->{inboxdir}/ssoma.lock: $!\n";
60         }
61 }
62
63 sub init_inbox {
64         my ($self, $shards, $skip_epoch, $skip_artnum) = @_;
65         if ($self->version == 1) {
66                 my $dir = assert_usable_dir($self);
67                 PublicInbox::Import::init_bare($dir);
68                 $self->umask_prepare;
69                 $self->with_umask(\&_init_v1, $self, $skip_artnum);
70         } else {
71                 my $v2w = importer($self);
72                 $v2w->init_inbox($shards, $skip_epoch, $skip_artnum);
73         }
74 }
75
76 sub importer {
77         my ($self, $parallel) = @_;
78         my $v = $self->version;
79         if ($v == 2) {
80                 eval { require PublicInbox::V2Writable };
81                 die "v2 not supported: $@\n" if $@;
82                 my $opt = $self->{-creat_opt};
83                 my $v2w = PublicInbox::V2Writable->new($self, $opt);
84                 $v2w->{parallel} = $parallel if defined $parallel;
85                 $v2w;
86         } elsif ($v == 1) {
87                 my @arg = (undef, undef, undef, $self);
88                 PublicInbox::Import->new(@arg);
89         } else {
90                 $! = 78; # EX_CONFIG 5.3.5 local configuration error
91                 die "unsupported inbox version: $v\n";
92         }
93 }
94
95 sub filter {
96         my ($self, $im) = @_;
97         my $f = $self->{filter};
98         if ($f && $f =~ /::/) {
99                 # v2 keeps msgmap open, which causes conflicts for filters
100                 # such as PublicInbox::Filter::RubyLang which overload msgmap
101                 # for a predictable serial number.
102                 if ($im && $self->version >= 2 && $self->{altid}) {
103                         $im->done;
104                 }
105
106                 my @args = (ibx => $self);
107                 # basic line splitting, only
108                 # Perhaps we can have proper quote splitting one day...
109                 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
110
111                 eval "require $f";
112                 if ($@) {
113                         warn $@;
114                 } else {
115                         # e.g: PublicInbox::Filter::Vger->new(@args)
116                         return $f->new(@args);
117                 }
118         }
119         undef;
120 }
121
122 sub is_maildir_basename ($) {
123         my ($bn) = @_;
124         return 0 if $bn !~ /\A[a-zA-Z0-9][\-\w:,=\.]+\z/;
125         if ($bn =~ /:2,([A-Z]+)\z/i) {
126                 my $flags = $1;
127                 return 0 if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
128         }
129         1;
130 }
131
132 sub is_maildir_path ($) {
133         my ($path) = @_;
134         my @p = split(m!/+!, $path);
135         (is_maildir_basename($p[-1]) && -f $path) ? 1 : 0;
136 }
137
138 sub eml_from_path ($) {
139         my ($path) = @_;
140         if (open my $fh, '<', $path) {
141                 my $str = do { local $/; <$fh> } or return;
142                 PublicInbox::Eml->new(\$str);
143         } else { # ENOENT is common with Maildir
144                 warn "failed to open $path: $!\n" if $! != ENOENT;
145                 undef;
146         }
147 }
148
149 sub import_maildir {
150         my ($self, $dir) = @_;
151         my $im = $self->importer(1);
152
153         foreach my $sub (qw(cur new tmp)) {
154                 -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
155         }
156         foreach my $sub (qw(cur new)) {
157                 opendir my $dh, "$dir/$sub" or die "opendir $dir/$sub: $!\n";
158                 while (defined(my $fn = readdir($dh))) {
159                         next unless is_maildir_basename($fn);
160                         my $mime = eml_from_path("$dir/$fn") or next;
161
162                         if (my $filter = $self->filter($im)) {
163                                 my $ret = $filter->scrub($mime) or return;
164                                 return if $ret == REJECT();
165                                 $mime = $ret;
166                         }
167                         $im->add($mime);
168                 }
169         }
170         $im->done;
171 }
172
173 # asctime: From example@example.com Fri Jun 23 02:56:55 2000
174 my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
175
176 sub mb_add ($$$$) {
177         my ($im, $variant, $filter, $msg) = @_;
178         $$msg =~ s/(\r?\n)+\z/$1/s;
179         if ($variant eq 'mboxrd') {
180                 $$msg =~ s/^>(>*From )/$1/gms;
181         } elsif ($variant eq 'mboxo') {
182                 $$msg =~ s/^>From /From /gms;
183         }
184         my $mime = PublicInbox::Eml->new($msg);
185         if ($filter) {
186                 my $ret = $filter->scrub($mime) or return;
187                 return if $ret == REJECT();
188                 $mime = $ret;
189         }
190         $im->add($mime)
191 }
192
193 sub import_mbox {
194         my ($self, $fh, $variant) = @_;
195         if ($variant !~ /\A(?:mboxrd|mboxo)\z/) {
196                 die "variant must be 'mboxrd' or 'mboxo'\n";
197         }
198         my $im = $self->importer(1);
199         my $prev = undef;
200         my $msg = '';
201         my $filter = $self->filter;
202         while (defined(my $l = <$fh>)) {
203                 if ($l =~ /$from_strict/o) {
204                         if (!defined($prev) || $prev =~ /^\r?$/) {
205                                 mb_add($im, $variant, $filter, \$msg) if $msg;
206                                 $msg = '';
207                                 $prev = $l;
208                                 next;
209                         }
210                         warn "W[$.] $l\n";
211                 }
212                 $prev = $l;
213                 $msg .= $l;
214         }
215         mb_add($im, $variant, $filter, \$msg) if $msg;
216         $im->done;
217 }
218
219 sub _read_git_config_perm {
220         my ($self) = @_;
221         chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
222         $perm;
223 }
224
225 sub _git_config_perm {
226         my $self = shift;
227         my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
228         return PERM_UMASK if (!defined($perm) || $perm eq '');
229         return PERM_UMASK if ($perm eq 'umask');
230         return PERM_GROUP if ($perm eq 'group');
231         if ($perm =~ /\A(?:all|world|everybody)\z/) {
232                 return PERM_EVERYBODY;
233         }
234         return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
235         return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
236
237         my $i = oct($perm);
238         return PERM_UMASK if ($i == PERM_UMASK);
239         return PERM_GROUP if ($i == OLD_PERM_GROUP);
240         return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
241
242         if (($i & 0600) != 0600) {
243                 die "core.sharedRepository mode invalid: ".
244                     sprintf('%.3o', $i) . "\nOwner must have permissions\n";
245         }
246         ($i & 0666);
247 }
248
249 sub _umask_for {
250         my ($perm) = @_; # _git_config_perm return value
251         my $rv = $perm;
252         return umask if $rv == 0;
253
254         # set +x bit if +r or +w were set
255         $rv |= 0100 if ($rv & 0600);
256         $rv |= 0010 if ($rv & 0060);
257         $rv |= 0001 if ($rv & 0006);
258         (~$rv & 0777);
259 }
260
261 sub with_umask {
262         my ($self, $cb, @arg) = @_;
263         my $old = umask $self->{umask};
264         my $rv = eval { $cb->(@arg) };
265         my $err = $@;
266         umask $old;
267         die $err if $err;
268         $rv;
269 }
270
271 sub umask_prepare {
272         my ($self) = @_;
273         my $perm = _git_config_perm($self);
274         my $umask = _umask_for($perm);
275         $self->{umask} = $umask;
276 }
277
278 sub cleanup ($) {
279         delete @{$_[0]}{qw(over mm git search)};
280 }
281
282 # warnings to ignore when handling spam mailboxes and maybe other places
283 sub warn_ignore {
284         my $s = "@_";
285         # Email::Address::XS warnings
286         $s =~ /^Argument contains empty address at /
287         || $s =~ /^Element at index [0-9]+ contains /
288         # PublicInbox::MsgTime
289         || $s =~ /^bogus TZ offset: .+?, ignoring and assuming \+0000/
290         || $s =~ /^bad Date: .+? in /
291         # Encode::Unicode::UTF7
292         || $s =~ /^Bad UTF7 data escape at /
293 }
294
295 # this expects to be RHS in this assignment: "local $SIG{__WARN__} = ..."
296 sub warn_ignore_cb {
297         my $cb = $SIG{__WARN__} // sub { print STDERR @_ };
298         sub {
299                 return if warn_ignore(@_);
300                 $cb->(@_);
301         }
302 }
303
304 # v2+ only, XXX: maybe we can just rely on ->max_git_epoch and remove
305 sub git_dir_latest {
306         my ($self, $max) = @_;
307         defined($$max = $self->max_git_epoch) ?
308                 "$self->{inboxdir}/git/$$max.git" : undef;
309 }
310
311 1;