From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id A49C71F4D7 for ; Wed, 4 Dec 2024 19:39:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1733341155; bh=5v9vkpR78WRqacmOm1VpR8P/emipcUvMlPed92H3ZaA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=BsGZdt3OxS2hz39cZOXj+kTt69d2DR9Vtghi85lkISf/cUiwIfUBw6+h0sI1LKNLv PuLDr0p6R1sF0Niazk/QmOrd0KCNmy01dhSabzAZIOnZ0Vb17816JkV7ZqqKZvpTmq 0lvVC8qhYljBgJKGrsMJxlN6weYU8vDaLCsrpbcA= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 3/3] SQLiteUtil: hoist out common create_db code Date: Wed, 4 Dec 2024 19:39:14 +0000 Message-ID: <20241204193914.3227868-4-e@80x24.org> In-Reply-To: <20241204193914.3227868-1-e@80x24.org> References: <20241204193914.3227868-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We want all SQLite files we create to respect the current umask and disable CoW for random access performance. --- lib/PublicInbox/IMAPTracker.pm | 5 ++--- lib/PublicInbox/LeiMailSync.pm | 6 +----- lib/PublicInbox/Over.pm | 9 +++------ lib/PublicInbox/POP3D.pm | 8 ++------ lib/PublicInbox/SQLiteUtil.pm | 10 ++++++++++ lib/PublicInbox/SharedKV.pm | 6 +----- 6 files changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/PublicInbox/IMAPTracker.pm b/lib/PublicInbox/IMAPTracker.pm index 4efa8a7e..2dd809b5 100644 --- a/lib/PublicInbox/IMAPTracker.pm +++ b/lib/PublicInbox/IMAPTracker.pm @@ -6,6 +6,7 @@ use parent qw(PublicInbox::Lock); use DBI; use DBD::SQLite; use PublicInbox::Config; +use PublicInbox::SQLiteUtil; sub create_tables ($) { my ($dbh) = @_; @@ -75,11 +76,9 @@ sub new { } if (!-f $dbname) { require File::Path; - require PublicInbox::Syscall; my ($dir) = ($dbname =~ m!(.*?/)[^/]+\z!); File::Path::mkpath($dir); - PublicInbox::Syscall::nodatacow_dir($dir); - open my $fh, '+>>', $dbname or die "failed to open $dbname: $!"; + PublicInbox::SQLiteUtil::create_db $dbname; } my $self = bless { lock_path => "$dbname.lock", url => $url }, $class; $self->lock_acquire; diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm index fc7963a1..cab5bbb3 100644 --- a/lib/PublicInbox/LeiMailSync.pm +++ b/lib/PublicInbox/LeiMailSync.pm @@ -18,11 +18,7 @@ sub dbh_new { my ($self) = @_; my $f = $self->{filename}; my $creat = !-s $f; - if ($creat) { - require PublicInbox::Syscall; - open my $fh, '+>>', $f or Carp::croak "open($f): $!"; - PublicInbox::Syscall::nodatacow_fh($fh); - } + PublicInbox::SQLiteUtil::create_db $f if $creat; my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', { AutoCommit => 1, RaiseError => 1, diff --git a/lib/PublicInbox/Over.pm b/lib/PublicInbox/Over.pm index ff5332e7..0ecdae3b 100644 --- a/lib/PublicInbox/Over.pm +++ b/lib/PublicInbox/Over.pm @@ -13,17 +13,14 @@ use PublicInbox::Smsg; use Compress::Zlib qw(uncompress); use constant DEFAULT_LIMIT => 1000; use List::Util (); # for max -use autodie qw(open); +use PublicInbox::SQLiteUtil; sub dbh_new { my ($self, $rw) = @_; my $f = delete $self->{filename}; - if (!-s $f) { # SQLite defaults mode to 0644, we want 0666 + if (!-s $f) { if ($rw) { - require PublicInbox::Syscall; - my ($dir) = ($f =~ m!(.+)/[^/]+\z!); - PublicInbox::Syscall::nodatacow_dir($dir); - open my $fh, '+>>', $f; + PublicInbox::SQLiteUtil::create_db $f; } else { $self->{filename} = $f; # die on stat() below: } diff --git a/lib/PublicInbox/POP3D.pm b/lib/PublicInbox/POP3D.pm index 1898c89d..a30fc677 100644 --- a/lib/PublicInbox/POP3D.pm +++ b/lib/PublicInbox/POP3D.pm @@ -10,7 +10,7 @@ use Carp (); use File::Temp 0.19 (); # 0.19 for ->newdir use PublicInbox::Config; use PublicInbox::POP3; -use PublicInbox::Syscall; +use PublicInbox::SQLiteUtil; use File::Temp 0.19 (); # 0.19 for ->newdir use Fcntl qw(F_SETLK F_UNLCK F_WRLCK SEEK_SET); my ($FLOCK_TMPL, @FLOCK_ORDER); @@ -73,7 +73,6 @@ sub refresh_groups { # PublicInbox::Daemon callback -d $d or do { require File::Path; File::Path::make_path($d, { mode => 0700 }); - PublicInbox::Syscall::nodatacow_dir($d); }; $self->{lock_path} //= "$d/db.lock"; if (my $old = $self->{pi_cfg}) { @@ -127,10 +126,7 @@ sub state_dbh_new { my ($self) = @_; my $f = "$self->{pi_cfg}->{'publicinbox.pop3state'}/db.sqlite3"; my $creat = !-s $f; - if ($creat) { - open my $fh, '+>>', $f or Carp::croak "open($f): $!"; - PublicInbox::Syscall::nodatacow_fh($fh); - } + PublicInbox::SQLiteUtil::create_db $f if $creat; my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', { AutoCommit => 1, diff --git a/lib/PublicInbox/SQLiteUtil.pm b/lib/PublicInbox/SQLiteUtil.pm index 68e0726d..fcec9e4c 100644 --- a/lib/PublicInbox/SQLiteUtil.pm +++ b/lib/PublicInbox/SQLiteUtil.pm @@ -4,6 +4,7 @@ # common bits for SQLite users in our codebase package PublicInbox::SQLiteUtil; use v5.12; +use autodie qw(open); my %SQLITE_GLOB_MAP = ( '[' => '[[]', @@ -27,4 +28,13 @@ sub mk_sqlite_re ($$) { : ($anywhere ? '.*' : '^')."\Q$pfx\E.*"; } +sub create_db ($) { + my ($f) = @_; + require PublicInbox::Syscall; + my ($dir) = ($f =~ m!(.+)/[^/]+\z!); + PublicInbox::Syscall::nodatacow_dir($dir); # for journal/shm/wal + # SQLite defaults mode to 0644, we want 0666 to respect umask + open my $fh, '+>>', $f; +} + 1; diff --git a/lib/PublicInbox/SharedKV.pm b/lib/PublicInbox/SharedKV.pm index 062d5e3e..3aafff50 100644 --- a/lib/PublicInbox/SharedKV.pm +++ b/lib/PublicInbox/SharedKV.pm @@ -49,11 +49,7 @@ sub new { $base //= ''; my $f = $self->{filename} = "$dir/$base.sqlite3"; $self->{lock_path} = $opt->{lock_path} // "$dir/$base.flock"; - unless (-s $f) { - require PublicInbox::Syscall; - PublicInbox::Syscall::nodatacow_dir($dir); # for journal/shm/wal - open my $fh, '+>>', $f or die "failed to open $f: $!"; - } + PublicInbox::SQLiteUtil::create_db $f if !-s $f; $self; }