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 |
|
|
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', |
62 |
my $ctime_start; |
my $ctime_start; |
63 |
|
|
64 |
sub read_filenames; |
sub read_filenames; |
65 |
|
sub fuse_module_loaded; |
66 |
|
|
67 |
sub mount { |
sub mount { |
68 |
my $class = shift; |
my $class = shift; |
83 |
carp "mount 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; |
$ctime_start = time(); |
87 |
|
|
88 |
print "start transaction\n"; |
my $pid; |
89 |
$dbh->begin_work || die $dbh->errstr; |
if ($arg->{'fork'}) { |
90 |
|
$pid = fork(); |
91 |
|
die "fork() failed: $!" unless defined $pid; |
92 |
|
# child will return to caller |
93 |
|
if ($pid) { |
94 |
|
return $self; |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
|
$dbh = DBI->connect($arg->{'dsn'},$arg->{'user'},$arg->{'password'}, {AutoCommit => 0, RaiseError => 1}) || die $DBI::errstr; |
99 |
|
|
100 |
$sth->{filenames} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr(); |
$sth->{filenames} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr(); |
101 |
|
|
102 |
$sth->{'read'} = $dbh->prepare($arg->{'read'}) || die $dbh->errstr(); |
$sth->{'read'} = $dbh->prepare($arg->{'read'}) || die $dbh->errstr(); |
103 |
$sth->{'update'} = $dbh->prepare($arg->{'update'}) || die $dbh->errstr(); |
$sth->{'update'} = $dbh->prepare($arg->{'update'}) || die $dbh->errstr(); |
104 |
|
|
105 |
$ctime_start = time(); |
$self->read_filenames; |
|
|
|
|
read_filenames; |
|
106 |
|
|
107 |
$self->{'proc'} = Proc::Simple->new(); |
Fuse::main( |
108 |
$self->{'proc'}->kill_on_destroy(1); |
mountpoint=>$arg->{'mount'}, |
109 |
|
getattr=>\&e_getattr, |
110 |
|
getdir=>\&e_getdir, |
111 |
|
open=>\&e_open, |
112 |
|
statfs=>\&e_statfs, |
113 |
|
read=>\&e_read, |
114 |
|
write=>\&e_write, |
115 |
|
utime=>\&e_utime, |
116 |
|
truncate=>\&e_truncate, |
117 |
|
unlink=>\&e_unlink, |
118 |
|
debug=>0, |
119 |
|
); |
120 |
|
|
121 |
$self->{'proc'}->start( sub { |
exit(0) if ($arg->{'fork'}); |
|
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, |
|
|
); |
|
|
} ); |
|
122 |
|
|
123 |
confess "Fuse::main failed" if (! $self->{'proc'}->poll); |
return 1; |
124 |
|
|
|
$self ? return $self : return undef; |
|
125 |
}; |
}; |
126 |
|
|
127 |
=head2 umount |
=head2 umount |
138 |
sub umount { |
sub umount { |
139 |
my $self = shift; |
my $self = shift; |
140 |
|
|
|
confess "no process running?" unless ($self->{'proc'}); |
|
|
|
|
141 |
system "fusermount -u ".$self->{'mount'} || croak "umount error: $!"; |
system "fusermount -u ".$self->{'mount'} || croak "umount error: $!"; |
142 |
|
|
143 |
if ($self->{'proc'}->poll) { |
return 1; |
144 |
$self->{'proc'}->kill; |
} |
145 |
return 1 if (! $self->{'proc'}->poll); |
|
146 |
} else { |
=head2 fuse_module_loaded |
147 |
|
|
148 |
|
Checks if C<fuse> module is loaded in kernel. |
149 |
|
|
150 |
|
die "no fuse module loaded in kernel" |
151 |
|
unless (Fuse::DBI::fuse_module_loaded); |
152 |
|
|
153 |
|
This function in called by L<mount>, but might be useful alone also. |
154 |
|
|
155 |
|
=cut |
156 |
|
|
157 |
|
sub fuse_module_loaded { |
158 |
|
my $lsmod = `lsmod`; |
159 |
|
die "can't start lsmod: $!" unless ($lsmod); |
160 |
|
if ($lsmod =~ m/fuse/s) { |
161 |
return 1; |
return 1; |
162 |
|
} else { |
163 |
|
return 0; |
164 |
} |
} |
165 |
} |
} |
166 |
|
|
|
|
|
167 |
my %files; |
my %files; |
168 |
my %dirs; |
my %dirs; |
169 |
|
|
269 |
return (keys %out),0; |
return (keys %out),0; |
270 |
} |
} |
271 |
|
|
272 |
|
sub read_content { |
273 |
|
my ($file,$id) = @_; |
274 |
|
|
275 |
|
die "read_content needs file and id" unless ($file && $id); |
276 |
|
|
277 |
|
$sth->{'read'}->execute($id) || die $sth->{'read'}->errstr; |
278 |
|
$files{$file}{cont} = $sth->{'read'}->fetchrow_array; |
279 |
|
print "file '$file' content [",length($files{$file}{cont})," bytes] read in cache\n"; |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
sub e_open { |
sub e_open { |
284 |
# 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. |
285 |
my $file = filename_fixup(shift); |
my $file = filename_fixup(shift); |
288 |
return -ENOENT() unless exists($files{$file}); |
return -ENOENT() unless exists($files{$file}); |
289 |
return -EISDIR() unless exists($files{$file}{id}); |
return -EISDIR() unless exists($files{$file}{id}); |
290 |
|
|
291 |
if (!exists($files{$file}{cont})) { |
read_content($file,$files{$file}{id}) unless exists($files{$file}{cont}); |
292 |
$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"; |
|
|
} |
|
293 |
print "open '$file' ",length($files{$file}{cont})," bytes\n"; |
print "open '$file' ",length($files{$file}{cont})," bytes\n"; |
294 |
return 0; |
return 0; |
295 |
} |
} |
310 |
return -EINVAL() if ($off > $len); |
return -EINVAL() if ($off > $len); |
311 |
return 0 if ($off == $len); |
return 0 if ($off == $len); |
312 |
|
|
313 |
$buf_len = $buf_len-$off if ($off+$buf_len > $len); |
$buf_len = $len-$off if ($len - $off < $buf_len); |
314 |
|
|
315 |
return substr($files{$file}{cont},$off,$buf_len); |
return substr($files{$file}{cont},$off,$buf_len); |
316 |
} |
} |
323 |
delete $files{$f}{cont}; |
delete $files{$f}{cont}; |
324 |
} |
} |
325 |
print "begin new transaction\n"; |
print "begin new transaction\n"; |
326 |
$dbh->begin_work || die $dbh->errstr; |
#$dbh->begin_work || die $dbh->errstr; |
327 |
} |
} |
328 |
|
|
329 |
|
|
332 |
|
|
333 |
$files{$file}{ctime} = time(); |
$files{$file}{ctime} = time(); |
334 |
|
|
335 |
if (!$sth->{'update'}->execute($files{$file}{cont},$files{$file}{id})) { |
my ($cont,$id) = ( |
336 |
|
$files{$file}{cont}, |
337 |
|
$files{$file}{id} |
338 |
|
); |
339 |
|
|
340 |
|
if (!$sth->{'update'}->execute($cont,$id)) { |
341 |
print "update problem: ",$sth->{'update'}->errstr; |
print "update problem: ",$sth->{'update'}->errstr; |
342 |
clear_cont; |
clear_cont; |
343 |
return 0; |
return 0; |
367 |
|
|
368 |
$files{$file}{cont} .= substr($cont,0,$off) if ($off > 0); |
$files{$file}{cont} .= substr($cont,0,$off) if ($off > 0); |
369 |
$files{$file}{cont} .= $buffer; |
$files{$file}{cont} .= $buffer; |
370 |
$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); |
371 |
|
|
372 |
$files{$file}{size} = length($files{$file}{cont}); |
$files{$file}{size} = length($files{$file}{cont}); |
373 |
|
|
404 |
|
|
405 |
sub e_statfs { return 255, 1, 1, 1, 1, 2 } |
sub e_statfs { return 255, 1, 1, 1, 1, 2 } |
406 |
|
|
407 |
|
sub e_unlink { |
408 |
|
my $file = filename_fixup(shift); |
409 |
|
|
410 |
|
return -ENOENT() unless exists($files{$file}); |
411 |
|
|
412 |
|
print "unlink '$file' will invalidate cache\n"; |
413 |
|
|
414 |
|
read_content($file,$files{$file}{id}); |
415 |
|
|
416 |
|
return 0; |
417 |
|
} |
418 |
1; |
1; |
419 |
__END__ |
__END__ |
420 |
|
|