/[fuse_dbi]/trunk/DBI.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/DBI.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 21 by dpavlin, Sat Oct 2 15:29:02 2004 UTC revision 26 by dpavlin, Fri Oct 8 22:55:36 2004 UTC
# Line 24  Fuse::DBI - mount your database as files Line 24  Fuse::DBI - mount your database as files
24    use Fuse::DBI;    use Fuse::DBI;
25    Fuse::DBI->mount( ... );    Fuse::DBI->mount( ... );
26    
27  See L<run> below for examples how to set parametars.  See C<run> below for examples how to set parametars.
28    
29  =head1 DESCRIPTION  =head1 DESCRIPTION
30    
31  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)>
32  available at L<http://sourceforge.net/projects/avf> to mount  available at L<http://sourceforge.net/projects/avf> to mount
33  your database as file system.  your database as file system.
34    
# Line 64  my $ctime_start; Line 64  my $ctime_start;
64  sub read_filenames;  sub read_filenames;
65  sub fuse_module_loaded;  sub fuse_module_loaded;
66    
67    # evil, evil way to solve this. It makes this module non-reentrant. But, since
68    # fuse calls another copy of this script for each mount anyway, this shouldn't
69    # be a problem.
70    my $fuse_self;
71    
72  sub mount {  sub mount {
73          my $class = shift;          my $class = shift;
74          my $self = {};          my $self = {};
# Line 77  sub mount { Line 82  sub mount {
82          carp "mount needs 'mount' as mountpoint" unless ($arg->{'mount'});          carp "mount needs 'mount' as mountpoint" unless ($arg->{'mount'});
83    
84          # save (some) arguments in self          # save (some) arguments in self
85          $self->{$_} = $arg->{$_} foreach (qw(mount));          foreach (qw(mount invalidate)) {
86                    $self->{$_} = $arg->{$_};
87            }
88    
89          foreach (qw(filenames read update)) {          foreach (qw(filenames read update)) {
90                  carp "mount needs '$_' SQL" unless ($arg->{$_});                  carp "mount needs '$_' SQL" unless ($arg->{$_});
# Line 85  sub mount { Line 92  sub mount {
92    
93          $ctime_start = time();          $ctime_start = time();
94    
95            my $pid;
96          if ($arg->{'fork'}) {          if ($arg->{'fork'}) {
97                  my $pid = fork();                  $pid = fork();
98                  die "fork() failed: $!" unless defined $pid;                  die "fork() failed: $!" unless defined $pid;
99                  # child will return to caller                  # child will return to caller
100                  if ($pid) {                  if ($pid) {
101                          $self ? return $self : return undef;                          return $self;
102                  }                  }
103          }          }
104    
105          $dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, {AutoCommit => 0, RaiseError => 1}) || die $DBI::errstr;          $dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, {AutoCommit => 0, RaiseError => 1}) || die $DBI::errstr;
106    
107          $sth->{filenames} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr();          $sth->{'filenames'} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr();
108    
109          $sth->{'read'} = $dbh->prepare($arg->{'read'}) || die $dbh->errstr();          $sth->{'read'} = $dbh->prepare($arg->{'read'}) || die $dbh->errstr();
110          $sth->{'update'} = $dbh->prepare($arg->{'update'}) || die $dbh->errstr();          $sth->{'update'} = $dbh->prepare($arg->{'update'}) || die $dbh->errstr();
111    
112    
113            $self->{'sth'} = $sth;
114    
115            $self->{'read_filenames'} = sub { $self->read_filenames };
116          $self->read_filenames;          $self->read_filenames;
117    
118          my $mount = Fuse::main(          $self->{'mounted'} = 1;
119    
120            $fuse_self = \$self;
121    
122            Fuse::main(
123                  mountpoint=>$arg->{'mount'},                  mountpoint=>$arg->{'mount'},
124                  getattr=>\&e_getattr,                  getattr=>\&e_getattr,
125                  getdir=>\&e_getdir,                  getdir=>\&e_getdir,
# Line 114  sub mount { Line 130  sub mount {
130                  utime=>\&e_utime,                  utime=>\&e_utime,
131                  truncate=>\&e_truncate,                  truncate=>\&e_truncate,
132                  unlink=>\&e_unlink,                  unlink=>\&e_unlink,
133                    rmdir=>\&e_unlink,
134                  debug=>0,                  debug=>0,
135          );          );
136            
137            $self->{'mounted'} = 0;
138    
139            exit(0) if ($arg->{'fork'});
140    
141            return 1;
142    
         if (! $mount) {  
                 warn "mount on ",$arg->{'mount'}," failed!\n";  
                 return undef;  
         }  
143  };  };
144    
145  =head2 umount  =head2 umount
# Line 137  database to filesystem. Line 156  database to filesystem.
156  sub umount {  sub umount {
157          my $self = shift;          my $self = shift;
158    
159          system "fusermount -u ".$self->{'mount'} || croak "umount error: $!";          if ($self->{'mounted'}) {
160                    system "fusermount -u ".$self->{'mount'} || croak "umount error: $!";
161            }
162    
163          return 1;          return 1;
164  }  }
165    
166    $SIG{'INT'} = sub {
167            print STDERR "umount called by SIG INT\n";
168            umount;
169    };
170    
171    sub DESTROY {
172            my $self = shift;
173            return if (! $self->{'mounted'});
174            print STDERR "umount called by DESTROY\n";
175            $self->umount;
176    }
177    
178  =head2 fuse_module_loaded  =head2 fuse_module_loaded
179    
180  Checks if C<fuse> module is loaded in kernel.  Checks if C<fuse> module is loaded in kernel.
# Line 169  my %dirs; Line 202  my %dirs;
202  sub read_filenames {  sub read_filenames {
203          my $self = shift;          my $self = shift;
204    
205            my $sth = $self->{'sth'} || die "no sth argument";
206    
207          # create empty filesystem          # create empty filesystem
208          (%files) = (          (%files) = (
209                  '.' => {                  '.' => {
# Line 275  sub read_content { Line 310  sub read_content {
310    
311          $sth->{'read'}->execute($id) || die $sth->{'read'}->errstr;          $sth->{'read'}->execute($id) || die $sth->{'read'}->errstr;
312          $files{$file}{cont} = $sth->{'read'}->fetchrow_array;          $files{$file}{cont} = $sth->{'read'}->fetchrow_array;
313            $files{$file}{ctime} = time();
314          print "file '$file' content [",length($files{$file}{cont})," bytes] read in cache\n";          print "file '$file' content [",length($files{$file}{cont})," bytes] read in cache\n";
315  }  }
316    
# Line 347  sub update_db { Line 383  sub update_db {
383                          return 0;                          return 0;
384                  }                  }
385                  print "updated '$file' [",$files{$file}{id},"]\n";                  print "updated '$file' [",$files{$file}{id},"]\n";
386    
387                    $$fuse_self->{'invalidate'}->() if (ref $$fuse_self->{'invalidate'});
388          }          }
389          return 1;          return 1;
390  }  }
# Line 406  sub e_statfs { return 255, 1, 1, 1, 1, 2 Line 444  sub e_statfs { return 255, 1, 1, 1, 1, 2
444  sub e_unlink {  sub e_unlink {
445          my $file = filename_fixup(shift);          my $file = filename_fixup(shift);
446    
447          return -ENOENT() unless exists($files{$file});          if (exists( $dirs{$file} )) {
448                    print "unlink '$file' will re-read template names\n";
449          print "unlink '$file' will invalidate cache\n";                  print Dumper($fuse_self);
450                    $$fuse_self->{'read_filenames'}->();
451          read_content($file,$files{$file}{id});                  return 0;
452            } elsif (exists( $files{$file} )) {
453                    print "unlink '$file' will invalidate cache\n";
454                    read_content($file,$files{$file}{id});
455                    return 0;
456            }
457    
458          return 0;          return -ENOENT();
459  }  }
460  1;  1;
461  __END__  __END__

Legend:
Removed from v.21  
changed lines
  Added in v.26

  ViewVC Help
Powered by ViewVC 1.1.26