3 use POSIX qw(ENOENT EISDIR EINVAL O_RDWR);
12 namespace||'/'||name||' ['||oid||']' as filename,
13 length(template) as size,
14 iseditable as writable
31 my $connect = "DBI:Pg:dbname=webgui";
33 my $dbh = DBI->connect($connect,"","") || die $DBI::errstr;
35 print STDERR "$sql_filenames\n";
37 my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr();
38 $sth_filenames->execute() || die $sth_filenames->errstr();
40 my $sth_read = $dbh->prepare($sql_read) || die $dbh->errstr();
41 my $sth_update = $dbh->prepare($sql_update) || die $dbh->errstr();
43 print "#",join(",",@{ $sth_filenames->{NAME} }),"\n";
45 my $ctime_start = time();
53 # cont => "File 'a'.\n",
55 # ctime => time()-2000
61 while (my $row = $sth_filenames->fetchrow_hashref() ) {
62 $files{$row->{'filename'}} = {
63 size => $row->{'size'},
64 mode => $row->{'writable'} ? 0644 : 0444,
65 id => $row->{'id'} || 99,
69 foreach (split(m!/!, $row->{'filename'})) {
70 # first, entry is assumed to be file
91 print scalar (keys %dirs), " dirs:",join(" ",keys %dirs),"\n";
96 $file = '.' unless length($file);
101 my ($file) = filename_fixup(shift);
103 $file = '.' unless length($file);
104 return -ENOENT() unless exists($files{$file});
105 my ($size) = $files{$file}{size} || 1;
106 my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0,0,0,1,0,0,1,1024);
107 my ($atime, $ctime, $mtime);
108 $atime = $ctime = $mtime = $files{$file}{ctime} || $ctime_start;
110 my ($modes) = (($files{$file}{type} || 0100)<<9) + $files{$file}{mode};
112 # 2 possible types of return values:
113 #return -ENOENT(); # or any other error you care to
114 #print(join(",",($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)),"\n");
115 return ($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
119 my ($dirname) = shift;
121 # return as many text filenames as you like, followed by the retval.
122 print((scalar keys %files)." files total\n");
124 foreach (keys %files) {
126 $f =~ s/^\E$dirname\Q//;
129 $out{$f}++ if (/^\E$dirname\Q/ && $f =~ /^[^\/]+$/);
131 $out{$f}++ if ($f =~ /^[^\/]+$/);
133 print "f: $_ -> $f\n";
136 $out{'no files? bug?'}++;
138 print scalar keys %out," files found for '$dirname': ",keys %out,"\n";
139 return (keys %out),0;
142 my $in_transaction = 0;
145 # VFS sanity check; it keeps all the necessary state, not much to do here.
146 my $file = filename_fixup(shift);
149 return -ENOENT() unless exists($files{$file});
150 return -EISDIR() unless exists($files{$file}{id});
152 if (! $in_transaction) {
154 if (! $dbh->begin_work) {
155 print "transaction begin: ",$dbh->errstr;
160 print "files opened: $in_transaction\n";
162 if (!exists($files{$file}{cont})) {
163 $sth_read->execute($files{$file}{id}) || die $sth_read->errstr;
164 $files{$file}{cont} = $sth_read->fetchrow_array;
166 print "open '$file' ",length($files{$file}{cont})," bytes\n";
171 # return an error numeric, or binary/text string.
172 # (note: 0 means EOF, "0" will give a byte (ascii "0")
173 # to the reading program)
174 my ($file) = filename_fixup(shift);
177 return -ENOENT() unless exists($files{$file});
179 my $len = length($files{$file}{cont});
181 print "read '$file' [$len bytes] offset $off length $buf\n";
183 return -EINVAL() if ($off > $len);
184 return 0 if ($off == $len);
186 $buf = $len-$off if ($off+$buf > $len);
188 return substr($files{$file}{cont},$off,$buf);
192 print "invalidate all cached content\n";
193 foreach my $f (keys %files) {
194 delete $files{$f}{cont};
200 my $file = shift || die;
202 if (!$sth_update->execute($files{$file}{cont},$files{$file}{id})) {
203 print "update problem: ",$sth_update->errstr;
210 print "commit problem: ",$sth_update->errstr;
216 print "updated '$file' [",$files{$file}{id},"]\n";
222 my $file = filename_fixup(shift);
225 return -ENOENT() unless exists($files{$file});
227 my $len = length($files{$file}{cont});
229 print "write '$file' [$len bytes] offset $off length $buf\n";
231 $files{$file}{cont} =
232 substr($files{$file}{cont},0,$off) .
234 substr($files{$file}{cont},$off+length($buf));
236 if (! update_db($file)) {
244 my $file = filename_fixup(shift);
247 $files{$file}{cont} = substr($files{$file}{cont},0,$size);
253 my ($atime,$mtime,$file) = @_;
254 $file = filename_fixup($file);
256 return -ENOENT() unless exists($files{$file});
258 $files{$file}{time} = $mtime;
262 sub e_statfs { return 255, 1, 1, 1, 1, 2 }
264 # If you run the script directly, it will run fusermount, which will in turn
265 # re-run this script. Hence the funky semantics.
266 my ($mountpoint) = "";
267 $mountpoint = shift(@ARGV) if @ARGV;
269 mountpoint=>$mountpoint,
270 getattr=>\&e_getattr,
277 truncate=>\&e_truncate,