/[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 5 by dpavlin, Wed Aug 4 09:25:31 2004 UTC revision 6 by dpavlin, Wed Aug 4 16:17:09 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 O_RDWR);
4  use Fuse;  use Fuse;
5    
6  use DBI;  use DBI;
# Line 15  my $sql_filenames = q{ Line 15  my $sql_filenames = q{
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 oid = ?;                  where oid = ?;
22    };
23    
24    my $sql_update = q{
25            update template
26                    set template = ?        
27                    where oid = ?;
28  };  };
29    
30    
# Line 31  print STDERR "$sql_filenames\n"; Line 37  print STDERR "$sql_filenames\n";
37  my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr();  my $sth_filenames = $dbh->prepare($sql_filenames) || die $dbh->errstr();
38  $sth_filenames->execute() || die $sth_filenames->errstr();  $sth_filenames->execute() || die $sth_filenames->errstr();
39    
40  my $sth_content = $dbh->prepare($sql_content) || die $dbh->errstr();  my $sth_read = $dbh->prepare($sql_read) || die $dbh->errstr();
41    my $sth_update = $dbh->prepare($sql_update) || die $dbh->errstr();
42    
43  print "#",join(",",@{ $sth_filenames->{NAME} }),"\n";  print "#",join(",",@{ $sth_filenames->{NAME} }),"\n";
44    
# Line 132  sub e_getdir { Line 139  sub e_getdir {
139          return (keys %out),0;          return (keys %out),0;
140  }  }
141    
142    my $in_transaction = 0;
143    
144  sub e_open {  sub e_open {
145          # 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.
146          my ($file) = filename_fixup(shift);          my $file = filename_fixup(shift);
147            my $flags = shift;
148    
149          return -ENOENT() unless exists($files{$file});          return -ENOENT() unless exists($files{$file});
150          return -EISDIR() unless exists($files{$file}{id});          return -EISDIR() unless exists($files{$file}{id});
151    
152            if (! $in_transaction) {
153                    # begin transaction
154                    if (! $dbh->begin_work) {
155                            print "transaction begin: ",$dbh->errstr;
156                            return -ENOENT();
157                    }
158            }
159            $in_transaction++;
160            print "files opened: $in_transaction\n";
161    
162          if (!exists($files{$file}{cont})) {          if (!exists($files{$file}{cont})) {
163                  $sth_content->execute($files{$file}{id});                  $sth_read->execute($files{$file}{id}) || die $sth_read->errstr;
164                  $files{$file}{cont} = $sth_content->fetchrow_array;                  $files{$file}{cont} = $sth_read->fetchrow_array;
165          }          }
166          print "open '$file' ",length($files{$file}{cont})," bytes\n";          print "open '$file' ",length($files{$file}{cont})," bytes\n";
167          return 0;          return 0;
# Line 166  sub e_read { Line 188  sub e_read {
188          return substr($files{$file}{cont},$off,$buf);          return substr($files{$file}{cont},$off,$buf);
189  }  }
190    
191    sub clear_cont {
192            print "invalidate all cached content\n";
193            foreach my $f (keys %files) {
194                    delete $files{$f}{cont};
195            }
196    }
197    
198    
199    sub update_db {
200            my $file = shift || die;
201    
202            if (!$sth_update->execute($files{$file}{cont},$files{$file}{id})) {
203                    print "update problem: ",$sth_update->errstr;
204                    $dbh->rollback;
205                    clear_cont;
206                    $dbh->begin_work;
207                    return 0;
208            } else {
209                    if ($dbh->commit) {
210                            print "commit problem: ",$sth_update->errstr;
211                            $dbh->rollback;
212                            clear_cont;
213                            $dbh->begin_work;
214                            return 0;
215                    }
216                    print "updated '$file' [",$files{$file}{id},"]\n";
217            }
218            return 1;
219    }
220    
221    sub e_write {
222            my $file = filename_fixup(shift);
223            my ($buf,$off) = @_;
224    
225            return -ENOENT() unless exists($files{$file});
226    
227            my $len = length($files{$file}{cont});
228    
229            print "write '$file' [$len bytes] offset $off length $buf\n";
230    
231            $files{$file}{cont} =
232                    substr($files{$file}{cont},0,$off) .
233                    $buf .
234                    substr($files{$file}{cont},$off+length($buf));
235    
236            if (! update_db($file)) {
237                    return -ENOSYS();
238            } else {
239                    return length($buf);
240            }
241    }
242    
243    sub e_truncate {
244            my $file = filename_fixup(shift);
245            my $size = shift;
246    
247            $files{$file}{cont} = substr($files{$file}{cont},0,$size);
248            return 0
249    };
250    
251    
252    sub e_utime {
253            my ($atime,$mtime,$file) = @_;
254            $file = filename_fixup($file);
255    
256            return -ENOENT() unless exists($files{$file});
257    
258            $files{$file}{time} = $mtime;
259            return 0;
260    }
261    
262  sub e_statfs { return 255, 1, 1, 1, 1, 2 }  sub e_statfs { return 255, 1, 1, 1, 1, 2 }
263    
264  # 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 179  Fuse::main( Line 272  Fuse::main(
272          open=>\&e_open,          open=>\&e_open,
273          statfs=>\&e_statfs,          statfs=>\&e_statfs,
274          read=>\&e_read,          read=>\&e_read,
275            write=>\&e_write,
276            utime=>\&e_utime,
277            truncate=>\&e_truncate,
278          debug=>1,          debug=>1,
279  );  );

Legend:
Removed from v.5  
changed lines
  Added in v.6

  ViewVC Help
Powered by ViewVC 1.1.26