/[fuse-comp]/fuse-comp.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 /fuse-comp.pl

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

revision 5 by dpavlin, Sun Jul 8 15:04:35 2007 UTC revision 12 by dpavlin, Mon Jul 9 10:35:02 2007 UTC
# Line 4  use threads; Line 4  use threads;
4  use threads::shared;  use threads::shared;
5    
6  use Fuse;  use Fuse;
 use IO::File;  
7  use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);  use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
8  use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);  use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);
9  require 'syscall.ph'; # for SYS_mknod and SYS_lchown  require 'syscall.ph'; # for SYS_mknod and SYS_lchown
# Line 12  use PerlIO::gzip; Line 11  use PerlIO::gzip;
11  use File::Path;  use File::Path;
12  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
13  use Carp qw/confess/;  use Carp qw/confess/;
14    use IO::File;
15    
16  my $mount = {  my $mount = {
17          from    => '/tmp/comp',          from    => '/tmp/comp',
# Line 24  my $debug = 1; Line 24  my $debug = 1;
24  my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i;  my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i;
25    
26  # don't compress files smaller than this  # don't compress files smaller than this
27  my $min_compress_size = 256;  my $min_compress_size = 512;
28    
29  foreach my $dir ( keys %$mount ) {  foreach my $dir ( keys %$mount ) {
30          if ( ! -e $mount->{$dir} ) {          if ( ! -e $mount->{$dir} ) {
# Line 71  sub gzip_original_size { Line 71  sub gzip_original_size {
71    
72  sub unlink_all {  sub unlink_all {
73          my $file = shift;          my $file = shift;
74          foreach my $dir ( keys %$mount ) {          warn "# unlink_all( $file )\n";
75                  my $path = $mount->{$dir} . '/' . $file;  
76                    my $path = fixup( $file );
77                  map {          unlink $path || return 0;
78                          my $path = $_;  
79                          if ( -e $path ) {          my $tmp = $mount->{tmp} . '/' . $file;
80                                  if ( unlink $path ) {          unlink $tmp if ( -e $tmp );
81                                          warn "## unlink $path\n" if $debug;  
                                 } else {  
                                         warn "can't unlink $path: $!\n";  
                                         return 0;  
                                 }  
                         }  
                 } [ $path, $path . '.gz' ];  
         }  
82          delete( $pending->{$file} );          delete( $pending->{$file} );
83          return 1;          return 1;
84  }  }
# Line 126  sub file_copy { Line 119  sub file_copy {
119          chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";          chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";
120          chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";          chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";
121          utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";          utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
122    
123            undef $d;
124            undef $s;
125  }  }
126    
127  sub x_open {  sub x_open {
128          my ($file) = shift;          my ($file) = shift;
129          my ($mode) = shift;          my ($mode) = shift;
130    
131            if ( $file eq '/.debug' ) {
132                    my $path = $mount->{from} . '/.debug';
133                    open( my $debug, '>', $path ) || die "can't open $path: $!";
134                    my $dump = dump( $pending );
135                    print $debug "pending = $dump\n";
136                    close($debug);
137                    $pending->{'/.debug'}->{path} = $path;
138                    warn "## created dump $path $dump\n";
139                    return 0;
140            }
141    
142          $pending->{$file}->{open}++;          $pending->{$file}->{open}++;
143          warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, "\n";  
144          my $fh;          my $mode_desc = {
145          if ( $pending->{$file}->{open} == 1 ) {                  rdonly => $mode && O_RDONLY,
146                  my $path = fixup($file);                  rdwr => $mode && O_RDWR,
147                  my $tmp = $mount->{tmp} . '/' . $file;                  append => $mode && O_APPEND,
148                  warn ">>> open abs path: $path\n";                  create => $mode && O_CREAT,
                 if ( -e $tmp ) {  
                         $path = $tmp;  
                 } elsif ( $path =~ m/\.gz$/ ) {  
                         my $dest_path = $tmp;  
                         $dest_path =~ s!/[^/]+$!!;      #!vim-fix  
                         mkpath $dest_path unless -e $dest_path;  
                         file_copy( '<:gzip', $path, '>', $tmp );  
                         $path = $tmp;  
                 }  
                 return -$! unless sysopen($fh , $path, $mode);  
                 $pending->{$file}->{fh} = $fh;  
                 $pending->{$file}->{path} = $path;  
         } elsif ( ! defined( $pending->{$file}->{fh} ) ) {  
                 confess "can't find fh for $file ", dump($pending);  
149          };          };
150            warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n";
151            my $fh;
152    
153            my $path = fixup($file);
154            my $tmp = $mount->{tmp} . '/' . $file;
155            if ( -e $tmp ) {
156                    $path = $tmp;
157            } elsif ( $path =~ m/\.gz$/ ) {
158                    my $dest_path = $tmp;
159                    $dest_path =~ s!/[^/]+$!!;      #!vim-fix
160                    mkpath $dest_path unless -e $dest_path;
161                    file_copy( '<:gzip', $path, '>', $tmp );
162                    $path = $tmp;
163            }
164            warn ">>> open abs path: $path ", -s $path, " bytes\n";
165            return -$! unless sysopen($fh , $path, $mode);
166            close($fh);
167    
168            $pending->{$file}->{path} = $path;
169          return 0;          return 0;
170  }  }
171    
# Line 160  sub x_read { Line 173  sub x_read {
173          my ($file,$bufsize,$off) = @_;          my ($file,$bufsize,$off) = @_;
174          my ($rv) = -ENOSYS();          my ($rv) = -ENOSYS();
175          my $path = fixup( $file );          my $path = fixup( $file );
176    
177            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
178    
179          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
180          my ($fsize) = -s $path;  
181          my $fh = $pending->{$file}->{fh} || confess "no fh? ", dump( $pending );          my $fh = new IO::File;
182            return -ENOSYS() unless open($fh,$pending->{$file}->{path});
183    
184          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
185                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
186          }          }
187    
188          return $rv;          return $rv;
189  }  }
190    
191  sub x_write {  sub x_write {
192          my ($file,$buf,$off) = @_;          my ($file,$buf,$off) = @_;
193          $pending->{$file}->{write}++;          $pending->{$file}->{write}++;
194          my ($rv);          my $rv;
195          my $path = fixup($file);          my $path = fixup($file);
196    
197            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
198    
199          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
200          my ($fsize) = -s $path;  
201          my $fh = $pending->{$file}->{fh};          my $fh = new IO::File;
202          return -ENOSYS() unless $fh;          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});
203          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
204                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
205                    warn "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
206          }          }
207          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
208            close($fh);
209          return length($buf);          return length($buf);
210  }  }
211    
# Line 243  sub x_release { Line 267  sub x_release {
267          } elsif ( ! defined( $pending->{$file}->{write} ) ) {          } elsif ( ! defined( $pending->{$file}->{write} ) ) {
268                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
269          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
                 close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";  
270                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
271                  my $dest = fixup( $file );                  my $dest = fixup( $file );
272    
273                  # cleanup old compressed copy                  # cleanup old compressed copy
274                  if ( $dest =~ /\.gz$/ ) {                  if ( $dest =~ /\.gz$/ ) {
275                          warn "## remove old $dest\n";                          warn "## remove old $dest\n";
276                          unlink_all( $file ) || confess "can't remove $dest: $!";                          unlink $dest || confess "can't remove $dest: $!";
277                          $dest =~ s/\.gz$//;                          $dest =~ s/\.gz$//;
278                  }                  }
279    
# Line 267  sub x_release { Line 290  sub x_release {
290                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );
291    
292                          # FIXME add timeout to remove uncompressed version?                          # FIXME add timeout to remove uncompressed version?
293                          unlink_all( $file ) || warn "can't remove $path: $!";                          unlink $path || warn "can't remove $path: $!";
294                  }                  }
295          } else {          } else {
296                  warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";                  warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";

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

  ViewVC Help
Powered by ViewVC 1.1.26