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 3/2] pop3: fix numerous bugs in delete handling
Date: Wed, 20 Jul 2022 07:27:48 +0000	[thread overview]
Message-ID: <20220720072748.GA10401@dcvr> (raw)
In-Reply-To: <20220719024950.1831808-1-e@80x24.org>

Eric Wong <e@80x24.org> wrote:
> The on-disk storage aspect still has me a little nervous :x

I think this needs to be squashed into [PATCH 1/2] to avoid
migration costs (probably nobody but me is running this, yet):
------8<-------
Subject: [PATCH] pop3: fix numerous bugs in delete handling

This requires a DB change to simplify our Perl code, but there's
no migration for it presently.

We were also incorrectly relying on array offsets instead of sequence
numbers for "EXPIRE 0" handling.
---
 lib/PublicInbox/POP3.pm  |  4 ++--
 lib/PublicInbox/POP3D.pm |  2 +-
 t/pop3d.t                | 37 +++++++++++++++++++++++++++++++++++--
 3 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/lib/PublicInbox/POP3.pm b/lib/PublicInbox/POP3.pm
index 5cffc556..51c2b71a 100644
--- a/lib/PublicInbox/POP3.pm
+++ b/lib/PublicInbox/POP3.pm
@@ -218,7 +218,7 @@ sub need_txn ($) {
 
 sub _stat_cache ($) {
 	my ($self) = @_;
-	my ($beg, $end) = ($self->{uid_dele} // 0, $self->{uid_max});
+	my ($beg, $end) = (($self->{uid_dele} // -1) + 1, $self->{uid_max});
 	PublicInbox::IMAP::uid_clamp($self, \$beg, \$end);
 	my $opt = { limit => PublicInbox::IMAP::UID_SLICE };
 	my $m = $self->{ibx}->over(1)->do_get(<<'', $opt, $beg, $end);
@@ -305,7 +305,7 @@ sub retr_cb { # called by git->cat_async via ibx_async_cat
 	$$bref .= substr($$bref, -2, 2) eq "\r\n" ? ".\r\n" : "\r\n.\r\n";
 	$self->msg_more("+OK message follows\r\n");
 	$self->write($bref);
-	$self->{expire} .= pack('S', $off) if exists $self->{expire};
+	$self->{expire} .= pack('S', $off + 1) if exists $self->{expire};
 	$self->requeue;
 }
 
diff --git a/lib/PublicInbox/POP3D.pm b/lib/PublicInbox/POP3D.pm
index 65997f6d..c7bf1755 100644
--- a/lib/PublicInbox/POP3D.pm
+++ b/lib/PublicInbox/POP3D.pm
@@ -84,7 +84,7 @@ CREATE TABLE IF NOT EXISTS deletes (
 	txn_id INTEGER PRIMARY KEY NOT NULL, /* -1 == txn lock offset */
 	user_id INTEGER NOT NULL REFERENCES users,
 	mailbox_id INTEGER NOT NULL REFERENCES mailboxes,
-	uid_dele INTEGER NULL, /* IMAP UID, NNTP article number */
+	uid_dele INTEGER NOT NULL DEFAULT -1, /* IMAP UID, NNTP article */
 	UNIQUE(user_id, mailbox_id) )
 
 }
diff --git a/t/pop3d.t b/t/pop3d.t
index e781987e..d5ccb0d8 100644
--- a/t/pop3d.t
+++ b/t/pop3d.t
@@ -54,10 +54,10 @@ unless (-r $key && -r $cert) {
 my $old = start_script(['-pop3d', '-W0',
 	"--stdout=$tmpdir/plain.out", "--stderr=$olderr" ],
 	$env, { 3 => $plain });
-my $oldc = Net::POP3->new($plain->sockhost, Port => $plain->sockport);
+my @old_args = ($plain->sockhost, Port => $plain->sockport);
+my $oldc = Net::POP3->new(@old_args);
 my $locked_mb = ('e'x32)."\@$group";
 ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP to old');
-my @old_args = ($plain->sockhost, Port => $plain->sockport);
 
 { # locking within the same process
 	my $x = Net::POP3->new(@old_args);
@@ -238,6 +238,39 @@ EOF
 {
 	my $capa = $oldc->capa;
 	ok(defined($capa->{PIPELINING}), 'pipelining supported by CAPA');
+	is($capa->{EXPIRE}, 0, 'EXPIRE 0 set');
+
+	# clients which see "EXPIRE 0" can elide DELE requests
+	my $list = $oldc->list;
+	ok($oldc->get($_), "RETR $_") for keys %$list;
+	ok($oldc->quit, 'QUIT after RETR');
+
+	$oldc = Net::POP3->new(@old_args);
+	ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP reconnect');
+	my $cont = $oldc->list;
+	is_deeply($cont, {}, 'no messages after implicit DELE from EXPIRE 0');
+	ok($oldc->quit, 'QUIT on noop');
+
+	# test w/o checking CAPA to trigger EXPIRE 0
+	$oldc = Net::POP3->new(@old_args);
+	ok($oldc->apop($locked_mb, 'anonymous'), 'APOP on latest slice');
+	my $l2 = $oldc->list;
+	is_deeply($l2, $list, 'different mailbox, different deletes');
+	ok($oldc->get($_), "RETR $_") for keys %$list;
+	ok($oldc->quit, 'QUIT w/o EXPIRE nor DELE');
+
+	$oldc = Net::POP3->new(@old_args);
+	ok($oldc->apop($locked_mb, 'anonymous'), 'APOP again on latest');
+	$l2 = $oldc->list;
+	is_deeply($l2, $list, 'no DELE nor EXPIRE preserves messages');
+	ok($oldc->delete(2), 'explicit DELE on latest');
+	ok($oldc->quit, 'QUIT w/ highest DELE');
+
+	# this is non-standard behavior, but necessary if we expect hundreds
+	# of thousands of users on cheap HW
+	$oldc = Net::POP3->new(@old_args);
+	ok($oldc->apop($locked_mb, 'anonymous'), 'APOP yet again on latest');
+	is_deeply($oldc->list, {}, 'highest DELE deletes older messages, too');
 }
 
 # TODO: more tests, but mpop was really helpful in helping me

  parent reply	other threads:[~2022-07-20  7:27 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-19  2:49 [PATCH 0/2] preliminary POP3 daemon Eric Wong
2022-07-19  2:49 ` [PATCH 1/2] public-inbox-pop3d - a mostly read-only POP3 server Eric Wong
2022-07-19  2:49 ` [PATCH 2/2] pop3: implement IN-USE from RESP-CODES (RFC 2449) Eric Wong
2022-07-20  7:27 ` Eric Wong [this message]
2022-07-20  9:24 ` [PATCH v2 0/5] public-inbox POP3 daemon Eric Wong
2022-07-20  9:24   ` [PATCH v2 1/5] public-inbox-pop3d - a mostly read-only POP3 server Eric Wong
2022-07-20  9:24   ` [PATCH v2 2/5] pop3: implement IN-USE from RESP-CODES (RFC 2449) Eric Wong
2022-07-20  9:24   ` [PATCH v2 3/5] pop3: TOP requests do not expire messages Eric Wong
2022-07-20  9:24   ` [PATCH v2 4/5] netd: setup TLS bits for well-known STARTTLS ports Eric Wong
2022-07-20  9:24   ` [PATCH v2 5/5] pop3: advertise STLS in CAPA if appropriate 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=20220720072748.GA10401@dcvr \
    --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).