From cd040238114c91f4942e0847448a84830fac4f7c Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Thu, 23 Jul 2015 21:15:01 +0200 Subject: Allow custom database path. --- imapsync | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'imapsync') diff --git a/imapsync b/imapsync index a18e4cd..4ad95f3 100755 --- a/imapsync +++ b/imapsync @@ -47,10 +47,29 @@ usage(1) unless GetOptions(\%CONFIG, qw/debug help|h config=s quiet|q oneshot|1/ usage(0) if $CONFIG{help}; -my $CONFFILE = delete $CONFIG{config} // 'imapsync'; -my $CACHEDIR = './imapsync.cache'; # XXX use a config option -my $DBFILE = "$CACHEDIR/imap.guilhem.org.db"; -my $LOCKFILE = "$CACHEDIR/.imap.guilhem.org.lck"; +my $CONF = read_config( delete $CONFIG{config} // $NAME + , [qw/_ local remote/] + , database => qr/\A(\P{Control}+)\z/ ); +my ($DBFILE, $LOCKFILE); + +{ + $DBFILE = $CONF->{_}->{database} if defined $CONF->{_}; + $DBFILE //= $CONF->{remote}->{host}.'.db' if defined $CONF->{remote}; + $DBFILE //= $CONF->{local}->{host}. '.db' if defined $CONF->{local}; + die "Missing option database" unless defined $DBFILE; + + unless ($DBFILE =~ /\A\//) { + my $dir = ($ENV{XDG_DATA_HOME} // "$ENV{HOME}/.local/share") .'/'. $NAME; + $dir =~ /\A(\/\p{Print}+)\z/ or die "Insecure $dir"; + $dir = $1; + $DBFILE = $dir .'/'. $DBFILE; + unless (-d $dir) { + mkdir $dir, 0700 or die "Cannot mkdir $dir: $!\n"; + } + } + + $LOCKFILE = $DBFILE =~ s/([^\/]+)\z/.$1.lck/r; +} my ($DBH, $IMAP); @@ -67,10 +86,7 @@ $SIG{$_} = sub { clean(); die "$!\n"; } foreach qw/INT TERM/; ############################################################################# # Lock the database { - if (!-d $CACHEDIR) { - mkdir $CACHEDIR, 0700 or die "Cannot mkdir $CACHEDIR: $!\n"; - } - elsif (-f $LOCKFILE) { + if (-f $LOCKFILE) { open my $lock, '<', $LOCKFILE or die "Cannot open $LOCKFILE: $!\n"; my $pid = <$lock>; close $lock; @@ -164,7 +180,7 @@ sub msg($@) { # Connect to the local and remote IMAP servers foreach my $name (qw/local remote/) { - my %config = Net::IMAP::Sync::read_config($CONFFILE, $name); + my %config = %{$CONF->{$name}}; $config{$_} = $CONFIG{$_} foreach keys %CONFIG; $config{enable} = 'QRESYNC'; $config{name} = $name; -- cgit v1.2.3