/[fuse_dbi]/fuse-couchdb/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 /fuse-couchdb/DBI.pm

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

revision 18 by dpavlin, Sun Sep 5 16:59:41 2004 UTC revision 26 by dpavlin, Fri Oct 8 22:55:36 2004 UTC
# Line 10  use POSIX qw(ENOENT EISDIR EINVAL ENOSYS Line 10  use POSIX qw(ENOENT EISDIR EINVAL ENOSYS
10  use Fuse;  use Fuse;
11  use DBI;  use DBI;
12  use Carp;  use Carp;
 use Proc::Simple;  
13  use Data::Dumper;  use Data::Dumper;
14    
15    
16  our $VERSION = '0.02';  our $VERSION = '0.03';
17    
18  =head1 NAME  =head1 NAME
19    
# Line 25  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 48  It's actually opposite of Oracle's inten Line 47  It's actually opposite of Oracle's inten
47  Mount your database as filesystem.  Mount your database as filesystem.
48    
49    my $mnt = Fuse::DBI->mount({    my $mnt = Fuse::DBI->mount({
50          filenames => 'select name from filenamefilenames,          filenames => 'select name from files_table as filenames',
51          read => 'sql read',          read => 'sql read',
52          update => 'sql update',          update => 'sql update',
53          dsn => 'DBI:Pg:dbname=webgui',          dsn => 'DBI:Pg:dbname=webgui',
# Line 63  my $sth; Line 62  my $sth;
62  my $ctime_start;  my $ctime_start;
63    
64  sub read_filenames;  sub read_filenames;
65    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;
# 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->{$_});
91          }          }
92    
93          $dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, { AutoCommit => 0 }) || die $DBI::errstr;          $ctime_start = time();
94    
95          print "start transaction\n";          my $pid;
96          $dbh->begin_work || die $dbh->errstr;          if ($arg->{'fork'}) {
97                    $pid = fork();
98                    die "fork() failed: $!" unless defined $pid;
99                    # child will return to caller
100                    if ($pid) {
101                            return $self;
102                    }
103            }
104    
105            $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    
         $ctime_start = time();  
112    
113          read_filenames;          $self->{'sth'} = $sth;
114    
115          $self->{'proc'} = Proc::Simple->new();          $self->{'read_filenames'} = sub { $self->read_filenames };
116          $self->{'proc'}->kill_on_destroy(1);          $self->read_filenames;
117    
118          $self->{'proc'}->start( sub {          $self->{'mounted'} = 1;
                 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,  
                 );  
         } );  
119    
120          confess "Fuse::main failed" if (! $self->{'proc'}->poll);          $fuse_self = \$self;
121    
122            Fuse::main(
123                    mountpoint=>$arg->{'mount'},
124                    getattr=>\&e_getattr,
125                    getdir=>\&e_getdir,
126                    open=>\&e_open,
127                    statfs=>\&e_statfs,
128                    read=>\&e_read,
129                    write=>\&e_write,
130                    utime=>\&e_utime,
131                    truncate=>\&e_truncate,
132                    unlink=>\&e_unlink,
133                    rmdir=>\&e_unlink,
134                    debug=>0,
135            );
136            
137            $self->{'mounted'} = 0;
138    
139            exit(0) if ($arg->{'fork'});
140    
141            return 1;
142    
         $self ? return $self : return undef;  
143  };  };
144    
145  =head2 umount  =head2 umount
# Line 134  database to filesystem. Line 156  database to filesystem.
156  sub umount {  sub umount {
157          my $self = shift;          my $self = shift;
158    
159          confess "no process running?" unless ($self->{'proc'});          if ($self->{'mounted'}) {
160                    system "fusermount -u ".$self->{'mount'} || croak "umount error: $!";
161            }
162    
163          system "fusermount -u ".$self->{'mount'} || croak "umount error: $!";          return 1;
164    }
165    
166          if ($self->{'proc'}->poll) {  $SIG{'INT'} = sub {
167                  $self->{'proc'}->kill;          print STDERR "umount called by SIG INT\n";
168                  return 1 if (! $self->{'proc'}->poll);          umount;
169          } else {  };
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
179    
180    Checks if C<fuse> module is loaded in kernel.
181    
182      die "no fuse module loaded in kernel"
183            unless (Fuse::DBI::fuse_module_loaded);
184    
185    This function in called by L<mount>, but might be useful alone also.
186    
187    =cut
188    
189    sub fuse_module_loaded {
190            my $lsmod = `lsmod`;
191            die "can't start lsmod: $!" unless ($lsmod);
192            if ($lsmod =~ m/fuse/s) {
193                  return 1;                  return 1;
194            } else {
195                    return 0;
196          }          }
197  }  }
198    
   
