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/3] SQLiteUtil: hoist out common create_db code
Date: Wed,  4 Dec 2024 19:39:14 +0000	[thread overview]
Message-ID: <20241204193914.3227868-4-e@80x24.org> (raw)
In-Reply-To: <20241204193914.3227868-1-e@80x24.org>

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;
 }
 

      parent reply	other threads:[~2024-12-04 19:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-04 19:39 [PATCH 0/3] some SQLite-related things Eric Wong
2024-12-04 19:39 ` [PATCH 1/3] sqlite: avoid incorrect/deprecated `LIKE' use Eric Wong
2024-12-04 19:39 ` [PATCH 2/3] sqlite: use `BLOB' column type instead of `VARBINARY' Eric Wong
2024-12-04 19:39 ` Eric Wong [this message]

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=20241204193914.3227868-4-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).