/[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 28 by dpavlin, Tue Jul 10 01:10:49 2007 UTC revision 39 by dpavlin, Sun Sep 2 12:03:52 2007 UTC
# Line 10  require 'syscall.ph'; # for SYS_mknod an Line 10  require 'syscall.ph'; # for SYS_mknod an
10  use PerlIO::gzip;  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 cluck/;
14  use IO::File;  use IO::File;
15  use Getopt::Long;  use Getopt::Long;
16    
17  my $debug = 0;  my $debug = 0;
18  my $fuse_debug = 0;  my $fuse_debug = 0;
19    my $stats = 1;
20    
21  GetOptions(  GetOptions(
22          'debug+' => \$debug,          'debug+' => \$debug,
23          'fuse-debug+' => \$fuse_debug,          'fuse-debug+' => \$fuse_debug,
24            'stats!' => \$stats,
25  );  );
26    
27  my $mount = {  my $mount = {
28          from    => '/tmp/comp',          from    => shift @ARGV || '/tmp/comp',
29          to              => '/tmp/no-comp',          to              => shift @ARGV || '/tmp/no-comp',
30          tmp             => '/dev/shm/comp',          tmp             => shift @ARGV || '/dev/shm/comp',
31  };  };
32    
33  my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i;  warn "mount $mount->{from} to $mount->{to} using $mount->{tmp} as cache\n";
34    
35    my $skip_extensions_regex = qr/gz|gz%|\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|tgz|avi|mpeg|mpg|tmp|temp)$/i;
36    
37  # don't compress files smaller than this  # don't compress files smaller than this
38  my $min_compress_size = 512;  my $min_compress_size = 512;
# Line 45  my $pending; Line 49  my $pending;
49  sub real_name {  sub real_name {
50          my ( $dir, $name ) = @_;          my ( $dir, $name ) = @_;
51          if ( -e "$dir/${name}.gz" ) {          if ( -e "$dir/${name}.gz" ) {
52                  confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name";                  cluck "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name";
53                  return "${name}.gz";                  return "${name}.gz";
54          }          }
55          return $name;          if ( $name =~ m/\.gz$/ ) {
56                    return $name . '%';     # protect (mingle) compressed files
57            } else {
58                    return $name;
59            }
60  }  }
61    
62  sub fixup {  sub fixup {
# Line 59  sub fixup { Line 67  sub fixup {
67  sub original_name {  sub original_name {
68          my $p = shift;          my $p = shift;
69          $p =~ s/\.gz$//;          $p =~ s/\.gz$//;
70            $p =~ s/\.gz%$/.gz/;    # demungle compressed .gz files
71          return $p;          return $p;
72  };  };
73    
# Line 122  sub file_copy { Line 131  sub file_copy {
131          undef $s;          undef $s;
132  }  }
133    
134  sub create_tmp_file {  sub tmp_path {
135          my $file = shift;          my $file = shift;
136    
137          my $path = fixup( $file );          my $path = fixup( $file );
138          my $tmp = $mount->{tmp} . '/' . $file;  
139          if ( -e $tmp ) {          my $op = 'UNKNOWN';
140                  $path = $tmp;  
141          } elsif ( $path =~ m/\.gz$/ ) {          if (defined( $pending->{$file} )) {
142                  my $dest_path = $tmp;                  $path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending );
143                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix                  $op = 'opened';
144                  mkpath $dest_path unless -e $dest_path;          } else {
145                  if ( -s $path ) {                  my $tmp = $mount->{tmp} . '/' . $file;
146                          file_copy( '<:gzip', $path, '>', $tmp )                  if ( -e $tmp ) {
147                  } else {                          $path = $tmp;
148                          warn "ERROR: filesystem corruption, $path is zero size\n";                          $op = 'existing';
149                    } elsif ( $path =~ m/\.gz$/ ) {
150                            my $dest_path = $tmp;
151                            $dest_path =~ s!/[^/]+$!!;      #!vim-fix
152                            mkpath $dest_path unless -e $dest_path;
153                            if ( -s $path ) {
154                                    file_copy( '<:gzip', $path, '>', $tmp )
155                            } else {
156                                    confess "ASSERT: filesystem corruption, $path is zero size in ",dump( $pending );
157                            }
158                            $path = $tmp;
159                            $op = 'created';
160                  }                  }
161                  $path = $tmp;                  confess "ASSERT: path shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{path} );
162                    confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} );
163                    $pending->{$file}->{path} = $path;
164                    $pending->{$file}->{open} = 0;  # not really opened, just uncompressed
165                    warn "## tmp_file( $file ) $op $path [", -s $path, "]\n" if $debug;
166          }          }
         warn "## create_temp_file( $file ) => $path [", -s $path, "]\n";  
