unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] search: respect core.sharedRepository in for Xapian DB
Date: Sun, 23 Aug 2015 18:14:04 +0000	[thread overview]
Message-ID: <1440353644-22799-1-git-send-email-e@80x24.org> (raw)

Extend the purpose of core.sharedRepository to apply to
the $GIT_DIR/public-inbox/xapian* directory.
---
 lib/PublicInbox/SearchIdx.pm | 67 ++++++++++++++++++++++++++++++++++++++++++--
 public-inbox-learn           |  4 +--
 public-inbox-mda             |  4 +--
 t/search.t                   | 25 +++++++++++++++++
 4 files changed, 92 insertions(+), 8 deletions(-)

diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 408b21f..5664c38 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -8,6 +8,14 @@ use base qw(PublicInbox::Search);
 use PublicInbox::MID qw/mid_clean mid_compressed/;
 *xpfx = *PublicInbox::Search::xpfx;
 
+use constant {
+	PERM_UMASK => 0,
+	OLD_PERM_GROUP => 1,
+	OLD_PERM_EVERYBODY => 2,
+	PERM_GROUP => 0660,
+	PERM_EVERYBODY => 0664,
+};
+
 sub new {
 	my ($class, $git_dir, $writable) = @_;
 	my $dir = $class->xdir($git_dir);
@@ -18,8 +26,15 @@ sub new {
 		File::Path::mkpath($dir);
 		$flag = Search::Xapian::DB_CREATE_OR_OPEN;
 	}
-	my $db = Search::Xapian::WritableDatabase->new($dir, $flag);
-	bless { xdb => $db, git_dir => $git_dir }, $class;
+	my $self = bless { git_dir => $git_dir }, $class;
+	my $umask = _umask_for($self->_git_config_perm);
+	my $old_umask = umask $umask;
+	my $db = eval { Search::Xapian::WritableDatabase->new($dir, $flag) };
+	my $err = $@;
+	umask $old_umask;
+	die $err if $err;
+	$self->{xdb} = $db;
+	$self;
 }
 
 sub add_message {
@@ -360,4 +375,52 @@ sub merge_threads {
 	}
 }
 
+sub _read_git_config_perm {
+	my ($self) = @_;
+	my @cmd = ('git', "--git-dir=$self->{git_dir}",
+		   qw(config core.sharedRepository));
+	my $pid = open(my $fh, '-|', @cmd) or
+		die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
+	my $perm = <$fh>;
+	close $fh;
+	chomp $perm if defined $perm;
+	$perm;
+}
+
+sub _git_config_perm {
+	my $self = shift;
+	my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
+	return PERM_GROUP if (!defined($perm) || !length($perm));
+	return PERM_UMASK if ($perm eq 'umask');
+	return PERM_GROUP if ($perm eq 'group');
+	if ($perm =~ /\A(?:all|world|everybody)\z/) {
+		return PERM_EVERYBODY;
+	}
+	return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
+	return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
+
+	my $i = oct($perm);
+	return PERM_UMASK if ($i == PERM_UMASK);
+	return PERM_GROUP if ($i == OLD_PERM_GROUP);
+	return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
+
+	if (($i & 0600) != 0600) {
+		die "core.sharedRepository mode invalid: ".
+		    sprintf('%.3o', $i) . "\nOwner must have permissions\n";
+	}
+	($i & 0666);
+}
+
+sub _umask_for {
+	my ($perm) = @_; # _git_config_perm return value
+	my $rv = $perm;
+	return umask if $rv == 0;
+
+	# set +x bit if +r or +w were set
+	$rv |= 0100 if ($rv & 0600);
+	$rv |= 0010 if ($rv & 0060);
+	$rv |= 0001 if ($rv & 0006);
+	(~$rv & 0777);
+}
+
 1;
diff --git a/public-inbox-learn b/public-inbox-learn
index c89ffb5..f0bbd42 100755
--- a/public-inbox-learn
+++ b/public-inbox-learn
@@ -79,9 +79,7 @@ foreach my $recipient (keys %dests) {
 
 	$err or eval {
 		require PublicInbox::SearchIdx;
-		umask 0022; # XXX FIXME use git config core.sharedRepository
-		my $s = PublicInbox::SearchIdx->new($git_dir, 2);
-		$s->index_sync;
+		PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;
 	};
 }
 
diff --git a/public-inbox-mda b/public-inbox-mda
index 630ffcb..8e98d6e 100755
--- a/public-inbox-mda
+++ b/public-inbox-mda
@@ -89,8 +89,6 @@ sub search_index_sync {
 	my ($git_dir) = @_;
 	eval {
 		require PublicInbox::Search;
-		umask 0022; # XXX FIXME use git config core.sharedRepository
-		my $s = PublicInbox::SearchIdx->new($git_dir, 2);
-		$s->index_sync;
+		PublicInbox::SearchIdx->new($git_dir, 2)->index_sync;
 	};
 }
diff --git a/t/search.t b/t/search.t
index be39410..17e9eaf 100644
--- a/t/search.t
+++ b/t/search.t
@@ -24,6 +24,31 @@ my $rw_commit = sub {
 };
 
 {
+	# git repository perms
+	is(PublicInbox::SearchIdx->_git_config_perm(undef),
+	   &PublicInbox::SearchIdx::PERM_GROUP,
+	   "undefined permission is group");
+	is(PublicInbox::SearchIdx::_umask_for(
+	     PublicInbox::SearchIdx->_git_config_perm('0644')),
+	   0022, "644 => umask(0022)");
+	is(PublicInbox::SearchIdx::_umask_for(
+	     PublicInbox::SearchIdx->_git_config_perm('0600')),
+	   0077, "600 => umask(0077)");
+	is(PublicInbox::SearchIdx::_umask_for(
+	     PublicInbox::SearchIdx->_git_config_perm('0640')),
+	   0027, "640 => umask(0027)");
+	is(PublicInbox::SearchIdx::_umask_for(
+	     PublicInbox::SearchIdx->_git_config_perm('group')),
+	   0007, 'group => umask(0007)');
+	is(PublicInbox::SearchIdx::_umask_for(
+	     PublicInbox::SearchIdx->_git_config_perm('everybody')),
+	   0002, 'everybody => umask(0002)');
+	is(PublicInbox::SearchIdx::_umask_for(
+	     PublicInbox::SearchIdx->_git_config_perm('umask')),
+	   umask, 'umask => existing umask');
+}
+
+{
 	my $root = Email::MIME->create(
 		header_str => [
 			Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
-- 
EW


                 reply	other threads:[~2015-08-23 18:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1440353644-22799-1-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).