--- fuse-comp.pl 2007/07/09 10:35:02 12 +++ fuse-comp.pl 2007/07/09 16:20:07 20 @@ -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,13 +103,13 @@ sub file_copy { my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; - warn "## file_copy( $s_opt $s_path $d_opt $d_path )\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; while( read( $s, $buff, 65535 ) ) { print $d $buff || confess "can't write into $d_path: $!"; - warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug; + warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug; } close($d) || warn "can't close $d_path: $!"; close($s) || warn "can't close $s_path: $!"; @@ -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,7 +162,7 @@ file_copy( '<:gzip', $path, '>', $tmp ); $path = $tmp; } - warn ">>> open abs path: $path ", -s $path, " bytes\n"; + warn "<<< open abs path: $path [", -e $path ? -s $path : 'new' , "]\n"; return -$! unless sysopen($fh , $path, $mode); close($fh); @@ -202,7 +203,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 +218,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 +265,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, "]\n"; + $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,26 +312,36 @@ } 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, " bytes\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"; - $pending->{$file}->{open}--; - return 0; } - delete( $pending->{$file} ); + $pending->{$file}->{open}--; + if ( $pending->{$file}->{open} == 0 ) { + warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; + delete( $pending->{$file} ); + } return 0; }