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>
4 # Extends read-only Inbox for writing
5 package PublicInbox::InboxWritable;
8 use base qw(PublicInbox::Inbox);
9 use PublicInbox::Import;
10 use PublicInbox::Filter::Base;
11 *REJECT = *PublicInbox::Filter::Base::REJECT;
16 OLD_PERM_EVERYBODY => 2,
18 PERM_EVERYBODY => 0664,
22 my ($class, $ibx, $creat_opt) = @_;
23 my $self = bless $ibx, $class;
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;
34 my ($self, $shards, $skip_epoch, $skip_artnum) = @_;
35 # TODO: honor skip_artnum
36 my $v = $self->{version} || 1;
38 my $dir = $self->{mainrepo} or die "no mainrepo in inbox\n";
39 PublicInbox::Import::init_bare($dir);
41 my $v2w = importer($self);
42 $v2w->init_inbox($shards, $skip_epoch, $skip_artnum);
47 my ($self, $parallel) = @_;
48 $self->{-importer} ||= do {
49 my $v = $self->{version} || 1;
51 eval { require PublicInbox::V2Writable };
52 die "v2 not supported: $@\n" if $@;
53 my $opt = $self->{-creat_opt};
54 my $v2w = PublicInbox::V2Writable->new($self, $opt);
55 $v2w->{parallel} = $parallel;
58 my @arg = (undef, undef, undef, $self);
59 PublicInbox::Import->new(@arg);
61 $! = 78; # EX_CONFIG 5.3.5 local configuration error
62 die "unsupported inbox version: $v\n";
69 my $f = $self->{filter};
70 if ($f && $f =~ /::/) {
71 # v2 keeps msgmap open, which causes conflicts for filters
72 # such as PublicInbox::Filter::RubyLang which overload msgmap
73 # for a predictable serial number.
74 if ($im && ($self->{version} || 1) >= 2 && $self->{altid}) {
78 my @args = (-inbox => $self);
79 # basic line splitting, only
80 # Perhaps we can have proper quote splitting one day...
81 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
87 # e.g: PublicInbox::Filter::Vger->new(@args)
88 return $f->new(@args);
94 sub is_maildir_basename ($) {
96 return 0 if $bn !~ /\A[a-zA-Z0-9][\-\w:,=\.]+\z/;
97 if ($bn =~ /:2,([A-Z]+)\z/i) {
99 return 0 if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
104 sub is_maildir_path ($) {
106 my @p = split(m!/+!, $path);
107 (is_maildir_basename($p[-1]) && -f $path) ? 1 : 0;
110 sub maildir_path_load ($) {
112 if (open my $fh, '<', $path) {
116 return PublicInbox::MIME->new(\$str);
117 } elsif ($!{ENOENT}) {
118 # common with Maildir
121 warn "failed to open $path: $!\n";
127 my ($self, $dir) = @_;
128 my $im = $self->importer(1);
130 foreach my $sub (qw(cur new tmp)) {
131 -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
133 foreach my $sub (qw(cur new)) {
134 opendir my $dh, "$dir/$sub" or die "opendir $dir/$sub: $!\n";
135 while (defined(my $fn = readdir($dh))) {
136 next unless is_maildir_basename($fn);
137 my $mime = maildir_file_load("$dir/$fn") or next;
139 if (my $filter = $self->filter($im)) {
140 my $ret = $filter->scrub($mime) or return;
141 return if $ret == REJECT();
150 # asctime: From example@example.com Fri Jun 23 02:56:55 2000
151 my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
154 my ($im, $variant, $filter, $msg) = @_;
155 $$msg =~ s/(\r?\n)+\z/$1/s;
156 my $mime = PublicInbox::MIME->new($msg);
157 if ($variant eq 'mboxrd') {
158 $$msg =~ s/^>(>*From )/$1/sm;
159 } elsif ($variant eq 'mboxo') {
160 $$msg =~ s/^>From /From /sm;
163 my $ret = $filter->scrub($mime) or return;
164 return if $ret == REJECT();
171 my ($self, $fh, $variant) = @_;
172 if ($variant !~ /\A(?:mboxrd|mboxo)\z/) {
173 die "variant must be 'mboxrd' or 'mboxo'\n";
175 my $im = $self->importer(1);
178 my $filter = $self->filter;
179 while (defined(my $l = <$fh>)) {
180 if ($l =~ /$from_strict/o) {
181 if (!defined($prev) || $prev =~ /^\r?$/) {
182 mb_add($im, $variant, $filter, \$msg) if $msg;
192 mb_add($im, $variant, $filter, \$msg) if $msg;
196 sub _read_git_config_perm {
198 chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
202 sub _git_config_perm {
204 my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
205 return PERM_UMASK if (!defined($perm) || $perm eq '');
206 return PERM_UMASK if ($perm eq 'umask');
207 return PERM_GROUP if ($perm eq 'group');
208 if ($perm =~ /\A(?:all|world|everybody)\z/) {
209 return PERM_EVERYBODY;
211 return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
212 return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
215 return PERM_UMASK if ($i == PERM_UMASK);
216 return PERM_GROUP if ($i == OLD_PERM_GROUP);
217 return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
219 if (($i & 0600) != 0600) {
220 die "core.sharedRepository mode invalid: ".
221 sprintf('%.3o', $i) . "\nOwner must have permissions\n";
227 my ($perm) = @_; # _git_config_perm return value
229 return umask if $rv == 0;
231 # set +x bit if +r or +w were set
232 $rv |= 0100 if ($rv & 0600);
233 $rv |= 0010 if ($rv & 0060);
234 $rv |= 0001 if ($rv & 0006);
239 my ($self, $cb) = @_;
240 my $old = umask $self->{umask};
241 my $rv = eval { $cb->() };
250 my $perm = _git_config_perm($self);
251 my $umask = _umask_for($perm);
252 $self->{umask} = $umask;