3 use POSIX qw(ENOENT EISDIR EINVAL ENOSYS 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,"","", { AutoCommit => 0 }) || die $DBI::errstr;
35 print "start transaction\n";
36 #$dbh->begin_work || die $dbh->errstr;
38 my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr();
39 $sth_filenames->execute() || die $sth_filenames->errstr();
41 my $sth_read = $dbh->prepare($sql_read) || die $dbh->errstr();
42 my $sth_update = $dbh->prepare($sql_update) || die $dbh->errstr();
44 my $ctime_start = time();
52 # cont => "File 'a'.\n",
54 # ctime => time()-2000
60 while (my $row = $sth_filenames->fetchrow_hashref() ) {
61 $files{$row->{'filename'}} = {
62 size => $row->{'size'},
63 mode => $row->{'writable'} ? 0644 : 0444,
64 id => $row->{'id'} || 99,
68 foreach (split(m!/!, $row->{'filename'})) {
69 # first, entry is assumed to be file
90 print "found ",scalar(keys %files)-scalar(keys %dirs)," files, ",scalar(keys %dirs), " dirs\n";
95 $file = '.' unless length($file);
100 my ($file) = filename_fixup(shift);
102 $file = '.' unless length($file);
103 return -ENOENT() unless exists($files{$file});
104 my ($size) = $files{$file}{size} || 1;
105 my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0,0,0,1,0,0,1,1024);
106 my ($atime, $ctime, $mtime);
107 $atime = $ctime = $mtime = $files{$file}{ctime} || $ctime_start;
109 my ($modes) = (($files{$file}{type} || 0100)<<9) + $files{$file}{mode};
111 # 2 possible types of return values:
112 #return -ENOENT(); # or any other error you care to
113 #print(join(",",($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)),"\n");
114 return ($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
118 my ($dirname) = shift;
120 # return as many text filenames as you like, followed by the retval.
121 print((scalar keys %files)." files total\n");
123 foreach (keys %files) {
125 $f =~ s/^\E$dirname\Q//;
128 $out{$f}++ if (/^\E$dirname\Q/ && $f =~ /^[^\/]+$/);
130 $out{$f}++ if ($f =~ /^[^\/]+$/);
134 $out{'no files? bug?'}++;
136 print scalar keys %out," files found for '$dirname': ",keys %out,"\n";
137 return (keys %out),0;
141 # VFS sanity check; it keeps all the necessary state, not much to do here.
142 my $file = filename_fixup(shift);
145 return -ENOENT() unless exists($files{$file});
146 return -EISDIR() unless exists($files{$file}{id});
148 if (!exists($files{$file}{cont})) {
149 $sth_read->execute($files{$file}{id}) || die $sth_read->errstr;
150 $files{$file}{cont} = $sth_read->fetchrow_array;
151 print "file '$file' content read in cache\n";
153 print "open '$file' ",length($files{$file}{cont})," bytes\n";
158 # return an error numeric, or binary/text string.
159 # (note: 0 means EOF, "0" will give a byte (ascii "0")
160 # to the reading program)
161 my ($file) = filename_fixup(shift);
164 return -ENOENT() unless exists($files{$file});
166 my $len = length($files{$file}{cont});
168 print "read '$file' [$len bytes] offset $off length $buf\n";
170 return -EINVAL() if ($off > $len);
171 return 0 if ($off == $len);
173 $buf = $len-$off if ($off+$buf > $len);
175 return substr($files{$file}{cont},$off,$buf);
179 print "transaction rollback\n";
180 $dbh->rollback || die $dbh->errstr;
181 print "invalidate all cached content\n";
182 foreach my $f (keys %files) {
183 delete $files{$f}{cont};
185 print "begin new transaction\n";
186 $dbh->begin_work || die $dbh->errstr;
191 my $file = shift || die;
193 if (!$sth_update->execute($files{$file}{cont},$files{$file}{id})) {
194 print "update problem: ",$sth_update->errstr;
198 if (! $dbh->commit) {
199 print "ERROR: commit problem: ",$sth_update->errstr;
203 print "updated '$file' [",$files{$file}{id},"]\n";
209 my $file = filename_fixup(shift);
212 return -ENOENT() unless exists($files{$file});
214 my $len = length($files{$file}{cont});
216 print "write '$file' [$len bytes] offset $off length $buf\n";
218 $files{$file}{cont} =
219 substr($files{$file}{cont},0,$off) .
221 substr($files{$file}{cont},$off+length($buf));
223 if (! update_db($file)) {
231 my $file = filename_fixup(shift);
234 $files{$file}{cont} = substr($files{$file}{cont},0,$size);
240 my ($atime,$mtime,$file) = @_;
241 $file = filename_fixup($file);
243 return -ENOENT() unless exists($files{$file});
245 $files{$file}{time} = $mtime;
249 sub e_statfs { return 255, 1, 1, 1, 1, 2 }
251 # If you run the script directly, it will run fusermount, which will in turn
252 # re-run this script. Hence the funky semantics.
253 my ($mountpoint) = "";
254 $mountpoint = shift(@ARGV) if @ARGV;
256 mountpoint=>$mountpoint,
257 getattr=>\&e_getattr,
264 truncate=>\&e_truncate,