199  my %files;  my %files;
200  my %dirs;  my %dirs;
201    
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 252  sub e_getdir { Line 303  sub e_getdir {
303          return (keys %out),0;          return (keys %out),0;
304  }  }
305    
306    sub read_content {
307            my ($file,$id) = @_;
308    
309            die "read_content needs file and id" unless ($file && $id);
310    
311            $sth->{'read'}->execute($id) || die $sth->{'read'}->errstr;
312            $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";
315    }
316    
317    
318  sub e_open {  sub e_open {
319          # VFS sanity check; it keeps all the necessary state, not much to do here.          # VFS sanity check; it keeps all the necessary state, not much to do here.
320          my $file = filename_fixup(shift);          my $file = filename_fixup(shift);
# Line 260  sub e_open { Line 323  sub e_open {
323          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files{$file});
324          return -EISDIR() unless exists($files{$file}{id});          return -EISDIR() unless exists($files{$file}{id});
325    
326          if (!exists($files{$file}{cont})) {          read_content($file,$files{$file}{id}) unless exists($files{$file}{cont});
327                  $sth->{'read'}->execute($files{$file}{id}) || die $sth->{'read'}->errstr;  
                 $files{$file}{cont} = $sth->{'read'}->fetchrow_array;  
                 print "file '$file' content read in cache\n";  
         }  
328          print "open '$file' ",length($files{$file}{cont})," bytes\n";          print "open '$file' ",length($files{$file}{cont})," bytes\n";
329          return 0;          return 0;
330  }  }
# Line 285  sub e_read { Line 345  sub e_read {
345          return -EINVAL() if ($off > $len);          return -EINVAL() if ($off > $len);
346          return 0 if ($off == $len);          return 0 if ($off == $len);
347    
348          $buf_len = $buf_len-$off if ($off+$buf_len > $len);          $buf_len = $len-$off if ($len - $off < $buf_len);
349    
350          return substr($files{$file}{cont},$off,$buf_len);          return substr($files{$file}{cont},$off,$buf_len);
351  }  }
# Line 298  sub clear_cont { Line 358  sub clear_cont {
358                  delete $files{$f}{cont};                  delete $files{$f}{cont};
359          }          }
360          print "begin new transaction\n";          print "begin new transaction\n";
361          $dbh->begin_work || die $dbh->errstr;          #$dbh->begin_work || die $dbh->errstr;
362  }  }
363    
364    
# Line 307  sub update_db { Line 367  sub update_db {
367    
368          $files{$file}{ctime} = time();          $files{$file}{ctime} = time();
369    
370          if (!$sth->{'update'}->execute($files{$file}{cont},$files{$file}{id})) {          my ($cont,$id) = (
371                    $files{$file}{cont},
372                    $files{$file}{id}
373            );
374    
375            if (!$sth->{'update'}->execute($cont,$id)) {
376                  print "update problem: ",$sth->{'update'}->errstr;                  print "update problem: ",$sth->{'update'}->errstr;
377                  clear_cont;                  clear_cont;
378                  return 0;                  return 0;
# Line 318  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 337  sub e_write { Line 404  sub e_write {
404    
405          $files{$file}{cont} .= substr($cont,0,$off) if ($off > 0);          $files{$file}{cont} .= substr($cont,0,$off) if ($off > 0);
406          $files{$file}{cont} .= $buffer;          $files{$file}{cont} .= $buffer;
407          $files{$file}{cont} .= substr($cont,-($off+length($buffer))) if ($off+length($buffer) > $len);          $files{$file}{cont} .= substr($cont,$off+length($buffer),$len-$off-length($buffer)) if ($off+length($buffer) < $len);
408    
409          $files{$file}{size} = length($files{$file}{cont});          $files{$file}{size} = length($files{$file}{cont});
410    
# Line 374  sub e_utime { Line 441  sub e_utime {
441    
442  sub e_statfs { return 255, 1, 1, 1, 1, 2 }  sub e_statfs { return 255, 1, 1, 1, 1, 2 }
443    
444    sub e_unlink {
445            my $file = filename_fixup(shift);
446    
447            if (exists( $dirs{$file} )) {
448                    print "unlink '$file' will re-read template names\n";
449                    print Dumper($fuse_self);
450                    $$fuse_self->{'read_filenames'}->();
451                    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 -ENOENT();
459    }
460  1;  1;
461  __END__  __END__
462    

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

  ViewVC Help
Powered by ViewVC 1.1.26