From e48c2c8bac8a057bc4f753e1a2f3bb12408997cb Mon Sep 17 00:00:00 2001 From: "git@daemon.de" Date: Thu, 21 May 2015 09:11:52 +0200 Subject: [PATCH] write tmpfile into vault dir, if writable. added sample script, bump version --- CHANGELOG | 5 +++++ README | 2 +- lib/Crypt/PWSafe3.pm | 24 +++++++++++++++++++--- sample/test.pl | 49 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100755 sample/test.pl diff --git a/CHANGELOG b/CHANGELOG index 4a2e89c..0d5eec8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +1.20: + applied another patch by David Dick: writing tmp files in + the same directory where the vault file resides (unless it's + not writable). + 1.19: applied patch by David Dick, which adds some more precautions of i/o error handling and flushing. diff --git a/README b/README index c613568..c15df10 100644 --- a/README +++ b/README @@ -52,5 +52,5 @@ AUTHOR VERSION - 1.19 + 1.20 diff --git a/lib/Crypt/PWSafe3.pm b/lib/Crypt/PWSafe3.pm index 98d1f00..9ba93bb 100644 --- a/lib/Crypt/PWSafe3.pm +++ b/lib/Crypt/PWSafe3.pm @@ -30,7 +30,7 @@ use Data::Dumper; use Exporter (); use vars qw(@ISA @EXPORT); -$Crypt::PWSafe3::VERSION = '1.19'; +$Crypt::PWSafe3::VERSION = '1.20'; use Crypt::PWSafe3::Field; use Crypt::PWSafe3::HeaderField; @@ -350,7 +350,25 @@ sub save { $this->addheader($whatsaved); $this->addheader($whosaved); - my $fd = File::Temp->new(TEMPLATE => '.vaultXXXXXXXX', TMPDIR => 1, EXLOCK => 0) or croak "Could not open tmpfile: $!\n"; + # open a temp vault file, first we try it in the same directory as the target vault file + my $tmpdir; + if (File::Spec->file_name_is_absolute($file)) { + my ($volume, $directories, undef) = File::Spec->splitpath($file); + $tmpdir = File::Spec->catdir($volume, $directories); + } + else { + my ($volume, $directories, undef) = File::Spec->splitpath(File::Spec->rel2abs($file)); + $tmpdir = File::Spec->abs2rel(File::Spec->catdir($volume, $directories)); + } + + my $fd; + if (-w $tmpdir) { + $fd = File::Temp->new(TEMPLATE => '.vaultXXXXXXXX', DIR => $tmpdir, EXLOCK => 0) or croak "Could not open tmpfile: $!\n"; + } + else { + # well, then we'll use one in the tmp dir of the system + $fd = File::Temp->new(TEMPLATE => '.vaultXXXXXXXX', TMPDIR => 1, EXLOCK => 0) or croak "Could not open tmpfile: $!\n"; + } my $tmpfile = "$fd"; $this->{fd} = $fd; @@ -979,7 +997,7 @@ License 2.0, see: L =head1 VERSION -Crypt::PWSafe3 Version 1.19. +Crypt::PWSafe3 Version 1.20. =cut diff --git a/sample/test.pl b/sample/test.pl new file mode 100755 index 0000000..9e06a25 --- /dev/null +++ b/sample/test.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +use lib qw(blib/lib); +use Crypt::PWSafe3; + + +my $file = shift; + +if (!$file) { + print STDERR "Usage $0 \n"; + exit 1; +} + +my $create = 1; +if (-e $file) { + $create = 0; +} + +my $vault = Crypt::PWSafe3->new(file => $file, create => $create, password => 'blah') or die "$!"; + +if ($create) { + my %record = ( + user => 'u3', + passwd => 'p3', + group => 'g3', + title => 't3', + notes => scalar localtime(time) + ); + $vault->newrecord(%record); + $vault->save(); + print "record saved to $file, execute $0 again to view it\n" +} +else { + my @r = $vault->getrecords; + foreach my $rec (@r) { + printf qq(%s: + User: %s +Passwd: %s + Group: %s + Title: %s + Notes: %s +), $rec->uuid, $rec->user, $rec->passwd, $rec->group, $rec->title, $rec->notes; + + $vault->modifyrecord($rec->uuid, notes => scalar localtime(time)); + } + + $vault->save; +} +