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 = { |
47 |
sub real_name { |
sub real_name { |
48 |
my ( $dir, $name ) = @_; |
my ( $dir, $name ) = @_; |
49 |
if ( -e "$dir/${name}.gz" ) { |
if ( -e "$dir/${name}.gz" ) { |
50 |
|
confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name"; |
51 |
return "${name}.gz"; |
return "${name}.gz"; |
52 |
} |
} |
53 |
return $name; |
return $name; |
124 |
undef $s; |
undef $s; |
125 |
} |
} |
126 |
|
|
127 |
|
sub tmp_path { |
128 |
|
my $file = shift; |
129 |
|
|
130 |
|
my $path = fixup( $file ); |
131 |
|
|
132 |
|
my $op = 'UNKNOWN'; |
133 |
|
|
134 |
|
if (defined( $pending->{$file} )) { |
135 |
|
$path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending ); |
136 |
|
$op = 'opened'; |
137 |
|
} else { |
138 |
|
my $tmp = $mount->{tmp} . '/' . $file; |
139 |
|
if ( -e $tmp ) { |
140 |
|
$path = $tmp; |
141 |
|
$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 |
|
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 |
|
} |
160 |
|
return $path; |
161 |
|
} |
162 |
|
|
163 |
|
sub compress_file2path { |
164 |
|
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 |
184 |
|
if ( $dest =~ /\.gz$/ ) { |
185 |
|
warn "## remove old $dest\n"; |
186 |
|
unlink $dest || confess "can't remove $dest: $!"; |
187 |
|
$dest =~ s/\.gz$//; |
188 |
|
confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest; |
189 |
|
} |
190 |
|
|
191 |
|
if ( $path =~ $skip_extensions_regex ) { |
192 |
|
warn "$path [",-s $path,"] skipped compression\n"; |
193 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
194 |
|
} elsif ( -s $path < $min_compress_size ) { |
195 |
|
warn "$path [",-s $path,"] uncompressed, too small\n"; |
196 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
197 |
|
} else { |
198 |
|
warn "$path [",-s $path,"] compressing\n"; |
199 |
|
|
200 |
|
my $comp = $dest . '.gz'; |
201 |
|
file_copy( '<', $path, '>:gzip', $comp ); |
202 |
|
|
203 |
|
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
204 |
|
|
205 |
|
if ( $size_path <= $size_comp ) { |
206 |
|
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
207 |
|
unlink $comp || confess "can't remove: $comp: $!"; |
208 |
|
} else { |
209 |
|
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n"; |
210 |
|
|
211 |
|
# FIXME add timeout to remove uncompressed version? |
212 |
|
unlink $path || confess "can't remove $path: $!"; |
213 |
|
|
214 |
|
if ( -e $dest ) { |
215 |
|
warn "## cleanup uncompressed $dest\n" if $debug; |
216 |
|
unlink $dest || confess "can't remove $dest: $!"; |
217 |
|
} |
218 |
|
} |
219 |
|
|
220 |
|
} |
221 |
|
} |
222 |
|
|
223 |
sub x_open { |
sub x_open { |
224 |
my ($file) = shift; |
my ($file) = shift; |
225 |
my ($mode) = shift; |
my ($mode) = shift; |
235 |
return 0; |
return 0; |
236 |
} |
} |
237 |
|
|
|
$pending->{$file}->{open}++; |
|
|
|
|
238 |
my $mode_desc = { |
my $mode_desc = { |
239 |
rdonly => $mode && O_RDONLY, |
rdonly => $mode && O_RDONLY, |
240 |
rdwr => $mode && O_RDWR, |
rdwr => $mode && O_RDWR, |
242 |
create => $mode && O_CREAT, |
create => $mode && O_CREAT, |
243 |
trunc => $mode && O_TRUNC, |
trunc => $mode && O_TRUNC, |
244 |
}; |
}; |
245 |
my $path = fixup($file); |
|
246 |
|
my $path = tmp_path( $file ); |
247 |
|
|
248 |
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; |
|
249 |
|
|
250 |
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; |
|
|
} |
|
251 |
|
|
252 |
if ( sysopen($fh , $path, $mode) ) { |
if ( sysopen($fh , $path, $mode) ) { |
253 |
close($fh) || confess "can't close $path: $!"; |
close($fh) || confess "can't close $path: $!"; |
254 |
warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n"; |
warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n"; |
255 |
$pending->{$file}->{path} = $path; |
$pending->{$file}->{open}++; |
256 |
return 0; |
return 0; |
257 |
} else { |
} else { |
258 |
warn "ERROR: can't open $path -- $!"; |
warn "ERROR: can't open $path -- $!"; |
275 |
|
|
276 |
if(seek($fh,$off,SEEK_SET)) { |
if(seek($fh,$off,SEEK_SET)) { |
277 |
read($fh,$rv,$bufsize); |
read($fh,$rv,$bufsize); |
278 |
|
$pending->{$file}->{read} += length($rv) if $stats; |
279 |
} |
} |
280 |
|
|
281 |
return $rv; |
return $rv; |
292 |
return -ENOENT() unless -e $path; |
return -ENOENT() unless -e $path; |
293 |
|
|
294 |
$path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending ); |
$path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending ); |
295 |
confess "write into non-existant $path for $file: $!" unless -e $path; |
confess "write into non-existant $path for $file" unless -e $path; |
296 |
|
|
297 |
my $fh = new IO::File; |
my $fh = new IO::File; |
298 |
return -ENOSYS() unless open($fh,'+<',$path); |
return -ENOSYS() unless open($fh,'+<',$path); |
299 |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
300 |
$rv = print( $fh $buf ); |
$rv = print( $fh $buf ); |
301 |
warn "## write $path offset $off [",length( $buf ), "]\n" if $debug; |
my $size = length($buf); |
302 |
$pending->{$file}->{write}++; |
warn "## write $path offset $off [$size]\n" if $debug; |
303 |
|
$pending->{$file}->{write} += $size; |
304 |
} |
} |
305 |
$rv = -ENOSYS() unless $rv; |
$rv = -ENOSYS() unless $rv; |
306 |
close($fh) || warn "can't close $path: $!"; |
close($fh) || warn "can't close $path: $!"; |
419 |
|
|
420 |
sub x_truncate { |
sub x_truncate { |
421 |
my ( $file,$size ) = @_; |
my ( $file,$size ) = @_; |
422 |
my $path = fixup($file); |
|
423 |
|
#confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} ); |
424 |
|
|
425 |
|
my $path = tmp_path( $file ); |
426 |
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
|
if ( $path =~ m/\.gz$/ ) { |
|
|
my $no_gz = $path; |
|
|
$no_gz =~ s/\.gz$//; |
|
|
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
|
|
} |
|
427 |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
428 |
$pending->{$file}->{write}++; |
compress_file2path( $file, $path ); |
|
|
|
|
my $tmp = $mount->{tmp} . '/' . $file; |
|
|
truncate( $tmp, $size ) if -e $tmp; |
|
429 |
|
|
430 |
return $rv; |
return $rv; |
431 |
} |
} |
432 |
|
|
433 |
sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } |
sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } |
434 |
|
|
435 |
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 -$!; } |
448 |
sub x_release { |
sub x_release { |
449 |
my ( $file, $mode ) = @_; |
my ( $file, $mode ) = @_; |
450 |
|
|
|
if ( $file =~ m#\Q/.fuse_hidden\E# ) { |
|
|
warn "release internal $file\n" if $debug; |
|
|
delete( $pending->{$file} ); |
|
|
return 0; |
|
|
} |
|
|
|
|
451 |
if ( ! defined( $pending->{$file} ) ) { |
if ( ! defined( $pending->{$file} ) ) { |
452 |
warn "release $file, NO PENDING DATA\n"; |
warn "release $file, NO PENDING DATA\n"; |
453 |
return 0; |
return 0; |
454 |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
455 |
warn "release $file, not written into\n"; |
warn "release $file, not written into\n"; |
456 |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
} elsif ( $file =~ m#\Q/.fuse_hidden\E# ) { |
457 |
my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending ); |
warn "release internal $file\n" if $debug; |
|
my $dest = fixup( $file ); |
|
|
|
|
|
# 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: $!"; |
|
|
} |
|
|
} |
|
458 |
} else { |
} else { |
459 |
warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; |
compress_file2path( $file ); |
460 |
} |
} |
461 |
|
|
462 |
$pending->{$file}->{open}--; |
$pending->{$file}->{open}--; |
463 |
if ( $pending->{$file}->{open} == 0 ) { |
if ( $pending->{$file}->{open} == 0 ) { |
464 |
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
465 |
delete( $pending->{$file} ); |
delete( $pending->{$file} ); |
466 |
} |
} |
467 |
|
|
468 |
return 0; |
return 0; |
469 |
} |
} |
470 |
|
|