/[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 12 by dpavlin, Sun Aug 29 19:16:01 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    
17  our $VERSION = '0.01';  our $VERSION = '0.01';
18    
# 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          my $arg = {@_};          print Dumper($arg);
75    
76          carp "run needs 'dsn' to connect to (e.g. dsn => 'DBI:Pg:dbname=test')" unless ($arg->{'dsn'});          carp "mount needs 'dsn' to connect to (e.g. dsn => 'DBI:Pg:dbname=test')" unless ($arg->{'dsn'});
77          carp "run needs 'mount' as mountpoint" unless ($arg->{'mount'});          carp "mount needs 'mount' as mountpoint" unless ($arg->{'mount'});
78    
79            # save (some) arguments in self
80            $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;
# 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                  '.' => {                  '.' => {

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

  ViewVC Help
Powered by ViewVC 1.1.26