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