call umount on DESTROY, support for optional 'invalidate' code ref which
[Fuse-DBI] / DBI.pm
diff --git a/DBI.pm b/DBI.pm
index 8caecd3..bb42967 100755 (executable)
--- a/DBI.pm
+++ b/DBI.pm
@@ -24,11 +24,11 @@ Fuse::DBI - mount your database as filesystem and use it
   use Fuse::DBI;
   Fuse::DBI->mount( ... );
 
   use Fuse::DBI;
   Fuse::DBI->mount( ... );
 
-See L<run> below for examples how to set parametars.
+See C<run> below for examples how to set parametars.
 
 =head1 DESCRIPTION
 
 
 =head1 DESCRIPTION
 
-This module will use L<Fuse> module, part of C<FUSE (Filesystem in USErspace)>
+This module will use C<Fuse> module, part of C<FUSE (Filesystem in USErspace)>
 available at L<http://sourceforge.net/projects/avf> to mount
 your database as file system.
 
 available at L<http://sourceforge.net/projects/avf> to mount
 your database as file system.
 
@@ -64,6 +64,11 @@ my $ctime_start;
 sub read_filenames;
 sub fuse_module_loaded;
 
 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 = {};
 sub mount {
        my $class = shift;
        my $self = {};
@@ -77,7 +82,10 @@ sub mount {
        carp "mount needs 'mount' as mountpoint" unless ($arg->{'mount'});
 
        # save (some) arguments in self
        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->{$_};
+               $fuse_self->{$_} = $arg->{$_};
+       }
 
        foreach (qw(filenames read update)) {
                carp "mount needs '$_' SQL" unless ($arg->{$_});
 
        foreach (qw(filenames read update)) {
                carp "mount needs '$_' SQL" unless ($arg->{$_});
@@ -85,12 +93,13 @@ sub mount {
 
        $ctime_start = time();
 
 
        $ctime_start = time();
 
+       my $pid;
        if ($arg->{'fork'}) {
        if ($arg->{'fork'}) {
-               my $pid = fork();
+               $pid = fork();
                die "fork() failed: $!" unless defined $pid;
                # child will return to caller
                if ($pid) {
                die "fork() failed: $!" unless defined $pid;
                # child will return to caller
                if ($pid) {
-                       $self ? return $self : return undef;
+                       return $self;
                }
        }
 
                }
        }
 
@@ -103,7 +112,7 @@ sub mount {
 
        $self->read_filenames;
 
 
        $self->read_filenames;
 
-       my $mount = Fuse::main(
+       Fuse::main(
                mountpoint=>$arg->{'mount'},
                getattr=>\&e_getattr,
                getdir=>\&e_getdir,
                mountpoint=>$arg->{'mount'},
                getattr=>\&e_getattr,
                getdir=>\&e_getdir,
@@ -117,10 +126,10 @@ sub mount {
                debug=>0,
        );
 
                debug=>0,
        );
 
-       if (! $mount) {
-               warn "mount on ",$arg->{'mount'}," failed!\n";
-               return undef;
-       }
+       exit(0) if ($arg->{'fork'});
+
+       return 1;
+
 };
 
 =head2 umount
 };
 
 =head2 umount
@@ -142,6 +151,17 @@ sub umount {
        return 1;
 }
 
        return 1;
 }
 
+#$SIG{'INT'} = sub {
+#      print STDERR "umount called by SIG INT\n";
+#      umount;
+#};
+
+sub DESTROY {
+       my $self = shift;
+       print STDERR "umount called by DESTROY\n";
+       $self->umount;
+}
+
 =head2 fuse_module_loaded
 
 Checks if C<fuse> module is loaded in kernel.
 =head2 fuse_module_loaded
 
 Checks if C<fuse> module is loaded in kernel.
@@ -347,6 +367,8 @@ sub update_db {
                        return 0;
                }
                print "updated '$file' [",$files{$file}{id},"]\n";
                        return 0;
                }
                print "updated '$file' [",$files{$file}{id},"]\n";
+
+               $fuse_self->{'invalidate'}->() if (ref $fuse_self->{'invalidate'});
        }
        return 1;
 }
        }
        return 1;
 }