1 |
#!/usr/bin/perl |
#!/usr/bin/perl |
2 |
|
|
3 |
use POSIX qw(ENOENT EISDIR EINVAL); |
use POSIX qw(ENOENT EISDIR EINVAL O_RDWR); |
4 |
use Fuse; |
use Fuse; |
5 |
|
|
6 |
use DBI; |
use DBI; |
8 |
|
|
9 |
my $sql_filenames = q{ |
my $sql_filenames = q{ |
10 |
select |
select |
11 |
templateid as id, |
oid as id, |
12 |
namespace||'/'||name as filename, |
namespace||'/'||name||' ['||oid||']' as filename, |
13 |
length(template) as size, |
length(template) as size, |
14 |
iseditable as writable |
iseditable as writable |
15 |
from template ; |
from template ; |
16 |
}; |
}; |
17 |
|
|
18 |
my $sql_content = q{ |
my $sql_read = q{ |
19 |
select template |
select template |
20 |
from template |
from template |
21 |
where templateid = ?; |
where oid = ?; |
22 |
|
}; |
23 |
|
|
24 |
|
my $sql_update = q{ |
25 |
|
update template |
26 |
|
set template = ? |
27 |
|
where oid = ?; |
28 |
}; |
}; |
29 |
|
|
30 |
|
|
37 |
my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr(); |
my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr(); |
38 |
$sth_filenames->execute() || die $sth_filenames->errstr(); |
$sth_filenames->execute() || die $sth_filenames->errstr(); |
39 |
|
|
40 |
my $sth_content = $dbh->prepare($sql_content) || die $dbh->errstr(); |
my $sth_read = $dbh->prepare($sql_read) || die $dbh->errstr(); |
41 |
|
my $sth_update = $dbh->prepare($sql_update) || die $dbh->errstr(); |
42 |
|
|
43 |
print "#",join(",",@{ $sth_filenames->{NAME} }),"\n"; |
print "#",join(",",@{ $sth_filenames->{NAME} }),"\n"; |
44 |
|
|
126 |
$f =~ s/^\E$dirname\Q//; |
$f =~ s/^\E$dirname\Q//; |
127 |
$f =~ s/^\///; |
$f =~ s/^\///; |
128 |
if ($dirname) { |
if ($dirname) { |
129 |
$out{$f}++ if (/^\E$dirname\Q/); |
$out{$f}++ if (/^\E$dirname\Q/ && $f =~ /^[^\/]+$/); |
130 |
} else { |
} else { |
131 |
$out{$f}++ if ($f =~ /^[^\/]+$/); |
$out{$f}++ if ($f =~ /^[^\/]+$/); |
132 |
} |
} |
139 |
return (keys %out),0; |
return (keys %out),0; |
140 |
} |
} |
141 |
|
|
142 |
|
my $in_transaction = 0; |
143 |
|
|
144 |
sub e_open { |
sub e_open { |
145 |
# 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. |
146 |
my ($file) = filename_fixup(shift); |
my $file = filename_fixup(shift); |
147 |
print("open called\n"); |
my $flags = shift; |
148 |
|
|
149 |
return -ENOENT() unless exists($files{$file}); |
return -ENOENT() unless exists($files{$file}); |
150 |
return -EISDIR() unless exists($files{$file}{id}); |
return -EISDIR() unless exists($files{$file}{id}); |
151 |
|
|
152 |
|
if (! $in_transaction) { |
153 |
|
# begin transaction |
154 |
|
if (! $dbh->begin_work) { |
155 |
|
print "transaction begin: ",$dbh->errstr; |
156 |
|
return -ENOENT(); |
157 |
|
} |
158 |
|
} |
159 |
|
$in_transaction++; |
160 |
|
print "files opened: $in_transaction\n"; |
161 |
|
|
162 |
if (!exists($files{$file}{cont})) { |
if (!exists($files{$file}{cont})) { |
163 |
$sth_content->execute($files{$file}{id}); |
$sth_read->execute($files{$file}{id}) || die $sth_read->errstr; |
164 |
($files{$file}{cont}) = $sth_content->fetchrow_array; |
$files{$file}{cont} = $sth_read->fetchrow_array; |
165 |
} |
} |
166 |
print("open ok\n"); |
print "open '$file' ",length($files{$file}{cont})," bytes\n"; |
167 |
return 0; |
return 0; |
168 |
} |
} |
169 |
|
|
170 |
sub e_read { |
sub e_read { |
171 |
# return an error numeric, or binary/text string. (note: 0 means EOF, "0" will |
# return an error numeric, or binary/text string. |
172 |
# give a byte (ascii "0") to the reading program) |
# (note: 0 means EOF, "0" will give a byte (ascii "0") |
173 |
|
# to the reading program) |
174 |
my ($file) = filename_fixup(shift); |
my ($file) = filename_fixup(shift); |
175 |
my ($buf,$off) = @_; |
my ($buf,$off) = @_; |
176 |
|
|
177 |
return -ENOENT() unless exists($files{$file}); |
return -ENOENT() unless exists($files{$file}); |
178 |
return -EINVAL() if $off > length($files{$file}{cont}); |
|
179 |
return 0 if $off == length($files{$file}{cont}); |
my $len = length($files{$file}{cont}); |
180 |
|
|
181 |
|
print "read '$file' [$len bytes] offset $off length $buf\n"; |
182 |
|
|
183 |
|
return -EINVAL() if ($off > $len); |
184 |
|
return 0 if ($off == $len); |
185 |
|
|
186 |
|
$buf = $len-$off if ($off+$buf > $len); |
187 |
|
|
188 |
return substr($files{$file}{cont},$off,$buf); |
return substr($files{$file}{cont},$off,$buf); |
189 |
} |
} |
190 |
|
|
191 |
|
sub clear_cont { |
192 |
|
print "invalidate all cached content\n"; |
193 |
|
foreach my $f (keys %files) { |
194 |
|
delete $files{$f}{cont}; |
195 |
|
} |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
sub update_db { |
200 |
|
my $file = shift || die; |
201 |
|
|
202 |
|
if (!$sth_update->execute($files{$file}{cont},$files{$file}{id})) { |
203 |
|
print "update problem: ",$sth_update->errstr; |
204 |
|
$dbh->rollback; |
205 |
|
clear_cont; |
206 |
|
$dbh->begin_work; |
207 |
|
return 0; |
208 |
|
} else { |
209 |
|
if ($dbh->commit) { |
210 |
|
print "commit problem: ",$sth_update->errstr; |
211 |
|
$dbh->rollback; |
212 |
|
clear_cont; |
213 |
|
$dbh->begin_work; |
214 |
|
return 0; |
215 |
|
} |
216 |
|
print "updated '$file' [",$files{$file}{id},"]\n"; |
217 |
|
} |
218 |
|
return 1; |
219 |
|
} |
220 |
|
|
221 |
|
sub e_write { |
222 |
|
my $file = filename_fixup(shift); |
223 |
|
my ($buf,$off) = @_; |
224 |
|
|
225 |
|
return -ENOENT() unless exists($files{$file}); |
226 |
|
|
227 |
|
my $len = length($files{$file}{cont}); |
228 |
|
|
229 |
|
print "write '$file' [$len bytes] offset $off length $buf\n"; |
230 |
|
|
231 |
|
$files{$file}{cont} = |
232 |
|
substr($files{$file}{cont},0,$off) . |
233 |
|
$buf . |
234 |
|
substr($files{$file}{cont},$off+length($buf)); |
235 |
|
|
236 |
|
if (! update_db($file)) { |
237 |
|
return -ENOSYS(); |
238 |
|
} else { |
239 |
|
return length($buf); |
240 |
|
} |
241 |
|
} |
242 |
|
|
243 |
|
sub e_truncate { |
244 |
|
my $file = filename_fixup(shift); |
245 |
|
my $size = shift; |
246 |
|
|
247 |
|
$files{$file}{cont} = substr($files{$file}{cont},0,$size); |
248 |
|
return 0 |
249 |
|
}; |
250 |
|
|
251 |
|
|
252 |
|
sub e_utime { |
253 |
|
my ($atime,$mtime,$file) = @_; |
254 |
|
$file = filename_fixup($file); |
255 |
|
|
256 |
|
return -ENOENT() unless exists($files{$file}); |
257 |
|
|
258 |
|
$files{$file}{time} = $mtime; |
259 |
|
return 0; |
260 |
|
} |
261 |
|
|
262 |
sub e_statfs { return 255, 1, 1, 1, 1, 2 } |
sub e_statfs { return 255, 1, 1, 1, 1, 2 } |
263 |
|
|
264 |
# If you run the script directly, it will run fusermount, which will in turn |
# If you run the script directly, it will run fusermount, which will in turn |
272 |
open=>\&e_open, |
open=>\&e_open, |
273 |
statfs=>\&e_statfs, |
statfs=>\&e_statfs, |
274 |
read=>\&e_read, |
read=>\&e_read, |
275 |
|
write=>\&e_write, |
276 |
|
utime=>\&e_utime, |
277 |
|
truncate=>\&e_truncate, |
278 |
debug=>1, |
debug=>1, |
279 |
); |
); |