45 |
sub real_name { |
sub real_name { |
46 |
my ( $dir, $name ) = @_; |
my ( $dir, $name ) = @_; |
47 |
if ( -e "$dir/${name}.gz" ) { |
if ( -e "$dir/${name}.gz" ) { |
48 |
|
confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; |
49 |
return "${name}.gz"; |
return "${name}.gz"; |
50 |
} |
} |
51 |
return $name; |
return $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: $!"; |
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; |
|
$pending->{$file}->{write}++; |
|
|
|
|
|
my $tmp = $mount->{tmp} . '/' . $file; |
|
|
truncate( $tmp, $size ) if -e $tmp; |
|
395 |
|
|
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 $path [",-s $path,"] skipped compression\n"; |
|
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
|
|
} elsif ( -s $path < $min_compress_size ) { |
|
|
warn "release $path [",-s $path,"] uncompressed, too small\n"; |
|
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
|
|
} else { |
|
|
warn "release $path [",-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 || confess "can't remove: $comp: $!"; |
|
|
} else { |
|
|
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; |
|
|
# FIXME add timeout to remove uncompressed version? |
|
|
unlink $path || confess "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 |
} |
} |