42 |
|
|
43 |
my $pending; |
my $pending; |
44 |
|
|
45 |
|
sub real_name { |
46 |
|
my ( $dir, $name ) = @_; |
47 |
|
if ( -e "$dir/${name}.gz" ) { |
48 |
|
confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; |
49 |
|
return "${name}.gz"; |
50 |
|
} |
51 |
|
return $name; |
52 |
|
} |
53 |
|
|
54 |
sub fixup { |
sub fixup { |
55 |
my ( $path ) = @_; |
my ( $path ) = @_; |
56 |
my $full = $mount->{from} . '/' . $path; |
return $mount->{from} . '/' . real_name( $mount->{from}, $path ); |
|
if ( -e $full . '.gz' ) { |
|
|
return $full . '.gz'; |
|
|
} |
|
|
return $full; |
|
57 |
} |
} |
58 |
|
|
59 |
sub original_name { |
sub original_name { |
122 |
undef $s; |
undef $s; |
123 |
} |
} |
124 |
|
|
125 |
|
sub create_tmp_file { |
126 |
|
my $file = shift; |
127 |
|
|
128 |
|
my $path = fixup( $file ); |
129 |
|
my $tmp = $mount->{tmp} . '/' . $file; |
130 |
|
if ( -e $tmp ) { |
131 |
|
$path = $tmp; |
132 |
|
} elsif ( $path =~ m/\.gz$/ ) { |
133 |
|
my $dest_path = $tmp; |
134 |
|
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
135 |
|
mkpath $dest_path unless -e $dest_path; |
136 |
|
if ( -s $path ) { |
137 |
|
file_copy( '<:gzip', $path, '>', $tmp ) |
138 |
|
} else { |
139 |
|
warn "ERROR: filesystem corruption, $path is zero size\n"; |
140 |
|
} |
141 |
|
$path = $tmp; |
142 |
|
} |
143 |
|
warn "## create_temp_file( $file ) => $path [", -s $path, "]\n"; |
144 |
|
return $path; |
145 |
|
} |
146 |
|
|
147 |
|
sub compress_path_dest { |
148 |
|
my ( $path, $dest ) = @_; |
149 |
|
|
150 |
|
# cleanup old compressed copy |
151 |
|
if ( $dest =~ /\.gz$/ ) { |
152 |
|
warn "## remove old $dest\n"; |
153 |
|
unlink $dest || confess "can't remove $dest: $!"; |
154 |
|
$dest =~ s/\.gz$//; |
155 |
|
} |
156 |
|
|
157 |
|
if ( $path =~ $skip_extensions_regex ) { |
158 |
|
warn "$path [",-s $path,"] skipped compression\n"; |
159 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
160 |
|
} elsif ( -s $path < $min_compress_size ) { |
161 |
|
warn "$path [",-s $path,"] uncompressed, too small\n"; |
162 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
163 |
|
} else { |
164 |
|
warn "$path [",-s $path,"] compressing\n"; |
165 |
|
|
166 |
|
my $comp = $dest . '.gz'; |
167 |
|
file_copy( '<', $path, '>:gzip', $comp ); |
168 |
|
|
169 |
|
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
170 |
|
|
171 |
|
if ( $size_path <= $size_comp ) { |
172 |
|
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
173 |
|
unlink $comp || confess "can't remove: $comp: $!"; |
174 |
|
} else { |
175 |
|
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; |
176 |
|
# FIXME add timeout to remove uncompressed version? |
177 |
|
unlink $path || confess "can't remove $path: $!"; |
178 |
|
} |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
sub x_open { |
sub x_open { |
183 |
my ($file) = shift; |
my ($file) = shift; |
184 |
my ($mode) = shift; |
my ($mode) = shift; |
203 |
create => $mode && O_CREAT, |
create => $mode && O_CREAT, |
204 |
trunc => $mode && O_TRUNC, |
trunc => $mode && O_TRUNC, |
205 |
}; |
}; |
206 |
my $path = fixup($file); |
|
207 |
|
my $path = create_tmp_file( $file ); |
208 |
|
|
209 |
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; |
|
my $fh; |
|
210 |
|
|
211 |
my $tmp = $mount->{tmp} . '/' . $file; |
my $fh; |
|
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"; |
|
|
} |
|
|
$path = $tmp; |
|
|
} |
|
212 |
|
|
213 |
if ( sysopen($fh , $path, $mode) ) { |
if ( sysopen($fh , $path, $mode) ) { |
214 |
close($fh) || confess "can't close $path: $!"; |
close($fh) || confess "can't close $path: $!"; |
268 |
|
|
269 |
sub err { return (-shift || -$!) } |
sub err { return (-shift || -$!) } |
270 |
|
|
271 |
sub x_readlink { return readlink(fixup(shift)); } |
sub x_readlink { return readlink(fixup(shift)); } |
272 |
|
|
273 |
sub x_unlink { |
sub x_unlink { |
274 |
my $file = shift; |
my $file = shift; |
275 |
my $path = fixup( $file ); |
my $path = fixup( $file ); |
289 |
return 0; |
return 0; |
290 |
} |
} |
291 |
|
|
292 |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
sub x_symlink { |
293 |
|
my ($from,$to) = @_; |
294 |
|
|
295 |
|
my $from_path = $from; #fixup( $from ); |
296 |
|
my $to_path = fixup( $to ); |
297 |
|
|
298 |
|
my $rv = symlink( $from_path, $to_path ) ? 0 : -$!; |
299 |
|
warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug; |
300 |
|
|
301 |
|
my $tmp = $mount->{tmp} . '/' . $from; |
302 |
|
if ( -e $tmp ) { |
303 |
|
my $tmp_to = $mount->{$tmp} . '/' . $to; |
304 |
|
symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!"; |
305 |
|
} |
306 |
|
return $rv; |
307 |
|
} |
308 |
|
|
309 |
|
sub x_link { |
310 |
|
my ($from,$to) = @_; |
311 |
|
|
312 |
|
my $from_path = fixup($from); |
313 |
|
my $to_path = fixup($to); |
314 |
|
$to_path .= '.gz' if ( $from_path =~ m/\.gz$/ && $to_path !~ m/\.gz$/ ); |
315 |
|
|
316 |
|
my $rv = link( $from_path, $to_path ) ? 0 : -$!; |
317 |
|
|
318 |
|
warn "# link( $from_path -> $to_path ) = $rv\n" if $debug; |
319 |
|
|
320 |
|
return $rv; |
321 |
|
} |
322 |
|
|
323 |
sub x_rename { |
sub x_rename { |
324 |
my ($old,$new) = @_; |
my ($old,$new) = @_; |
352 |
|
|
353 |
return $err; |
return $err; |
354 |
} |
} |
|
sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } |
|
355 |
|
|
356 |
sub x_chown { |
sub x_chown { |
357 |
my ($file,$uid,$gid) = @_; |
my ($file,$uid,$gid) = @_; |
378 |
|
|
379 |
sub x_truncate { |
sub x_truncate { |
380 |
my ( $file,$size ) = @_; |
my ( $file,$size ) = @_; |
381 |
my $path = fixup($file); |
|
382 |
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
#confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} ); |
383 |
if ( $path =~ m/\.gz$/ ) { |
|
384 |
my $no_gz = $path; |
my $path; |
385 |
$no_gz =~ s/\.gz$//; |
|
386 |
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
if (defined( $pending->{$file} )) { |
387 |
|
$pending->{$file}->{write}++; |
388 |
|
$path = fixup( $file ); |
389 |
|
} else { |
390 |
|
$path = create_tmp_file( $file ); |
391 |
|
compress_path_dest( $path, fixup( $file ) ); |
392 |
} |
} |
393 |
|
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
394 |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
395 |
$pending->{$file}->{write}++; |
|
396 |
return $rv; |
return $rv; |
397 |
} |
} |
398 |
|
|
399 |
sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } |
sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } |
400 |
|
|
401 |
sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; } |
sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; } |
428 |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
429 |
my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending ); |
my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending ); |
430 |
my $dest = fixup( $file ); |
my $dest = fixup( $file ); |
431 |
|
compress_path_dest( $path, $dest ); |
432 |
|
|
|
# cleanup old compressed copy |
|
|
if ( $dest =~ /\.gz$/ ) { |
|
|
warn "## remove old $dest\n"; |
|
|
unlink $dest || confess "can't remove $dest: $!"; |
|
|
$dest =~ s/\.gz$//; |
|
|
} |
|
|
|
|
|
if ( $file =~ $skip_extensions_regex ) { |
|
|
warn "release $file [",-s $path,"] skipped compression\n"; |
|
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
|
|
} elsif ( -s $path < $min_compress_size ) { |
|
|
warn "release $file [",-s $path,"] uncompressed, too small\n"; |
|
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
|
|
} else { |
|
|
warn "release $file [",-s $path,"] compressing\n"; |
|
|
|
|
|
my $comp = $dest . '.gz'; |
|
|
file_copy( '<', $path, '>:gzip', $comp ); |
|
|
|
|
|
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
|
|
|
|
|
if ( $size_path <= $size_comp ) { |
|
|
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
|
|
unlink $comp || warn "can't reamove: $comp: $!"; |
|
|
} else { |
|
|
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; |
|
|
# FIXME add timeout to remove uncompressed version? |
|
|
unlink $path || warn "can't remove $path: $!"; |
|
|
} |
|
|
} |
|
433 |
} else { |
} else { |
434 |
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"; |
435 |
} |
} |