--- fuse-comp.pl 2007/07/10 00:22:00 27 +++ fuse-comp.pl 2007/07/16 08:34:47 36 @@ -10,16 +10,18 @@ use PerlIO::gzip; use File::Path; use Data::Dump qw/dump/; -use Carp qw/confess/; +use Carp qw/confess cluck/; use IO::File; use Getopt::Long; my $debug = 0; my $fuse_debug = 0; +my $stats = 1; GetOptions( 'debug+' => \$debug, 'fuse-debug+' => \$fuse_debug, + 'stats!' => \$stats, ); my $mount = { @@ -28,7 +30,7 @@ tmp => '/dev/shm/comp', }; -my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i; +my $skip_extensions_regex = qr/gz|gz%|\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|tgz|avi|mpeg|mpg|tmp|temp)$/i; # don't compress files smaller than this my $min_compress_size = 512; @@ -45,9 +47,14 @@ sub real_name { my ( $dir, $name ) = @_; if ( -e "$dir/${name}.gz" ) { + cluck "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; return "${name}.gz"; } - return $name; + if ( $name =~ m/\.gz$/ ) { + return $name . '%'; # protect (mingle) compressed files + } else { + return $name; + } } sub fixup { @@ -58,6 +65,7 @@ sub original_name { my $p = shift; $p =~ s/\.gz$//; + $p =~ s/\.gz%$/.gz/; # demungle compressed .gz files return $p; }; @@ -121,6 +129,103 @@ 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 in ",dump( $pending ); + } + $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" if $debug; + } + 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"; + } + } + + 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 $dest\n"; + unlink $comp || confess "can't remove: $comp: $!"; + file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); + } 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; @@ -136,8 +241,6 @@ return 0; } - $pending->{$file}->{open}++; - my $mode_desc = { rdonly => $mode && O_RDONLY, rdwr => $mode && O_RDWR, @@ -145,35 +248,33 @@ 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 $rv = 0; - 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; + if ( ! -w $path ) { + my $old_mode = (stat $path)[2]; + my $new_mode = $old_mode | 0600; + chmod $new_mode, $path || confess "can't chmod $new_mode $path"; + warn "### modify mode $old_mode -> $new_mode for $path\n"; + $pending->{$file}->{mode} = $old_mode; } 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; + warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; + $pending->{$file}->{open}++; } else { warn "ERROR: can't open $path -- $!"; - return -$!; + $rv = -$!; } + return $rv; + } sub x_read { @@ -190,6 +291,7 @@ if(seek($fh,$off,SEEK_SET)) { read($fh,$rv,$bufsize); + $pending->{$file}->{read} += length($rv) if $stats; } return $rv; @@ -206,14 +308,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: $!"; @@ -286,7 +389,7 @@ 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"; + unlink $tmp || confess "can't unlink $tmp for $new"; } else { my $new_tmp = $mount->{tmp} . '/' . $new; rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; @@ -332,21 +435,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}++; - - my $tmp = $mount->{tmp} . '/' . $file; - truncate( $tmp, $size ) if -e $tmp; + 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 -$!; } @@ -365,59 +464,32 @@ 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$//; - } + } elsif ( $file =~ m#\Q/.fuse_hidden\E# ) { + warn "release internal $file\n" if $debug; + } else { + compress_file2path( $file ); + } - 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"; + $pending->{$file}->{open}--; + if ( $pending->{$file}->{open} == 0 ) { - my $comp = $dest . '.gz'; - file_copy( '<', $path, '>:gzip', $comp ); + my $path = fixup( $file ); - my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); + if ( my $old_mode = $pending->{$file}->{mode} ) { + chmod $old_mode, $path || confess "can't chmod $old_mode $path"; + warn "### restored mode $old_mode $path\n"; - 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"; - } - $pending->{$file}->{open}--; - if ( $pending->{$file}->{open} == 0 ) { - warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; + + warn "## cleanup pending $file [", -s $path, "]\n" if $debug; delete( $pending->{$file} ); } + return 0; }