--- fuse-comp.pl 2007/07/16 07:41:55 33 +++ fuse-comp.pl 2007/07/16 08:34:47 36 @@ -10,7 +10,7 @@ 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; @@ -30,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; @@ -47,10 +47,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 { @@ -61,6 +65,7 @@ sub original_name { my $p = shift; $p =~ s/\.gz$//; + $p =~ s/\.gz%$/.gz/; # demungle compressed .gz files return $p; }; @@ -249,17 +254,27 @@ 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 "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; $pending->{$file}->{open}++; - return 0; } else { warn "ERROR: can't open $path -- $!"; - return -$!; + $rv = -$!; } + return $rv; + } sub x_read { @@ -462,7 +477,16 @@ $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} ); }