--- trunk/DBI.pm 2004/10/02 16:54:42 23 +++ trunk/DBI.pm 2004/10/08 22:55:36 26 @@ -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->{$_}); @@ -97,13 +104,21 @@ $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; + $self->{'mounted'} = 1; + + $fuse_self = \$self; + Fuse::main( mountpoint=>$arg->{'mount'}, getattr=>\&e_getattr, @@ -115,8 +130,11 @@ utime=>\&e_utime, truncate=>\&e_truncate, unlink=>\&e_unlink, + rmdir=>\&e_unlink, debug=>0, ); + + $self->{'mounted'} = 0; exit(0) if ($arg->{'fork'}); @@ -138,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. @@ -170,6 +202,8 @@ sub read_filenames { my $self = shift; + my $sth = $self->{'sth'} || die "no sth argument"; + # create empty filesystem (%files) = ( '.' => { @@ -276,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"; } @@ -348,6 +383,8 @@ return 0; } print "updated '$file' [",$files{$file}{id},"]\n"; + + $$fuse_self->{'invalidate'}->() if (ref $$fuse_self->{'invalidate'}); } return 1; } @@ -407,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__