]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxWritable.pm
www_stream: add trailing slash for help and color links
[public-inbox.git] / lib / PublicInbox / InboxWritable.pm
1 # Copyright (C) 2018-2021 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);
13 use Fcntl qw(O_RDONLY O_NONBLOCK);
14
15 use constant {
16         PERM_UMASK => 0,
17         OLD_PERM_GROUP => 1,
18         OLD_PERM_EVERYBODY => 2,
19         PERM_GROUP => 0660,
20         PERM_EVERYBODY => 0664,
21 };
22
23 sub new {
24         my ($class, $ibx, $creat_opt) = @_;
25         return $ibx if ref($ibx) eq $class;
26         my $self = bless $ibx, $class;
27
28         # TODO: maybe stop supporting this
29         if ($creat_opt) { # for { nproc => $N }
30                 $self->{-creat_opt} = $creat_opt;
31                 init_inbox($self) if $self->version == 1;
32         }
33         $self;
34 }
35
36 sub assert_usable_dir {
37         my ($self) = @_;
38         my $dir = $self->{inboxdir};
39         return $dir if defined($dir) && $dir ne '';
40         die "no inboxdir defined for $self->{name}\n";
41 }
42
43 sub _init_v1 {
44         my ($self, $skip_artnum) = @_;
45         if (defined($self->{indexlevel}) || defined($skip_artnum)) {
46                 require PublicInbox::SearchIdx;
47                 require PublicInbox::Msgmap;
48                 my $sidx = PublicInbox::SearchIdx->new($self, 1); # just create
49                 $sidx->begin_txn_lazy;
50                 my $mm = PublicInbox::Msgmap->new($self->{inboxdir}, 1);
51                 if (defined $skip_artnum) {
52                         $mm->{dbh}->begin_work;
53                         $mm->skip_artnum($skip_artnum);
54                         $mm->{dbh}->commit;
55                 }
56                 undef $mm; # ->created_at set
57                 $sidx->commit_txn_lazy;
58         } else {
59                 open my $fh, '>>', "$self->{inboxdir}/ssoma.lock" or
60                         die "$self->{inboxdir}/ssoma.lock: $!\n";
61         }
62 }
63
64 sub init_inbox {
65         my ($self, $shards, $skip_epoch, $skip_artnum) = @_;
66         if ($self->version == 1) {
67                 my $dir = assert_usable_dir($self);
68                 PublicInbox::Import::init_bare($dir);
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 eml_from_path ($) {
123         my ($path) = @_;
124         if (sysopen(my $fh, $path, O_RDONLY|O_NONBLOCK)) {
125                 return unless -f $fh; # no FIFOs or directories
126                 my $str = do { local $/; <$fh> } or return;
127                 PublicInbox::Eml->new(\$str);
128         } else { # ENOENT is common with Maildir
129                 warn "failed to open $path: $!\n" if $! != ENOENT;
130                 undef;
131         }
132 }
133
134 sub _each_maildir_fn {
135         my ($fn, $im, $self) = @_;
136         if ($fn =~ /:2,([A-Za-z]*)\z/) {
137                 my $fl = $1;
138                 return if $fl =~ /[DT]/; # no Drafts or Trash for public
139         }
140         my $eml = eml_from_path($fn) or return;
141         if ($self && (my $filter = $self->filter($im))) {
142                 my $ret = $filter->scrub($eml) or return;
143                 return if $ret == REJECT();
144                 $eml = $ret;
145         }
146         $im->add($eml);
147 }
148
149 sub import_maildir {
150         my ($self, $dir) = @_;
151         foreach my $sub (qw(cur new tmp)) {
152                 -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
153         }
154         my $im = $self->importer(1);
155         my @self = $self->filter($im) ? ($self) : ();
156         require PublicInbox::MdirReader;
157         PublicInbox::MdirReader::maildir_each_file(\&_each_maildir_fn,
158                                                 $im, @self);
159         $im->done;
160 }
161
162 sub _mbox_eml_cb { # MboxReader->mbox* callback
163         my ($eml, $im, $filter) = @_;
164         if ($filter) {
165                 my $ret = $filter->scrub($eml) or return;
166                 return if $ret == REJECT();
167                 $eml = $ret;
168         }
169         $im->add($eml);
170 }
171
172 sub import_mbox {
173         my ($self, $fh, $variant) = @_;
174         require PublicInbox::MboxReader;
175         my $cb = PublicInbox::MboxReader->can($variant) or
176                 die "$variant not supported\n";
177         my $im = $self->importer(1);
178         $cb->(undef, $fh, \&_mbox_eml_cb, $im, $self->filter);
179         $im->done;
180 }
181
182 sub _read_git_config_perm {
183         my ($self) = @_;
184         chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
185         $perm;
186 }
187
188 sub _git_config_perm {
189         my $self = shift;
190         my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
191         return PERM_UMASK if (!defined($perm) || $perm eq '');
192         return PERM_UMASK if ($perm eq 'umask');
193         return PERM_GROUP if ($perm eq 'group');
194         if ($perm =~ /\A(?:all|world|everybody)\z/) {
195                 return PERM_EVERYBODY;
196         }
197         return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
198         return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
199
200         my $i = oct($perm);
201         return PERM_UMASK if ($i == PERM_UMASK);
202         return PERM_GROUP if ($i == OLD_PERM_GROUP);
203         return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
204
205         if (($i & 0600) != 0600) {
206                 die "core.sharedRepository mode invalid: ".
207                     sprintf('%.3o', $i) . "\nOwner must have permissions\n";
208         }
209         ($i & 0666);
210 }
211
212 sub _umask_for {
213         my ($perm) = @_; # _git_config_perm return value
214         my $rv = $perm;
215         return umask if $rv == 0;
216
217         # set +x bit if +r or +w were set
218         $rv |= 0100 if ($rv & 0600);
219         $rv |= 0010 if ($rv & 0060);
220         $rv |= 0001 if ($rv & 0006);
221         (~$rv & 0777);
222 }
223
224 sub with_umask {
225         my ($self, $cb, @arg) = @_;
226         my $old = umask($self->{umask} //= umask_prepare($self));
227         my $rv = eval { $cb->(@arg) };
228         my $err = $@;
229         umask $old;
230         die $err if $err;
231         $rv;
232 }
233
234 sub umask_prepare {
235         my ($self) = @_;
236         my $perm = _git_config_perm($self);
237         _umask_for($perm);
238 }
239
240 sub cleanup ($) {
241         delete @{$_[0]}{qw(over mm git search)};
242 }
243
244 # v2+ only, XXX: maybe we can just rely on ->max_git_epoch and remove
245 sub git_dir_latest {
246         my ($self, $max) = @_;
247         defined($$max = $self->max_git_epoch) ?
248                 "$self->{inboxdir}/git/$$max.git" : undef;
249 }
250
251 1;