/[fuse_dbi]/trunk/fuse_dbi.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/fuse_dbi.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by dpavlin, Wed Aug 4 08:58:46 2004 UTC revision 7 by dpavlin, Sat Aug 7 14:48:23 2004 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3  use POSIX qw(ENOENT EISDIR EINVAL);  use POSIX qw(ENOENT EISDIR EINVAL ENOSYS O_RDWR);
4  use Fuse;  use Fuse;
5    
6  use DBI;  use DBI;
# Line 8  use strict; Line 8  use strict;
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    
31  my $connect = "DBI:Pg:dbname=webgui";  my $connect = "DBI:Pg:dbname=webgui";
32    
33  my $dbh = DBI->connect($connect,"","") || die $DBI::errstr;  my $dbh = DBI->connect($connect,"","", { AutoCommit => 0 }) || die $DBI::errstr;
34    
35  print STDERR "$sql_filenames\n";  print "start transaction\n";
36    #$dbh->begin_work || die $dbh->errstr;
37    
38  my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr();  my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr();
39  $sth_filenames->execute() || die $sth_filenames->errstr();  $sth_filenames->execute() || die $sth_filenames->errstr();
40    
41  my $sth_content = $dbh->prepare($sql_content) || die $dbh->errstr();  my $sth_read = $dbh->prepare($sql_read) || die $dbh->errstr();
42    my $sth_update = $dbh->prepare($sql_update) || die $dbh->errstr();
 print "#",join(",",@{ $sth_filenames->{NAME} }),"\n";  
43    
44  my $ctime_start = time();  my $ctime_start = time();
45    
# Line 81  while (my $row = $sth_filenames->fetchro Line 87  while (my $row = $sth_filenames->fetchro
87          }          }
88  }  }
89    
90  print scalar (keys %dirs), " dirs:",join(" ",keys %dirs),"\n";  print "found ",scalar(keys %files)-scalar(keys %dirs)," files, ",scalar(keys %dirs), " dirs\n";
91    
92  sub filename_fixup {  sub filename_fixup {
93          my ($file) = shift;          my ($file) = shift;
# Line 119  sub e_getdir { Line 125  sub e_getdir {
125                  $f =~ s/^\E$dirname\Q//;                  $f =~ s/^\E$dirname\Q//;
126                  $f =~ s/^\///;                  $f =~ s/^\///;
127                  if ($dirname) {                  if ($dirname) {
128                          $out{$f}++ if (/^\E$dirname\Q/);                          $out{$f}++ if (/^\E$dirname\Q/ && $f =~ /^[^\/]+$/);
129                  } else {                  } else {
130                          $out{$f}++ if ($f =~ /^[^\/]+$/);                          $out{$f}++ if ($f =~ /^[^\/]+$/);
131                  }                  }
                 print "f: $_ -> $f\n";  
132          }          }
133          if (! %out) {          if (! %out) {
134                  $out{'no files? bug?'}++;                  $out{'no files? bug?'}++;
# Line 134  sub e_getdir { Line 139  sub e_getdir {
139    
140  sub e_open {  sub e_open {
141          # 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.
142          my ($file) = filename_fixup(shift);          my $file = filename_fixup(shift);
143          print("open called\n");          my $flags = shift;
144    
145          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files{$file});
146          return -EISDIR() unless exists($files{$file}{id});          return -EISDIR() unless exists($files{$file}{id});
147    
148          if (!exists($files{$file}{cont})) {          if (!exists($files{$file}{cont})) {
149                  $sth_content->execute($files{$file}{id});                  $sth_read->execute($files{$file}{id}) || die $sth_read->errstr;
150                  ($files{$file}{cont}) = $sth_content->fetchrow_array;                  $files{$file}{cont} = $sth_read->fetchrow_array;
151                    print "file '$file' content read in cache\n";
152          }          }
153          print("open ok\n");          print "open '$file' ",length($files{$file}{cont})," bytes\n";
154          return 0;          return 0;
155  }  }
156    
157  sub e_read {  sub e_read {
158          # return an error numeric, or binary/text string.  (note: 0 means EOF, "0" will          # return an error numeric, or binary/text string.
159          # give a byte (ascii "0") to the reading program)          # (note: 0 means EOF, "0" will give a byte (ascii "0")
160            # to the reading program)
161          my ($file) = filename_fixup(shift);          my ($file) = filename_fixup(shift);
162          my ($buf,$off) = @_;          my ($buf,$off) = @_;
163    
164          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files{$file});
165          return -EINVAL() if $off > length($files{$file}{cont});  
166          return 0 if $off == length($files{$file}{cont});          my $len = length($files{$file}{cont});
167    
168            print "read '$file' [$len bytes] offset $off length $buf\n";
169    
170            return -EINVAL() if ($off > $len);
171            return 0 if ($off == $len);
172    
173            $buf = $len-$off if ($off+$buf > $len);
174    
175          return substr($files{$file}{cont},$off,$buf);          return substr($files{$file}{cont},$off,$buf);
176  }  }
177    
178    sub clear_cont {
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};
184            }
185            print "begin new transaction\n";
186            $dbh->begin_work || die $dbh->errstr;
187    }
188    
189    
190    sub update_db {
191            my $file = shift || die;
192    
193            if (!$sth_update->execute($files{$file}{cont},$files{$file}{id})) {
194                    print "update problem: ",$sth_update->errstr;
195                    clear_cont;
196                    return 0;
197            } else {
198                    if (! $dbh->commit) {
199                            print "ERROR: commit problem: ",$sth_update->errstr;
200                            clear_cont;
201                            return 0;
202                    }
203                    print "updated '$file' [",$files{$file}{id},"]\n";
204            }
205            return 1;
206    }
207    
208    sub e_write {
209            my $file = filename_fixup(shift);
210            my ($buf,$off) = @_;
211    
212            return -ENOENT() unless exists($files{$file});
213    
214            my $len = length($files{$file}{cont});
215    
216            print "write '$file' [$len bytes] offset $off length $buf\n";
217    
218            $files{$file}{cont} =
219                    substr($files{$file}{cont},0,$off) .
220                    $buf .
221                    substr($files{$file}{cont},$off+length($buf));
222    
223            if (! update_db($file)) {
224                    return -ENOSYS();
225            } else {
226                    return length($buf);
227            }
228    }
229    
230    sub e_truncate {
231            my $file = filename_fixup(shift);
232            my $size = shift;
233    
234            $files{$file}{cont} = substr($files{$file}{cont},0,$size);
235            return 0
236    };
237    
238    
239    sub e_utime {
240            my ($atime,$mtime,$file) = @_;
241            $file = filename_fixup($file);
242    
243            return -ENOENT() unless exists($files{$file});
244    
245            $files{$file}{time} = $mtime;
246            return 0;
247    }
248    
249  sub e_statfs { return 255, 1, 1, 1, 1, 2 }  sub e_statfs { return 255, 1, 1, 1, 1, 2 }
250    
251  # 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
# Line 170  Fuse::main( Line 259  Fuse::main(
259          open=>\&e_open,          open=>\&e_open,
260          statfs=>\&e_statfs,          statfs=>\&e_statfs,
261          read=>\&e_read,          read=>\&e_read,
262          debug=>1,          write=>\&e_write,
263            utime=>\&e_utime,
264            truncate=>\&e_truncate,
265            debug=>0,
266  );  );

Legend:
Removed from v.1  
changed lines
  Added in v.7

  ViewVC Help
Powered by ViewVC 1.1.26