--- trunk/DBI.pm 2004/10/02 15:29:02 21 +++ trunk/DBI.pm 2004/10/08 22:55:36 26 @@ -24,11 +24,11 @@ use Fuse::DBI; Fuse::DBI->mount( ... ); -See L below for examples how to set parametars. +See C below for examples how to set parametars. =head1 DESCRIPTION -This module will use L module, part of C +This module will use C module, part of C available at L to mount your database as file system. @@ -64,6 +64,11 @@ sub read_filenames; sub fuse_module_loaded; +# evil, evil way to solve this. It makes this module non-reentrant. But, since +# fuse calls another copy of this script for each mount anyway, this shouldn't +# be a problem. +my $fuse_self; + sub mount { my $class = shift; my $self = {}; @@ -77,7 +82,9 @@ carp "mount needs 'mount' as mountpoint" unless ($arg->{'mount'}); # save (some) arguments in self - $self->{$_} = $arg->{$_} foreach (qw(mount)); + foreach (qw(mount invalidate)) { + $self->{$_} = $arg->{$_}; + } foreach (qw(filenames read update)) { carp "mount needs '$_' SQL" unless ($arg->{$_}); @@ -85,25 +92,34 @@ $ctime_start = time(); + my $pid; if ($arg->{'fork'}) { - my $pid = fork(); + $pid = fork(); die "fork() failed: $!" unless defined $pid; # child will return to caller if ($pid) { - $self ? return $self : return undef; + return $self; } } $dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, {AutoCommit => 0, RaiseError => 1}) || die $DBI::errstr; - $sth->{filenames} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr(); + $sth->{'filenames'} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr(); $sth->{'read'} = $dbh->prepare($arg->{'read'}) || die $dbh->errstr(); $sth->{'update'} = $dbh->prepare($arg->{'update'}) || die $dbh->errstr(); + + $self->{'sth'} = $sth; + + $self->{'read_filenames'} = sub { $self->read_filenames }; $self->read_filenames; - my $mount = Fuse::main( + $self->{'mounted'} = 1; + + $fuse_self = \$self; + + Fuse::main( mountpoint=>$arg->{'mount'}, getattr=>\&e_getattr, getdir=>\&e_getdir, @@ -114,13 +130,16 @@ utime=>\&e_utime, truncate=>\&e_truncate, unlink=>\&e_unlink, + rmdir=>\&e_unlink, debug=>0, ); + + $self->{'mounted'} = 0; + + exit(0) if ($arg->{'fork'}); + + return 1; - if (! $mount) { - warn "mount on ",$arg->{'mount'}," failed!\n"; - return undef; - } }; =head2 umount @@ -137,11 +156,25 @@ sub umount { my $self = shift; - system "fusermount -u ".$self->{'mount'} || croak "umount error: $!"; + if ($self->{'mounted'}) { + system "fusermount -u ".$self->{'mount'} || croak "umount error: $!"; + } return 1; } +$SIG{'INT'} = sub { + print STDERR "umount called by SIG INT\n"; + umount; +}; + +sub DESTROY { + my $self = shift; + return if (! $self->{'mounted'}); + print STDERR "umount called by DESTROY\n"; + $self->umount; +} + =head2 fuse_module_loaded Checks if C module is loaded in kernel. @@ -169,6 +202,8 @@ sub read_filenames { my $self = shift; + my $sth = $self->{'sth'} || die "no sth argument"; + # create empty filesystem (%files) = ( '.' => { @@ -275,6 +310,7 @@ $sth->{'read'}->execute($id) || die $sth->{'read'}->errstr; $files{$file}{cont} = $sth->{'read'}->fetchrow_array; + $files{$file}{ctime} = time(); print "file '$file' content [",length($files{$file}{cont})," bytes] read in cache\n"; } @@ -347,6 +383,8 @@ return 0; } print "updated '$file' [",$files{$file}{id},"]\n"; + + $$fuse_self->{'invalidate'}->() if (ref $$fuse_self->{'invalidate'}); } return 1; } @@ -406,13 +444,18 @@ sub e_unlink { my $file = filename_fixup(shift); - return -ENOENT() unless exists($files{$file}); - - print "unlink '$file' will invalidate cache\n"; - - read_content($file,$files{$file}{id}); + if (exists( $dirs{$file} )) { + print "unlink '$file' will re-read template names\n"; + print Dumper($fuse_self); + $$fuse_self->{'read_filenames'}->(); + return 0; + } elsif (exists( $files{$file} )) { + print "unlink '$file' will invalidate cache\n"; + read_content($file,$files{$file}{id}); + return 0; + } - return 0; + return -ENOENT(); } 1; __END__