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 |
|
|
25 |
); |
); |
26 |
|
|
27 |
my $mount = { |
my $mount = { |
28 |
from => '/tmp/comp', |
from => shift @ARGV || '/tmp/comp', |
29 |
to => '/tmp/no-comp', |
to => shift @ARGV || '/tmp/no-comp', |
30 |
tmp => '/dev/shm/comp', |
tmp => shift @ARGV || '/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; |
warn "mount $mount->{from} to $mount->{to} using $mount->{tmp} as cache\n"; |
34 |
|
|
35 |
|
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; |
36 |
|
|
37 |
# don't compress files smaller than this |
# don't compress files smaller than this |
38 |
my $min_compress_size = 512; |
my $min_compress_size = 512; |
49 |
sub real_name { |
sub real_name { |
50 |
my ( $dir, $name ) = @_; |
my ( $dir, $name ) = @_; |
51 |
if ( -e "$dir/${name}.gz" ) { |
if ( -e "$dir/${name}.gz" ) { |
52 |
confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; |
cluck "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; |
53 |
return "${name}.gz"; |
return "${name}.gz"; |
54 |
} |
} |
55 |
return $name; |
if ( $name =~ m/\.gz$/ ) { |
56 |
|
return $name . '%'; # protect (mingle) compressed files |
57 |
|
} else { |
58 |
|
return $name; |
59 |
|
} |
60 |
} |
} |
61 |
|
|
62 |
sub fixup { |
sub fixup { |
67 |
sub original_name { |
sub original_name { |
68 |
my $p = shift; |
my $p = shift; |
69 |
$p =~ s/\.gz$//; |
$p =~ s/\.gz$//; |
70 |
|
$p =~ s/\.gz%$/.gz/; # demungle compressed .gz files |
71 |
return $p; |
return $p; |
72 |
}; |
}; |
73 |
|
|
153 |
if ( -s $path ) { |
if ( -s $path ) { |
154 |
file_copy( '<:gzip', $path, '>', $tmp ) |
file_copy( '<:gzip', $path, '>', $tmp ) |
155 |
} else { |
} else { |
156 |
confess "ASSERT: filesystem corruption, $path is zero size\n"; |
confess "ASSERT: filesystem corruption, $path is zero size in ",dump( $pending ); |
157 |
} |
} |
158 |
$path = $tmp; |
$path = $tmp; |
159 |
$op = 'created'; |
$op = 'created'; |
162 |
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} ); |
163 |
$pending->{$file}->{path} = $path; |
$pending->{$file}->{path} = $path; |
164 |
$pending->{$file}->{open} = 0; # not really opened, just uncompressed |
$pending->{$file}->{open} = 0; # not really opened, just uncompressed |
165 |
warn "## tmp_file( $file ) $op $path [", -s $path, "]\n"; |
warn "## tmp_file( $file ) $op $path [", -s $path, "]\n" if $debug; |
166 |
} |
} |
167 |
return $path; |
return $path; |
168 |
} |
} |
181 |
} elsif ( ! $path ) { |
} elsif ( ! $path ) { |
182 |
$path = $pending_path; |
$path = $pending_path; |
183 |
} elsif ( $pending_path ne $path ) { |
} elsif ( $pending_path ne $path ) { |
184 |
confess "ASSERT: compressing into $path instead of $pending_path\n"; |
confess "ASSERT: compressing into $path instead of $pending_path"; |
185 |
} |
} |
186 |
} |
} |
187 |
|
|
210 |
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
211 |
|
|
212 |
if ( $size_path <= $size_comp ) { |
if ( $size_path <= $size_comp ) { |
213 |
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
warn ">>> $size_path <= $size_comp leaving uncompressed $dest\n"; |
214 |
unlink $comp || confess "can't remove: $comp: $!"; |
unlink $comp || confess "can't remove: $comp: $!"; |
215 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
216 |
} else { |
} else { |
217 |
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"; |
218 |
|
|
219 |
# FIXME add timeout to remove uncompressed version? |
# FIXME add timeout to remove uncompressed version? |
220 |
unlink $path || confess "can't remove $path: $!"; |
unlink $path || confess "can't remove $path: $!"; |
221 |
|
|
222 |
|
if ( -e $dest ) { |
223 |
|
warn "## cleanup uncompressed $dest\n" if $debug; |
224 |
|
unlink $dest || confess "can't remove $dest: $!"; |
225 |
|
} |
226 |
} |
} |
227 |
|
|
228 |
} |
} |
229 |
} |
} |
230 |
|
|
256 |
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; |
257 |
|
|
258 |
my $fh; |
my $fh; |
259 |
|
my $rv = 0; |
260 |
|
|
261 |
|
if ( ! -w $path ) { |
262 |
|
my $old_mode = (stat $path)[2]; |
263 |
|
my $new_mode = $old_mode | 0600; |
264 |
|
chmod $new_mode, $path || confess "can't chmod $new_mode $path"; |
265 |
|
warn "### modify mode $old_mode -> $new_mode for $path\n"; |
266 |
|
$pending->{$file}->{mode} = $old_mode; |
267 |
|
} |
268 |
|
|
269 |
if ( sysopen($fh , $path, $mode) ) { |
if ( sysopen($fh , $path, $mode) ) { |
270 |
close($fh) || confess "can't close $path: $!"; |
close($fh) || confess "can't close $path: $!"; |
271 |
warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; |
warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; |
272 |
$pending->{$file}->{open}++; |
$pending->{$file}->{open}++; |
|
return 0; |
|
273 |
} else { |
} else { |
274 |
warn "ERROR: can't open $path -- $!"; |
warn "ERROR: can't open $path -- $!"; |
275 |
return -$!; |
$rv = -$!; |
276 |
} |
} |
277 |
|
|
278 |
|
return $rv; |
279 |
|
|
280 |
} |
} |
281 |
|
|
282 |
sub x_read { |
sub x_read { |
358 |
warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug; |
warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug; |
359 |
|
|
360 |
my $tmp = $mount->{tmp} . '/' . $from; |
my $tmp = $mount->{tmp} . '/' . $from; |
361 |
if ( -e $tmp ) { |
my $tmp_to = $mount->{tmp} . '/' . $to; |
362 |
my $tmp_to = $mount->{$tmp} . '/' . $to; |
if ( $rv == 0 && -e $tmp_to ) { |
363 |
symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!"; |
symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!"; |
364 |
} |
} |
365 |
return $rv; |
return $rv; |
391 |
my $tmp = $mount->{tmp} . '/' . $old; |
my $tmp = $mount->{tmp} . '/' . $old; |
392 |
if ( -e $tmp ) { |
if ( -e $tmp ) { |
393 |
if ( $new =~ m#\Q/.fuse_hidden\E# ) { |
if ( $new =~ m#\Q/.fuse_hidden\E# ) { |
394 |
unlink $tmp || confess "can't unlink $tmp for $new\n"; |
unlink $tmp || confess "can't unlink $tmp for $new"; |
395 |
} else { |
} else { |
396 |
my $new_tmp = $mount->{tmp} . '/' . $new; |
my $new_tmp = $mount->{tmp} . '/' . $new; |
397 |
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
479 |
|
|
480 |
$pending->{$file}->{open}--; |
$pending->{$file}->{open}--; |
481 |
if ( $pending->{$file}->{open} == 0 ) { |
if ( $pending->{$file}->{open} == 0 ) { |
482 |
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
|
483 |
|
my $path = fixup( $file ); |
484 |
|
|
485 |
|
if ( my $old_mode = $pending->{$file}->{mode} ) { |
486 |
|
chmod $old_mode, $path || confess "can't chmod $old_mode $path"; |
487 |
|
warn "### restored mode $old_mode $path\n"; |
488 |
|
|
489 |
|
} |
490 |
|
|
491 |
|
warn "## cleanup pending $file [", -s $path, "]\n" if $debug; |
492 |
delete( $pending->{$file} ); |
delete( $pending->{$file} ); |
493 |
} |
} |
494 |
|
|