167          return $path;          return $path;
168  }  }
169    
170  sub compress_path_dest {  sub compress_file2path {
171          my ( $path, $dest ) = @_;          my ( $file, $path ) = @_;
172    
173            my $dest = fixup( $file );
174    
175            if ( defined($pending->{$file}) ) {
176                    my $pending_path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending );
177    
178                    if ( $pending->{$file}->{open} > 1 ) {
179                            warn "$file used ", $pending->{$file}->{open}, " times, delaying compression\n";
180                            return;
181                    } elsif ( ! $path ) {
182                            $path = $pending_path;
183                    } elsif ( $pending_path ne $path ) {
184                            confess "ASSERT: compressing into $path instead of $pending_path";
185                    }
186            }
187    
188            confess "need path" unless $path;
189    
190          # cleanup old compressed copy          # cleanup old compressed copy
191          if ( $dest =~ /\.gz$/ ) {          if ( $dest =~ /\.gz$/ ) {
192                  warn "## remove old $dest\n";                  warn "## remove old $dest\n";
193                  unlink $dest || confess "can't remove $dest: $!";                  unlink $dest || confess "can't remove $dest: $!";
194                  $dest =~ s/\.gz$//;                  $dest =~ s/\.gz$//;
195                    confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest;
196          }          }
197    
198          if ( $path =~ $skip_extensions_regex ) {          if ( $path =~ $skip_extensions_regex ) {
# Line 169  sub compress_path_dest { Line 210  sub compress_path_dest {
210                  my ( $size_path, $size_comp ) = ( -s $path, -s $comp );                  my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
211    
212                  if ( $size_path <= $size_comp ) {                  if ( $size_path <= $size_comp ) {
213                          warn ">>> $size_path <= $size_comp leaving uncompressed\n";                          warn ">>> $size_path <= $size_comp leaving uncompressed $dest\n";
214                          unlink $comp || confess "can't remove: $comp: $!";                          unlink $comp || confess "can't remove: $comp: $!";
215                            file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
216                  } else {                  } else {
217                          warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";                          warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n";
218    
219                          # FIXME add timeout to remove uncompressed version?                          # FIXME add timeout to remove uncompressed version?
220                          unlink $path || confess "can't remove $path: $!";                          unlink $path || confess "can't remove $path: $!";
221    
222                            if ( -e $dest ) {
223                                    warn "## cleanup uncompressed $dest\n" if $debug;
224                                    unlink $dest || confess "can't remove $dest: $!";
225                            }
226                  }                  }
227    
228          }          }
229  }  }
230    
# Line 194  sub x_open { Line 243  sub x_open {
243                  return 0;                  return 0;
244          }          }
245    
         $pending->{$file}->{open}++;  
   
