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 = { |
30 |
tmp => '/dev/shm/comp', |
tmp => '/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; |
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; |
34 |
|
|
35 |
# don't compress files smaller than this |
# don't compress files smaller than this |
36 |
my $min_compress_size = 512; |
my $min_compress_size = 512; |
50 |
confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; |
confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; |
51 |
return "${name}.gz"; |
return "${name}.gz"; |
52 |
} |
} |
53 |
return $name; |
if ( $name =~ m/\.gz$/ ) { |
54 |
|
return $name . '%'; # protect (mingle) compressed files |
55 |
|
} else { |
56 |
|
return $name; |
57 |
|
} |
58 |
} |
} |
59 |
|
|
60 |
sub fixup { |
sub fixup { |
65 |
sub original_name { |
sub original_name { |
66 |
my $p = shift; |
my $p = shift; |
67 |
$p =~ s/\.gz$//; |
$p =~ s/\.gz$//; |
68 |
|
$p =~ s/\.gz%$/.gz/; # demungle compressed .gz files |
69 |
return $p; |
return $p; |
70 |
}; |
}; |
71 |
|
|
129 |
undef $s; |
undef $s; |
130 |
} |
} |
131 |
|
|
132 |
sub create_tmp_file { |
sub tmp_path { |
133 |
my $file = shift; |
my $file = shift; |
134 |
|
|
135 |
my $path = fixup( $file ); |
my $path = fixup( $file ); |
136 |
my $tmp = $mount->{tmp} . '/' . $file; |
|
137 |
if ( -e $tmp ) { |
my $op = 'UNKNOWN'; |
138 |
$path = $tmp; |
|
139 |
} elsif ( $path =~ m/\.gz$/ ) { |
if (defined( $pending->{$file} )) { |
140 |
my $dest_path = $tmp; |
$path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending ); |
141 |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
$op = 'opened'; |
142 |
mkpath $dest_path unless -e $dest_path; |
} else { |
143 |
if ( -s $path ) { |
my $tmp = $mount->{tmp} . '/' . $file; |
144 |
file_copy( '<:gzip', $path, '>', $tmp ) |
if ( -e $tmp ) { |
145 |
} else { |
$path = $tmp; |
146 |
warn "ERROR: filesystem corruption, $path is zero size\n"; |
$op = 'existing'; |
147 |
|
} elsif ( $path =~ m/\.gz$/ ) { |
148 |
|
my $dest_path = $tmp; |
149 |
|
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
150 |
|
mkpath $dest_path unless -e $dest_path; |
151 |
|
if ( -s $path ) { |
152 |
|
file_copy( '<:gzip', $path, '>', $tmp ) |
153 |
|
} else { |
154 |
|
confess "ASSERT: filesystem corruption, $path is zero size in ",dump( $pending ); |
155 |
|
} |
156 |
|
$path = $tmp; |
157 |
|
$op = 'created'; |
158 |
} |
} |
159 |
$path = $tmp; |
confess "ASSERT: path shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{path} ); |
160 |
|
confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} ); |
161 |
|
$pending->{$file}->{path} = $path; |
162 |
|
$pending->{$file}->{open} = 0; # not really opened, just uncompressed |
163 |
|
warn "## tmp_file( $file ) $op $path [", -s $path, "]\n" if $debug; |
164 |
} |
} |
|
warn "## create_temp_file( $file ) => $path [", -s $path, "]\n"; |
|
165 |
return $path; |
return $path; |
166 |
} |
} |
167 |
|
|
168 |
sub compress_path_dest { |
sub compress_file2path { |
169 |
my ( $path, $dest ) = @_; |
my ( $file, $path ) = @_; |
170 |
|
|
171 |
|
my $dest = fixup( $file ); |
172 |
|
|
173 |
|
if ( defined($pending->{$file}) ) { |
174 |
|
my $pending_path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending ); |
175 |
|
|
176 |
|
if ( $pending->{$file}->{open} > 1 ) { |
177 |
|
warn "$file used ", $pending->{$file}->{open}, " times, delaying compression\n"; |
178 |
|
return; |
179 |
|
} elsif ( ! $path ) { |
180 |
|
$path = $pending_path; |
181 |
|
} elsif ( $pending_path ne $path ) { |
182 |
|
confess "ASSERT: compressing into $path instead of $pending_path"; |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
confess "need path" unless $path; |
187 |
|
|
188 |
# cleanup old compressed copy |
# cleanup old compressed copy |
189 |
if ( $dest =~ /\.gz$/ ) { |
if ( $dest =~ /\.gz$/ ) { |
190 |
warn "## remove old $dest\n"; |
warn "## remove old $dest\n"; |
191 |
unlink $dest || confess "can't remove $dest: $!"; |
unlink $dest || confess "can't remove $dest: $!"; |
192 |
$dest =~ s/\.gz$//; |
$dest =~ s/\.gz$//; |
193 |
|
confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest; |
194 |
} |
} |
195 |
|
|
196 |
if ( $path =~ $skip_extensions_regex ) { |
if ( $path =~ $skip_extensions_regex ) { |
208 |
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
209 |
|
|
210 |
if ( $size_path <= $size_comp ) { |
if ( $size_path <= $size_comp ) { |
211 |
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
warn ">>> $size_path <= $size_comp leaving uncompressed $dest\n"; |
212 |
unlink $comp || confess "can't remove: $comp: $!"; |
unlink $comp || confess "can't remove: $comp: $!"; |
213 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
214 |
} else { |
} else { |
215 |
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"; |
216 |
|
|
217 |
# FIXME add timeout to remove uncompressed version? |
# FIXME add timeout to remove uncompressed version? |
218 |
unlink $path || confess "can't remove $path: $!"; |
unlink $path || confess "can't remove $path: $!"; |
219 |
|
|
220 |
|
if ( -e $dest ) { |
221 |
|
warn "## cleanup uncompressed $dest\n" if $debug; |
222 |
|
unlink $dest || confess "can't remove $dest: $!"; |
223 |
|
} |
224 |
} |
} |
225 |
|
|
226 |
} |
} |
227 |
} |
} |
228 |
|
|
241 |
return 0; |
return 0; |
242 |
} |
} |
243 |
|
|
|
$pending->{$file}->{open}++; |
|
|
|
|
244 |
my $mode_desc = { |
my $mode_desc = { |
245 |
rdonly => $mode && O_RDONLY, |
rdonly => $mode && O_RDONLY, |
246 |
rdwr => $mode && O_RDWR, |
rdwr => $mode && O_RDWR, |
249 |
trunc => $mode && O_TRUNC, |
trunc => $mode && O_TRUNC, |
250 |
}; |
}; |
251 |
|
|
252 |
my $path = create_tmp_file( $file ); |
my $path = tmp_path( $file ); |
253 |
|
|
254 |
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; |
255 |
|
|
257 |
|
|
258 |
if ( sysopen($fh , $path, $mode) ) { |
if ( sysopen($fh , $path, $mode) ) { |
259 |
close($fh) || confess "can't close $path: $!"; |
close($fh) || confess "can't close $path: $!"; |
260 |
warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n"; |
warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; |
261 |
$pending->{$file}->{path} = $path; |
$pending->{$file}->{open}++; |
262 |
return 0; |
return 0; |
263 |
} else { |
} else { |
264 |
warn "ERROR: can't open $path -- $!"; |
warn "ERROR: can't open $path -- $!"; |
281 |
|
|
282 |
if(seek($fh,$off,SEEK_SET)) { |
if(seek($fh,$off,SEEK_SET)) { |
283 |
read($fh,$rv,$bufsize); |
read($fh,$rv,$bufsize); |
284 |
|
$pending->{$file}->{read} += length($rv) if $stats; |
285 |
} |
} |
286 |
|
|
287 |
return $rv; |
return $rv; |
298 |
return -ENOENT() unless -e $path; |
return -ENOENT() unless -e $path; |
299 |
|
|
300 |
$path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending ); |
$path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending ); |
301 |
confess "write into non-existant $path for $file: $!" unless -e $path; |
confess "write into non-existant $path for $file" unless -e $path; |
302 |
|
|
303 |
my $fh = new IO::File; |
my $fh = new IO::File; |
304 |
return -ENOSYS() unless open($fh,'+<',$path); |
return -ENOSYS() unless open($fh,'+<',$path); |
305 |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
306 |
$rv = print( $fh $buf ); |
$rv = print( $fh $buf ); |
307 |
warn "## write $path offset $off [",length( $buf ), "]\n" if $debug; |
my $size = length($buf); |
308 |
$pending->{$file}->{write}++; |
warn "## write $path offset $off [$size]\n" if $debug; |
309 |
|
$pending->{$file}->{write} += $size; |
310 |
} |
} |
311 |
$rv = -ENOSYS() unless $rv; |
$rv = -ENOSYS() unless $rv; |
312 |
close($fh) || warn "can't close $path: $!"; |
close($fh) || warn "can't close $path: $!"; |
379 |
my $tmp = $mount->{tmp} . '/' . $old; |
my $tmp = $mount->{tmp} . '/' . $old; |
380 |
if ( -e $tmp ) { |
if ( -e $tmp ) { |
381 |
if ( $new =~ m#\Q/.fuse_hidden\E# ) { |
if ( $new =~ m#\Q/.fuse_hidden\E# ) { |
382 |
unlink $tmp || confess "can't unlink $tmp for $new\n"; |
unlink $tmp || confess "can't unlink $tmp for $new"; |
383 |
} else { |
} else { |
384 |
my $new_tmp = $mount->{tmp} . '/' . $new; |
my $new_tmp = $mount->{tmp} . '/' . $new; |
385 |
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
428 |
|
|
429 |
#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} ); |
430 |
|
|
431 |
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 ) ); |
|
|
} |
|
432 |
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
433 |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
434 |
|
compress_file2path( $file, $path ); |
435 |
|
|
436 |
return $rv; |
return $rv; |
437 |
} |
} |
454 |
sub x_release { |
sub x_release { |
455 |
my ( $file, $mode ) = @_; |
my ( $file, $mode ) = @_; |
456 |
|
|
|
if ( $file =~ m#\Q/.fuse_hidden\E# ) { |
|
|
warn "release internal $file\n" if $debug; |
|
|
delete( $pending->{$file} ); |
|
|
return 0; |
|
|
} |
|
|
|
|
457 |
if ( ! defined( $pending->{$file} ) ) { |
if ( ! defined( $pending->{$file} ) ) { |
458 |
warn "release $file, NO PENDING DATA\n"; |
warn "release $file, NO PENDING DATA\n"; |
459 |
return 0; |
return 0; |
460 |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
461 |
warn "release $file, not written into\n"; |
warn "release $file, not written into\n"; |
462 |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
} elsif ( $file =~ m#\Q/.fuse_hidden\E# ) { |
463 |
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 ); |
|
|
|
|
464 |
} else { |
} else { |
465 |
warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; |
compress_file2path( $file ); |
466 |
} |
} |
467 |
|
|
468 |
$pending->{$file}->{open}--; |
$pending->{$file}->{open}--; |
469 |
if ( $pending->{$file}->{open} == 0 ) { |
if ( $pending->{$file}->{open} == 0 ) { |
470 |
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
471 |
delete( $pending->{$file} ); |
delete( $pending->{$file} ); |
472 |
} |
} |
473 |
|
|
474 |
return 0; |
return 0; |
475 |
} |
} |
476 |
|
|