1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # based on notmuch, but with no concept of folders, files or flags
5 # Indexes mail with Xapian and our (SQLite-based) ::Msgmap for use
6 # with the web and NNTP interfaces. This index maintains thread
7 # relationships for use by PublicInbox::SearchThread.
8 # This writes to the search index.
9 package PublicInbox::SearchIdx;
12 use base qw(PublicInbox::Search PublicInbox::Lock);
13 use PublicInbox::MIME;
14 use PublicInbox::MID qw/mid_clean id_compress mid_mime mids references/;
15 use PublicInbox::MsgIter;
17 use POSIX qw(strftime);
18 require PublicInbox::Git;
23 OLD_PERM_EVERYBODY => 2,
25 PERM_EVERYBODY => 0664,
26 BATCH_BYTES => 1_000_000,
27 DEBUG => !!$ENV{DEBUG},
42 return $s unless ($s =~ /\A"(.*)"\z/);
44 $s =~ s/\\([abfnrtv])/$GIT_ESC{$1}/g;
45 $s =~ s/\\([0-7]{1,3})/chr(oct($1))/ge;
50 my ($class, $ibx, $creat, $part) = @_;
51 my $mainrepo = $ibx; # for "public-inbox-index" w/o entry in config
52 my $git_dir = $mainrepo;
56 $mainrepo = $ibx->{mainrepo};
57 $altid = $ibx->{altid};
58 $version = $ibx->{version} || 1;
60 require PublicInbox::AltId;
62 PublicInbox::AltId->new($ibx, $_);
67 $git = PublicInbox::Git->new($git_dir); # v1 only
69 require Search::Xapian::WritableDatabase;
71 mainrepo => $mainrepo,
76 my $perm = $self->_git_config_perm;
77 my $umask = _umask_for($perm);
78 $self->{umask} = $umask;
80 $self->{lock_path} = "$mainrepo/ssoma.lock";
81 } elsif ($version == 2) {
82 defined $part or die "partition is required for v2\n";
83 # partition is a number or "all"
84 $self->{partition} = $part;
85 $self->{lock_path} = undef;
86 $self->{msgmap_path} = "$mainrepo/msgmap.sqlite3";
88 die "unsupported inbox version=$version\n";
90 $self->{creat} = ($creat || 0) == 1;
96 my $xdb = delete $self->{xdb} or croak 'not acquired';
98 $self->lock_release if $self->{creat};
104 croak 'already acquired' if $self->{xdb};
105 my $dir = $self->xdir;
106 my $flag = Search::Xapian::DB_OPEN;
107 if ($self->{creat}) {
110 File::Path::mkpath($dir);
111 $flag = Search::Xapian::DB_CREATE_OR_OPEN;
113 $self->{xdb} = Search::Xapian::WritableDatabase->new($dir, $flag);
117 my ($doc, $col, $num) = @_;
118 $num = Search::Xapian::sortable_serialise($num);
119 $doc->add_value($col, $num);
122 sub add_values ($$) {
123 my ($doc, $values) = @_;
125 my $ts = $values->[PublicInbox::Search::TS];
126 add_val($doc, PublicInbox::Search::TS, $ts);
128 my $num = $values->[PublicInbox::Search::NUM];
129 defined($num) and add_val($doc, PublicInbox::Search::NUM, $num);
131 my $bytes = $values->[PublicInbox::Search::BYTES];
132 defined($bytes) and add_val($doc, PublicInbox::Search::BYTES, $bytes);
134 my $lines = $values->[PublicInbox::Search::LINES];
135 add_val($doc, PublicInbox::Search::LINES, $lines);
137 my $ds = $values->[PublicInbox::Search::DS];
138 add_val($doc, PublicInbox::Search::DS, $ds);
139 my $yyyymmdd = strftime('%Y%m%d', gmtime($ds));
140 add_val($doc, PublicInbox::Search::YYYYMMDD, $yyyymmdd);
143 sub index_users ($$) {
144 my ($tg, $smsg) = @_;
146 my $from = $smsg->from;
150 $tg->index_text($from, 1, 'A'); # A - author
151 $tg->increase_termpos;
152 $tg->index_text($to, 1, 'XTO') if $to ne '';
153 $tg->increase_termpos;
154 $tg->index_text($cc, 1, 'XCC') if $cc ne '';
155 $tg->increase_termpos;
158 sub index_diff_inc ($$$$) {
159 my ($tg, $text, $pfx, $xnq) = @_;
161 $tg->index_text(join("\n", @$xnq), 1, 'XNQ');
162 $tg->increase_termpos;
165 $tg->index_text($text, 1, $pfx);
166 $tg->increase_termpos;
169 sub index_old_diff_fn {
170 my ($tg, $seen, $fa, $fb, $xnq) = @_;
172 # no renames or space support for traditional diffs,
173 # find the number of leading common paths to strip:
174 my @fa = split('/', $fa);
175 my @fb = split('/', $fb);
176 while (scalar(@fa) && scalar(@fb)) {
177 $fa = join('/', @fa);
178 $fb = join('/', @fb);
180 unless ($seen->{$fa}++) {
181 index_diff_inc($tg, $fa, 'XDFN', $xnq);
191 sub index_diff ($$$) {
192 my ($tg, $lines, $doc) = @_;
198 if ($in_diff && s/^ //) { # diff context
199 index_diff_inc($tg, $_, 'XDFCTX', $xnq);
200 } elsif (/^-- $/) { # email signature begins
202 } elsif (m!^diff --git ("?a/.+) ("?b/.+)\z!) {
203 my ($fa, $fb) = ($1, $2);
204 my $fn = (split('/', git_unquote($fa), 2))[1];
205 $seen{$fn}++ or index_diff_inc($tg, $fn, 'XDFN', $xnq);
206 $fn = (split('/', git_unquote($fb), 2))[1];
207 $seen{$fn}++ or index_diff_inc($tg, $fn, 'XDFN', $xnq);
210 } elsif (m/^diff -(.+) (\S+) (\S+)$/) {
211 my ($opt, $fa, $fb) = ($1, $2, $3);
213 # only support unified:
214 next unless $opt =~ /[uU]/;
215 $in_diff = index_old_diff_fn($tg, \%seen, $fa, $fb,
217 } elsif (m!^--- ("?a/.+)!) {
218 my $fn = (split('/', git_unquote($1), 2))[1];
219 $seen{$fn}++ or index_diff_inc($tg, $fn, 'XDFN', $xnq);
221 } elsif (m!^\+\+\+ ("?b/.+)!) {
222 my $fn = (split('/', git_unquote($1), 2))[1];
223 $seen{$fn}++ or index_diff_inc($tg, $fn, 'XDFN', $xnq);
225 } elsif (/^--- (\S+)/) {
228 } elsif (defined $in_diff && /^\+\+\+ (\S+)/) {
229 $in_diff = index_old_diff_fn($tg, \%seen, $in_diff, $1,
231 } elsif ($in_diff && s/^\+//) { # diff added
232 index_diff_inc($tg, $_, 'XDFB', $xnq);
233 } elsif ($in_diff && s/^-//) { # diff removed
234 index_diff_inc($tg, $_, 'XDFA', $xnq);
235 } elsif (m!^index ([a-f0-9]+)\.\.([a-f0-9]+)!) {
236 my ($ba, $bb) = ($1, $2);
237 index_git_blob_id($doc, 'XDFPRE', $ba);
238 index_git_blob_id($doc, 'XDFPOST', $bb);
240 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*$/) {
241 # traditional diff w/o -p
242 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*(\S+.*)$/) {
243 # hunk header context
244 index_diff_inc($tg, $1, 'XDFHH', $xnq);
245 # ignore the following lines:
246 } elsif (/^(?:dis)similarity index/ ||
247 /^(?:old|new) mode/ ||
248 /^(?:deleted|new) file mode/ ||
249 /^(?:copy|rename) (?:from|to) / ||
250 /^(?:dis)?similarity index / ||
251 /^\\ No newline at end of file/ ||
252 /^Binary files .* differ/) {
258 warn "non-diff line: $_\n" if DEBUG && $_ ne '';
263 $tg->index_text(join("\n", @xnq), 1, 'XNQ');
264 $tg->increase_termpos;
267 sub index_body ($$$) {
268 my ($tg, $lines, $doc) = @_;
269 my $txt = join("\n", @$lines);
271 # does it look like a diff?
272 if ($txt =~ /^(?:diff|---|\+\+\+) /ms) {
274 index_diff($tg, $lines, $doc);
276 $tg->index_text($txt, 1, 'XNQ');
279 $tg->index_text($txt, 0, 'XQUOT');
281 $tg->increase_termpos;
286 # mime = Email::MIME object
287 my ($self, $mime, $bytes, $num, $oid, $mid0) = @_;
289 my $mids = mids($mime->header_obj);
290 my $skel = $self->{skeleton};
293 my $smsg = PublicInbox::SearchMsg->new($mime);
294 my $doc = $smsg->{doc};
295 my $subj = $smsg->subject;
298 $xpath = $self->subject_path($subj);
299 $xpath = id_compress($xpath);
302 my $lines = $mime->body_raw =~ tr!\n!\n!;
303 my @values = ($smsg->ds, $num, $bytes, $lines, $smsg->ts);
304 add_values($doc, \@values);
306 my $tg = $self->term_generator;
308 $tg->set_document($doc);
309 $tg->index_text($subj, 1, 'S') if $subj;
310 $tg->increase_termpos;
312 index_users($tg, $smsg);
314 msg_iter($mime, sub {
315 my ($part, $depth, @idx) = @{$_[0]};
316 my $ct = $part->content_type || 'text/plain';
317 my $fn = $part->filename;
318 if (defined $fn && $fn ne '') {
319 $tg->index_text($fn, 1, 'XFN');
322 return if $ct =~ m!\btext/x?html\b!i;
324 my $s = eval { $part->body_str };
326 if ($ct =~ m!\btext/plain\b!i) {
327 # Try to assume UTF-8 because Alpine
328 # seems to do wacky things and set
330 $part->charset_set('UTF-8');
331 $s = eval { $part->body_str };
332 $s = $part->body if $@;
335 defined $s or return;
338 my $body = $part->body;
339 my @lines = split(/\n/, $body);
340 while (defined(my $l = shift @lines)) {
342 index_body($tg, \@orig, $doc) if @orig;
345 index_body($tg, \@quot, 0) if @quot;
349 index_body($tg, \@quot, 0) if @quot;
350 index_body($tg, \@orig, $doc) if @orig;
353 # populates smsg->references for smsg->to_doc_data
354 my $refs = parse_references($smsg);
355 $mid0 = $mids->[0] unless defined $mid0; # v1 compatibility
356 my $data = $smsg->to_doc_data($oid, $mid0);
357 foreach my $mid (@$mids) {
358 $tg->index_text($mid, 1, 'XM');
360 $doc->set_data($data);
361 if (my $altid = $self->{-altid}) {
362 foreach my $alt (@$altid) {
363 my $pfx = $alt->{xprefix};
364 foreach my $mid (@$mids) {
365 my $id = $alt->mid2alt($mid);
366 next unless defined $id;
367 $doc->add_boolean_term($pfx . $id);
372 $self->delete_article($num) if defined $num; # for reindexing
374 push @values, $mids, $xpath, $data;
375 $skel->index_skeleton(\@values);
376 $doc->add_boolean_term('Q' . $_) foreach @$mids;
377 $doc->add_boolean_term('XNUM' . $num) if defined $num;
378 $doc_id = $self->{xdb}->add_document($doc);
380 $doc_id = link_and_save($self, $doc, $mids, $refs,
386 warn "failed to index message <".join('> <',@$mids).">: $@\n";
392 # returns begin and end PostingIterator
394 my ($self, $termval) = @_;
395 my $db = $self->{xdb};
397 ($db->postlist_begin($termval), $db->postlist_end($termval));
401 my ($self, $termval, $cb) = @_;
402 my $batch_size = 1000; # don't let @ids grow too large to avoid OOM
404 my ($head, $tail) = $self->find_doc_ids($termval);
405 return if $head == $tail;
407 for (; $head != $tail && @ids < $batch_size; $head->inc) {
408 push @ids, $head->get_docid;
415 my ($self, $mid) = @_;
416 my $db = $self->{xdb};
418 $mid = mid_clean($mid);
421 batch_do($self, 'Q' . $mid, sub {
423 $db->delete_document($_) for @$ids;
428 warn "failed to remove message <$mid>: $@\n";
430 warn "cannot remove non-existent <$mid>\n";
435 my ($self, $num) = @_;
437 batch_do($self, 'XNUM' . $num, sub {
439 $ndel += scalar @$ids;
440 $self->{xdb}->delete_document($_) for @$ids;
444 # MID is a hint in V2
446 my ($self, $oid, $mid) = @_;
447 my $db = $self->{xdb};
449 # XXX careful, we cannot use batch_do here since we conditionally
450 # delete documents based on other factors, so we cannot call
451 # find_doc_ids twice.
452 my ($head, $tail) = $self->find_doc_ids('Q' . $mid);
453 return if $head == $tail;
455 # there is only ONE element in @delete unless we
456 # have bugs in our v2writable deduplication check
458 for (; $head != $tail; $head->inc) {
459 my $docid = $head->get_docid;
460 my $doc = $db->get_document($docid);
461 my $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
463 push(@delete, $docid) if $smsg->{blob} eq $oid;
465 $db->delete_document($_) foreach @delete;
469 sub term_generator { # write-only
472 my $tg = $self->{term_generator};
475 $tg = Search::Xapian::TermGenerator->new;
476 $tg->set_stemmer($self->stemmer);
478 $self->{term_generator} = $tg;
481 # increments last_thread_id counter
482 # returns a 64-bit integer represented as a decimal string
485 my $db = $self->{xdb};
486 my $last_thread_id = int($db->get_metadata('last_thread_id') || 0);
488 $db->set_metadata('last_thread_id', ++$last_thread_id);
493 sub parse_references ($) {
495 my $mime = $smsg->{mime};
496 my $hdr = $mime->header_obj;
497 my $refs = references($hdr);
498 return $refs if scalar(@$refs) == 0;
500 # prevent circular references via References here:
501 my %mids = map { $_ => 1 } @{mids($hdr)};
503 foreach my $ref (@$refs) {
504 if (length($ref) > PublicInbox::MID::MAX_MID_SIZE) {
505 warn "References: <$ref> too long, ignoring\n";
511 $smsg->{references} = '<'.join('> <', @keep).'>' if @keep;
516 my ($self, $doc, $refs, $old_tid) = @_;
520 # first ref *should* be the thread root,
521 # but we can never trust clients to do the right thing
522 my $ref = shift @$refs;
523 $tid = resolve_mid_to_tid($self, $ref);
524 merge_threads($self, $tid, $old_tid) if defined $old_tid;
526 # the rest of the refs should point to this tid:
527 foreach $ref (@$refs) {
528 my $ptid = resolve_mid_to_tid($self, $ref);
529 merge_threads($self, $tid, $ptid);
532 $tid = defined $old_tid ? $old_tid : $self->next_thread_id;
534 $doc->add_boolean_term('G' . $tid);
539 my ($self, $doc, $mids, $refs, $num, $xpath) = @_;
540 my $db = $self->{xdb};
543 $doc->add_boolean_term('XNUM' . $num) if defined $num;
544 $doc->add_boolean_term('XPATH' . $xpath) if defined $xpath;
545 $doc->add_boolean_term('Q' . $_) foreach @$mids;
548 $self->{skel} and die "Should not have read-only skel here\n";;
549 foreach my $mid (@$mids) {
550 $self->each_smsg_by_mid($mid, sub {
552 my $type = $cur->type;
553 my $cur_tid = $cur->thread_id;
554 $old_tid = $cur_tid unless defined $old_tid;
555 if ($type eq 'mail') {
556 # do not break existing mail messages,
557 # just merge the threads
558 merge_threads($self, $old_tid, $cur_tid);
561 if ($type ne 'ghost') {
562 die "<$mid> has a bad type: $type\n";
564 my $tid = link_doc($self, $doc, $refs, $old_tid);
565 $old_tid = $tid unless defined $old_tid;
566 $doc_id = $cur->{doc_id};
567 $self->{xdb}->replace_document($doc_id, $doc);
573 my $id = '<'.join('> <', @$mids).'>';
574 warn "BUG: vivified multiple ($vivified) ghosts for $id\n";
576 # not really important, but we return any vivified ghost docid, here:
577 return $doc_id if defined $doc_id;
578 link_doc($self, $doc, $refs, $old_tid);
579 $self->{xdb}->add_document($doc);
582 sub index_git_blob_id {
583 my ($doc, $pfx, $objid) = @_;
585 my $len = length($objid);
586 for (my $len = length($objid); $len >= 7; ) {
587 $doc->add_term($pfx.$objid);
588 $objid = substr($objid, 0, --$len);
593 my ($self, $mime) = @_;
594 my $mid = eval { mid_clean(mid_mime($mime)) };
595 $self->remove_message($mid) if defined $mid;
599 my ($self, $mime) = @_;
600 my $mid = mid_clean(mid_mime($mime));
601 my $mm = $self->{mm};
602 my $num = $mm->mid_insert($mid);
603 return $num if defined $num;
605 # fallback to num_for since filters like RubyLang set the number
610 my ($self, $mime) = @_;
611 $self->{mm}->mid_delete(mid_clean(mid_mime($mime)));
615 my ($self, $mime, $bytes, $blob) = @_;
616 my $num = $self->{mm}->num_for(mid_clean(mid_mime($mime)));
617 add_message($self, $mime, $bytes, $num, $blob);
621 my ($self, $mime) = @_;
622 $self->{mm}->mid_delete(mid_clean(mid_mime($mime)));
623 unindex_blob($self, $mime);
627 my ($self, $mime, $bytes, $blob) = @_;
628 my $num = index_mm($self, $mime);
629 add_message($self, $mime, $bytes, $num, $blob);
633 my ($self, $mime) = @_;
634 unindex_blob($self, $mime);
635 unindex_mm($self, $mime);
639 my ($git, $blob, $sizeref) = @_;
641 my $str = $git->cat_file($blob, $sizeref);
642 # fixup bugs from import:
643 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
644 PublicInbox::MIME->new($str);
650 my ($self, $opts) = @_;
651 with_umask($self, sub { $self->_index_sync($opts) });
654 sub batch_adjust ($$$$) {
655 my ($max, $bytes, $batch_cb, $latest) = @_;
659 $batch_cb->($latest, 1);
665 my ($self, $log, $add_cb, $del_cb, $batch_cb) = @_;
666 my $hex = '[a-f0-9]';
667 my $h40 = $hex .'{40}';
668 my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!;
669 my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!;
670 my $git = $self->{git};
673 my $max = BATCH_BYTES;
676 while (defined($line = <$log>)) {
677 if ($line =~ /$addmsg/o) {
679 my $mime = do_cat_mail($git, $blob, \$bytes) or next;
680 batch_adjust(\$max, $bytes, $batch_cb, $latest);
681 $add_cb->($self, $mime, $bytes, $blob);
682 } elsif ($line =~ /$delmsg/o) {
684 my $mime = do_cat_mail($git, $blob, \$bytes) or next;
685 batch_adjust(\$max, $bytes, $batch_cb, $latest);
686 $del_cb->($self, $mime);
687 } elsif ($line =~ /^commit ($h40)/o) {
691 $batch_cb->($latest, 0);
696 $self->{mm} ||= eval {
697 require PublicInbox::Msgmap;
698 my $msgmap_path = $self->{msgmap_path};
699 if (defined $msgmap_path) { # v2
700 PublicInbox::Msgmap->new_file($msgmap_path, 1);
702 PublicInbox::Msgmap->new($self->{mainrepo}, 1);
708 my ($self, $range) = @_;
709 $self->{git}->popen(qw/log --reverse --no-notes --no-color
710 --raw -r --no-abbrev/, $range);
713 # indexes all unindexed messages
715 my ($self, $opts) = @_;
716 my $tip = $opts->{ref} || 'HEAD';
717 my $reindex = $opts->{reindex};
718 my ($mkey, $last_commit, $lx, $xlog);
719 $self->{git}->batch_prepare;
720 my $xdb = _xdb_acquire($self);
721 $xdb->begin_transaction;
724 $mkey = 'last_commit';
725 $last_commit = $xdb->get_metadata('last_commit');
729 $mkey = undef if $last_commit ne '';
731 $xdb->cancel_transaction;
732 $xdb = _xdb_release($self);
734 # ensure we leak no FDs to "git log"
735 my $range = $lx eq '' ? $tip : "$lx..$tip";
736 $xlog = _git_log($self, $range);
738 $xdb = _xdb_acquire($self);
739 $xdb->begin_transaction;
740 } while ($xdb->get_metadata('last_commit') ne $last_commit);
742 my $mm = _msgmap_init($self);
743 my $dbh = $mm->{dbh} if $mm;
746 my ($commit, $more) = @_;
748 $mm->last_commit($commit) if $commit;
752 $xdb->set_metadata($mkey, $commit) if $mkey && $commit;
753 $xdb->commit_transaction;
754 $xdb = _xdb_release($self);
756 # let another process do some work... <
759 $xdb = _xdb_acquire($self);
760 $xdb->begin_transaction;
762 $dbh->begin_work if $dbh;
768 my $lm = $mm->last_commit || '';
770 # Common case is the indexes are synced,
771 # we only need to run git-log once:
772 rlog($self, $xlog, *index_both, *unindex_both, $cb);
774 # Uncommon case, msgmap and xapian are out-of-sync
775 # do not care for performance (but git is fast :>)
776 # This happens if we have to reindex Xapian since
777 # msgmap is a frozen format and our Xapian format
779 my $r = $lm eq '' ? $tip : "$lm..$tip";
781 # first, ensure msgmap is up-to-date:
782 my $mkey_prev = $mkey;
783 $mkey = undef; # ignore xapian, for now
784 my $mlog = _git_log($self, $r);
786 rlog($self, $mlog, *index_mm, *unindex_mm, $cb);
787 $mm_only = $mlog = undef;
789 # now deal with Xapian
792 rlog($self, $xlog, *index_mm2, *unindex_mm2, $cb);
795 # user didn't install DBD::SQLite and DBI
796 rlog($self, $xlog, *add_message, *unindex_blob, $cb);
800 # this will create a ghost as necessary
801 sub resolve_mid_to_tid {
802 my ($self, $mid) = @_;
804 $self->each_smsg_by_mid($mid, sub {
806 my $cur_tid = $smsg->thread_id;
808 merge_threads($self, $tid, $cur_tid);
810 $tid = $smsg->thread_id;
814 return $tid if defined $tid;
816 $self->create_ghost($mid)->thread_id;
820 my ($self, $mid) = @_;
822 my $tid = $self->next_thread_id;
823 my $doc = Search::Xapian::Document->new;
824 $doc->add_boolean_term('Q' . $mid);
825 $doc->add_boolean_term('G' . $tid);
826 $doc->add_boolean_term('T' . 'ghost');
828 my $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
829 $self->{xdb}->add_document($doc);
835 my ($self, $winner_tid, $loser_tid) = @_;
836 return if $winner_tid == $loser_tid;
837 my $db = $self->{xdb};
838 batch_do($self, 'G' . $loser_tid, sub {
840 foreach my $docid (@$ids) {
841 my $doc = $db->get_document($docid);
842 $doc->remove_term('G' . $loser_tid);
843 $doc->add_boolean_term('G' . $winner_tid);
844 $db->replace_document($docid, $doc);
849 sub _read_git_config_perm {
851 my @cmd = qw(config);
852 if ($self->{version} == 2) {
853 push @cmd, "--file=$self->{mainrepo}/all.git/config";
855 my $fh = $self->{git}->popen(@cmd, 'core.sharedRepository');
858 chomp $perm if defined $perm;
862 sub _git_config_perm {
864 my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
865 return PERM_GROUP if (!defined($perm) || $perm eq '');
866 return PERM_UMASK if ($perm eq 'umask');
867 return PERM_GROUP if ($perm eq 'group');
868 if ($perm =~ /\A(?:all|world|everybody)\z/) {
869 return PERM_EVERYBODY;
871 return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
872 return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
875 return PERM_UMASK if ($i == PERM_UMASK);
876 return PERM_GROUP if ($i == OLD_PERM_GROUP);
877 return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
879 if (($i & 0600) != 0600) {
880 die "core.sharedRepository mode invalid: ".
881 sprintf('%.3o', $i) . "\nOwner must have permissions\n";
887 my ($perm) = @_; # _git_config_perm return value
889 return umask if $rv == 0;
891 # set +x bit if +r or +w were set
892 $rv |= 0100 if ($rv & 0600);
893 $rv |= 0010 if ($rv & 0060);
894 $rv |= 0001 if ($rv & 0006);
899 my ($self, $cb) = @_;
900 my $old = umask $self->{umask};
901 my $rv = eval { $cb->() };
909 # order matters for unlocking
910 $_[0]->{xdb} = undef;
911 $_[0]->{lockfh} = undef;
914 # remote_* subs are only used by SearchIdxPart and SearchIdxSkeleton
917 if (my $w = $self->{w}) {
918 print $w "commit\n" or die "failed to write commit: $!";
920 $self->commit_txn_lazy;
921 if (my $skel = $self->{skeleton}) {
922 $skel->commit_txn_lazy;
929 if (my $w = delete $self->{w}) {
930 my $pid = delete $self->{pid} or die "no process to wait on\n";
931 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
932 close $w or die "failed to close pipe for pid:$pid: $!\n";
933 waitpid($pid, 0) == $pid or die "remote process did not finish";
934 $? == 0 or die ref($self)." pid:$pid exited with: $?";
936 die "transaction in progress $self\n" if $self->{txn};
937 $self->_xdb_release if $self->{xdb};
942 my ($self, $oid, $mid) = @_;
943 if (my $w = $self->{w}) {
944 # triggers remove_by_oid in partition or skeleton
945 print $w "D $oid $mid\n" or die "failed to write remove $!";
947 $self->begin_txn_lazy;
948 $self->remove_by_oid($oid, $mid);
954 return if $self->{txn};
955 my $xdb = $self->{xdb} || $self->_xdb_acquire;
956 $xdb->begin_transaction;
960 sub commit_txn_lazy {
962 delete $self->{txn} or return;
963 $self->{xdb}->commit_transaction;
968 die "$$ $0 xdb not released\n" if $self->{xdb};
969 die "$$ $0 still in transaction\n" if $self->{txn};