246          my $mode_desc = {          my $mode_desc = {
247                  rdonly => $mode && O_RDONLY,                  rdonly => $mode && O_RDONLY,
248                  rdwr => $mode && O_RDWR,                  rdwr => $mode && O_RDWR,
# Line 204  sub x_open { Line 251  sub x_open {
251                  trunc => $mode && O_TRUNC,                  trunc => $mode && O_TRUNC,
252          };          };
253    
254          my $path = create_tmp_file( $file );          my $path = tmp_path( $file );
255    
256          warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;          warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
257    
258          my $fh;          my $fh;
259            my $rv = 0;
260    
261            if ( ! -w $path ) {
262                    my $old_mode = (stat $path)[2];
263                    my $new_mode = $old_mode | 0600;
264                    chmod $new_mode, $path || confess "can't chmod $new_mode $path";
265                    warn "### modify mode $old_mode -> $new_mode for $path\n";
266                    $pending->{$file}->{mode} = $old_mode;
267            }
268    
269          if ( sysopen($fh , $path, $mode) ) {          if ( sysopen($fh , $path, $mode) ) {
270                  close($fh) || confess "can't close $path: $!";                  close($fh) || confess "can't close $path: $!";
271                  warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n";                  warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n";
272                  $pending->{$file}->{path} = $path;                  $pending->{$file}->{open}++;
                 return 0;  
273          } else {          } else {
274                  warn "ERROR: can't open $path -- $!";                  warn "ERROR: can't open $path -- $!";
275                  return -$!;                  $rv = -$!;
276          }          }
277    
278            return $rv;
279    
280  }  }
281    
282  sub x_read {  sub x_read {
# Line 236  sub x_read { Line 293  sub x_read {
293    
294          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
295                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
296                    $pending->{$file}->{read} += length($rv) if $stats;
297          }          }
298    
299          return $rv;          return $rv;
# Line 252  sub x_write { Line 310  sub x_write {
310          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
311    
312          $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );          $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
313          confess "write into non-existant $path for $file: $!" unless -e $path;          confess "write into non-existant $path for $file" unless -e $path;
314    
315          my $fh = new IO::File;          my $fh = new IO::File;
316          return -ENOSYS() unless open($fh,'+<',$path);          return -ENOSYS() unless open($fh,'+<',$path);
317          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
318                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
319                  warn "## write $path offset $off [",length( $buf ), "]\n" if $debug;                  my $size = length($buf);
320                  $pending->{$file}->{write}++;                  warn "## write $path offset $off [$size]\n" if $debug;
321                    $pending->{$file}->{write} += $size;
322          }          }
323          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
324          close($fh) || warn "can't close $path: $!";          close($fh) || warn "can't close $path: $!";
# Line 299  sub x_symlink { Line 358  sub x_symlink {
358          warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug;          warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug;
359    
360          my $tmp = $mount->{tmp} . '/' . $from;          my $tmp = $mount->{tmp} . '/' . $from;
361          if ( -e $tmp ) {          my $tmp_to = $mount->{tmp} . '/' . $to;
362                  my $tmp_to = $mount->{$tmp} . '/' . $to;          if ( $rv == 0 && -e $tmp_to ) {
363                  symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!";                  symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!";
364          }          }
365          return $rv;          return $rv;
# Line 332  sub x_rename { Line 391  sub x_rename {
391          my $tmp = $mount->{tmp} . '/' . $old;          my $tmp = $mount->{tmp} . '/' . $old;
392          if ( -e $tmp ) {          if ( -e $tmp ) {
393                  if ( $new =~ m#\Q/.fuse_hidden\E# ) {                  if ( $new =~ m#\Q/.fuse_hidden\E# ) {
394                          unlink $tmp || confess "can't unlink $tmp for $new\n";                          unlink $tmp || confess "can't unlink $tmp for $new";
395                  } else {                  } else {
396                          my $new_tmp = $mount->{tmp} . '/' . $new;                          my $new_tmp = $mount->{tmp} . '/' . $new;
397                          rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";                          rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
# Line 381  sub x_truncate { Line 440  sub x_truncate {
440    
441          #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} );          #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} );
442    
443          my $path;          my $path = tmp_path( $file );
   
         if (defined( $pending->{$file} )) {  
                 $pending->{$file}->{write}++;  
                 $path = fixup( $file );  
         } else {  
                 $path = create_tmp_file( $file );  
                 compress_path_dest( $path, fixup( $file ) );  
         }  
444          my $rv = truncate( $path, $size ) ? 0 : -$! ;          my $rv = truncate( $path, $size ) ? 0 : -$! ;
445          warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;          warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
446            compress_file2path( $file, $path );
447    
448          return $rv;          return $rv;
449  }  }
# Line 414  sub x_mknod { Line 466  sub x_mknod {
466  sub x_release {  sub x_release {
467          my ( $file, $mode ) = @_;          my ( $file, $mode ) = @_;
468    
         if ( $file =~ m#\Q/.fuse_hidden\E# ) {  
                 warn "release internal $file\n" if $debug;  
                 delete( $pending->{$file} );  
                 return 0;  
         }  
   
469          if ( ! defined( $pending->{$file} ) ) {          if ( ! defined( $pending->{$file} ) ) {
470                  warn "release $file, NO PENDING DATA\n";                  warn "release $file, NO PENDING DATA\n";
471                  return 0;                  return 0;
472          } elsif ( ! defined( $pending->{$file}->{write} ) ) {          } elsif ( ! defined( $pending->{$file}->{write} ) ) {
473                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
474          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {          } elsif ( $file =~ m#\Q/.fuse_hidden\E# ) {
475                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );                  warn "release internal $file\n" if $debug;
                 my $dest = fixup( $file );  
                 compress_path_dest( $path, $dest );  
   
476          } else {          } else {
477                  warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";                  compress_file2path( $file );
478          }          }
479    
480          $pending->{$file}->{open}--;          $pending->{$file}->{open}--;
481          if ( $pending->{$file}->{open} == 0 ) {          if ( $pending->{$file}->{open} == 0 ) {
482                  warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;  
483                    my $path = fixup( $file );
484    
485                    if ( my $old_mode = $pending->{$file}->{mode} ) {
486                            chmod $old_mode, $path || confess "can't chmod $old_mode $path";
487                            warn "### restored mode $old_mode $path\n";
488    
489                    }
490    
491                    warn "## cleanup pending $file [", -s $path, "]\n" if $debug;
492                  delete( $pending->{$file} );                  delete( $pending->{$file} );
493          }          }
494    
495          return 0;          return 0;
496  }  }
497    

Legend:
Removed from v.28  
changed lines
  Added in v.39

  ViewVC Help
Powered by ViewVC 1.1.26