16 |
|
|
17 |
my $debug = 0; |
my $debug = 0; |
18 |
my $fuse_debug = 0; |
my $fuse_debug = 0; |
19 |
|
my $stats = 1; |
20 |
|
|
21 |
GetOptions( |
GetOptions( |
22 |
'debug+' => \$debug, |
'debug+' => \$debug, |
23 |
'fuse-debug+' => \$fuse_debug, |
'fuse-debug+' => \$fuse_debug, |
24 |
|
'stats!' => \$stats, |
25 |
); |
); |
26 |
|
|
27 |
my $mount = { |
my $mount = { |
124 |
undef $s; |
undef $s; |
125 |
} |
} |
126 |
|
|
127 |
sub create_tmp_file { |
sub tmp_path { |
128 |
my $file = shift; |
my $file = shift; |
129 |
|
|
130 |
my $path = fixup( $file ); |
my $path = fixup( $file ); |
131 |
my $tmp = $mount->{tmp} . '/' . $file; |
|
132 |
if ( -e $tmp ) { |
my $op = 'UNKNOWN'; |
133 |
$path = $tmp; |
|
134 |
} elsif ( $path =~ m/\.gz$/ ) { |
if (defined( $pending->{$file} )) { |
135 |
my $dest_path = $tmp; |
$path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending ); |
136 |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
$op = 'opened'; |
137 |
mkpath $dest_path unless -e $dest_path; |
} else { |
138 |
if ( -s $path ) { |
my $tmp = $mount->{tmp} . '/' . $file; |
139 |
file_copy( '<:gzip', $path, '>', $tmp ) |
if ( -e $tmp ) { |
140 |
} else { |
$path = $tmp; |
141 |
warn "ERROR: filesystem corruption, $path is zero size\n"; |
$op = 'existing'; |
142 |
|
} elsif ( $path =~ m/\.gz$/ ) { |
143 |
|
my $dest_path = $tmp; |
144 |
|
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
145 |
|
mkpath $dest_path unless -e $dest_path; |
146 |
|
if ( -s $path ) { |
147 |
|
file_copy( '<:gzip', $path, '>', $tmp ) |
148 |
|
} else { |
149 |
|
confess "ASSERT: filesystem corruption, $path is zero size\n"; |
150 |
|
} |
151 |
|
$path = $tmp; |
152 |
|
$op = 'created'; |
153 |
} |
} |
154 |
$path = $tmp; |
confess "ASSERT: path shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{path} ); |
155 |
|
confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} ); |
156 |
|
$pending->{$file}->{path} = $path; |
157 |
|
$pending->{$file}->{open} = 0; # not really opened, just uncompressed |
158 |
|
warn "## tmp_file( $file ) $op $path [", -s $path, "]\n"; |
159 |
} |
} |
|
warn "## create_temp_file( $file ) => $path [", -s $path, "]\n"; |
|
160 |
return $path; |
return $path; |
161 |
} |
} |
162 |
|
|
163 |
sub compress_path_dest { |
sub compress_file2path { |
164 |
my ( $path, $dest ) = @_; |
my ( $file, $path ) = @_; |
165 |
|
|
166 |
|
my $dest = fixup( $file ); |
167 |
|
|
168 |
|
if ( defined($pending->{$file}) ) { |
169 |
|
my $pending_path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending ); |
170 |
|
|
171 |
|
if ( $pending->{$file}->{open} > 1 ) { |
172 |
|
warn "$file used ", $pending->{$file}->{open}, " times, delaying compression\n"; |
173 |
|
return; |
174 |
|
} elsif ( ! $path ) { |
175 |
|
$path = $pending_path; |
176 |
|
} elsif ( $pending_path ne $path ) { |
177 |
|
confess "ASSERT: compressing into $path instead of $pending_path\n"; |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
confess "need path" unless $path; |
182 |
|
|
183 |
# cleanup old compressed copy |
# cleanup old compressed copy |
184 |
if ( $dest =~ /\.gz$/ ) { |
if ( $dest =~ /\.gz$/ ) { |
185 |
warn "## remove old $dest\n"; |
warn "## remove old $dest\n"; |
186 |
unlink $dest || confess "can't remove $dest: $!"; |
unlink $dest || confess "can't remove $dest: $!"; |
187 |
$dest =~ s/\.gz$//; |
$dest =~ s/\.gz$//; |
188 |
|
confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest; |
189 |
} |
} |
190 |
|
|
191 |
if ( $path =~ $skip_extensions_regex ) { |
if ( $path =~ $skip_extensions_regex ) { |
206 |
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
207 |
unlink $comp || confess "can't remove: $comp: $!"; |
unlink $comp || confess "can't remove: $comp: $!"; |
208 |
} else { |
} else { |
209 |
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; |
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n"; |
210 |
# FIXME add timeout to remove uncompressed version? |
# FIXME add timeout to remove uncompressed version? |
211 |
unlink $path || confess "can't remove $path: $!"; |
unlink $path || confess "can't remove $path: $!"; |
212 |
} |
} |
228 |
return 0; |
return 0; |
229 |
} |
} |
230 |
|
|
|
$pending->{$file}->{open}++; |
|
|
|
|
231 |
my $mode_desc = { |
my $mode_desc = { |
232 |
rdonly => $mode && O_RDONLY, |
rdonly => $mode && O_RDONLY, |
233 |
rdwr => $mode && O_RDWR, |
rdwr => $mode && O_RDWR, |
236 |
trunc => $mode && O_TRUNC, |
trunc => $mode && O_TRUNC, |
237 |
}; |
}; |
238 |
|
|
239 |
my $path = create_tmp_file( $file ); |
my $path = tmp_path( $file ); |
240 |
|
|
241 |
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; |
242 |
|
|
244 |
|
|
245 |
if ( sysopen($fh , $path, $mode) ) { |
if ( sysopen($fh , $path, $mode) ) { |
246 |
close($fh) || confess "can't close $path: $!"; |
close($fh) || confess "can't close $path: $!"; |
247 |
warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n"; |
warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; |
248 |
$pending->{$file}->{path} = $path; |
$pending->{$file}->{open}++; |
249 |
return 0; |
return 0; |
250 |
} else { |
} else { |
251 |
warn "ERROR: can't open $path -- $!"; |
warn "ERROR: can't open $path -- $!"; |
268 |
|
|
269 |
if(seek($fh,$off,SEEK_SET)) { |
if(seek($fh,$off,SEEK_SET)) { |
270 |
read($fh,$rv,$bufsize); |
read($fh,$rv,$bufsize); |
271 |
|
$pending->{$file}->{read} += length($rv) if $stats; |
272 |
} |
} |
273 |
|
|
274 |
return $rv; |
return $rv; |
285 |
return -ENOENT() unless -e $path; |
return -ENOENT() unless -e $path; |
286 |
|
|
287 |
$path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending ); |
$path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending ); |
288 |
confess "write into non-existant $path for $file: $!" unless -e $path; |
confess "write into non-existant $path for $file" unless -e $path; |
289 |
|
|
290 |
my $fh = new IO::File; |
my $fh = new IO::File; |
291 |
return -ENOSYS() unless open($fh,'+<',$path); |
return -ENOSYS() unless open($fh,'+<',$path); |
292 |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
293 |
$rv = print( $fh $buf ); |
$rv = print( $fh $buf ); |
294 |
warn "## write $path offset $off [",length( $buf ), "]\n" if $debug; |
my $size = length($buf); |
295 |
$pending->{$file}->{write}++; |
warn "## write $path offset $off [$size]\n" if $debug; |
296 |
|
$pending->{$file}->{write} += $size; |
297 |
} |
} |
298 |
$rv = -ENOSYS() unless $rv; |
$rv = -ENOSYS() unless $rv; |
299 |
close($fh) || warn "can't close $path: $!"; |
close($fh) || warn "can't close $path: $!"; |
415 |
|
|
416 |
#confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} ); |
#confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} ); |
417 |
|
|
418 |
my $path; |
my $path = tmp_path( $file ); |
|
|
|
|
if (defined( $pending->{$file} )) { |
|
|
$pending->{$file}->{write}++; |
|
|
$path = fixup( $file ); |
|
|
} else { |
|
|
$path = create_tmp_file( $file ); |
|
|
compress_path_dest( $path, fixup( $file ) ); |
|
|
} |
|
419 |
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
420 |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
421 |
|
compress_file2path( $file, $path ); |
422 |
|
|
423 |
return $rv; |
return $rv; |
424 |
} |
} |
441 |
sub x_release { |
sub x_release { |
442 |
my ( $file, $mode ) = @_; |
my ( $file, $mode ) = @_; |
443 |
|
|
|
if ( $file =~ m#\Q/.fuse_hidden\E# ) { |
|
|
warn "release internal $file\n" if $debug; |
|
|
delete( $pending->{$file} ); |
|
|
return 0; |
|
|
} |
|
|
|
|
444 |
if ( ! defined( $pending->{$file} ) ) { |
if ( ! defined( $pending->{$file} ) ) { |
445 |
warn "release $file, NO PENDING DATA\n"; |
warn "release $file, NO PENDING DATA\n"; |
446 |
return 0; |
return 0; |
447 |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
448 |
warn "release $file, not written into\n"; |
warn "release $file, not written into\n"; |
449 |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
} elsif ( $file =~ m#\Q/.fuse_hidden\E# ) { |
450 |
my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending ); |
warn "release internal $file\n" if $debug; |
|
my $dest = fixup( $file ); |
|
|
compress_path_dest( $path, $dest ); |
|
|
|
|
451 |
} else { |
} else { |
452 |
warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; |
compress_file2path( $file ); |
453 |
} |
} |
454 |
|
|
455 |
$pending->{$file}->{open}--; |
$pending->{$file}->{open}--; |
456 |
if ( $pending->{$file}->{open} == 0 ) { |
if ( $pending->{$file}->{open} == 0 ) { |
457 |
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
458 |
delete( $pending->{$file} ); |
delete( $pending->{$file} ); |
459 |
} |
} |
460 |
|
|
461 |
return 0; |
return 0; |
462 |
} |
} |
463 |
|
|