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] lei: show searches prefixed with `.'
Date: Sun, 10 Nov 2024 20:43:26 +0000	[thread overview]
Message-ID: <20241110204326.22684-1-e@80x24.org> (raw)

Sometimes, a user will use an output with a basename which
starts with a `.'.  glob("*") won't list files prefixed with `.'
by default and glob(".* *") requires iterating twice on my
system.  So just rely on Perl regexps instead of glob to get the
directory listing done in a single pass.   We can improve error
detection with autodie for opendir, as well.
---
 lib/PublicInbox/LeiSavedSearch.pm | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm
index c27b6f86..ab0a2858 100644
--- a/lib/PublicInbox/LeiSavedSearch.pm
+++ b/lib/PublicInbox/LeiSavedSearch.pm
@@ -4,6 +4,7 @@
 # pretends to be like LeiDedupe and also PublicInbox::Inbox
 package PublicInbox::LeiSavedSearch;
 use v5.12;
+use autodie qw(opendir);
 use parent qw(PublicInbox::Lock);
 use PublicInbox::Git qw(git_exe);
 use PublicInbox::OverIdx;
@@ -57,16 +58,18 @@ sub lss_dir_for ($$;$) {
 		my @cur = stat(_);
 		my $want = pack('dd', @cur[1,0]); # st_ino + st_dev
 		my ($c, $o, @st);
-		for my $g ("$pfx-*", '*') {
-			my @maybe = glob("$lss_dir$g/lei.saved-search");
-			for my $f (@maybe) {
-				$c = $lei->cfg_dump($f) // next;
-				$o = $c->{'lei.q.output'} // next;
-				$o =~ s!$LOCAL_PFX!! or next;
-				@st = stat($o) or next;
-				next if pack('dd', @st[1,0]) ne $want;
-				$f =~ m!\A(.+?)/[^/]+\z! and return $1;
-			}
+		opendir(my $dh, $lss_dir);
+		my @d = sort(grep(!/\A\.\.?\z/, readdir($dh)));
+		my $re = qr/\A\Q$pfx\E-\./;
+		for my $d (grep(/$re/, @d), grep(!/$re/, @d)) {
+			my $f = "$lss_dir/$d/lei.saved-search";
+			-f $f // next;
+			$c = $lei->cfg_dump($f) // next;
+			$o = $c->{'lei.q.output'} // next;
+			$o =~ s!$LOCAL_PFX!! or next;
+			@st = stat($o) or next;
+			next if pack('dd', @st[1,0]) ne $want;
+			$f =~ m!\A(.+?)/[^/]+\z! and return $1;
 		}
 	}
 	$d;
@@ -80,8 +83,11 @@ sub list {
 	my $fh = File::Temp->new(TEMPLATE => 'lss_list-XXXX', TMPDIR => 1) or
 		die "File::Temp->new: $!";
 	print $fh "[include]\n";
-	for my $p (glob("$lss_dir/*/lei.saved-search")) {
-		print $fh "\tpath = ", cquote_val($p), "\n";
+	opendir(my $dh, $lss_dir);
+	for my $d (sort(grep(!/\A\.\.?\z/, readdir($dh)))) {
+		next if $d eq '.' || $d eq '..';
+		my $p = "$lss_dir/$d/lei.saved-search";
+		say $fh "\tpath = ", cquote_val($p) if -f $p;
 	}
 	$fh->flush or die "flush: $fh";
 	my $cfg = $lei->cfg_dump($fh->filename);

                 reply	other threads:[~2024-11-10 20:43 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=20241110204326.22684-1-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).