--- fuse-comp.pl 2007/07/09 13:04:25 14 +++ fuse-comp.pl 2007/07/09 22:44:20 25 @@ -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 @@ -69,20 +76,6 @@ return unpack("L", $buff); } -sub unlink_all { - my $file = shift; - warn "# unlink_all( $file )\n"; - - my $path = fixup( $file ); - unlink $path || return 0; - - my $tmp = $mount->{tmp} . '/' . $file; - unlink $tmp if ( -e $tmp ); - - delete( $pending->{$file} ); - return 1; -} - sub x_getattr { my ($file) = fixup(shift); my (@list) = lstat($file); @@ -103,7 +96,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 +142,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 +152,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 { @@ -192,6 +193,7 @@ sub x_write { my ($file,$buf,$off) = @_; + $pending->{$file}->{write}++; my $rv; my $path = fixup($file); @@ -204,7 +206,7 @@ return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path}); if($rv = seek( $fh ,$off,SEEK_SET)) { $rv = print( $fh $buf ); - warn "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; + warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; } $rv = -ENOSYS() unless $rv; close($fh); @@ -214,14 +216,57 @@ sub err { return (-shift || -$!) } sub x_readlink { return readlink(fixup(shift)); } -sub x_unlink { return unlink_all( shift ) ? 0 : -$! } +sub x_unlink { + my $file = shift; + my $path = fixup( $file ); + + if ( $file =~ m#\Q/.fuse_hidden\E# ) { + return unlink $path ? 0 : -$1; + } + + warn "# unlink( $file )\n"; + + unlink $path || return 0; + + my $tmp = $mount->{tmp} . '/' . $file; + unlink $tmp if ( -e $tmp ); + + delete( $pending->{$file} ); + return 0; +} 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 ) { + if ( $new =~ m#\Q/.fuse_hidden\E# ) { + unlink $tmp || confess "can't unlink $tmp for $new\n"; + } else { + 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; + + delete( $pending->{$old} ); + warn "## tweaking pending to ", dump( $pending ) if $debug; + } + return $err; } sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } @@ -254,7 +299,8 @@ $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; } sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } @@ -274,6 +320,13 @@ sub x_release { my ( $file, $mode ) = @_; + + if ( $file =~ m#\Q/.fuse_hidden\E# ) { + warn "release internal $file\n" if $debug; + delete( $pending->{$file} ); + return 0; + } + if ( ! defined( $pending->{$file} ) ) { warn "release $file, NO PENDING DATA\n"; return 0; @@ -291,20 +344,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"; @@ -341,5 +401,5 @@ statfs =>"main::x_statfs", release =>"main::x_release", # threaded=>1, -# debug => 1, + debug => $fuse_debug, );