From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id AE1F11FB06 for ; Tue, 14 Sep 2021 02:39:05 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 5/5] lei up: fix env/cwd mismatches with multiple folders Date: Tue, 14 Sep 2021 02:39:05 +0000 Message-Id: <20210914023905.21410-6-e@80x24.org> In-Reply-To: <20210914023905.21410-1-e@80x24.org> References: <20210914023905.21410-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: By moving %ENV localization and fchdir into ->dispatch, we can maintain a consistent environment across multiple dispatches while having different clients. --- lib/PublicInbox/LEI.pm | 15 +++++---------- lib/PublicInbox/LeiUp.pm | 15 +++++++-------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index e529c86a..0a30bc36 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -769,6 +769,8 @@ sub lazy_cb ($$$) { sub dispatch { my ($self, $cmd, @argv) = @_; + fchdir($self) or return; + local %ENV = %{$self->{env}}; local $current_lei = $self; # for __WARN__ $self->{2}->autoflush(1); # keep stdout buffered until x_it|DESTROY return _help($self, 'no command given') unless defined($cmd); @@ -1104,14 +1106,9 @@ sub accept_dispatch { # Listener {post_accept} callback my ($argc, @argv) = split(/\0/, $buf, -1); undef $buf; my %env = map { split(/=/, $_, 2) } splice(@argv, $argc); - if (chdir($self->{3})) { - local %ENV = %env; - $self->{env} = \%env; - eval { dispatch($self, @argv) }; - send($sock, $@, MSG_EOR) if $@; - } else { - send($sock, "fchdir: $!", MSG_EOR); # implicit close - } + $self->{env} = \%env; + eval { dispatch($self, @argv) }; + send($sock, $@, MSG_EOR) if $@; } sub dclose { @@ -1181,7 +1178,6 @@ sub cfg2lei ($) { open($lei->{1}, '>>&', \*STDOUT) or die "dup 1: $!"; open($lei->{2}, '>>&', \*STDERR) or die "dup 2: $!"; open($lei->{3}, '/') or die "open /: $!"; - chdir($lei->{3}) or die "chdir /': $!"; my ($x, $y); socketpair($x, $y, AF_UNIX, SOCK_SEQPACKET, 0) or die "socketpair: $!"; $lei->{sock} = $x; @@ -1199,7 +1195,6 @@ sub dir_idle_handler ($) { # PublicInbox::DirIdle callback for my $f (keys %{$MDIR2CFGPATH->{$mdir} // {}}) { my $cfg = $PATH2CFG{$f} // next; eval { - local %ENV = %{$cfg->{-env}}; my $lei = cfg2lei($cfg); $lei->dispatch('note-event', "maildir:$mdir", $nc, $bn, $fn); diff --git a/lib/PublicInbox/LeiUp.pm b/lib/PublicInbox/LeiUp.pm index 53f06dbc..4637cb46 100644 --- a/lib/PublicInbox/LeiUp.pm +++ b/lib/PublicInbox/LeiUp.pm @@ -11,7 +11,6 @@ use PublicInbox::LeiSavedSearch; use PublicInbox::DS; use PublicInbox::PktOp; use PublicInbox::LeiFinmsg; -use PublicInbox::LEI; my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP sub up1 ($$) { @@ -60,19 +59,16 @@ sub up1_redispatch { } else { # multiple outputs $l = bless { %$lei }, ref($lei); $l->{opt} = { %{$l->{opt}} }; # deep copy + delete $l->{opt}->{all}; delete $l->{sock}; # do not close # make close($l->{1}) happy in lei->dclose open my $fh, '>&', $l->{1} or return $l->child_error(0, "dup: $!"); $l->{1} = $fh; + $l->qerr("# updating $out"); } - local $PublicInbox::LEI::current_lei = $l; - local %ENV = %{$l->{env}}; $l->{''} = $op_p; # daemon only ($l => $lei => script/lei) - eval { - $l->qerr("# updating $out"); - up1($l, $out); - }; + eval { $l->dispatch('up', $out) }; $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail() } @@ -94,7 +90,6 @@ sub lei_up { my ($lei, @outs) = @_; my $opt = $lei->{opt}; my $self = bless { -mail_sync => 1 }, __PACKAGE__; - $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search; if (defined(my $all = $opt->{all})) { return $lei->fail("--all and @outs incompatible") if @outs; defined($opt->{mua}) and return @@ -110,10 +105,14 @@ sub lei_up { } else { $lei->fail("only --all=$all not understood"); } + } elsif ($lei->{lse}) { + scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]"; + return up1($lei, $outs[0]); } else { $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ]; $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ]; } + $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search; ((@{$self->{local} // []} + @{$self->{remote} // []}) > 1 && defined($opt->{mua})) and return $lei->fail(<