write tmpfile into vault dir, if writable. added sample script, bump version

This commit is contained in:
git@daemon.de
2015-05-21 09:11:52 +02:00
parent dc5038d15b
commit e48c2c8bac
4 changed files with 76 additions and 4 deletions

View File

@@ -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.

2
README
View File

@@ -52,5 +52,5 @@ AUTHOR
VERSION
1.19
1.20

View File

@@ -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<http://www.perlfoundation.org/artistic_license_2_0>
=head1 VERSION
Crypt::PWSafe3 Version 1.19.
Crypt::PWSafe3 Version 1.20.
=cut

49
sample/test.pl Executable file
View File

@@ -0,0 +1,49 @@
#!/usr/bin/perl
use lib qw(blib/lib);
use Crypt::PWSafe3;
my $file = shift;
if (!$file) {
print STDERR "Usage $0 <vault>\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;
}