]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxWritable.pm
imap+nntp: share COMPRESS implementation
[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_file($self, 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_eml {
135         my ($fn, $kw, $eml, $im, $self) = @_;
136         return if grep(/\Adraft\z/, @$kw);
137         if ($self && (my $filter = $self->filter($im))) {
138                 my $ret = $filter->scrub($eml) or return;
139                 return if $ret == REJECT();
140                 $eml = $ret;
141         }
142         $im->add($eml);
143 }
144
145 # XXX does anybody use this?
146 sub import_maildir {
147         my ($self, $dir) = @_;
148         foreach my $sub (qw(cur new tmp)) {
149                 -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
150         }
151         my $im = $self->importer(1);
152         my @self = $self->filter($im) ? ($self) : ();
153         require PublicInbox::MdirReader;
154         PublicInbox::MdirReader->new->maildir_each_eml($dir,
155                                         \&_each_maildir_eml, $im, @self);
156         $im->done;
157 }
158
159 sub _mbox_eml_cb { # MboxReader->mbox* callback
160         my ($eml, $im, $filter) = @_;
161         if ($filter) {
162                 my $ret = $filter->scrub($eml) or return;
163                 return if $ret == REJECT();
164                 $eml = $ret;
165         }
166         $im->add($eml);
167 }
168
169 sub import_mbox {
170         my ($self, $fh, $variant) = @_;
171         require PublicInbox::MboxReader;
172         my $cb = PublicInbox::MboxReader->reads($variant) or
173                 die "$variant not supported\n";
174         my $im = $self->importer(1);
175         $cb->(undef, $fh, \&_mbox_eml_cb, $im, $self->filter);
176         $im->done;
177 }
178
179 sub _read_git_config_perm {
180         my ($self) = @_;
181         chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
182         $perm;
183 }
184
185 sub _git_config_perm {
186         my $self = shift;
187         my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
188         return PERM_UMASK if (!defined($perm) || $perm eq '');
189         return PERM_UMASK if ($perm eq 'umask');
190         return PERM_GROUP if ($perm eq 'group');
191         if ($perm =~ /\A(?:all|world|everybody)\z/) {
192                 return PERM_EVERYBODY;
193         }
194         return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
195         return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
196
197         my $i = oct($perm);
198         return PERM_UMASK if ($i == PERM_UMASK);
199         return PERM_GROUP if ($i == OLD_PERM_GROUP);
200         return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
201
202         if (($i & 0600) != 0600) {
203                 die "core.sharedRepository mode invalid: ".
204                     sprintf('%.3o', $i) . "\nOwner must have permissions\n";
205         }
206         ($i & 0666);
207 }
208
209 sub _umask_for {
210         my ($perm) = @_; # _git_config_perm return value
211         my $rv = $perm;
212         return umask if $rv == 0;
213
214         # set +x bit if +r or +w were set
215         $rv |= 0100 if ($rv & 0600);
216         $rv |= 0010 if ($rv & 0060);
217         $rv |= 0001 if ($rv & 0006);
218         (~$rv & 0777);
219 }
220
221 sub with_umask {
222         my ($self, $cb, @arg) = @_;
223         my $old = umask($self->{umask} //= umask_prepare($self));
224         my $rv = eval { $cb->(@arg) };
225         my $err = $@;
226         umask $old;
227         die $err if $err;
228         $rv;
229 }
230
231 sub umask_prepare {
232         my ($self) = @_;
233         my $perm = _git_config_perm($self);
234         _umask_for($perm);
235 }
236
237 sub cleanup ($) {
238         delete @{$_[0]}{qw(over mm git search)};
239 }
240
241 # v2+ only, XXX: maybe we can just rely on ->max_git_epoch and remove
242 sub git_dir_latest {
243         my ($self, $max) = @_;
244         defined($$max = $self->max_git_epoch) ?
245                 "$self->{inboxdir}/git/$$max.git" : undef;
246 }
247
248 1;