--- fuse-comp.pl 2007/07/09 23:28:58 26 +++ fuse-comp.pl 2007/07/10 10:48:21 30 @@ -16,10 +16,12 @@ my $debug = 0; my $fuse_debug = 0; +my $stats = 1; GetOptions( 'debug+' => \$debug, 'fuse-debug+' => \$fuse_debug, + 'stats!' => \$stats, ); my $mount = { @@ -42,13 +44,18 @@ my $pending; +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; +} + sub fixup { my ( $path ) = @_; - my $full = $mount->{from} . '/' . $path; - if ( -e $full . '.gz' ) { - return $full . '.gz'; - } - return $full; + return $mount->{from} . '/' . real_name( $mount->{from}, $path ); } sub original_name { @@ -117,6 +124,102 @@ undef $s; } +sub tmp_path { + my $file = shift; + + my $path = fixup( $file ); + + my $op = 'UNKNOWN'; + + if (defined( $pending->{$file} )) { + $path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending ); + $op = 'opened'; + } else { + my $tmp = $mount->{tmp} . '/' . $file; + if ( -e $tmp ) { + $path = $tmp; + $op = 'existing'; + } 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 { + confess "ASSERT: filesystem corruption, $path is zero size\n"; + } + $path = $tmp; + $op = 'created'; + } + confess "ASSERT: path shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{path} ); + confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} ); + $pending->{$file}->{path} = $path; + $pending->{$file}->{open} = 0; # not really opened, just uncompressed + warn "## tmp_file( $file ) $op $path [", -s $path, "]\n"; + } + return $path; +} + +sub compress_file2path { + my ( $file, $path ) = @_; + + my $dest = fixup( $file ); + + if ( defined($pending->{$file}) ) { + my $pending_path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending ); + + if ( $pending->{$file}->{open} > 1 ) { + warn "$file used ", $pending->{$file}->{open}, " times, delaying compression\n"; + return; + } elsif ( ! $path ) { + $path = $pending_path; + } elsif ( $pending_path ne $path ) { + confess "ASSERT: compressing into $path instead of $pending_path\n"; + } + } + + confess "need path" unless $path; + + # cleanup old compressed copy + if ( $dest =~ /\.gz$/ ) { + warn "## remove old $dest\n"; + unlink $dest || confess "can't remove $dest: $!"; + $dest =~ s/\.gz$//; + confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest; + } + + 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),"% $comp\n"; + + # FIXME add timeout to remove uncompressed version? + unlink $path || confess "can't remove $path: $!"; + + if ( -e $dest ) { + warn "## cleanup uncompressed $dest\n" if $debug; + unlink $dest || confess "can't remove $dest: $!"; + } + } + + } +} + sub x_open { my ($file) = shift; my ($mode) = shift; @@ -132,8 +235,6 @@ return 0; } - $pending->{$file}->{open}++; - my $mode_desc = { rdonly => $mode && O_RDONLY, rdwr => $mode && O_RDWR, @@ -141,29 +242,17 @@ create => $mode && O_CREAT, trunc => $mode && O_TRUNC, }; - my $path = fixup($file); + + my $path = tmp_path( $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: $!"; - warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n"; - $pending->{$file}->{path} = $path; + warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; + $pending->{$file}->{open}++; return 0; } else { warn "ERROR: can't open $path -- $!"; @@ -186,6 +275,7 @@ if(seek($fh,$off,SEEK_SET)) { read($fh,$rv,$bufsize); + $pending->{$file}->{read} += length($rv) if $stats; } return $rv; @@ -202,14 +292,15 @@ return -ENOENT() unless -e $path; $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending ); - confess "write into non-existant $path for $file: $!" unless -e $path; + confess "write into non-existant $path for $file" unless -e $path; my $fh = new IO::File; return -ENOSYS() unless open($fh,'+<',$path); if($rv = seek( $fh ,$off,SEEK_SET)) { $rv = print( $fh $buf ); - warn "## write $path offset $off [",length( $buf ), "]\n" if $debug; - $pending->{$file}->{write}++; + my $size = length($buf); + warn "## write $path offset $off [$size]\n" if $debug; + $pending->{$file}->{write} += $size; } $rv = -ENOSYS() unless $rv; close($fh) || warn "can't close $path: $!"; @@ -218,7 +309,8 @@ sub err { return (-shift || -$!) } -sub x_readlink { return readlink(fixup(shift)); } +sub x_readlink { return readlink(fixup(shift)); } + sub x_unlink { my $file = shift; my $path = fixup( $file ); @@ -238,7 +330,36 @@ return 0; } -sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } +sub x_symlink { + my ($from,$to) = @_; + + my $from_path = $from; #fixup( $from ); + my $to_path = fixup( $to ); + + my $rv = symlink( $from_path, $to_path ) ? 0 : -$!; + warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug; + + my $tmp = $mount->{tmp} . '/' . $from; + if ( -e $tmp ) { + my $tmp_to = $mount->{$tmp} . '/' . $to; + symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!"; + } + return $rv; +} + +sub x_link { + my ($from,$to) = @_; + + my $from_path = fixup($from); + my $to_path = fixup($to); + $to_path .= '.gz' if ( $from_path =~ m/\.gz$/ && $to_path !~ m/\.gz$/ ); + + my $rv = link( $from_path, $to_path ) ? 0 : -$!; + + warn "# link( $from_path -> $to_path ) = $rv\n" if $debug; + + return $rv; +} sub x_rename { my ($old,$new) = @_; @@ -272,7 +393,6 @@ return $err; } -sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } sub x_chown { my ($file,$uid,$gid) = @_; @@ -299,17 +419,17 @@ sub x_truncate { my ( $file,$size ) = @_; - my $path = fixup($file); + + #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} ); + + my $path = tmp_path( $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}++; + compress_file2path( $file, $path ); + 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 -$!; } @@ -328,59 +448,23 @@ 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; } elsif ( ! defined( $pending->{$file}->{write} ) ) { warn "release $file, not written into\n"; - } 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 ); - - # 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 $file [",-s $path,"] skipped compression\n"; - file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); - } elsif ( -s $path < $min_compress_size ) { - warn "release $file [",-s $path,"] uncompressed, too small\n"; - file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); - } else { - warn "release $file [",-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 || 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: $!"; - } - } + } elsif ( $file =~ m#\Q/.fuse_hidden\E# ) { + warn "release internal $file\n" if $debug; } else { - warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; + compress_file2path( $file ); } + $pending->{$file}->{open}--; if ( $pending->{$file}->{open} == 0 ) { warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; delete( $pending->{$file} ); } + return 0; }