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 |
|
|
|
my $debug = shift @ARGV; |
|
|
|
|
31 |
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; |
32 |
|
|
33 |
# don't compress files smaller than this |
# don't compress files smaller than this |
76 |
return unpack("L", $buff); |
return unpack("L", $buff); |
77 |
} |
} |
78 |
|
|
|
sub unlink_all { |
|
|
my $file = shift; |
|
|
warn "# unlink_all( $file )\n"; |
|
|
|
|
|
my $path = fixup( $file ); |
|
|
unlink $path || return 0; |
|
|
|
|
|
my $tmp = $mount->{tmp} . '/' . $file; |
|
|
unlink $tmp if ( -e $tmp ); |
|
|
|
|
|
delete( $pending->{$file} ); |
|
|
return 1; |
|
|
} |
|
|
|
|
79 |
sub x_getattr { |
sub x_getattr { |
80 |
my ($file) = fixup(shift); |
my ($file) = fixup(shift); |
81 |
my (@list) = lstat($file); |
my (@list) = lstat($file); |
152 |
my $dest_path = $tmp; |
my $dest_path = $tmp; |
153 |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
154 |
mkpath $dest_path unless -e $dest_path; |
mkpath $dest_path unless -e $dest_path; |
155 |
file_copy( '<:gzip', $path, '>', $tmp ); |
if ( -s $path ) { |
156 |
|
file_copy( '<:gzip', $path, '>', $tmp ) |
157 |
|
} else { |
158 |
|
warn "ERROR: filesystem corruption, $path is zero size\n"; |
159 |
|
} |
160 |
$path = $tmp; |
$path = $tmp; |
161 |
} |
} |
|
warn "<<< open abs path: $path [", -e $path ? -s $path : 'new' , "]\n"; |
|
|
return -$! unless sysopen($fh , $path, $mode); |
|
|
close($fh); |
|
162 |
|
|
163 |
$pending->{$file}->{path} = $path; |
if ( sysopen($fh , $path, $mode) ) { |
164 |
return 0; |
close($fh) || confess "can't close $path: $!"; |
165 |
|
warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n"; |
166 |
|
$pending->{$file}->{path} = $path; |
167 |
|
return 0; |
168 |
|
} else { |
169 |
|
warn "ERROR: can't open $path -- $!"; |
170 |
|
return -$!; |
171 |
|
} |
172 |
|
|
173 |
} |
} |
174 |
|
|
175 |
sub x_read { |
sub x_read { |
193 |
|
|
194 |
sub x_write { |
sub x_write { |
195 |
my ($file,$buf,$off) = @_; |
my ($file,$buf,$off) = @_; |
196 |
$pending->{$file}->{write}++; |
|
197 |
my $rv; |
my $rv; |
198 |
my $path = fixup($file); |
my $path = fixup($file); |
199 |
|
|
201 |
|
|
202 |
return -ENOENT() unless -e $path; |
return -ENOENT() unless -e $path; |
203 |
|
|
204 |
|
$path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending ); |
205 |
|
confess "write into non-existant $path for $file: $!" unless -e $path; |
206 |
|
|
207 |
my $fh = new IO::File; |
my $fh = new IO::File; |
208 |
return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path}); |
return -ENOSYS() unless open($fh,'+<',$path); |
209 |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
210 |
$rv = print( $fh $buf ); |
$rv = print( $fh $buf ); |
211 |
warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; |
warn "## write $path offset $off [",length( $buf ), "]\n" if $debug; |
212 |
|
$pending->{$file}->{write}++; |
213 |
} |
} |
214 |
$rv = -ENOSYS() unless $rv; |
$rv = -ENOSYS() unless $rv; |
215 |
close($fh); |
close($fh) || warn "can't close $path: $!"; |
216 |
return length($buf); |
return length($buf); |
217 |
} |
} |
218 |
|
|
219 |
sub err { return (-shift || -$!) } |
sub err { return (-shift || -$!) } |
220 |
|
|
221 |
sub x_readlink { return readlink(fixup(shift)); } |
sub x_readlink { return readlink(fixup(shift)); } |
222 |
sub x_unlink { return unlink_all( shift ) ? 0 : -$! } |
sub x_unlink { |
223 |
|
my $file = shift; |
224 |
|
my $path = fixup( $file ); |
225 |
|
|
226 |
|
if ( $file =~ m#\Q/.fuse_hidden\E# ) { |
227 |
|
return unlink $path ? 0 : -$1; |
228 |
|
} |
229 |
|
|
230 |
|
warn "# unlink( $file )\n"; |
231 |
|
|
232 |
|
unlink $path || return 0; |
233 |
|
|
234 |
|
my $tmp = $mount->{tmp} . '/' . $file; |
235 |
|
unlink $tmp if ( -e $tmp ); |
236 |
|
|
237 |
|
delete( $pending->{$file} ); |
238 |
|
return 0; |
239 |
|
} |
240 |
|
|
241 |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
242 |
|
|
251 |
|
|
252 |
my $tmp = $mount->{tmp} . '/' . $old; |
my $tmp = $mount->{tmp} . '/' . $old; |
253 |
if ( -e $tmp ) { |
if ( -e $tmp ) { |
254 |
my $new_tmp = $mount->{tmp} . '/' . $new; |
if ( $new =~ m#\Q/.fuse_hidden\E# ) { |
255 |
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
unlink $tmp || confess "can't unlink $tmp for $new\n"; |
256 |
|
} else { |
257 |
|
my $new_tmp = $mount->{tmp} . '/' . $new; |
258 |
|
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
259 |
|
} |
260 |
} |
} |
261 |
|
|
262 |
if (defined( $pending->{$old} )) { |
if (defined( $pending->{$old} )) { |
267 |
$pending->{$new}->{path} = $path; |
$pending->{$new}->{path} = $path; |
268 |
|
|
269 |
delete( $pending->{$old} ); |
delete( $pending->{$old} ); |
270 |
|
warn "## tweaking pending to ", dump( $pending ) if $debug; |
271 |
} |
} |
272 |
|
|
273 |
return $err; |
return $err; |
275 |
sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } |
sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } |
276 |
|
|
277 |
sub x_chown { |
sub x_chown { |
278 |
my ($path) = fixup(shift); |
my ($file,$uid,$gid) = @_; |
279 |
|
my $path = fixup($file); |
280 |
print "nonexistent $path\n" unless -e $path; |
print "nonexistent $path\n" unless -e $path; |
|
my ($uid,$gid) = @_; |
|
281 |
# perl's chown() does not chown symlinks, it chowns the symlink's |
# perl's chown() does not chown symlinks, it chowns the symlink's |
282 |
# target. it fails when the link's target doesn't exist, because |
# target. it fails when the link's target doesn't exist, because |
283 |
# the stat64() syscall fails. |
# the stat64() syscall fails. |
284 |
# this causes error messages when unpacking symlinks in tarballs. |
# this causes error messages when unpacking symlinks in tarballs. |
285 |
my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0; |
my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0; |
286 |
|
|
287 |
|
my $tmp = $mount->{tmp} . '/' . $file; |
288 |
|
syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp; |
289 |
|
|
290 |
return $err; |
return $err; |
291 |
} |
} |
292 |
|
|
306 |
$no_gz =~ s/\.gz$//; |
$no_gz =~ s/\.gz$//; |
307 |
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
308 |
} |
} |
309 |
warn "## truncate( $file $size ) $path [", -s $path, "]\n"; |
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
310 |
$pending->{$file}->{write}++; |
$pending->{$file}->{write}++; |
311 |
return $rv; |
return $rv; |
312 |
} |
} |
327 |
|
|
328 |
sub x_release { |
sub x_release { |
329 |
my ( $file, $mode ) = @_; |
my ( $file, $mode ) = @_; |
330 |
|
|
331 |
|
if ( $file =~ m#\Q/.fuse_hidden\E# ) { |
332 |
|
warn "release internal $file\n" if $debug; |
333 |
|
delete( $pending->{$file} ); |
334 |
|
return 0; |
335 |
|
} |
336 |
|
|
337 |
if ( ! defined( $pending->{$file} ) ) { |
if ( ! defined( $pending->{$file} ) ) { |
338 |
warn "release $file, NO PENDING DATA\n"; |
warn "release $file, NO PENDING DATA\n"; |
339 |
return 0; |
return 0; |
408 |
statfs =>"main::x_statfs", |
statfs =>"main::x_statfs", |
409 |
release =>"main::x_release", |
release =>"main::x_release", |
410 |
# threaded=>1, |
# threaded=>1, |
411 |
# debug => 1, |
debug => $fuse_debug, |
412 |
); |
); |