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 1/2] lei_input: always prefix `maildir:' internally
Date: Sat, 30 Sep 2023 00:36:15 +0000	[thread overview]
Message-ID: <20230930003616.3224311-2-e@80x24.org> (raw)
In-Reply-To: <20230930003616.3224311-1-e@80x24.org>

This allows us to reduce stats for `new' and `cur' subdirs
of the Maildir and will also make it easier for us to support
MH, v2, v1, and extindex directories as inputs.
---
 lib/PublicInbox/LeiInput.pm | 57 ++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 26 deletions(-)

diff --git a/lib/PublicInbox/LeiInput.pm b/lib/PublicInbox/LeiInput.pm
index b6c2b6bb..f88c5374 100644
--- a/lib/PublicInbox/LeiInput.pm
+++ b/lib/PublicInbox/LeiInput.pm
@@ -1,10 +1,9 @@
-# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # parent class for LeiImport, LeiConvert, LeiIndex
 package PublicInbox::LeiInput;
-use strict;
-use v5.10.1;
+use v5.12;
 use PublicInbox::DS;
 use PublicInbox::Spawn qw(which popen_rd);
 use PublicInbox::InboxWritable qw(eml_from_path);
@@ -181,10 +180,7 @@ sub input_path_url {
 		}
 		local $PublicInbox::DS::in_loop = 0 if $zsfx; # awaitpid
 		$self->input_fh($ifmt, $mbl->{fh}, $input, @args);
-	} elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
-		return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
-$input appears to be a maildir, not $ifmt
-EOM
+	} elsif (-d _ && $ifmt eq 'maildir') {
 		my $mdr = PublicInbox::MdirReader->new;
 		if (my $pmd = $self->{pmd}) {
 			$mdr->maildir_each_file($input,
@@ -259,6 +255,17 @@ sub prepare_http_input ($$$) {
 	$self->{"-curl-$url"} = [ @curl_opt, $uri ]; # for handle_http_input
 }
 
+sub add_dir ($$$$) {
+	my ($lei, $istate, $ifmt, $input) = @_;
+	if ($istate->{-may_sync}) {
+		$$input = "$ifmt:".$lei->abs_path($$input);
+		push @{$istate->{-sync}->{ok}}, $$input if $istate->{-sync};
+	} else {
+		substr($$input, 0, 0) = "$ifmt:"; # prefix
+	}
+	push @{$istate->{$ifmt}}, $$input;
+}
+
 sub prepare_inputs { # returns undef on error
 	my ($self, $lei, $inputs) = @_;
 	my $in_fmt = $lei->{opt}->{'in-format'};
@@ -272,7 +279,8 @@ sub prepare_inputs { # returns undef on error
 		push @{$sync->{no}}, '/dev/stdin' if $sync;
 	}
 	my $net = $lei->{net}; # NetWriter may be created by l2m
-	my (@f, @md);
+	my @f;
+	my $istate = { -sync => $sync, -may_sync => $may_sync };
 	# e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
 	for my $input (@$inputs) {
 		my $input_path = $input;
@@ -292,11 +300,8 @@ sub prepare_inputs { # returns undef on error
 --in-format=$in_fmt and `$ifmt:' conflict
 
 			}
-			if ($ifmt =~ /\A(?:maildir|mh)\z/i) {
-				push @{$sync->{ok}}, $input if $sync;
-			} else {
-				push @{$sync->{no}}, $input if $sync;
-			}
+			($sync && $ifmt !~ /\A(?:maildir|mh)\z/i) and
+				push(@{$sync->{no}}, $input);
 			my $devfd = $lei->path_to_fd($input_path) // return;
 			if ($devfd >= 0 || (-f $input_path || -p _)) {
 				require PublicInbox::MboxLock;
@@ -304,11 +309,10 @@ sub prepare_inputs { # returns undef on error
 				PublicInbox::MboxReader->reads($ifmt) or return
 					$lei->fail("$ifmt not supported");
 			} elsif (-d $input_path) {
-				$ifmt eq 'maildir' or return
+				$ifmt eq 'maildir' or return # TODO v1/v2/ei
 					$lei->fail("$ifmt not supported");
-				$may_sync and $input = 'maildir:'.
-						$lei->abs_path($input_path);
-				push @md, $input;
+				$input = $input_path;
+				add_dir $lei, $istate, $ifmt, \$input;
 			} elsif ($self->{missing_ok} && !-e _) {
 				# for "lei rm-watch" on missing Maildir
 				$may_sync and $input = 'maildir:'.
@@ -345,12 +349,13 @@ $input is `eml', not --in-format=$in_fmt
 				push @{$sync->{no}}, $input if $sync;
 				push @f, $input;
 			} elsif (-d "$input/new" && -d "$input/cur") {
-				if ($may_sync) {
-					$input = 'maildir:'.
-						$lei->abs_path($input);
-					push @{$sync->{ok}}, $input if $sync;
-				}
-				push @md, $input;
+				add_dir $lei, $istate, 'maildir', \$input;
+			} elsif (-e "$input/inbox.lock") { # TODO
+				$lei->fail('v2 inputs not yet supported (TODO)');
+				#add_dir $lei, $istate, 'v2', \$input;
+			} elsif (-e "$input/ssoma.lock") { # TODO
+				$lei->fail('v1 inputs not yet supported (TODO)');
+				#add_dir $lei, $istate, 'v1', \$input;
 			} elsif ($self->{missing_ok} && !-e $input) {
 				if ($lei->{cmd} eq 'p2q') {
 					# will run "git format-patch"
@@ -382,17 +387,17 @@ $input is `eml', not --in-format=$in_fmt
 		$lei->{auth} //= PublicInbox::LeiAuth->new;
 		$lei->{net} //= $net;
 	}
-	if (scalar(@md)) {
+	if (my $md = $istate->{maildir}) {
 		require PublicInbox::MdirReader;
 		if ($self->can('pmdir_cb')) {
 			require PublicInbox::LeiPmdir;
 			$self->{pmd} = PublicInbox::LeiPmdir->new($lei, $self);
 		}
+		grep(!m!\Amaildir:/!i, @$md) and die "BUG: @$md (no pfx)";
 
 		# start watching Maildirs ASAP
 		if ($may_sync && $lei->{sto}) {
-			grep(!m!\Amaildir:/!i, @md) and die "BUG: @md (no pfx)";
-			$lei->lms(1)->lms_write_prepare->add_folders(@md);
+			$lei->lms(1)->lms_write_prepare->add_folders(@$md);
 			$lei->refresh_watches;
 		}
 	}

  reply	other threads:[~2023-09-30  0:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-30  0:36 [PATCH 0/2] lei: support reading inboxes & extindex w/o search Eric Wong
2023-09-30  0:36 ` Eric Wong [this message]
2023-09-30  0:36 ` [PATCH 2/2] lei convert: support reading from v1, v2, and extindex Eric Wong

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=20230930003616.3224311-2-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).