]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
SQLiteUtil: hoist out common create_db code
authorEric Wong <e@80x24.org>
Wed, 4 Dec 2024 19:39:14 +0000 (19:39 +0000)
committerEric Wong <e@80x24.org>
Fri, 6 Dec 2024 20:07:19 +0000 (20:07 +0000)
We want all SQLite files we create to respect the current umask
and disable CoW for random access performance.

lib/PublicInbox/IMAPTracker.pm
lib/PublicInbox/LeiMailSync.pm
lib/PublicInbox/Over.pm
lib/PublicInbox/POP3D.pm
lib/PublicInbox/SQLiteUtil.pm
lib/PublicInbox/SharedKV.pm

index 4efa8a7e7e38b774ee09709b1de027d7c4cd8b79..2dd809b51bc09242fcfce5891453502e733f9940 100644 (file)
@@ -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;
index fc7963a18ea1e6640dbdc3d3eeac529874768072..cab5bbb3814c4fd9b2e782288fd424e5f86872c4 100644 (file)
@@ -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,
index ff5332e777e625ac8b905b16ae3e761da085bc25..0ecdae3b937c2bd78fcd2b5297d6ab47e287bf72 100644 (file)
@@ -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:
                }
index 1898c89d44434274acf4548e149e25946e90862f..a30fc6770e650596431d66616343b5ebd6306d28 100644 (file)
@@ -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,
index 68e0726dcec03efcb86ba0c9f45d8861d463a3eb..fcec9e4cc43580a18093dff34484d671546baa13 100644 (file)
@@ -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;
index 062d5e3e5280903f436177506a8cc4c270a2ea6f..3aafff506982cc99685c268e0bd04768c781b1c7 100644 (file)
@@ -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;
 }