1 |
#!/usr/bin/perl |
2 |
|
3 |
use POSIX qw(ENOENT EISDIR EINVAL); |
4 |
use Fuse; |
5 |
|
6 |
use DBI; |
7 |
use strict; |
8 |
|
9 |
my $sql_filenames = q{ |
10 |
select |
11 |
oid as id, |
12 |
namespace||'/'||name||' ['||oid||']' as filename, |
13 |
length(template) as size, |
14 |
iseditable as writable |
15 |
from template ; |
16 |
}; |
17 |
|
18 |
my $sql_content = q{ |
19 |
select template |
20 |
from template |
21 |
where oid = ?; |
22 |
}; |
23 |
|
24 |
|
25 |
my $connect = "DBI:Pg:dbname=webgui"; |
26 |
|
27 |
my $dbh = DBI->connect($connect,"","") || die $DBI::errstr; |
28 |
|
29 |
print STDERR "$sql_filenames\n"; |
30 |
|
31 |
my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr(); |
32 |
$sth_filenames->execute() || die $sth_filenames->errstr(); |
33 |
|
34 |
my $sth_content = $dbh->prepare($sql_content) || die $dbh->errstr(); |
35 |
|
36 |
print "#",join(",",@{ $sth_filenames->{NAME} }),"\n"; |
37 |
|
38 |
my $ctime_start = time(); |
39 |
|
40 |
my (%files) = ( |
41 |
'.' => { |
42 |
type => 0040, |
43 |
mode => 0755, |
44 |
}, |
45 |
# a => { |
46 |
# cont => "File 'a'.\n", |
47 |
# type => 0100, |
48 |
# ctime => time()-2000 |
49 |
# }, |
50 |
); |
51 |
|
52 |
my %dirs; |
53 |
|
54 |
while (my $row = $sth_filenames->fetchrow_hashref() ) { |
55 |
$files{$row->{'filename'}} = { |
56 |
size => $row->{'size'}, |
57 |
mode => $row->{'writable'} ? 0644 : 0444, |
58 |
id => $row->{'id'} || 99, |
59 |
}; |
60 |
|
61 |
my $d; |
62 |
foreach (split(m!/!, $row->{'filename'})) { |
63 |
# first, entry is assumed to be file |
64 |
if ($d) { |
65 |
$files{$d} = { |
66 |
size => $dirs{$d}++, |
67 |
mode => 0755, |
68 |
type => 0040 |
69 |
}; |
70 |
$files{$d.'/.'} = { |
71 |
mode => 0755, |
72 |
type => 0040 |
73 |
}; |
74 |
$files{$d.'/..'} = { |
75 |
mode => 0755, |
76 |
type => 0040 |
77 |
}; |
78 |
} |
79 |
$d .= "/" if ($d); |
80 |
$d .= "$_"; |
81 |
} |
82 |
} |
83 |
|
84 |
print scalar (keys %dirs), " dirs:",join(" ",keys %dirs),"\n"; |
85 |
|
86 |
sub filename_fixup { |
87 |
my ($file) = shift; |
88 |
$file =~ s,^/,,; |
89 |
$file = '.' unless length($file); |
90 |
return $file; |
91 |
} |
92 |
|
93 |
sub e_getattr { |
94 |
my ($file) = filename_fixup(shift); |
95 |
$file =~ s,^/,,; |
96 |
$file = '.' unless length($file); |
97 |
return -ENOENT() unless exists($files{$file}); |
98 |
my ($size) = $files{$file}{size} || 1; |
99 |
my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0,0,0,1,0,0,1,1024); |
100 |
my ($atime, $ctime, $mtime); |
101 |
$atime = $ctime = $mtime = $files{$file}{ctime} || $ctime_start; |
102 |
|
103 |
my ($modes) = (($files{$file}{type} || 0100)<<9) + $files{$file}{mode}; |
104 |
|
105 |
# 2 possible types of return values: |
106 |
#return -ENOENT(); # or any other error you care to |
107 |
#print(join(",",($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)),"\n"); |
108 |
return ($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks); |
109 |
} |
110 |
|
111 |
sub e_getdir { |
112 |
my ($dirname) = shift; |
113 |
$dirname =~ s!^/!!; |
114 |
# return as many text filenames as you like, followed by the retval. |
115 |
print((scalar keys %files)." files total\n"); |
116 |
my %out; |
117 |
foreach (keys %files) { |
118 |
my $f = $_; |
119 |
$f =~ s/^\E$dirname\Q//; |
120 |
$f =~ s/^\///; |
121 |
if ($dirname) { |
122 |
$out{$f}++ if (/^\E$dirname\Q/ && $f =~ /^[^\/]+$/); |
123 |
} else { |
124 |
$out{$f}++ if ($f =~ /^[^\/]+$/); |
125 |
} |
126 |
print "f: $_ -> $f\n"; |
127 |
} |
128 |
if (! %out) { |
129 |
$out{'no files? bug?'}++; |
130 |
} |
131 |
print scalar keys %out," files found for '$dirname': ",keys %out,"\n"; |
132 |
return (keys %out),0; |
133 |
} |
134 |
|
135 |
sub e_open { |
136 |
# VFS sanity check; it keeps all the necessary state, not much to do here. |
137 |
my ($file) = filename_fixup(shift); |
138 |
return -ENOENT() unless exists($files{$file}); |
139 |
return -EISDIR() unless exists($files{$file}{id}); |
140 |
if (!exists($files{$file}{cont})) { |
141 |
$sth_content->execute($files{$file}{id}); |
142 |
$files{$file}{cont} = $sth_content->fetchrow_array; |
143 |
} |
144 |
print "open '$file' ",length($files{$file}{cont})," bytes\n"; |
145 |
return 0; |
146 |
} |
147 |
|
148 |
sub e_read { |
149 |
# return an error numeric, or binary/text string. |
150 |
# (note: 0 means EOF, "0" will give a byte (ascii "0") |
151 |
# to the reading program) |
152 |
my ($file) = filename_fixup(shift); |
153 |
my ($buf,$off) = @_; |
154 |
|
155 |
return -ENOENT() unless exists($files{$file}); |
156 |
|
157 |
my $len = length($files{$file}{cont}); |
158 |
|
159 |
print "read '$file' [$len bytes] offset $off length $buf\n"; |
160 |
|
161 |
return -EINVAL() if ($off > $len); |
162 |
return 0 if ($off == $len); |
163 |
|
164 |
$buf = $len-$off if ($off+$buf > $len); |
165 |
|
166 |
return substr($files{$file}{cont},$off,$buf); |
167 |
} |
168 |
|
169 |
sub e_statfs { return 255, 1, 1, 1, 1, 2 } |
170 |
|
171 |
# If you run the script directly, it will run fusermount, which will in turn |
172 |
# re-run this script. Hence the funky semantics. |
173 |
my ($mountpoint) = ""; |
174 |
$mountpoint = shift(@ARGV) if @ARGV; |
175 |
Fuse::main( |
176 |
mountpoint=>$mountpoint, |
177 |
getattr=>\&e_getattr, |
178 |
getdir=>\&e_getdir, |
179 |
open=>\&e_open, |
180 |
statfs=>\&e_statfs, |
181 |
read=>\&e_read, |
182 |
debug=>1, |
183 |
); |