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