/[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 9 by dpavlin, Sat Aug 7 19:06:03 2004 UTC revision 18 by dpavlin, Sun Sep 5 16:59:41 2004 UTC
# Line 9  use warnings; Line 9  use warnings;
9  use POSIX qw(ENOENT EISDIR EINVAL ENOSYS O_RDWR);  use POSIX qw(ENOENT EISDIR EINVAL ENOSYS O_RDWR);
10  use Fuse;  use Fuse;
11  use DBI;  use DBI;
12    use Carp;
13    use Proc::Simple;
14    use Data::Dumper;
15    
16  our $VERSION = '0.01';  
17    our $VERSION = '0.02';
18    
19  =head1 NAME  =head1 NAME
20    
# Line 19  Fuse::DBI - mount your database as files Line 23  Fuse::DBI - mount your database as files
23  =head1 SYNOPSIS  =head1 SYNOPSIS
24    
25    use Fuse::DBI;    use Fuse::DBI;
26    Fuse::DBI->run( ... );    Fuse::DBI->mount( ... );
27    
28  See L<run> below for examples how to set parametars.  See L<run> below for examples how to set parametars.
29    
# Line 39  It's actually opposite of Oracle's inten Line 43  It's actually opposite of Oracle's inten
43    
44  =cut  =cut
45    
46  =head2 run  =head2 mount
47    
48  Mount your database as filesystem.  Mount your database as filesystem.
49    
50    Fuse::DBI->run({    my $mnt = Fuse::DBI->mount({
51          filenames => 'select name from filenamefilenames,          filenames => 'select name from filenamefilenames,
52          read => 'sql read',          read => 'sql read',
53          update => 'sql update',          update => 'sql update',
# Line 58  my $dbh; Line 62  my $dbh;
62  my $sth;  my $sth;
63  my $ctime_start;  my $ctime_start;
64    
65  sub run {  sub read_filenames;
66          my $self = shift  
67    sub mount {
68            my $class = shift;
69            my $self = {};
70            bless($self, $class);
71    
72            my $arg = shift;
73    
74            print Dumper($arg);
75    
76          my $arg = {@_};          carp "mount needs 'dsn' to connect to (e.g. dsn => 'DBI:Pg:dbname=test')" unless ($arg->{'dsn'});
77            carp "mount needs 'mount' as mountpoint" unless ($arg->{'mount'});
78    
79          carp "run needs 'dsn' to connect to (e.g. dsn => 'DBI:Pg:dbname=test')" unless ($arg->{'dsn'});          # save (some) arguments in self
80          carp "run needs 'mount' as mountpoint" unless ($arg->{'mount'});          $self->{$_} = $arg->{$_} foreach (qw(mount));
81    
82          foreach (qw(filenames read update)) {          foreach (qw(filenames read update)) {
83                  carp "run needs '$_' SQL" unless ($arg->{$_});                  carp "mount needs '$_' SQL" unless ($arg->{$_});
84          }          }
85    
86          $dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, { AutoCommit => 0 }) || die $DBI::errstr;          $dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, { AutoCommit => 0 }) || die $DBI::errstr;
87    
88          print "start transaction\n";          print "start transaction\n";
89          #$dbh->begin_work || die $dbh->errstr;          $dbh->begin_work || die $dbh->errstr;
90    
91          $sth->{filenames} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr();          $sth->{filenames} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr();
92    
# Line 84  sub run { Line 97  sub run {
97    
98          read_filenames;          read_filenames;
99    
100          Fuse::main(          $self->{'proc'} = Proc::Simple->new();
101                  mountpoint=>$arg->{'mount'},          $self->{'proc'}->kill_on_destroy(1);
102                  getattr=>\&e_getattr,  
103                  getdir=>\&e_getdir,          $self->{'proc'}->start( sub {
104                  open=>\&e_open,                  Fuse::main(
105                  statfs=>\&e_statfs,                          mountpoint=>$arg->{'mount'},
106                  read=>\&e_read,                          getattr=>\&e_getattr,
107                  write=>\&e_write,                          getdir=>\&e_getdir,
108                  utime=>\&e_utime,                          open=>\&e_open,
109                  truncate=>\&e_truncate,                          statfs=>\&e_statfs,
110                  debug=>0,                          read=>\&e_read,
111          );                          write=>\&e_write,
112                            utime=>\&e_utime,
113                            truncate=>\&e_truncate,
114                            debug=>0,
115                    );
116            } );
117    
118            confess "Fuse::main failed" if (! $self->{'proc'}->poll);
119    
120            $self ? return $self : return undef;
121  };  };
122    
123    =head2 umount
124    
125    Unmount your database as filesystem.
126    
127      $mnt->umount;
128    
129    This will also kill background process which is translating
130    database to filesystem.
131    
132    =cut
133    
134    sub umount {
135            my $self = shift;
136    
137            confess "no process running?" unless ($self->{'proc'});
138    
139            system "fusermount -u ".$self->{'mount'} || croak "umount error: $!";
140    
141            if ($self->{'proc'}->poll) {
142                    $self->{'proc'}->kill;
143                    return 1 if (! $self->{'proc'}->poll);
144            } else {
145                    return 1;
146            }
147    }
148    
149    
150  my %files;  my %files;
151  my %dirs;  my %dirs;
152    
153  sub read_filenames {  sub read_filenames {
154            my $self = shift;
155    
156          # create empty filesystem          # create empty filesystem
157          (%files) = (          (%files) = (
158                  '.' => {                  '.' => {
# Line 184  sub e_getdir { Line 235  sub e_getdir {
235          # return as many text filenames as you like, followed by the retval.          # return as many text filenames as you like, followed by the retval.
236          print((scalar keys %files)." files total\n");          print((scalar keys %files)." files total\n");
237          my %out;          my %out;
238          foreach (keys %files) {          foreach my $f (sort keys %files) {
                 my $f = $_;  
                 $f =~ s/^\E$dirname\Q//;  
                 $f =~ s/^\///;  
239                  if ($dirname) {                  if ($dirname) {
240                          $out{$f}++ if (/^\E$dirname\Q/ && $f =~ /^[^\/]+$/);                          if ($f =~ s/^\E$dirname\Q\///) {
241                                    $out{$f}++ if ($f =~ /^[^\/]+$/);
242                            }
243                  } else {                  } else {
244                          $out{$f}++ if ($f =~ /^[^\/]+$/);                          $out{$f}++ if ($f =~ /^[^\/]+$/);
245                  }                  }
# Line 198  sub e_getdir { Line 248  sub e_getdir {
248                  $out{'no files? bug?'}++;                  $out{'no files? bug?'}++;
249          }          }
250          print scalar keys %out," files in dir '$dirname'\n";          print scalar keys %out," files in dir '$dirname'\n";
251            print "## ",join(" ",keys %out),"\n";
252          return (keys %out),0;          return (keys %out),0;
253  }  }
254    
# Line 273  sub update_db { Line 324  sub update_db {
324    
325  sub e_write {  sub e_write {
326          my $file = filename_fixup(shift);          my $file = filename_fixup(shift);
327          my ($buf_len,$off) = @_;          my ($buffer,$off) = @_;
328    
329          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files{$file});
330    
331          my $len = length($files{$file}{cont});          my $cont = $files{$file}{cont};
332            my $len = length($cont);
333    
334          print "write '$file' [$len bytes] offset $off length\n";          print "write '$file' [$len bytes] offset $off length ",length($buffer),"\n";
335    
336          $files{$file}{cont} =          $files{$file}{cont} = "";
337                  substr($files{$file}{cont},0,$off) .  
338                  $buf_len .          $files{$file}{cont} .= substr($cont,0,$off) if ($off > 0);
339                  substr($files{$file}{cont},$off+length($buf_len));          $files{$file}{cont} .= $buffer;
340            $files{$file}{cont} .= substr($cont,-($off+length($buffer))) if ($off+length($buffer) > $len);
341    
342            $files{$file}{size} = length($files{$file}{cont});
343    
344          if (! update_db($file)) {          if (! update_db($file)) {
345                  return -ENOSYS();                  return -ENOSYS();
346          } else {          } else {
347                  return length($buf_len);                  return length($buffer);
348          }          }
349  }  }
350    
# Line 297  sub e_truncate { Line 352  sub e_truncate {
352          my $file = filename_fixup(shift);          my $file = filename_fixup(shift);
353          my $size = shift;          my $size = shift;
354    
355            print "truncate to $size\n";
356    
357          $files{$file}{cont} = substr($files{$file}{cont},0,$size);          $files{$file}{cont} = substr($files{$file}{cont},0,$size);
358            $files{$file}{size} = $size;
359          return 0          return 0
360  };  };
361    

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

  ViewVC Help
Powered by ViewVC 1.1.26