1 |
dpavlin |
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 |
|
|
templateid as id, |
12 |
|
|
namespace||'/'||name 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 templateid = ?; |
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/); |
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 |
|
|
print("open called\n"); |
139 |
|
|
return -ENOENT() unless exists($files{$file}); |
140 |
|
|
return -EISDIR() unless exists($files{$file}{id}); |
141 |
|
|
if (!exists($files{$file}{cont})) { |
142 |
|
|
$sth_content->execute($files{$file}{id}); |
143 |
|
|
($files{$file}{cont}) = $sth_content->fetchrow_array; |
144 |
|
|
} |
145 |
|
|
print("open ok\n"); |
146 |
|
|
return 0; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
sub e_read { |
150 |
|
|
# return an error numeric, or binary/text string. (note: 0 means EOF, "0" will |
151 |
|
|
# give a byte (ascii "0") to the reading program) |
152 |
|
|
my ($file) = filename_fixup(shift); |
153 |
|
|
my ($buf,$off) = @_; |
154 |
|
|
return -ENOENT() unless exists($files{$file}); |
155 |
|
|
return -EINVAL() if $off > length($files{$file}{cont}); |
156 |
|
|
return 0 if $off == length($files{$file}{cont}); |
157 |
|
|
return substr($files{$file}{cont},$off,$buf); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
sub e_statfs { return 255, 1, 1, 1, 1, 2 } |
161 |
|
|
|
162 |
|
|
# If you run the script directly, it will run fusermount, which will in turn |
163 |
|
|
# re-run this script. Hence the funky semantics. |
164 |
|
|
my ($mountpoint) = ""; |
165 |
|
|
$mountpoint = shift(@ARGV) if @ARGV; |
166 |
|
|
Fuse::main( |
167 |
|
|
mountpoint=>$mountpoint, |
168 |
|
|
getattr=>\&e_getattr, |
169 |
|
|
getdir=>\&e_getdir, |
170 |
|
|
open=>\&e_open, |
171 |
|
|
statfs=>\&e_statfs, |
172 |
|
|
read=>\&e_read, |
173 |
|
|
debug=>1, |
174 |
|
|
); |