--- fuse-comp.pl 2007/07/10 00:22:00 27 +++ fuse-comp.pl 2007/07/10 01:10:49 28 @@ -45,6 +45,7 @@ sub real_name { my ( $dir, $name ) = @_; if ( -e "$dir/${name}.gz" ) { + confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; return "${name}.gz"; } return $name; @@ -121,6 +122,63 @@ undef $s; } +sub create_tmp_file { + my $file = shift; + + my $path = fixup( $file ); + my $tmp = $mount->{tmp} . '/' . $file; + if ( -e $tmp ) { + $path = $tmp; + } elsif ( $path =~ m/\.gz$/ ) { + my $dest_path = $tmp; + $dest_path =~ s!/[^/]+$!!; #!vim-fix + mkpath $dest_path unless -e $dest_path; + if ( -s $path ) { + file_copy( '<:gzip', $path, '>', $tmp ) + } else { + warn "ERROR: filesystem corruption, $path is zero size\n"; + } + $path = $tmp; + } + warn "## create_temp_file( $file ) => $path [", -s $path, "]\n"; + return $path; +} + +sub compress_path_dest { + my ( $path, $dest ) = @_; + + # cleanup old compressed copy + if ( $dest =~ /\.gz$/ ) { + warn "## remove old $dest\n"; + unlink $dest || confess "can't remove $dest: $!"; + $dest =~ s/\.gz$//; + } + + if ( $path =~ $skip_extensions_regex ) { + warn "$path [",-s $path,"] skipped compression\n"; + file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); + } elsif ( -s $path < $min_compress_size ) { + warn "$path [",-s $path,"] uncompressed, too small\n"; + file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); + } else { + warn "$path [",-s $path,"] compressing\n"; + + my $comp = $dest . '.gz'; + file_copy( '<', $path, '>:gzip', $comp ); + + my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); + + if ( $size_path <= $size_comp ) { + warn ">>> $size_path <= $size_comp leaving uncompressed\n"; + unlink $comp || confess "can't remove: $comp: $!"; + } else { + warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; + # FIXME add timeout to remove uncompressed version? + unlink $path || confess "can't remove $path: $!"; + } + } +} + sub x_open { my ($file) = shift; my ($mode) = shift; @@ -145,24 +203,12 @@ create => $mode && O_CREAT, trunc => $mode && O_TRUNC, }; - my $path = fixup($file); + + my $path = create_tmp_file( $file ); + 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; - if ( -e $tmp ) { - $path = $tmp; - } elsif ( $path =~ m/\.gz$/ ) { - my $dest_path = $tmp; - $dest_path =~ s!/[^/]+$!!; #!vim-fix - mkpath $dest_path unless -e $dest_path; - if ( -s $path ) { - file_copy( '<:gzip', $path, '>', $tmp ) - } else { - warn "ERROR: filesystem corruption, $path is zero size\n"; - } - $path = $tmp; - } + my $fh; if ( sysopen($fh , $path, $mode) ) { close($fh) || confess "can't close $path: $!"; @@ -332,21 +378,24 @@ 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: $!"; + + #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} ); + + my $path; + + if (defined( $pending->{$file} )) { + $pending->{$file}->{write}++; + $path = fixup( $file ); + } else { + $path = create_tmp_file( $file ); + compress_path_dest( $path, fixup( $file ) ); } + my $rv = truncate( $path, $size ) ? 0 : -$! ; warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; - $pending->{$file}->{write}++; - - my $tmp = $mount->{tmp} . '/' . $file; - truncate( $tmp, $size ) if -e $tmp; 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 -$!; } @@ -379,37 +428,8 @@ } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending ); my $dest = fixup( $file ); + compress_path_dest( $path, $dest ); - # cleanup old compressed copy - if ( $dest =~ /\.gz$/ ) { - warn "## remove old $dest\n"; - unlink $dest || confess "can't remove $dest: $!"; - $dest =~ s/\.gz$//; - } - - if ( $file =~ $skip_extensions_regex ) { - warn "release $path [",-s $path,"] skipped compression\n"; - file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); - } elsif ( -s $path < $min_compress_size ) { - warn "release $path [",-s $path,"] uncompressed, too small\n"; - file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); - } else { - warn "release $path [",-s $path,"] compressing\n"; - - my $comp = $dest . '.gz'; - file_copy( '<', $path, '>:gzip', $comp ); - - my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); - - if ( $size_path <= $size_comp ) { - warn ">>> $size_path <= $size_comp leaving uncompressed\n"; - unlink $comp || confess "can't remove: $comp: $!"; - } else { - warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; - # FIXME add timeout to remove uncompressed version? - unlink $path || confess "can't remove $path: $!"; - } - } } else { warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; }