--- fuse-comp.pl 2007/07/09 11:12:48 13 +++ fuse-comp.pl 2007/07/09 17:35:26 21 @@ -19,7 +19,7 @@ tmp => '/dev/shm/comp', }; -my $debug = 1; +my $debug = shift @ARGV; my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i; @@ -103,7 +103,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; @@ -146,11 +146,12 @@ rdwr => $mode && O_RDWR, append => $mode && O_APPEND, create => $mode && O_CREAT, + trunc => $mode && O_TRUNC, }; - warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n"; + my $path = fixup($file); + warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug; my $fh; - my $path = fixup($file); my $tmp = $mount->{tmp} . '/' . $file; if ( -e $tmp ) { $path = $tmp; @@ -161,12 +162,17 @@ file_copy( '<:gzip', $path, '>', $tmp ); $path = $tmp; } - warn ">>> open abs path: $path [", -s $path, "]\n"; - return -$! unless sysopen($fh , $path, $mode); - 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 { @@ -202,7 +208,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); @@ -217,9 +223,30 @@ 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; + + delete( $pending->{$old} ); + } + return $err; } sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } @@ -243,7 +270,19 @@ return $err; } -sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; } +sub x_truncate { + my ( $file,$size ) = @_; + my $path = fixup($file); + my $rv = truncate( $path, $size ) ? 0 : -$! ; + if ( $path =~ m/\.gz$/ ) { + my $no_gz = $path; + $no_gz =~ s/\.gz$//; + rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; + } + 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:-$!; } sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; } @@ -278,19 +317,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? - 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";