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