/[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 55 by dpavlin, Tue Apr 26 20:12:55 2005 UTC revision 61 by dpavlin, Tue Jan 3 14:56:35 2006 UTC
# Line 12  use DBI; Line 12  use DBI;
12  use Carp;  use Carp;
13  use Data::Dumper;  use Data::Dumper;
14    
15  our $VERSION = '0.08';  our $VERSION = '0.09_1';
16    
17  # block size for this filesystem  # block size for this filesystem
18  use constant BLOCK => 1024;  use constant BLOCK => 1024;
# Line 300  sub fuse_module_loaded { Line 300  sub fuse_module_loaded {
300          }          }
301  }  }
302    
303  my %files;  my $files;
304    
305  sub read_filenames {  sub read_filenames {
306          my $self = shift;          my $self = shift;
# Line 308  sub read_filenames { Line 308  sub read_filenames {
308          my $sth = $self->{'sth'} || die "no sth argument";          my $sth = $self->{'sth'} || die "no sth argument";
309    
310          # create empty filesystem          # create empty filesystem
311          (%files) = (          $files = {
312                  '.' => {                  '.' => {
313                          type => 0040,                          type => 0040,
314                          mode => 0755,                          mode => 0755,
# Line 322  sub read_filenames { Line 322  sub read_filenames {
322          #               type => 0100,          #               type => 0100,
323          #               ctime => time()-2000          #               ctime => time()-2000
324          #       },          #       },
325          );          };
326    
327          # fetch new filename list from database          # fetch new filename list from database
328          $sth->{'filenames'}->execute() || die $sth->{'filenames'}->errstr();          $sth->{'filenames'}->execute() || die $sth->{'filenames'}->errstr();
# Line 330  sub read_filenames { Line 330  sub read_filenames {
330          # read them in with sesible defaults          # read them in with sesible defaults
331          while (my $row = $sth->{'filenames'}->fetchrow_hashref() ) {          while (my $row = $sth->{'filenames'}->fetchrow_hashref() ) {
332                  $row->{'filename'} ||= 'NULL-'.$row->{'id'};                  $row->{'filename'} ||= 'NULL-'.$row->{'id'};
333                  $files{$row->{'filename'}} = {                  $files->{$row->{'filename'}} = {
334                          size => $row->{'size'},                          size => $row->{'size'},
335                          mode => $row->{'writable'} ? 0644 : 0444,                          mode => $row->{'writable'} ? 0644 : 0444,
336                          id => $row->{'id'} || 99,                          id => $row->{'id'} || 99,
# Line 341  sub read_filenames { Line 341  sub read_filenames {
341                  foreach (split(m!/!, $row->{'filename'})) {                  foreach (split(m!/!, $row->{'filename'})) {
342                          # first, entry is assumed to be file                          # first, entry is assumed to be file
343                          if ($d) {                          if ($d) {
344                                  $files{$d} = {                                  $files->{$d} = {
345                                                  mode => 0755,                                                  mode => 0755,
346                                                  type => 0040                                                  type => 0040
347                                  };                                  };
348                                  $files{$d.'/.'} = {                                  $files->{$d.'/.'} = {
349                                                  mode => 0755,                                                  mode => 0755,
350                                                  type => 0040                                                  type => 0040
351                                  };                                  };
352                                  $files{$d.'/..'} = {                                  $files->{$d.'/..'} = {
353                                                  mode => 0755,                                                  mode => 0755,
354                                                  type => 0040                                                  type => 0040
355                                  };                                  };
# Line 359  sub read_filenames { Line 359  sub read_filenames {
359                  }                  }
360          }          }
361    
362          print "found ",scalar(keys %files)," files\n";          print "found ",scalar(keys %{$files})," files\n";
363  }  }
364    
365    
# Line 374  sub e_getattr { Line 374  sub e_getattr {
374          my ($file) = filename_fixup(shift);          my ($file) = filename_fixup(shift);
375          $file =~ s,^/,,;          $file =~ s,^/,,;
376          $file = '.' unless length($file);          $file = '.' unless length($file);
377          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files->{$file});
378          my ($size) = $files{$file}{size} || 0;          my ($size) = $files->{$file}->{size} || 0;
379          my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0,0,0,int(($size+BLOCK-1)/BLOCK),0,0,1,BLOCK);          my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0,0,0,int(($size+BLOCK-1)/BLOCK),0,0,1,BLOCK);
380          my ($atime, $ctime, $mtime);          my ($atime, $ctime, $mtime);
381          $atime = $ctime = $mtime = $files{$file}{ctime} || $ctime_start;          $atime = $ctime = $mtime = $files->{$file}->{ctime} || $ctime_start;
382    
383          my ($modes) = (($files{$file}{type} || 0100)<<9) + $files{$file}{mode};          my ($modes) = (($files->{$file}->{type} || 0100)<<9) + $files->{$file}->{mode};
384    
385          # 2 possible types of return values:          # 2 possible types of return values:
386          #return -ENOENT(); # or any other error you care to          #return -ENOENT(); # or any other error you care to
# Line 392  sub e_getdir { Line 392  sub e_getdir {
392          my ($dirname) = shift;          my ($dirname) = shift;
393          $dirname =~ s!^/!!;          $dirname =~ s!^/!!;
394          # return as many text filenames as you like, followed by the retval.          # return as many text filenames as you like, followed by the retval.
395          print((scalar keys %files)." files total\n");          print((scalar keys %{$files})." files total\n");
396          my %out;          my %out;
397          foreach my $f (sort keys %files) {          foreach my $f (sort keys %{$files}) {
398                  if ($dirname) {                  if ($dirname) {
399                          if ($f =~ s/^\Q$dirname\E\///) {                          if ($f =~ s/^\Q$dirname\E\///) {
400                                  $out{$f}++ if ($f =~ /^[^\/]+$/);                                  $out{$f}++ if ($f =~ /^[^\/]+$/);
# Line 417  sub read_content { Line 417  sub read_content {
417          die "read_content needs file and id" unless ($file && $id);          die "read_content needs file and id" unless ($file && $id);
418    
419          $sth->{'read'}->execute($id) || die $sth->{'read'}->errstr;          $sth->{'read'}->execute($id) || die $sth->{'read'}->errstr;
420          $files{$file}{cont} = $sth->{'read'}->fetchrow_array;          $files->{$file}->{cont} = $sth->{'read'}->fetchrow_array;
421          # I should modify ctime only if content in database changed          # I should modify ctime only if content in database changed
422          #$files{$file}{ctime} = time() unless ($files{$file}{ctime});          #$files->{$file}->{ctime} = time() unless ($files->{$file}->{ctime});
423          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";
424  }  }
425    
426    
# Line 429  sub e_open { Line 429  sub e_open {
429          my $file = filename_fixup(shift);          my $file = filename_fixup(shift);
430          my $flags = shift;          my $flags = shift;
431    
432          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files->{$file});
433          return -EISDIR() unless exists($files{$file}{id});          return -EISDIR() unless exists($files->{$file}->{id});
434    
435          read_content($file,$files{$file}{id}) unless exists($files{$file}{cont});          read_content($file,$files->{$file}->{id}) unless exists($files->{$file}->{cont});
436    
437          $files{$file}{cont} ||= '';          $files->{$file}->{cont} ||= '';
438          print "open '$file' ",length($files{$file}{cont})," bytes\n";          print "open '$file' ",length($files->{$file}->{cont})," bytes\n";
439          return 0;          return 0;
440  }  }
441    
# Line 446  sub e_read { Line 446  sub e_read {
446          my ($file) = filename_fixup(shift);          my ($file) = filename_fixup(shift);
447          my ($buf_len,$off) = @_;          my ($buf_len,$off) = @_;
448    
449          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files->{$file});
450    
451          my $len = length($files{$file}{cont});          my $len = length($files->{$file}->{cont});
452    
453          print "read '$file' [$len bytes] offset $off length $buf_len\n";          print "read '$file' [$len bytes] offset $off length $buf_len\n";
454    
# Line 457  sub e_read { Line 457  sub e_read {
457    
458          $buf_len = $len-$off if ($len - $off < $buf_len);          $buf_len = $len-$off if ($len - $off < $buf_len);
459    
460          return substr($files{$file}{cont},$off,$buf_len);          return substr($files->{$file}->{cont},$off,$buf_len);
461  }  }
462    
463  sub clear_cont {  sub clear_cont {
464          print "transaction rollback\n";          print "transaction rollback\n";
465          $dbh->rollback || die $dbh->errstr;          $dbh->rollback || die $dbh->errstr;
466          print "invalidate all cached content\n";          print "invalidate all cached content\n";
467          foreach my $f (keys %files) {          foreach my $f (keys %{$files}) {
468                  delete $files{$f}{cont};                  delete $files->{$f}->{cont};
469                  delete $files{$f}{ctime};                  delete $files->{$f}->{ctime};
470          }          }
471          print "begin new transaction\n";          print "begin new transaction\n";
472          #$dbh->begin_work || die $dbh->errstr;          #$dbh->begin_work || die $dbh->errstr;
# Line 476  sub clear_cont { Line 476  sub clear_cont {
476  sub update_db {  sub update_db {
477          my $file = shift || die;          my $file = shift || die;
478    
479          $files{$file}{ctime} = time();          $files->{$file}->{ctime} = time();
480    
481          my ($cont,$id) = (          my ($cont,$id) = (
482                  $files{$file}{cont},                  $files->{$file}->{cont},
483                  $files{$file}{id}                  $files->{$file}->{id}
484          );          );
485    
486          if (!$sth->{'update'}->execute($cont,$id)) {          if (!$sth->{'update'}->execute($cont,$id)) {
# Line 493  sub update_db { Line 493  sub update_db {
493                          clear_cont;                          clear_cont;
494                          return 0;                          return 0;
495                  }                  }
496                  print "updated '$file' [",$files{$file}{id},"]\n";                  print "updated '$file' [",$files->{$file}->{id},"]\n";
497    
498                  $$fuse_self->{'invalidate'}->() if (ref $$fuse_self->{'invalidate'});                  $$fuse_self->{'invalidate'}->() if (ref $$fuse_self->{'invalidate'});
499          }          }
# Line 504  sub e_write { Line 504  sub e_write {
504          my $file = filename_fixup(shift);          my $file = filename_fixup(shift);
505          my ($buffer,$off) = @_;          my ($buffer,$off) = @_;
506    
507          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files->{$file});
508    
509          my $cont = $files{$file}{cont};          my $cont = $files->{$file}->{cont};
510          my $len = length($cont);          my $len = length($cont);
511    
512          print "write '$file' [$len bytes] offset $off length ",length($buffer),"\n";          print "write '$file' [$len bytes] offset $off length ",length($buffer),"\n";
513    
514          $files{$file}{cont} = "";          $files->{$file}->{cont} = "";
515    
516          $files{$file}{cont} .= substr($cont,0,$off) if ($off > 0);          $files->{$file}->{cont} .= substr($cont,0,$off) if ($off > 0);
517          $files{$file}{cont} .= $buffer;          $files->{$file}->{cont} .= $buffer;
518          $files{$file}{cont} .= substr($cont,$off+length($buffer),$len-$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);
519    
520          $files{$file}{size} = length($files{$file}{cont});          $files->{$file}->{size} = length($files->{$file}->{cont});
521    
522          if (! update_db($file)) {          if (! update_db($file)) {
523                  return -ENOSYS();                  return -ENOSYS();
# Line 532  sub e_truncate { Line 532  sub e_truncate {
532    
533          print "truncate to $size\n";          print "truncate to $size\n";
534    
535          $files{$file}{cont} = substr($files{$file}{cont},0,$size);          $files->{$file}->{cont} = substr($files->{$file}->{cont},0,$size);
536          $files{$file}{size} = $size;          $files->{$file}->{size} = $size;
537          return 0          return 0
538  };  };
539    
# Line 542  sub e_utime { Line 542  sub e_utime {
542          my ($atime,$mtime,$file) = @_;          my ($atime,$mtime,$file) = @_;
543          $file = filename_fixup($file);          $file = filename_fixup($file);
544    
545          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files->{$file});
546    
547          print "utime '$file' $atime $mtime\n";          print "utime '$file' $atime $mtime\n";
548    
549          $files{$file}{time} = $mtime;          $files->{$file}->{time} = $mtime;
550          return 0;          return 0;
551  }  }
552    
# Line 555  sub e_statfs { Line 555  sub e_statfs {
555          my $size = 0;          my $size = 0;
556          my $inodes = 0;          my $inodes = 0;
557    
558          foreach my $f (keys %files) {          foreach my $f (keys %{$files}) {
559                  if ($f !~ /(^|\/)\.\.?$/) {                  if ($f !~ /(^|\/)\.\.?$/) {
560                          $size += $files{$f}{size} || 0;                          $size += $files->{$f}->{size} || 0;
561                          $inodes++;                          $inodes++;
562                  }                  }
563                  print "$inodes: $f [$size]\n";                  print "$inodes: $f [$size]\n";
# Line 580  sub e_unlink { Line 580  sub e_unlink {
580  #               print Dumper($fuse_self);  #               print Dumper($fuse_self);
581  #               $$fuse_self->{'read_filenames'}->();  #               $$fuse_self->{'read_filenames'}->();
582  #               return 0;  #               return 0;
583          if (exists( $files{$file} )) {          if (exists( $files->{$file} )) {
584                  print "unlink '$file' will invalidate cache\n";                  print "unlink '$file' will invalidate cache\n";
585                  read_content($file,$files{$file}{id});                  read_content($file,$files->{$file}->{id});
586                  return 0;                  return 0;
587          }          }
588    

Legend:
Removed from v.55  
changed lines
  Added in v.61

  ViewVC Help
Powered by ViewVC 1.1.26