--- fuse-comp.pl 2007/07/09 13:45:53 17 +++ fuse-comp.pl 2007/07/09 21:57:11 24 @@ -12,6 +12,15 @@ use Data::Dump qw/dump/; use Carp qw/confess/; use IO::File; +use Getopt::Long; + +my $debug = 0; +my $fuse_debug = 0; + +GetOptions( + 'debug+' => \$debug, + 'fuse-debug+' => \$fuse_debug, +); my $mount = { from => '/tmp/comp', @@ -19,8 +28,6 @@ tmp => '/dev/shm/comp', }; -my $debug = 1; - my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i; # don't compress files smaller than this @@ -103,7 +110,7 @@ sub file_copy { my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; - warn "## file_copy( $s_opt $s_path [",-s $s_path,"] $d_opt $d_path [",-e $d_path ? -s $d_path : 'new',"])\n"; + warn "## file_copy( $s_opt $s_path [",-s $s_path,"] $d_opt $d_path [",-e $d_path ? -s $d_path : 'new',"])\n" if $debug; open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending ); open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!"; my $buff; @@ -149,7 +156,7 @@ trunc => $mode && O_TRUNC, }; my $path = fixup($file); - warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n"; + warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug; my $fh; my $tmp = $mount->{tmp} . '/' . $file; @@ -159,16 +166,24 @@ my $dest_path = $tmp; $dest_path =~ s!/[^/]+$!!; #!vim-fix mkpath $dest_path unless -e $dest_path; - file_copy( '<:gzip', $path, '>', $tmp ); + if ( -s $path ) { + file_copy( '<:gzip', $path, '>', $tmp ) + } else { + warn "ERROR: filesystem corruption, $path is zero size\n"; + } $path = $tmp; } - warn ">>> open abs path: $path [", -e $path ? -s $path : 'new' , "]\n"; - return -$! unless sysopen($fh , $path, $mode); - warn ">>> after open [",-s $path, "]\n"; - close($fh); - $pending->{$file}->{path} = $path; - return 0; + if ( sysopen($fh , $path, $mode) ) { + close($fh) || confess "can't close $path: $!"; + warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n"; + $pending->{$file}->{path} = $path; + return 0; + } else { + warn "ERROR: can't open $path : $!"; + return -$!; + } + } sub x_read { @@ -219,9 +234,31 @@ sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } sub x_rename { - my ($old) = fixup(shift); - my ($new) = fixup(shift); - my ($err) = rename($old,$new) ? 0 : -ENOENT(); + my ($old,$new) = @_; + my $old_path = fixup($old); + my $new_path = fixup($new); + $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ ); + + my $err = rename($old_path,$new_path) ? 0 : -ENOENT(); + warn "## rename( $old_path => $new_path ) = $err\n"; + + my $tmp = $mount->{tmp} . '/' . $old; + if ( -e $tmp ) { + my $new_tmp = $mount->{tmp} . '/' . $new; + rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; + } + + if (defined( $pending->{$old} )) { + $pending->{$new} = $pending->{$old}; + + my $path = $pending->{$old}->{path}; + $path =~ s/\Q$old\E/$new/; + $pending->{$new}->{path} = $path; + $pending->{$old}->{path} = $path; + + #delete( $pending->{$old} ); + } + return $err; } sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } @@ -254,7 +291,7 @@ $no_gz =~ s/\.gz$//; rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; } - warn "## truncate( $file $size ) $path [", -s $path, "]\n"; + warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; $pending->{$file}->{write}++; return $rv; } @@ -292,20 +329,27 @@ } if ( $file =~ $skip_extensions_regex ) { - warn "release $file $mode -- uncompressed\n"; + warn "release $file [",-s $path,"] skipped compression\n"; file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); } elsif ( -s $path < $min_compress_size ) { - warn "release $file -- uncompressed, too small [", -s $path, "]\n"; + warn "release $file [",-s $path,"] uncompressed, too small\n"; file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); } else { - warn "release $file $mode -- compressing\n"; + warn "release $file [",-s $path,"] compressing\n"; + my $comp = $dest . '.gz'; + file_copy( '<', $path, '>:gzip', $comp ); - file_copy( '<', $path, '>:gzip', $dest . '.gz' ); + my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); - # FIXME add timeout to remove uncompressed version? - # FIXME leave uncompressed file if smaller than compressed - unlink $path || warn "can't remove $path: $!"; + if ( $size_path <= $size_comp ) { + warn ">>> $size_path <= $size_comp leaving uncompressed\n"; + unlink $comp || warn "can't reamove: $comp: $!"; + } else { + warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; + # FIXME add timeout to remove uncompressed version? + unlink $path || warn "can't remove $path: $!"; + } } } else { warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; @@ -342,5 +386,5 @@ statfs =>"main::x_statfs", release =>"main::x_release", # threaded=>1, -# debug => 1, + debug => $fuse_debug, );