diff --git a/lib/Crypt/PWSafe3.pm b/lib/Crypt/PWSafe3.pm index f38010c..62ef3d5 100644 --- a/lib/Crypt/PWSafe3.pm +++ b/lib/Crypt/PWSafe3.pm @@ -470,6 +470,26 @@ sub modifyrecord { $this->markmodified(); } +sub deleterecord { + # + # delete a record identified by the given uuid, if present + # + # returns 1 if record was actually removed, 0 if it was not present + my($this, $uuid) = @_; + + if (! exists $this->{record}->{$uuid}) { + return 0; + } + + delete $this->{record}->{$uuid}; + + # mark vault as modified + $this->markmodified(); + + return 1; +} + + sub markmodified { # # mark the vault as modified by setting the appropriate header fields diff --git a/t/run.t b/t/run.t index 078d587..b3dd84b 100644 --- a/t/run.t +++ b/t/run.t @@ -108,6 +108,31 @@ eval { }; ok(!$@, "modify some header fields ($@)"); +### 6 delete +eval { + my $vault6 = new Crypt::PWSafe3(file => 't/3.out', password => 'tom'); + my $uuid = $vault6->newrecord(user => 'xxx', passwd => 'y'); + $vault6->save(file=>'t/6.out'); + + my $rvault6 = new Crypt::PWSafe3(file => 't/6.out', password => 'tom'); + my $rec = $rvault6->getrecord($uuid); + if ($rec->user ne 'xxx') { + die "oop way record change failed"; + } + $rvault6->deleterecord($uuid); + if ($rvault6->getrecord($uuid)) { + die "deleted record still present in open vault"; + } + $vault6->save(file=>'t/6a.out'); + + my $rvault6a = new Crypt::PWSafe3(file => 't/6a.out', password => 'tom'); + if ($rvault6->getrecord($uuid)) { + die "deleted record reappears after save and reload"; + } +}; +ok(!$@, "delete record\n" . $@ . "\n"); + + ### clean temporary files unlink('t/3.out'); unlink('t/4.out');