--- trunk/DBI.pm 2004/08/07 19:06:03 9 +++ trunk/DBI.pm 2004/09/05 16:59:41 18 @@ -9,8 +9,12 @@ use POSIX qw(ENOENT EISDIR EINVAL ENOSYS O_RDWR); use Fuse; use DBI; +use Carp; +use Proc::Simple; +use Data::Dumper; -our $VERSION = '0.01'; + +our $VERSION = '0.02'; =head1 NAME @@ -19,7 +23,7 @@ =head1 SYNOPSIS use Fuse::DBI; - Fuse::DBI->run( ... ); + Fuse::DBI->mount( ... ); See L below for examples how to set parametars. @@ -39,11 +43,11 @@ =cut -=head2 run +=head2 mount Mount your database as filesystem. - Fuse::DBI->run({ + my $mnt = Fuse::DBI->mount({ filenames => 'select name from filenamefilenames, read => 'sql read', update => 'sql update', @@ -58,22 +62,31 @@ my $sth; my $ctime_start; -sub run { - my $self = shift +sub read_filenames; + +sub mount { + my $class = shift; + my $self = {}; + bless($self, $class); + + my $arg = shift; + + print Dumper($arg); - my $arg = {@_}; + 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 "run needs 'dsn' to connect to (e.g. dsn => 'DBI:Pg:dbname=test')" unless ($arg->{'dsn'}); - carp "run needs 'mount' as mountpoint" unless ($arg->{'mount'}); + # save (some) arguments in self + $self->{$_} = $arg->{$_} foreach (qw(mount)); foreach (qw(filenames read update)) { - carp "run needs '$_' SQL" unless ($arg->{$_}); + carp "mount needs '$_' SQL" unless ($arg->{$_}); } $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(); @@ -84,24 +97,62 @@ read_filenames; - Fuse::main( - mountpoint=>$arg->{'mount'}, - getattr=>\&e_getattr, - getdir=>\&e_getdir, - open=>\&e_open, - statfs=>\&e_statfs, - read=>\&e_read, - write=>\&e_write, - utime=>\&e_utime, - truncate=>\&e_truncate, - debug=>0, - ); + $self->{'proc'} = Proc::Simple->new(); + $self->{'proc'}->kill_on_destroy(1); + + $self->{'proc'}->start( sub { + Fuse::main( + mountpoint=>$arg->{'mount'}, + getattr=>\&e_getattr, + getdir=>\&e_getdir, + open=>\&e_open, + statfs=>\&e_statfs, + read=>\&e_read, + write=>\&e_write, + utime=>\&e_utime, + truncate=>\&e_truncate, + debug=>0, + ); + } ); + + confess "Fuse::main failed" if (! $self->{'proc'}->poll); + + $self ? return $self : return undef; }; +=head2 umount + +Unmount your database as filesystem. + + $mnt->umount; + +This will also kill background process which is translating +database to filesystem. + +=cut + +sub umount { + my $self = shift; + + confess "no process running?" unless ($self->{'proc'}); + + system "fusermount -u ".$self->{'mount'} || croak "umount error: $!"; + + if ($self->{'proc'}->poll) { + $self->{'proc'}->kill; + return 1 if (! $self->{'proc'}->poll); + } else { + return 1; + } +} + + my %files; my %dirs; sub read_filenames { + my $self = shift; + # create empty filesystem (%files) = ( '.' => { @@ -184,12 +235,11 @@ # 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) { - $out{$f}++ if (/^\E$dirname\Q/ && $f =~ /^[^\/]+$/); + if ($f =~ s/^\E$dirname\Q\///) { + $out{$f}++ if ($f =~ /^[^\/]+$/); + } } else { $out{$f}++ if ($f =~ /^[^\/]+$/); } @@ -198,6 +248,7 @@ $out{'no files? bug?'}++; } print scalar keys %out," files in dir '$dirname'\n"; + print "## ",join(" ",keys %out),"\n"; return (keys %out),0; } @@ -273,23 +324,27 @@ sub e_write { my $file = filename_fixup(shift); - my ($buf_len,$off) = @_; + my ($buffer,$off) = @_; 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 { - return length($buf_len); + return length($buffer); } } @@ -297,7 +352,10 @@ my $file = filename_fixup(shift); my $size = shift; + print "truncate to $size\n"; + $files{$file}{cont} = substr($files{$file}{cont},0,$size); + $files{$file}{size} = $size; return 0 };