12 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
13 |
use Carp qw/confess/; |
use Carp qw/confess/; |
14 |
use IO::File; |
use IO::File; |
15 |
|
use Getopt::Long; |
16 |
|
|
17 |
|
my $debug = 0; |
18 |
|
my $fuse_debug = 0; |
19 |
|
|
20 |
|
GetOptions( |
21 |
|
'debug+' => \$debug, |
22 |
|
'fuse-debug+' => \$fuse_debug, |
23 |
|
); |
24 |
|
|
25 |
my $mount = { |
my $mount = { |
26 |
from => '/tmp/comp', |
from => '/tmp/comp', |
28 |
tmp => '/dev/shm/comp', |
tmp => '/dev/shm/comp', |
29 |
}; |
}; |
30 |
|
|
31 |
my $debug = 1; |
my $debug = shift @ARGV; |
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/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i; |
34 |
|
|
112 |
|
|
113 |
sub file_copy { |
sub file_copy { |
114 |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
115 |
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; |
116 |
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 ); |
117 |
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: $!"; |
118 |
my $buff; |
my $buff; |
158 |
trunc => $mode && O_TRUNC, |
trunc => $mode && O_TRUNC, |
159 |
}; |
}; |
160 |
my $path = fixup($file); |
my $path = fixup($file); |
161 |
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; |
162 |
my $fh; |
my $fh; |
163 |
|
|
164 |
my $tmp = $mount->{tmp} . '/' . $file; |
my $tmp = $mount->{tmp} . '/' . $file; |
168 |
my $dest_path = $tmp; |
my $dest_path = $tmp; |
169 |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
170 |
mkpath $dest_path unless -e $dest_path; |
mkpath $dest_path unless -e $dest_path; |
171 |
file_copy( '<:gzip', $path, '>', $tmp ); |
if ( -s $path ) { |
172 |
|
file_copy( '<:gzip', $path, '>', $tmp ) |
173 |
|
} else { |
174 |
|
warn "ERROR: filesystem corruption, $path is zero size\n"; |
175 |
|
} |
176 |
$path = $tmp; |
$path = $tmp; |
177 |
} |
} |
|
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); |
|
178 |
|
|
179 |
$pending->{$file}->{path} = $path; |
if ( sysopen($fh , $path, $mode) ) { |
180 |
return 0; |
close($fh) || confess "can't close $path: $!"; |
181 |
|
warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n"; |
182 |
|
$pending->{$file}->{path} = $path; |
183 |
|
return 0; |
184 |
|
} else { |
185 |
|
warn "ERROR: can't open $path : $!"; |
186 |
|
return -$!; |
187 |
|
} |
188 |
|
|
189 |
} |
} |
190 |
|
|
191 |
sub x_read { |
sub x_read { |
221 |
return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path}); |
return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path}); |
222 |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
223 |
$rv = print( $fh $buf ); |
$rv = print( $fh $buf ); |
224 |
warn "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; |
warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; |
225 |
} |
} |
226 |
$rv = -ENOSYS() unless $rv; |
$rv = -ENOSYS() unless $rv; |
227 |
close($fh); |
close($fh); |
236 |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
237 |
|
|
238 |
sub x_rename { |
sub x_rename { |
239 |
my ($old) = fixup(shift); |
my ($old,$new) = @_; |
240 |
my ($new) = fixup(shift); |
my $old_path = fixup($old); |
241 |
my ($err) = rename($old,$new) ? 0 : -ENOENT(); |
my $new_path = fixup($new); |
242 |
|
$new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ ); |
243 |
|
|
244 |
|
my $err = rename($old_path,$new_path) ? 0 : -ENOENT(); |
245 |
|
warn "## rename( $old_path => $new_path ) = $err\n"; |
246 |
|
|
247 |
|
my $tmp = $mount->{tmp} . '/' . $old; |
248 |
|
if ( -e $tmp ) { |
249 |
|
my $new_tmp = $mount->{tmp} . '/' . $new; |
250 |
|
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
251 |
|
} |
252 |
|
|
253 |
|
if (defined( $pending->{$old} )) { |
254 |
|
$pending->{$new} = $pending->{$old}; |
255 |
|
|
256 |
|
my $path = $pending->{$old}->{path}; |
257 |
|
$path =~ s/\Q$old\E/$new/; |
258 |
|
$pending->{$new}->{path} = $path; |
259 |
|
$pending->{$old}->{path} = $path; |
260 |
|
|
261 |
|
#delete( $pending->{$old} ); |
262 |
|
} |
263 |
|
|
264 |
return $err; |
return $err; |
265 |
} |
} |
266 |
sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } |
sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } |
293 |
$no_gz =~ s/\.gz$//; |
$no_gz =~ s/\.gz$//; |
294 |
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
295 |
} |
} |
296 |
warn "## truncate( $file $size ) $path [", -s $path, "]\n"; |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
297 |
$pending->{$file}->{write}++; |
$pending->{$file}->{write}++; |
298 |
return $rv; |
return $rv; |
299 |
} |
} |
331 |
} |
} |
332 |
|
|
333 |
if ( $file =~ $skip_extensions_regex ) { |
if ( $file =~ $skip_extensions_regex ) { |
334 |
warn "release $file $mode -- uncompressed\n"; |
warn "release $file [",-s $path,"] skipped compression\n"; |
335 |
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
336 |
} elsif ( -s $path < $min_compress_size ) { |
} elsif ( -s $path < $min_compress_size ) { |
337 |
warn "release $file -- uncompressed, too small [", -s $path, "]\n"; |
warn "release $file [",-s $path,"] uncompressed, too small\n"; |
338 |
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
339 |
} else { |
} else { |
340 |
warn "release $file $mode -- compressing\n"; |
warn "release $file [",-s $path,"] compressing\n"; |
341 |
|
|
342 |
|
my $comp = $dest . '.gz'; |
343 |
|
file_copy( '<', $path, '>:gzip', $comp ); |
344 |
|
|
345 |
file_copy( '<', $path, '>:gzip', $dest . '.gz' ); |
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
346 |
|
|
347 |
# FIXME add timeout to remove uncompressed version? |
if ( $size_path <= $size_comp ) { |
348 |
# FIXME leave uncompressed file if smaller than compressed |
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
349 |
unlink $path || warn "can't remove $path: $!"; |
unlink $comp || warn "can't reamove: $comp: $!"; |
350 |
|
} else { |
351 |
|
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; |
352 |
|
# FIXME add timeout to remove uncompressed version? |
353 |
|
unlink $path || warn "can't remove $path: $!"; |
354 |
|
} |
355 |
} |
} |
356 |
} else { |
} else { |
357 |
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"; |
388 |
statfs =>"main::x_statfs", |
statfs =>"main::x_statfs", |
389 |
release =>"main::x_release", |
release =>"main::x_release", |
390 |
# threaded=>1, |
# threaded=>1, |
391 |
# debug => 1, |
debug => $fuse_debug, |
392 |
); |
); |