From 949eca0e5c352946edb3145dcb24148a80628f6c Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sun, 29 Aug 2004 18:51:29 +0000 Subject: [PATCH] first try at making this module (late commit) git-svn-id: svn://svn.rot13.org/fuse_dbi/trunk@11 17f4e80c-d0e0-0310-8903-bfc3ae804c12 --- DBI.pm | 80 ++++++++++++++++++++++++++++++++++------------ Makefile.PL | 6 ++-- examples/webgui.pl | 33 +++++++++++++++++++ t/02database.t | 46 ++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 23 deletions(-) create mode 100755 examples/webgui.pl create mode 100755 t/02database.t diff --git a/DBI.pm b/DBI.pm index 572af5b..58fbdce 100755 --- a/DBI.pm +++ b/DBI.pm @@ -9,6 +9,10 @@ use warnings; use POSIX qw(ENOENT EISDIR EINVAL ENOSYS O_RDWR); use Fuse; use DBI; +use Carp; +use Proc::Simple; +use Data::Dumper; + our $VERSION = '0.01'; @@ -19,7 +23,7 @@ Fuse::DBI - mount your database as filesystem and use it =head1 SYNOPSIS use Fuse::DBI; - Fuse::DBI->run( ... ); + Fuse::DBI->mount( ... ); See L below for examples how to set parametars. @@ -39,11 +43,11 @@ It's actually opposite of Oracle's intention to put everything into database. =cut -=head2 run +=head2 mount Mount your database as filesystem. - Fuse::DBI->run({ + my $mnt = Fuse::DBI->mount({ filenames => 'select name from filenamefilenames, read => 'sql read', update => 'sql update', @@ -58,16 +62,22 @@ my $dbh; my $sth; my $ctime_start; -sub run { - my $self = shift +sub read_filenames; + +sub mount { + my $class = shift; + my $self = {}; + bless($self, $class); - my $arg = {@_}; + my $arg = shift; - carp "run needs 'dsn' to connect to (e.g. dsn => 'DBI:Pg:dbname=test')" unless ($arg->{'dsn'}); - carp "run needs 'mount' as mountpoint" unless ($arg->{'mount'}); + print Dumper($arg); + + carp "mount needs 'dsn' to connect to (e.g. dsn => 'DBI:Pg:dbname=test')" unless ($arg->{'dsn'}); + carp "mount needs 'mount' as mountpoint" unless ($arg->{'mount'}); foreach (qw(filenames read update)) { - carp "run needs '$_' SQL" unless ($arg->{$_}); + carp "mount needs '$_' SQL" unless ($arg->{$_}); } $dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, { AutoCommit => 0 }) || die $DBI::errstr; @@ -84,24 +94,52 @@ sub run { read_filenames; - 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, - ); + $self->{'proc'} = Proc::Simple->new(); + $self->{'proc'}->kill_on_destroy(1); + + $self->{'proc'}->start( sub { + 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, + ); + } ); + + $self ? return $self : return undef; }; +=head2 umount + +Unmount your database as filesystem. + + $mnt->umount; + +This will also kill background process which is translating +database to filesystem. + +=cut + +sub umount { + my $self = shift; + + confess "no process running?" unless ($self->{'proc'}); + $self->{'proc'}->kill; +} + + my %files; my %dirs; sub read_filenames { + my $self = shift; + # create empty filesystem (%files) = ( '.' => { diff --git a/Makefile.PL b/Makefile.PL index 21fa72f..af1fcf8 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -6,11 +6,13 @@ WriteMakefile( NAME => 'Fuse::DBI', VERSION_FROM => 'DBI.pm', # finds $VERSION PREREQ_PM => { - Fuse => 0, POSIX => 0, + Fuse => 0, + DBI => 0, + Carp => 0, }, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 - (ABSTRACT_FROM => 'lib/Fuse/DBI.pm', # retrieve abstract from module + (ABSTRACT_FROM => 'DBI.pm', # retrieve abstract from module AUTHOR => 'Dobrica Pavlinusic ') : ()), ); diff --git a/examples/webgui.pl b/examples/webgui.pl new file mode 100755 index 0000000..ce04fcf --- /dev/null +++ b/examples/webgui.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl -w + +use strict; +use blib; +use Fuse::DBI; + +my $sql_filenames = q{ + select + oid as id, + namespace||'/'||name||' ['||oid||']' as filename, + length(template) as size, + iseditable as writable + from template ; +}; + +my $sql_read = q{ + select template + from template + where oid = ?; +}; + +my $sql_update = q{ + update template + set template = ? + where oid = ?; +}; + +Fuse::DBI->run({ + filenames => $sql_filenames, + read => $sql_read, + update => $sql_update, + dsn => 'DBI:Pg:dbname=webgui', +}); diff --git a/t/02database.t b/t/02database.t new file mode 100755 index 0000000..e9d57b6 --- /dev/null +++ b/t/02database.t @@ -0,0 +1,46 @@ +#!/usr/bin/perl -w + +use strict; +use warnings; + +use Test::More tests => 3; +use blib; + +use_ok('Fuse::DBI'); + +my $sql_filenames = q{ + select + oid as id, + namespace||'/'||name||' ['||oid||']' as filename, + length(template) as size, + iseditable as writable + from template ; +}; + +my $sql_read = q{ + select template + from template + where oid = ?; +}; + +my $sql_update = q{ + update template + set template = ? + where oid = ?; +}; + +my $mnt = Fuse::DBI->mount({ + filenames => $sql_filenames, + read => $sql_read, + update => $sql_update, + dsn => 'DBI:Pg:dbname=webgui', + mount => '/mnt2', +}); + +ok($mnt, "mount"); + +diag "mounted sleeping for 5 sec"; +system "ls -lR /mnt2"; +sleep(5); + +ok($mnt->umount,"umount"); -- 2.20.1