use DBI;
use DBD::SQLite;
use PublicInbox::Config;
+use PublicInbox::SQLiteUtil;
sub create_tables ($) {
my ($dbh) = @_;
}
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;
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,
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:
}
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);
-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}) {
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,
# common bits for SQLite users in our codebase
package PublicInbox::SQLiteUtil;
use v5.12;
+use autodie qw(open);
my %SQLITE_GLOB_MAP = (
'[' => '[[]',
: ($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;
$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;
}