]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: always open mail_sync.sqlite3 R/W
authorEric Wong <e@80x24.org>
Tue, 5 Apr 2022 08:18:24 +0000 (08:18 +0000)
committerEric Wong <e@80x24.org>
Tue, 5 Apr 2022 21:58:31 +0000 (21:58 +0000)
This will make transparently upgrading from 1.7.0 -> 1.8.x
easier.  Only a single user has access to mail_sync.sqlite3,
and R/W at the kernel-level is required for WAL, anyways.

lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiImport.pm
lib/PublicInbox/LeiImportKw.pm
lib/PublicInbox/LeiLcat.pm
lib/PublicInbox/LeiMailSync.pm
lib/PublicInbox/LeiSearch.pm
lib/PublicInbox/NetReader.pm

index 4e0295fa4e8a18200aa67719b3281add5b5a696e..a7ddc21f4da0de7ea55448994d80f90ec96f4f81 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020-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>
 
 # Backend for `lei' (local email interface).  Unlike the C10K-oriented
@@ -1502,11 +1502,11 @@ sub git_oid {
 }
 
 sub lms {
-       my ($lei, $rw) = @_;
+       my ($lei, $creat) = @_;
        my $sto = $lei->{sto} // _lei_store($lei) // return;
        require PublicInbox::LeiMailSync;
        my $f = "$sto->{priv_eidx}->{topdir}/mail_sync.sqlite3";
-       (-f $f || $rw) ? PublicInbox::LeiMailSync->new($f) : undef;
+       (-f $f || $creat) ? PublicInbox::LeiMailSync->new($f) : undef;
 }
 
 sub sto_done_request {
index bbc0634e820e653f21dd35bacdca19817000d24f..b9865829e39d6e7f5c7131ee772f99d13e74928a 100644 (file)
@@ -1,4 +1,4 @@
-# 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>
 
 # front-end for the "lei import" sub-command
@@ -36,7 +36,7 @@ sub pmdir_cb { # called via wq_io_do from LeiPmdir->each_mdir_fn
        my $kw = PublicInbox::MdirReader::flags2kw($fl);
        substr($folder, 0, 0) = 'maildir:'; # add prefix
        my $lse = $self->{lse} //= $self->{lei}->{sto}->search;
-       my $lms = $self->{-lms_ro} //= $self->{lei}->lms; # may be 0 or undef
+       my $lms = $self->{-lms_rw} //= $self->{lei}->lms; # may be 0 or undef
        my @oidbin = $lms ? $lms->name_oidbin($folder, $bn) : ();
        @oidbin > 1 and warn("W: $folder/*/$$bn not unique:\n",
                                map { "\t".unpack('H*', $_)."\n" } @oidbin);
@@ -87,8 +87,8 @@ sub do_import_index ($$@) {
                # $j = $net->net_concurrency($j); TODO
                if ($lei->{opt}->{incremental} // 1) {
                        $net->{incremental} = 1;
-                       $net->{-lms_ro} = $lei->lms // 0;
-                       if ($self->{-import_kw} && $net->{-lms_ro} &&
+                       $net->{-lms_rw} = $lei->lms // 0;
+                       if ($self->{-import_kw} && $net->{-lms_rw} &&
                                        !$lei->{opt}->{'new-only'} &&
                                        $net->{imap_order}) {
                                require PublicInbox::LeiImportKw;
index 54454511f7bbee48dae0103d84c3a1bae3df16c4..52fd40436a7d262187eee11f70700a68c095eff8 100644 (file)
@@ -1,4 +1,4 @@
-# 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>
 
 # WQ worker for dealing with LeiImport IMAP flags on already-imported messages
@@ -28,13 +28,13 @@ sub ipc_atfork_child {
        $self->{verbose} = $lei->{opt}->{verbose};
        $self->{lse} = $self->{sto}->search;
        $self->{over} = $self->{lse}->over;
-       $self->{-lms_ro} = $net->{-lms_ro} || die 'BUG: net->{-lms_ro} FALSE';
+       $self->{-lms_rw} = $net->{-lms_rw} || die 'BUG: net->{-lms_rw} FALSE';
        $self->SUPER::ipc_atfork_child;
 }
 
 sub ck_update_kw { # via wq_io_do
        my ($self, $url, $uid, $kw) = @_;
-       my @oidbin = $self->{-lms_ro}->num_oidbin($url, $uid);
+       my @oidbin = $self->{-lms_rw}->num_oidbin($url, $uid);
        my $uid_url = "$url/;UID=$uid";
        @oidbin > 1 and warn("W: $uid_url not unique:\n",
                                map { "\t".unpack('H*', $_)."\n" } @oidbin);
index 191f6f244857221d0086db1b0548d64ed35b2c8a..8d89cb7303f2b081b8005daea1dfdf2875f1747b 100644 (file)
@@ -1,4 +1,4 @@
-# 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>
 
 # lcat: local cat, display a local message by Message-ID or blob,
@@ -13,7 +13,7 @@ use PublicInbox::MID qw($MID_EXTRACT);
 
 sub lcat_folder ($$;$$) {
        my ($lei, $folder, $beg, $end) = @_;
-       my $lms = $lei->{-lms_ro} //= $lei->lms // return;
+       my $lms = $lei->{-lms_rw} //= $lei->lms // return;
        my $folders = [ $folder ];
        eval { $lms->arg2folder($lei, $folders) };
        return $lei->child_error(0, "# unknown folder: $folder") if $@;
@@ -31,7 +31,7 @@ sub lcat_folder ($$;$$) {
 sub lcat_imap_uri ($$) {
        my ($lei, $uri) = @_;
        # cf. LeiXSearch->lcat_dump
-       my $lms = $lei->{-lms_ro} //= $lei->lms // return;
+       my $lms = $lei->{-lms_rw} //= $lei->lms // return;
        if (defined $uri->uid) {
                push @{$lei->{lcat_todo}}, $lms->imap_oidhex($lei, $uri);
        } elsif (defined(my $fid = $lms->fid_for($$uri))) {
@@ -45,7 +45,7 @@ sub lcat_nntp_uri ($$) {
        my ($lei, $uri) = @_;
        my $mid = $uri->message; # already unescaped by URI::news
        return "mid:$mid" if defined($mid);
-       my $lms = $lei->{-lms_ro} //= $lei->lms // return;
+       my $lms = $lei->{-lms_rw} //= $lei->lms // return;
        my ($ng, $beg, $end) = $uri->group;
        $uri->group($ng);
        lcat_folder($lei, $$uri, $beg, $end);
@@ -118,7 +118,7 @@ could not extract Message-ID from $x
 
                }
        }
-       delete $lei->{-lms_ro};
+       delete $lei->{-lms_rw};
        @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv");
 }
 
index a9a65fd6f312397909b5bb20b5aa79aa17df60eb..85480599818de3ee1759b7622f35bd789b5fa770 100644 (file)
@@ -11,9 +11,9 @@ use PublicInbox::ContentHash qw(git_sha);
 use Carp ();
 
 sub dbh_new {
-       my ($self, $rw) = @_;
+       my ($self) = @_;
        my $f = $self->{filename};
-       my $creat = $rw && !-s $f;
+       my $creat = !-s $f;
        if ($creat) {
                require PublicInbox::Syscall;
                open my $fh, '+>>', $f or Carp::croak "open($f): $!";
@@ -23,11 +23,10 @@ sub dbh_new {
                AutoCommit => 1,
                RaiseError => 1,
                PrintError => 0,
-               ReadOnly => !$rw,
                sqlite_use_immediate_transaction => 1,
        });
        # no sqlite_unicode, here, all strings are binary
-       create_tables($self, $dbh) if $rw;
+       create_tables($self, $dbh);
        $dbh->do('PRAGMA journal_mode = WAL') if $creat;
        $dbh->do('PRAGMA case_sensitive_like = ON');
        $dbh;
@@ -42,7 +41,7 @@ sub new {
        }, $cls;
 }
 
-sub lms_write_prepare { ($_[0]->{dbh} //= dbh_new($_[0], 1)); $_[0] }
+sub lms_write_prepare { ($_[0]->{dbh} //= dbh_new($_[0])); $_[0] }
 
 sub lms_pause {
        my ($self) = @_;
@@ -102,7 +101,7 @@ UPDATE folders SET loc = ? WHERE fid = ?
 }
 
 sub get_fid ($$$) {
-       my ($sth, $folder, $dbh) = @_; # $dbh is set iff RW
+       my ($sth, $folder, $dbh) = @_;
        $sth->bind_param(1, $folder, SQL_BLOB);
        $sth->execute;
        my ($fid) = $sth->fetchrow_array;
@@ -118,36 +117,37 @@ sub get_fid ($$$) {
 }
 
 sub fid_for {
-       my ($self, $folder, $rw) = @_;
-       my $dbh = $self->{dbh} //= dbh_new($self, $rw);
+       my ($self, $folder, $creat) = @_;
+       my $dbh = $self->{dbh} //= dbh_new($self);
        my $sth = $dbh->prepare_cached(<<'', undef, 1);
 SELECT fid FROM folders WHERE loc = ? LIMIT 1
 
-       my $rw_dbh = $dbh->{ReadOnly} ? undef : $dbh;
-       my $fid = get_fid($sth, $folder, $rw_dbh);
+       my $fid = get_fid($sth, $folder, $dbh);
        return $fid if defined($fid);
 
        # caller had trailing slash (LeiToMail)
        if ($folder =~ s!\A((?:maildir|mh):.*?)/+\z!$1!i) {
-               $fid = get_fid($sth, $folder, $rw_dbh);
+               $fid = get_fid($sth, $folder, $dbh);
                if (defined $fid) {
-                       update_fid($dbh, $fid, $folder) if $rw;
+                       update_fid($dbh, $fid, $folder);
                        return $fid;
                }
        # sometimes we stored trailing slash..
        } elsif ($folder =~ m!\A(?:maildir|mh):!i) {
-               $fid = get_fid($sth, $folder, $rw_dbh);
+               $fid = get_fid($sth, $folder, $dbh);
                if (defined $fid) {
-                       update_fid($dbh, $fid, $folder) if $rw;
+                       update_fid($dbh, $fid, $folder);
                        return $fid;
                }
-       } elsif ($rw && $folder =~ m!\Aimaps?://!i) {
+       } elsif ($creat && $folder =~ m!\Aimaps?://!i) {
                require PublicInbox::URIimap;
-               PublicInbox::URIimap->new($folder)->uidvalidity //
+               my $uri = PublicInbox::URIimap->new($folder);
+               $uri->uidvalidity //
                        Carp::croak("BUG: $folder has no UIDVALIDITY");
+               defined($uri->uid) and Carp::confess("BUG: $folder has UID");
        }
-       return unless $rw;
 
+       return unless $creat;
        ($fid) = $dbh->selectrow_array('SELECT MAX(fid) FROM folders');
 
        $fid += 1;
index d0ca13f0aa11e444b2fc5292b927dbc777b0a677..936c275178dfc6908577487ce97d3a83c58c5449 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020-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>
 
 # read-only counterpart for PublicInbox::LeiStore
@@ -101,7 +101,7 @@ sub xoids_for {
        my $git = $self->git;
        my $xoids = {};
        # no lms when used via {ale}:
-       my $lms = $self->{-lms_ro} //= lms($self) if defined($self->{topdir});
+       my $lms = $self->{-lms_rw} //= lms($self) if defined($self->{topdir});
        for my $mid (@$mids) {
                for my $o (@overs) {
                        my ($id, $prev);
index 032b4fda557ca70d54a4959c8beff3e3ae91c6f1..c1af03a3f8b972db9a8ff38f0b081652dbb90a53 100644 (file)
@@ -1,4 +1,4 @@
-# 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>
 
 # common reader code for IMAP and NNTP (and maybe JMAP)
@@ -481,7 +481,7 @@ sub itrk_last ($$;$$) {
        my ($self, $uri, $r_uidval, $mic) = @_;
        return (undef, undef, $r_uidval) unless $self->{incremental};
        my ($itrk, $l_uid, $l_uidval);
-       if (defined(my $lms = $self->{-lms_ro})) { # LeiMailSync or 0
+       if (defined(my $lms = $self->{-lms_rw})) { # LeiMailSync or 0
                $uri->uidvalidity($r_uidval) if defined $r_uidval;
                if ($mic) {
                        my $auth = $mic->Authmechanism // '';