--- fuse-comp.pl 2007/07/10 01:10:49 28 +++ fuse-comp.pl 2007/09/02 12:03:52 39 @@ -10,25 +10,29 @@ 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 = { - from => '/tmp/comp', - to => '/tmp/no-comp', - tmp => '/dev/shm/comp', + from => shift @ARGV || '/tmp/comp', + to => shift @ARGV || '/tmp/no-comp', + tmp => shift @ARGV || '/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; +warn "mount $mount->{from} to $mount->{to} using $mount->{tmp} as cache\n"; + +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,10 +49,14 @@ sub real_name { my ( $dir, $name ) = @_; if ( -e "$dir/${name}.gz" ) { - confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; + 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 { @@ -59,6 +67,7 @@ sub original_name { my $p = shift; $p =~ s/\.gz$//; + $p =~ s/\.gz%$/.gz/; # demungle compressed .gz files return $p; }; @@ -122,36 +131,68 @@ undef $s; } -sub create_tmp_file { +sub tmp_path { 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"; + + 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'; } - $path = $tmp; + 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; } - warn "## create_temp_file( $file ) => $path [", -s $path, "]\n"; return $path; } -sub compress_path_dest { - my ( $path, $dest ) = @_; +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 ) { @@ -169,13 +210,21 @@ my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); if ( $size_path <= $size_comp ) { - warn ">>> $size_path <= $size_comp leaving uncompressed\n"; + 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),"%\n"; + 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: $!"; + } } + } } @@ -194,8 +243,6 @@ return 0; } - $pending->{$file}->{open}++; - my $mode_desc = { rdonly => $mode && O_RDONLY, rdwr => $mode && O_RDWR, @@ -204,22 +251,32 @@ trunc => $mode && O_TRUNC, }; - my $path = create_tmp_file( $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; + + 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 { @@ -236,6 +293,7 @@ if(seek($fh,$off,SEEK_SET)) { read($fh,$rv,$bufsize); + $pending->{$file}->{read} += length($rv) if $stats; } return $rv; @@ -252,14 +310,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: $!"; @@ -299,8 +358,8 @@ warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug; my $tmp = $mount->{tmp} . '/' . $from; - if ( -e $tmp ) { - my $tmp_to = $mount->{$tmp} . '/' . $to; + my $tmp_to = $mount->{tmp} . '/' . $to; + if ( $rv == 0 && -e $tmp_to ) { symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!"; } return $rv; @@ -332,7 +391,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 : $!"; @@ -381,17 +440,10 @@ #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 $path = tmp_path( $file ); my $rv = truncate( $path, $size ) ? 0 : -$! ; warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; + compress_file2path( $file, $path ); return $rv; } @@ -414,30 +466,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 ); - compress_path_dest( $path, $dest ); - + } 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; + + my $path = fixup( $file ); + + 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"; + + } + + warn "## cleanup pending $file [", -s $path, "]\n" if $debug; delete( $pending->{$file} ); } + return 0; }