10 |
use PerlIO::gzip; |
use PerlIO::gzip; |
11 |
use File::Path; |
use File::Path; |
12 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
13 |
use Carp qw/confess/; |
use Carp qw/confess cluck/; |
14 |
use IO::File; |
use IO::File; |
15 |
use Getopt::Long; |
use Getopt::Long; |
16 |
|
|
30 |
tmp => '/dev/shm/comp', |
tmp => '/dev/shm/comp', |
31 |
}; |
}; |
32 |
|
|
33 |
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; |
34 |
|
|
35 |
# don't compress files smaller than this |
# don't compress files smaller than this |
36 |
my $min_compress_size = 512; |
my $min_compress_size = 512; |
47 |
sub real_name { |
sub real_name { |
48 |
my ( $dir, $name ) = @_; |
my ( $dir, $name ) = @_; |
49 |
if ( -e "$dir/${name}.gz" ) { |
if ( -e "$dir/${name}.gz" ) { |
50 |
confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; |
cluck "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; |
51 |
return "${name}.gz"; |
return "${name}.gz"; |
52 |
} |
} |
53 |
return $name; |
if ( $name =~ m/\.gz$/ ) { |
54 |
|
return $name . '%'; # protect (mingle) compressed files |
55 |
|
} else { |
56 |
|
return $name; |
57 |
|
} |
58 |
} |
} |
59 |
|
|
60 |
sub fixup { |
sub fixup { |
65 |
sub original_name { |
sub original_name { |
66 |
my $p = shift; |
my $p = shift; |
67 |
$p =~ s/\.gz$//; |
$p =~ s/\.gz$//; |
68 |
|
$p =~ s/\.gz%$/.gz/; # demungle compressed .gz files |
69 |
return $p; |
return $p; |
70 |
}; |
}; |
71 |
|
|
151 |
if ( -s $path ) { |
if ( -s $path ) { |
152 |
file_copy( '<:gzip', $path, '>', $tmp ) |
file_copy( '<:gzip', $path, '>', $tmp ) |
153 |
} else { |
} else { |
154 |
confess "ASSERT: filesystem corruption, $path is zero size\n"; |
confess "ASSERT: filesystem corruption, $path is zero size in ",dump( $pending ); |
155 |
} |
} |
156 |
$path = $tmp; |
$path = $tmp; |
157 |
$op = 'created'; |
$op = 'created'; |
160 |
confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} ); |
confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} ); |
161 |
$pending->{$file}->{path} = $path; |
$pending->{$file}->{path} = $path; |
162 |
$pending->{$file}->{open} = 0; # not really opened, just uncompressed |
$pending->{$file}->{open} = 0; # not really opened, just uncompressed |
163 |
warn "## tmp_file( $file ) $op $path [", -s $path, "]\n"; |
warn "## tmp_file( $file ) $op $path [", -s $path, "]\n" if $debug; |
164 |
} |
} |
165 |
return $path; |
return $path; |
166 |
} |
} |
179 |
} elsif ( ! $path ) { |
} elsif ( ! $path ) { |
180 |
$path = $pending_path; |
$path = $pending_path; |
181 |
} elsif ( $pending_path ne $path ) { |
} elsif ( $pending_path ne $path ) { |
182 |
confess "ASSERT: compressing into $path instead of $pending_path\n"; |
confess "ASSERT: compressing into $path instead of $pending_path"; |
183 |
} |
} |
184 |
} |
} |
185 |
|
|
208 |
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
209 |
|
|
210 |
if ( $size_path <= $size_comp ) { |
if ( $size_path <= $size_comp ) { |
211 |
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
warn ">>> $size_path <= $size_comp leaving uncompressed $dest\n"; |
212 |
unlink $comp || confess "can't remove: $comp: $!"; |
unlink $comp || confess "can't remove: $comp: $!"; |
213 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
214 |
} else { |
} else { |
215 |
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n"; |
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n"; |
216 |
|
|
217 |
# FIXME add timeout to remove uncompressed version? |
# FIXME add timeout to remove uncompressed version? |
218 |
unlink $path || confess "can't remove $path: $!"; |
unlink $path || confess "can't remove $path: $!"; |
219 |
|
|
220 |
|
if ( -e $dest ) { |
221 |
|
warn "## cleanup uncompressed $dest\n" if $debug; |
222 |
|
unlink $dest || confess "can't remove $dest: $!"; |
223 |
|
} |
224 |
} |
} |
225 |
|
|
226 |
} |
} |
227 |
} |
} |
228 |
|
|
254 |
warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug; |
warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug; |
255 |
|
|
256 |
my $fh; |
my $fh; |
257 |
|
my $rv = 0; |
258 |
|
|
259 |
|
if ( ! -w $path ) { |
260 |
|
my $old_mode = (stat $path)[2]; |
261 |
|
my $new_mode = $old_mode | 0600; |
262 |
|
chmod $new_mode, $path || confess "can't chmod $new_mode $path"; |
263 |
|
warn "### modify mode $old_mode -> $new_mode for $path\n"; |
264 |
|
$pending->{$file}->{mode} = $old_mode; |
265 |
|
} |
266 |
|
|
267 |
if ( sysopen($fh , $path, $mode) ) { |
if ( sysopen($fh , $path, $mode) ) { |
268 |
close($fh) || confess "can't close $path: $!"; |
close($fh) || confess "can't close $path: $!"; |
269 |
warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; |
warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; |
270 |
$pending->{$file}->{open}++; |
$pending->{$file}->{open}++; |
|
return 0; |
|
271 |
} else { |
} else { |
272 |
warn "ERROR: can't open $path -- $!"; |
warn "ERROR: can't open $path -- $!"; |
273 |
return -$!; |
$rv = -$!; |
274 |
} |
} |
275 |
|
|
276 |
|
return $rv; |
277 |
|
|
278 |
} |
} |
279 |
|
|
280 |
sub x_read { |
sub x_read { |
389 |
my $tmp = $mount->{tmp} . '/' . $old; |
my $tmp = $mount->{tmp} . '/' . $old; |
390 |
if ( -e $tmp ) { |
if ( -e $tmp ) { |
391 |
if ( $new =~ m#\Q/.fuse_hidden\E# ) { |
if ( $new =~ m#\Q/.fuse_hidden\E# ) { |
392 |
unlink $tmp || confess "can't unlink $tmp for $new\n"; |
unlink $tmp || confess "can't unlink $tmp for $new"; |
393 |
} else { |
} else { |
394 |
my $new_tmp = $mount->{tmp} . '/' . $new; |
my $new_tmp = $mount->{tmp} . '/' . $new; |
395 |
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
477 |
|
|
478 |
$pending->{$file}->{open}--; |
$pending->{$file}->{open}--; |
479 |
if ( $pending->{$file}->{open} == 0 ) { |
if ( $pending->{$file}->{open} == 0 ) { |
480 |
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
|
481 |
|
my $path = fixup( $file ); |
482 |
|
|
483 |
|
if ( my $old_mode = $pending->{$file}->{mode} ) { |
484 |
|
chmod $old_mode, $path || confess "can't chmod $old_mode $path"; |
485 |
|
warn "### restored mode $old_mode $path\n"; |
486 |
|
|
487 |
|
} |
488 |
|
|
489 |
|
warn "## cleanup pending $file [", -s $path, "]\n" if $debug; |
490 |
delete( $pending->{$file} ); |
delete( $pending->{$file} ); |
491 |
} |
} |
492 |
|
|