/[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 1 by dpavlin, Sun Jul 8 12:51:33 2007 UTC revision 5 by dpavlin, Sun Jul 8 15:04:35 2007 UTC
# Line 11  require 'syscall.ph'; # for SYS_mknod an Line 11  require 'syscall.ph'; # for SYS_mknod an
11  use PerlIO::gzip;  use PerlIO::gzip;
12  use File::Path;  use File::Path;
13  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
14    use Carp qw/confess/;
15    
16  my $mount = {  my $mount = {
17          from    => '/tmp/comp',          from    => '/tmp/comp',
# Line 20  my $mount = { Line 21  my $mount = {
21    
22  my $debug = 1;  my $debug = 1;
23    
24  my $skip_extensions_regex = qr/\.(sw[px]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg)$/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
27    my $min_compress_size = 256;
28    
29  foreach my $dir ( keys %$mount ) {  foreach my $dir ( keys %$mount ) {
30          if ( ! -e $mount->{$dir} ) {          if ( ! -e $mount->{$dir} ) {
# Line 65  sub gzip_original_size { Line 69  sub gzip_original_size {
69          return unpack("L", $buff);          return unpack("L", $buff);
70  }  }
71    
72    sub unlink_all {
73            my $file = shift;
74            foreach my $dir ( keys %$mount ) {
75                    my $path = $mount->{$dir} . '/' . $file;
76            
77                    map {
78                            my $path = $_;
79                            if ( -e $path ) {
80                                    if ( unlink $path ) {
81                                            warn "## unlink $path\n" if $debug;
82                                    } else {
83                                            warn "can't unlink $path: $!\n";
84                                            return 0;
85                                    }
86                            }
87                    } [ $path, $path . '.gz' ];
88            }
89            delete( $pending->{$file} );
90            return 1;
91    }
92    
93  sub x_getattr {  sub x_getattr {
94          my ($file) = fixup(shift);          my ($file) = fixup(shift);
95          my (@list) = lstat($file);          my (@list) = lstat($file);
# Line 86  sub x_getdir { Line 111  sub x_getdir {
111  sub file_copy {  sub file_copy {
112          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
113          warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n";          warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n";
114          open(my $s, $s_opt, $s_path ) || die "can't open $s_path: $!";          open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending );
115          open(my $d, $d_opt, $d_path ) || die "can't open $d_path: $!";          open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!";
116          my $buff;          my $buff;
117          while( read( $s, $buff, 65535 ) ) {          while( read( $s, $buff, 65535 ) ) {
118                  print $d $buff || die "can't write into $d_path: $!";                  print $d $buff || confess "can't write into $d_path: $!";
119                  warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;                  warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;
120          }          }
121          close($d) || warn "can't close $d_path: $!";          close($d) || warn "can't close $d_path: $!";
122          close($s) || warn "can't close $s_path: $!";          close($s) || warn "can't close $s_path: $!";
123            warn "-- $s_path [", -s $s_path, "] >>> $d_path [", -s $d_path, "]\n" if $debug;
124            my ($mode,$uid,$gid,$atime,$mtime) = (stat $s_path)[2,4,5,8,9];
125    
126            chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";
127            chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";
128            utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
129  }  }
130    
131  sub x_open {  sub x_open {
132          my ($file) = shift;          my ($file) = shift;
133          my ($mode) = shift;          my ($mode) = shift;
134          $pending->{$file}->{open}++;          $pending->{$file}->{open}++;
135            warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, "\n";
136          my $fh;          my $fh;
137          if ( $pending->{$file}->{open} == 1 ) {          if ( $pending->{$file}->{open} == 1 ) {
                 warn "# open( $file, $mode )\n";  
138                  my $path = fixup($file);                  my $path = fixup($file);
139                  my $tmp = $mount->{tmp} . '/' . $file;                  my $tmp = $mount->{tmp} . '/' . $file;
140                    warn ">>> open abs path: $path\n";
141                  if ( -e $tmp ) {                  if ( -e $tmp ) {
142                          $path = $tmp;                          $path = $tmp;
143                  } elsif ( $path =~ m/\.gz$/ ) {                  } elsif ( $path =~ m/\.gz$/ ) {
# Line 119  sub x_open { Line 151  sub x_open {
151                  $pending->{$file}->{fh} = $fh;                  $pending->{$file}->{fh} = $fh;
152                  $pending->{$file}->{path} = $path;                  $pending->{$file}->{path} = $path;
153          } elsif ( ! defined( $pending->{$file}->{fh} ) ) {          } elsif ( ! defined( $pending->{$file}->{fh} ) ) {
154                  die "can't find fh for $file ", dump($pending);                  confess "can't find fh for $file ", dump($pending);
155          }          };
156          return 0;          return 0;
157  }  }
158    
# Line 130  sub x_read { Line 162  sub x_read {
162          my $path = fixup( $file );          my $path = fixup( $file );
163          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
164          my ($fsize) = -s $path;          my ($fsize) = -s $path;
165          my $fh = $pending->{$file}->{fh} || die "no fh? ", dump( $pending );          my $fh = $pending->{$file}->{fh} || confess "no fh? ", dump( $pending );
166          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
167                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
168          }          }
# Line 156  sub x_write { Line 188  sub x_write {
188  sub err { return (-shift || -$!) }  sub err { return (-shift || -$!) }
189    
190  sub x_readlink { return readlink(fixup(shift));         }  sub x_readlink { return readlink(fixup(shift));         }
191  sub x_unlink   { return unlink(fixup(shift)) ? 0 : -$!; }  sub x_unlink   { return unlink_all( shift ) ? 0 : -$! }
192    
193  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
194    
# Line 212  sub x_release { Line 244  sub x_release {
244                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
245          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
246                  close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";                  close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";
247                    my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
248                    my $dest = fixup( $file );
249    
250                    # cleanup old compressed copy
251                    if ( $dest =~ /\.gz$/ ) {
252                            warn "## remove old $dest\n";
253                            unlink_all( $file ) || confess "can't remove $dest: $!";
254                            $dest =~ s/\.gz$//;
255                    }
256    
257                  if ( $file =~ $skip_extensions_regex ) {                  if ( $file =~ $skip_extensions_regex ) {
258                          warn "release $file $mode -- uncompressed\n";                          warn "release $file $mode -- uncompressed\n";
259                            file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
260                    } elsif ( -s $path < $min_compress_size ) {
261                            warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";
262                            file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
263                  } else {                  } else {
264                          warn "release $file $mode -- compressing\n";                          warn "release $file $mode -- compressing\n";
265    
                         my $path = $pending->{$file}->{path} || die "no path for $file ? ", dump( $pending );  
                         my $dest = fixup( $file );  
   
                         if ( $dest =~ /\.gz$/ ) {  
                                 warn "## remove old $dest\n";  
                                 unlink $dest || die "can't remove $dest: $!";  
                                 $dest =~ s/\.gz$//;  
                         }  
266    
267                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );
268    
269                          # FIXME add timeout to remove uncompressed version?                          # FIXME add timeout to remove uncompressed version?
270                          unlink $path || warn "can't remove $path: $!";                          unlink_all( $file ) || warn "can't remove $path: $!";
271                  }                  }
272          } else {          } else {
273                  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.1  
changed lines
  Added in v.5

  ViewVC Help
Powered by ViewVC 1.1.26