added PostgreSQL test
[Fuse-DBI] / DBI.pm
diff --git a/DBI.pm b/DBI.pm
index 58fbdce..96b3ea3 100755 (executable)
--- a/DBI.pm
+++ b/DBI.pm
@@ -14,7 +14,7 @@ use Proc::Simple;
 use Data::Dumper;
 
 
 use Data::Dumper;
 
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 =head1 NAME
 
 
 =head1 NAME
 
@@ -76,6 +76,9 @@ sub mount {
        carp "mount needs 'dsn' to connect to (e.g. dsn => 'DBI:Pg:dbname=test')" unless ($arg->{'dsn'});
        carp "mount needs 'mount' as mountpoint" unless ($arg->{'mount'});
 
        carp "mount needs 'dsn' to connect to (e.g. dsn => 'DBI:Pg:dbname=test')" unless ($arg->{'dsn'});
        carp "mount needs 'mount' as mountpoint" unless ($arg->{'mount'});
 
+       # save (some) arguments in self
+       $self->{$_} = $arg->{$_} foreach (qw(mount));
+
        foreach (qw(filenames read update)) {
                carp "mount needs '$_' SQL" unless ($arg->{$_});
        }
        foreach (qw(filenames read update)) {
                carp "mount needs '$_' SQL" unless ($arg->{$_});
        }
@@ -83,7 +86,7 @@ sub mount {
        $dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, { AutoCommit => 0 }) || die $DBI::errstr;
 
        print "start transaction\n";
        $dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, { AutoCommit => 0 }) || die $DBI::errstr;
 
        print "start transaction\n";
-       #$dbh->begin_work || die $dbh->errstr;
+       $dbh->begin_work || die $dbh->errstr;
 
        $sth->{filenames} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr();
 
 
        $sth->{filenames} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr();
 
@@ -112,6 +115,8 @@ sub mount {
                );
        } );
 
                );
        } );
 
+       confess "Fuse::main failed" if (! $self->{'proc'}->poll);
+
        $self ? return $self : return undef;
 };
 
        $self ? return $self : return undef;
 };
 
@@ -130,7 +135,15 @@ sub umount {
        my $self = shift;
 
        confess "no process running?" unless ($self->{'proc'});
        my $self = shift;
 
        confess "no process running?" unless ($self->{'proc'});
-       $self->{'proc'}->kill;
+
+       system "fusermount -u ".$self->{'mount'} || croak "umount error: $!";
+
+       if ($self->{'proc'}->poll) {
+               $self->{'proc'}->kill;
+               return 1 if (! $self->{'proc'}->poll);
+       } else {
+               return 1;
+       }
 }
 
 
 }
 
 
@@ -222,12 +235,11 @@ sub e_getdir {
        # return as many text filenames as you like, followed by the retval.
        print((scalar keys %files)." files total\n");
        my %out;
        # return as many text filenames as you like, followed by the retval.
        print((scalar keys %files)." files total\n");
        my %out;
-       foreach (keys %files) {
-               my $f = $_;
-               $f =~ s/^\E$dirname\Q//;
-               $f =~ s/^\///;
+       foreach my $f (sort keys %files) {
                if ($dirname) {
                if ($dirname) {
-                       $out{$f}++ if (/^\E$dirname\Q/ && $f =~ /^[^\/]+$/);
+                       if ($f =~ s/^\E$dirname\Q\///) {
+                               $out{$f}++ if ($f =~ /^[^\/]+$/);
+                       }
                } else {
                        $out{$f}++ if ($f =~ /^[^\/]+$/);
                }
                } else {
                        $out{$f}++ if ($f =~ /^[^\/]+$/);
                }
@@ -236,6 +248,7 @@ sub e_getdir {
                $out{'no files? bug?'}++;
        }
        print scalar keys %out," files in dir '$dirname'\n";
                $out{'no files? bug?'}++;
        }
        print scalar keys %out," files in dir '$dirname'\n";
+       print "## ",join(" ",keys %out),"\n";
        return (keys %out),0;
 }
 
        return (keys %out),0;
 }
 
@@ -311,23 +324,27 @@ sub update_db {
 
 sub e_write {
        my $file = filename_fixup(shift);
 
 sub e_write {
        my $file = filename_fixup(shift);
-       my ($buf_len,$off) = @_;
+       my ($buffer,$off) = @_;
 
        return -ENOENT() unless exists($files{$file});
 
 
        return -ENOENT() unless exists($files{$file});
 
-       my $len = length($files{$file}{cont});
+       my $cont = $files{$file}{cont};
+       my $len = length($cont);
 
 
-       print "write '$file' [$len bytes] offset $off length\n";
+       print "write '$file' [$len bytes] offset $off length ",length($buffer),"\n";
 
 
-       $files{$file}{cont} =
-               substr($files{$file}{cont},0,$off) .
-               $buf_len .
-               substr($files{$file}{cont},$off+length($buf_len));
+       $files{$file}{cont} = "";
+
+       $files{$file}{cont} .= substr($cont,0,$off) if ($off > 0);
+       $files{$file}{cont} .= $buffer;
+       $files{$file}{cont} .= substr($cont,-($off+length($buffer))) if ($off+length($buffer) > $len);
+
+       $files{$file}{size} = length($files{$file}{cont});
 
        if (! update_db($file)) {
                return -ENOSYS();
        } else {
 
        if (! update_db($file)) {
                return -ENOSYS();
        } else {
-               return length($buf_len);
+               return length($buffer);
        }
 }
 
        }
 }
 
@@ -335,7 +352,10 @@ sub e_truncate {
        my $file = filename_fixup(shift);
        my $size = shift;
 
        my $file = filename_fixup(shift);
        my $size = shift;
 
+       print "truncate to $size\n";
+
        $files{$file}{cont} = substr($files{$file}{cont},0,$size);
        $files{$file}{cont} = substr($files{$file}{cont},0,$size);
+       $files{$file}{size} = $size;
        return 0
 };
 
        return 0
 };