19 |
tmp => '/dev/shm/comp', |
tmp => '/dev/shm/comp', |
20 |
}; |
}; |
21 |
|
|
22 |
my $debug = 1; |
my $debug = shift @ARGV; |
23 |
|
|
24 |
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/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i; |
25 |
|
|
103 |
|
|
104 |
sub file_copy { |
sub file_copy { |
105 |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
106 |
warn "## file_copy( $s_opt $s_path [",-s $s_path,"] $d_opt $d_path [",-e $d_path ? -s $d_path : 'new',"])\n"; |
warn "## file_copy( $s_opt $s_path [",-s $s_path,"] $d_opt $d_path [",-e $d_path ? -s $d_path : 'new',"])\n" if $debug; |
107 |
open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending ); |
open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending ); |
108 |
open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!"; |
open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!"; |
109 |
my $buff; |
my $buff; |
149 |
trunc => $mode && O_TRUNC, |
trunc => $mode && O_TRUNC, |
150 |
}; |
}; |
151 |
my $path = fixup($file); |
my $path = fixup($file); |
152 |
warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n"; |
warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug; |
153 |
my $fh; |
my $fh; |
154 |
|
|
155 |
my $tmp = $mount->{tmp} . '/' . $file; |
my $tmp = $mount->{tmp} . '/' . $file; |
162 |
file_copy( '<:gzip', $path, '>', $tmp ); |
file_copy( '<:gzip', $path, '>', $tmp ); |
163 |
$path = $tmp; |
$path = $tmp; |
164 |
} |
} |
|
warn ">>> open abs path: $path [", -e $path ? -s $path : 'new' , "]\n"; |
|
|
return -$! unless sysopen($fh , $path, $mode); |
|
|
warn ">>> after open [",-s $path, "]\n"; |
|
|
close($fh); |
|
165 |
|
|
166 |
$pending->{$file}->{path} = $path; |
if ( sysopen($fh , $path, $mode) ) { |
167 |
return 0; |
close($fh) || confess "can't close $path: $!"; |
168 |
|
warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n"; |
169 |
|
$pending->{$file}->{path} = $path; |
170 |
|
return 0; |
171 |
|
} else { |
172 |
|
warn "ERROR: can't open $path : $!"; |
173 |
|
return -$!; |
174 |
|
} |
175 |
|
|
176 |
} |
} |
177 |
|
|
178 |
sub x_read { |
sub x_read { |
223 |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
224 |
|
|
225 |
sub x_rename { |
sub x_rename { |
226 |
my ($old) = fixup(shift); |
my ($old,$new) = @_; |
227 |
my ($new) = fixup(shift); |
my $old_path = fixup($old); |
228 |
my ($err) = rename($old,$new) ? 0 : -ENOENT(); |
my $new_path = fixup($new); |
229 |
|
$new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ ); |
230 |
|
|
231 |
|
my $err = rename($old_path,$new_path) ? 0 : -ENOENT(); |
232 |
|
warn "## rename( $old_path => $new_path ) = $err\n"; |
233 |
|
|
234 |
|
my $tmp = $mount->{tmp} . '/' . $old; |
235 |
|
if ( -e $tmp ) { |
236 |
|
my $new_tmp = $mount->{tmp} . '/' . $new; |
237 |
|
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
238 |
|
} |
239 |
|
|
240 |
|
if (defined( $pending->{$old} )) { |
241 |
|
$pending->{$new} = $pending->{$old}; |
242 |
|
|
243 |
|
my $path = $pending->{$old}->{path}; |
244 |
|
$path =~ s/\Q$old\E/$new/; |
245 |
|
$pending->{$new}->{path} = $path; |
246 |
|
|
247 |
|
delete( $pending->{$old} ); |
248 |
|
} |
249 |
|
|
250 |
return $err; |
return $err; |
251 |
} |
} |
252 |
sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } |
sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } |
279 |
$no_gz =~ s/\.gz$//; |
$no_gz =~ s/\.gz$//; |
280 |
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
281 |
} |
} |
282 |
warn "## truncate( $file $size ) $path [", -s $path, "]\n"; |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
283 |
$pending->{$file}->{write}++; |
$pending->{$file}->{write}++; |
284 |
return $rv; |
return $rv; |
285 |
} |
} |
317 |
} |
} |
318 |
|
|
319 |
if ( $file =~ $skip_extensions_regex ) { |
if ( $file =~ $skip_extensions_regex ) { |
320 |
warn "release $file $mode -- uncompressed\n"; |
warn "release $file [",-s $path,"] skipped compression\n"; |
321 |
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
322 |
} elsif ( -s $path < $min_compress_size ) { |
} elsif ( -s $path < $min_compress_size ) { |
323 |
warn "release $file -- uncompressed, too small [", -s $path, "]\n"; |
warn "release $file [",-s $path,"] uncompressed, too small\n"; |
324 |
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
325 |
} else { |
} else { |
326 |
warn "release $file $mode -- compressing\n"; |
warn "release $file [",-s $path,"] compressing\n"; |
327 |
|
|
328 |
|
my $comp = $dest . '.gz'; |
329 |
|
file_copy( '<', $path, '>:gzip', $comp ); |
330 |
|
|
331 |
file_copy( '<', $path, '>:gzip', $dest . '.gz' ); |
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
332 |
|
|
333 |
# FIXME add timeout to remove uncompressed version? |
if ( $size_path <= $size_comp ) { |
334 |
# FIXME leave uncompressed file if smaller than compressed |
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
335 |
unlink $path || warn "can't remove $path: $!"; |
unlink $comp || warn "can't reamove: $comp: $!"; |
336 |
|
} else { |
337 |
|
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; |
338 |
|
# FIXME add timeout to remove uncompressed version? |
339 |
|
unlink $path || warn "can't remove $path: $!"; |
340 |
|
} |
341 |
} |
} |
342 |
} else { |
} else { |
343 |
warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; |
warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; |