1 |
#!/usr/bin/perl |
#!/usr/bin/perl |
2 |
|
|
3 |
use POSIX qw(ENOENT EISDIR EINVAL O_RDWR); |
use POSIX qw(ENOENT EISDIR EINVAL ENOSYS O_RDWR); |
4 |
use Fuse; |
use Fuse; |
5 |
|
|
6 |
use DBI; |
use DBI; |
30 |
|
|
31 |
my $connect = "DBI:Pg:dbname=webgui"; |
my $connect = "DBI:Pg:dbname=webgui"; |
32 |
|
|
33 |
my $dbh = DBI->connect($connect,"","") || die $DBI::errstr; |
my $dbh = DBI->connect($connect,"","", { AutoCommit => 0 }) || die $DBI::errstr; |
34 |
|
|
35 |
print STDERR "$sql_filenames\n"; |
print "start transaction\n"; |
36 |
|
#$dbh->begin_work || die $dbh->errstr; |
37 |
|
|
38 |
my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr(); |
my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr(); |
39 |
$sth_filenames->execute() || die $sth_filenames->errstr(); |
$sth_filenames->execute() || die $sth_filenames->errstr(); |
41 |
my $sth_read = $dbh->prepare($sql_read) || die $dbh->errstr(); |
my $sth_read = $dbh->prepare($sql_read) || die $dbh->errstr(); |
42 |
my $sth_update = $dbh->prepare($sql_update) || die $dbh->errstr(); |
my $sth_update = $dbh->prepare($sql_update) || die $dbh->errstr(); |
43 |
|
|
|
print "#",join(",",@{ $sth_filenames->{NAME} }),"\n"; |
|
|
|
|
44 |
my $ctime_start = time(); |
my $ctime_start = time(); |
45 |
|
|
46 |
my (%files) = ( |
my (%files) = ( |
87 |
} |
} |
88 |
} |
} |
89 |
|
|
90 |
print scalar (keys %dirs), " dirs:",join(" ",keys %dirs),"\n"; |
print "found ",scalar(keys %files)-scalar(keys %dirs)," files, ",scalar(keys %dirs), " dirs\n"; |
91 |
|
|
92 |
sub filename_fixup { |
sub filename_fixup { |
93 |
my ($file) = shift; |
my ($file) = shift; |
129 |
} else { |
} else { |
130 |
$out{$f}++ if ($f =~ /^[^\/]+$/); |
$out{$f}++ if ($f =~ /^[^\/]+$/); |
131 |
} |
} |
|
print "f: $_ -> $f\n"; |
|
132 |
} |
} |
133 |
if (! %out) { |
if (! %out) { |
134 |
$out{'no files? bug?'}++; |
$out{'no files? bug?'}++; |
135 |
} |
} |
136 |
print scalar keys %out," files found for '$dirname': ",keys %out,"\n"; |
print scalar keys %out," files in dir '$dirname'\n"; |
137 |
return (keys %out),0; |
return (keys %out),0; |
138 |
} |
} |
139 |
|
|
|
my $in_transaction = 0; |
|
|
|
|
140 |
sub e_open { |
sub e_open { |
141 |
# 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. |
142 |
my $file = filename_fixup(shift); |
my $file = filename_fixup(shift); |
145 |
return -ENOENT() unless exists($files{$file}); |
return -ENOENT() unless exists($files{$file}); |
146 |
return -EISDIR() unless exists($files{$file}{id}); |
return -EISDIR() unless exists($files{$file}{id}); |
147 |
|
|
|
if (! $in_transaction) { |
|
|
# begin transaction |
|
|
if (! $dbh->begin_work) { |
|
|
print "transaction begin: ",$dbh->errstr; |
|
|
return -ENOENT(); |
|
|
} |
|
|
} |
|
|
$in_transaction++; |
|
|
print "files opened: $in_transaction\n"; |
|
|
|
|
148 |
if (!exists($files{$file}{cont})) { |
if (!exists($files{$file}{cont})) { |
149 |
$sth_read->execute($files{$file}{id}) || die $sth_read->errstr; |
$sth_read->execute($files{$file}{id}) || die $sth_read->errstr; |
150 |
$files{$file}{cont} = $sth_read->fetchrow_array; |
$files{$file}{cont} = $sth_read->fetchrow_array; |
151 |
|
print "file '$file' content read in cache\n"; |
152 |
} |
} |
153 |
print "open '$file' ",length($files{$file}{cont})," bytes\n"; |
print "open '$file' ",length($files{$file}{cont})," bytes\n"; |
154 |
return 0; |
return 0; |
159 |
# (note: 0 means EOF, "0" will give a byte (ascii "0") |
# (note: 0 means EOF, "0" will give a byte (ascii "0") |
160 |
# to the reading program) |
# to the reading program) |
161 |
my ($file) = filename_fixup(shift); |
my ($file) = filename_fixup(shift); |
162 |
my ($buf,$off) = @_; |
my ($buf_len,$off) = @_; |
163 |
|
|
164 |
return -ENOENT() unless exists($files{$file}); |
return -ENOENT() unless exists($files{$file}); |
165 |
|
|
166 |
my $len = length($files{$file}{cont}); |
my $len = length($files{$file}{cont}); |
167 |
|
|
168 |
print "read '$file' [$len bytes] offset $off length $buf\n"; |
print "read '$file' [$len bytes] offset $off length $buf_len\n"; |
169 |
|
|
170 |
return -EINVAL() if ($off > $len); |
return -EINVAL() if ($off > $len); |
171 |
return 0 if ($off == $len); |
return 0 if ($off == $len); |
172 |
|
|
173 |
$buf = $len-$off if ($off+$buf > $len); |
$buf_len = $buf_len-$off if ($off+$buf_len > $len); |
174 |
|
|
175 |
return substr($files{$file}{cont},$off,$buf); |
return substr($files{$file}{cont},$off,$buf_len); |
176 |
} |
} |
177 |
|
|
178 |
sub clear_cont { |
sub clear_cont { |
179 |
|
print "transaction rollback\n"; |
180 |
|
$dbh->rollback || die $dbh->errstr; |
181 |
print "invalidate all cached content\n"; |
print "invalidate all cached content\n"; |
182 |
foreach my $f (keys %files) { |
foreach my $f (keys %files) { |
183 |
delete $files{$f}{cont}; |
delete $files{$f}{cont}; |
184 |
} |
} |
185 |
|
print "begin new transaction\n"; |
186 |
|
$dbh->begin_work || die $dbh->errstr; |
187 |
} |
} |
188 |
|
|
189 |
|
|
190 |
sub update_db { |
sub update_db { |
191 |
my $file = shift || die; |
my $file = shift || die; |
192 |
|
|
193 |
|
$files{$file}{ctime} = time(); |
194 |
|
|
195 |
if (!$sth_update->execute($files{$file}{cont},$files{$file}{id})) { |
if (!$sth_update->execute($files{$file}{cont},$files{$file}{id})) { |
196 |
print "update problem: ",$sth_update->errstr; |
print "update problem: ",$sth_update->errstr; |
|
$dbh->rollback; |
|
197 |
clear_cont; |
clear_cont; |
|
$dbh->begin_work; |
|
198 |
return 0; |
return 0; |
199 |
} else { |
} else { |
200 |
if ($dbh->commit) { |
if (! $dbh->commit) { |
201 |
print "commit problem: ",$sth_update->errstr; |
print "ERROR: commit problem: ",$sth_update->errstr; |
|
$dbh->rollback; |
|
202 |
clear_cont; |
clear_cont; |
|
$dbh->begin_work; |
|
203 |
return 0; |
return 0; |
204 |
} |
} |
205 |
print "updated '$file' [",$files{$file}{id},"]\n"; |
print "updated '$file' [",$files{$file}{id},"]\n"; |
209 |
|
|
210 |
sub e_write { |
sub e_write { |
211 |
my $file = filename_fixup(shift); |
my $file = filename_fixup(shift); |
212 |
my ($buf,$off) = @_; |
my ($buf_len,$off) = @_; |
213 |
|
|
214 |
return -ENOENT() unless exists($files{$file}); |
return -ENOENT() unless exists($files{$file}); |
215 |
|
|
216 |
my $len = length($files{$file}{cont}); |
my $len = length($files{$file}{cont}); |
217 |
|
|
218 |
print "write '$file' [$len bytes] offset $off length $buf\n"; |
print "write '$file' [$len bytes] offset $off length\n"; |
219 |
|
|
220 |
$files{$file}{cont} = |
$files{$file}{cont} = |
221 |
substr($files{$file}{cont},0,$off) . |
substr($files{$file}{cont},0,$off) . |
222 |
$buf . |
$buf_len . |
223 |
substr($files{$file}{cont},$off+length($buf)); |
substr($files{$file}{cont},$off+length($buf_len)); |
224 |
|
|
225 |
if (! update_db($file)) { |
if (! update_db($file)) { |
226 |
return -ENOSYS(); |
return -ENOSYS(); |
227 |
} else { |
} else { |
228 |
return length($buf); |
return length($buf_len); |
229 |
} |
} |
230 |
} |
} |
231 |
|
|
244 |
|
|
245 |
return -ENOENT() unless exists($files{$file}); |
return -ENOENT() unless exists($files{$file}); |
246 |
|
|
247 |
|
print "utime '$file' $atime $mtime\n"; |
248 |
|
|
249 |
$files{$file}{time} = $mtime; |
$files{$file}{time} = $mtime; |
250 |
return 0; |
return 0; |
251 |
} |
} |
266 |
write=>\&e_write, |
write=>\&e_write, |
267 |
utime=>\&e_utime, |
utime=>\&e_utime, |
268 |
truncate=>\&e_truncate, |
truncate=>\&e_truncate, |
269 |
debug=>1, |
debug=>0, |
270 |
); |
); |