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>
4 # Extends read-only Inbox for writing
5 package PublicInbox::InboxWritable;
8 use parent qw(PublicInbox::Inbox Exporter);
9 use PublicInbox::Import;
10 use PublicInbox::Filter::Base qw(REJECT);
12 our @EXPORT_OK = qw(eml_from_path);
13 use Fcntl qw(O_RDONLY O_NONBLOCK);
18 OLD_PERM_EVERYBODY => 2,
20 PERM_EVERYBODY => 0664,
24 my ($class, $ibx, $creat_opt) = @_;
25 return $ibx if ref($ibx) eq $class;
26 my $self = bless $ibx, $class;
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;
36 sub assert_usable_dir {
38 my $dir = $self->{inboxdir};
39 return $dir if defined($dir) && $dir ne '';
40 die "no inboxdir defined for $self->{name}\n";
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);
56 undef $mm; # ->created_at set
57 $sidx->commit_txn_lazy;
59 open my $fh, '>>', "$self->{inboxdir}/ssoma.lock" or
60 die "$self->{inboxdir}/ssoma.lock: $!\n";
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);
71 my $v2w = importer($self);
72 $v2w->init_inbox($shards, $skip_epoch, $skip_artnum);
77 my ($self, $parallel) = @_;
78 my $v = $self->version;
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;
87 my @arg = (undef, undef, undef, $self);
88 PublicInbox::Import->new(@arg);
90 $! = 78; # EX_CONFIG 5.3.5 local configuration error
91 die "unsupported inbox version: $v\n";
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}) {
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+/;
115 # e.g: PublicInbox::Filter::Vger->new(@args)
116 return $f->new(@args);
122 sub eml_from_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;
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();
145 # XXX does anybody use this?
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";
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);
159 sub _mbox_eml_cb { # MboxReader->mbox* callback
160 my ($eml, $im, $filter) = @_;
162 my $ret = $filter->scrub($eml) or return;
163 return if $ret == REJECT();
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);
179 sub _read_git_config_perm {
181 chomp(my $perm = $self->git->qx('config', 'core.sharedRepository'));
185 sub _git_config_perm {
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;
194 return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
195 return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
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);
202 if (($i & 0600) != 0600) {
203 die "core.sharedRepository mode invalid: ".
204 sprintf('%.3o', $i) . "\nOwner must have permissions\n";
210 my ($perm) = @_; # _git_config_perm return value
212 return umask if $rv == 0;
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);
222 my ($self, $cb, @arg) = @_;
223 my $old = umask($self->{umask} //= umask_prepare($self));
224 my $rv = eval { $cb->(@arg) };
233 my $perm = _git_config_perm($self);
238 delete @{$_[0]}{qw(over mm git search)};
241 # v2+ only, XXX: maybe we can just rely on ->max_git_epoch and remove
243 my ($self, $max) = @_;
244 defined($$max = $self->max_git_epoch) ?
245 "$self->{inboxdir}/git/$$max.git" : undef;