added lots of unittests and while I was at it, fixed a couple of bugs in the

backend modules.
This commit is contained in:
git@daemon.de
2013-07-08 15:36:04 +02:00
parent e4cf9ff015
commit f40e4c97e7
9 changed files with 173 additions and 74 deletions

View File

@@ -5,7 +5,7 @@
#
package NOTEDB::binary;
$NOTEDB::binary::VERSION = "1.10";
$NOTEDB::binary::VERSION = "1.11";
use strict;
use IO::Seekable;
@@ -28,7 +28,7 @@ sub new {
my $self = {};
bless($self,$class);
$self->{NOTEDB} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
$self->{NOTEDB} = $self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
my $MAX_NOTE = $param{max_note} || 4096;
my $MAX_TIME = $param{max_time} || 64;
@@ -384,17 +384,22 @@ sub _retrieve {
my $file = $this->{dbname};
if (-s $file) {
if ($this->changed() || $this->{unread}) {
my $fh = new FileHandle "<$this->{dbname}" or die "could not open $this->{dbname}\n";
flock $fh, LOCK_EX;
open NOTE, "+<$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
flock NOTE, LOCK_EX;
my($buffer, $t, $n, %res);
seek(NOTE, 0, 0); # START FROM BEGINNING
while(read(NOTE, $buffer, $this->{sizeof})) {
my ($num, $note, $date) = unpack($this->{typedef}, $buffer);
$t = $this->ude($date);
$n = $this->ude($note);
$res{$num}->{'note'} = $n;
$res{$num}->{'date'} = $t;
}
flock NOTE, LOCK_UN;
close NOTE;
my %data = ParseConfig(-ConfigFile => $fh) or die "could not read to database: $!\n";
flock $fh, LOCK_UN;
$fh->close();
$this->{unread} = 0;
$this->{data} = \%data;
return %data;
$this->cache(%res);
return %res;
}
else {
return %{$this->{data}};
@@ -405,6 +410,10 @@ sub _retrieve {
}
}
sub _store {
# compatibility dummy
return 1;
}
1; # keep this!

View File

@@ -6,7 +6,7 @@
package NOTEDB::dbm;
$NOTEDB::dbm::VERSION = "1.40";
$NOTEDB::dbm::VERSION = "1.41";
use DB_File;
use NOTEDB;
@@ -29,7 +29,7 @@ sub new
my $notefile = "note.dbm";
my $timefile = "date.dbm";
my $dbm_dir = $param{directory} || File::Spec->catfile($ENV{HOME}, ".note_dbm");
my $dbm_dir = $self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".note_dbm");
if (! -d $dbm_dir) {
# try to make it

View File

@@ -4,7 +4,7 @@
package NOTEDB::dumper;
$NOTEDB::dumper::VERSION = "1.01";
$NOTEDB::dumper::VERSION = "1.02";
use strict;
use Data::Dumper;
@@ -29,7 +29,7 @@ sub new {
my $self = {};
bless($self,$class);
$self->{NOTEDB} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
$self->{NOTEDB} = $self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
if(! -e $param{dbname}) {
open(TT,">$param{dbname}") or die "Could not create $param{dbname}: $!\n";
@@ -76,7 +76,7 @@ sub set_del_all {
sub get_single {
my($this, $num) = @_;
my($address, $note, $date, $buffer, $n, $t, $buffer, );
my($address, $note, $date, $n, $t, $buffer, );
my %data = $this->get_all();
return ($data{$num}->{note}, $data{$num}->{date});

View File

@@ -4,7 +4,7 @@
package NOTEDB::general;
$NOTEDB::general::VERSION = "1.03";
$NOTEDB::general::VERSION = "1.04";
use strict;
#use Data::Dumper;
@@ -44,6 +44,7 @@ sub new {
$self->{mtime} = $self->get_stat();
$self->{unread} = 1;
$self->{changed} = 1;
$self->{data} = {};
$self->{LOCKFILE} = $param{dbname} . "~LOCK";
@@ -88,7 +89,7 @@ sub set_del_all {
sub get_single {
my($this, $num) = @_;
my($address, $note, $date, $buffer, $n, $t, $buffer, );
my($address, $note, $date, $n, $t, $buffer, );
my %data = $this->get_all();
@@ -143,7 +144,6 @@ sub get_nextnum {
my @numbers = sort { $a <=> $b } keys %data;
$num = pop @numbers;
$num++;
return $num;
return $num;
}
@@ -267,6 +267,7 @@ sub uen {
$crypted = $raw;
}
my $coded = encode_base64($crypted);
chomp $coded;
return $coded;
}
@@ -310,7 +311,7 @@ sub _retrieve {
my ($this) = @_;
my $file = $this->{dbname};
if (-s $file) {
if ($this->changed() || $this->{unread}) {
if ($this->{changed} || $this->{unread}) {
my $fh = new FileHandle "<$this->{dbname}" or die "could not open $this->{dbname}\n";
flock $fh, LOCK_EX;

View File

@@ -3,8 +3,8 @@
package NOTEDB::pwsafe3;
$NOTEDB::pwsafe3::VERSION = "1.03";
$NOTEDB::pwsafe3::VERSION = "1.04";
use lib qw(/home/scip/D/github/Crypt--PWSafe3/blib/lib);
use strict;
use Data::Dumper;
use Time::Local;
@@ -51,14 +51,19 @@ sub version {
sub get_stat {
my ($this) = @_;
my $mtime = (stat($this->{dbname}))[9];
return $mtime;
if(-e $this->{dbname}) {
return (stat($this->{dbname}))[9];
}
else {
return time;
}
}
sub filechanged {
my ($this) = @_;
my $current = $this->get_stat();
if ($current > $this->{mtime}) {
if ($current >= $this->{mtime}) {
$this->{mtime} = $current;
return $current;
}
@@ -77,7 +82,7 @@ sub set_del_all {
sub get_single {
my($this, $num) = @_;
my($address, $note, $date, $buffer, $n, $t, $buffer, );
my($address, $note, $date, $n, $t, $buffer, );
my %data = $this->get_all();
@@ -329,6 +334,7 @@ sub _retrieve {
notes => $record->notes,
group => $record->group,
lastmod=> $record->lastmod,
ctime => $record->ctime,
);
$data{$num}->{note} = \%entry;
}
@@ -355,7 +361,7 @@ sub _pwsafe3tonote {
#
# convert pwsafe3 record to note record
my ($this, $record) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($record->{lastmod});
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($record->{ctime});
my $date = sprintf("%02d.%02d.%04d %02d:%02d:%02d", $mday, $mon+1, $year+1900, $hour, $min, $sec);
chomp $date;
my $note;
@@ -406,6 +412,9 @@ sub _notetopwsafe3 {
($title, $content) = split /\n/, $text, 2;
}
if(!defined $content) { $content = ""; }
if(!defined $group) { $group = ""; }
$user = $passwd = '';
if ($content =~ /(user|username|login|account|benutzer):\s*(.+)/i) {
$user = $2;
@@ -414,9 +423,10 @@ sub _notetopwsafe3 {
$passwd = $2;
}
# 1 2 3 5 6 7
if ($date =~ /^(\d\d)\.(\d\d)\.(\{4}) (\d\d):(\d\d):(\d\d)$/) {
$ts = timelocal($7, $6, $5, $1, $2-1, $3-1900);
# 1 2 3 4 5 6
if ($date =~ /^(\d\d)\.(\d\d)\.(\d{4}) (\d\d):(\d\d):(\d\d)$/) {
# timelocal($sec,$min,$hour,$mday,$mon,$year);
$ts = timelocal($6, $5, $4, $1, $2-1, $3-1900);
}
# make our topics pwsafe3 compatible groups
@@ -426,17 +436,16 @@ sub _notetopwsafe3 {
# pwsafe3 uses windows newlines, so convert ours
$content =~ s/\n/\r\n/gs;
my %record = (
uuid => $this->_getuuid($num),
user => $user,
passwd => $passwd,
group => $group,
title => $title,
ctime => $ts,
lastmod=> $ts,
notes => $content,
);
return %record;
}

View File

@@ -4,7 +4,7 @@
package NOTEDB::text;
$NOTEDB::text::VERSION = "1.03";
$NOTEDB::text::VERSION = "1.04";
use strict;
#use Data::Dumper;
@@ -30,7 +30,7 @@ sub new {
my $self = {};
bless($self,$class);
$self->{NOTEDB} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
$self->{NOTEDB} = $self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
if(! -e $param{dbname}) {
open(TT,">$param{dbname}") or die "Could not create $param{dbname}: $!\n";
@@ -77,7 +77,7 @@ sub set_del_all {
sub get_single {
my($this, $num) = @_;
my($address, $note, $date, $buffer, $n, $t, $buffer, );
my($address, $note, $date, $n, $t, $buffer, );
my %data = $this->get_all();