--- fuse-comp.pl 2007/07/09 17:35:26 21 +++ fuse-comp.pl 2007/07/09 23:28:58 26 @@ -12,6 +12,15 @@ use Data::Dump qw/dump/; use Carp qw/confess/; use IO::File; +use Getopt::Long; + +my $debug = 0; +my $fuse_debug = 0; + +GetOptions( + 'debug+' => \$debug, + 'fuse-debug+' => \$fuse_debug, +); my $mount = { from => '/tmp/comp', @@ -19,8 +28,6 @@ tmp => '/dev/shm/comp', }; -my $debug = shift @ARGV; - my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i; # don't compress files smaller than this @@ -69,20 +76,6 @@ return unpack("L", $buff); } -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; -} - sub x_getattr { my ($file) = fixup(shift); my (@list) = lstat($file); @@ -159,7 +152,11 @@ my $dest_path = $tmp; $dest_path =~ s!/[^/]+$!!; #!vim-fix mkpath $dest_path unless -e $dest_path; - file_copy( '<:gzip', $path, '>', $tmp ); + if ( -s $path ) { + file_copy( '<:gzip', $path, '>', $tmp ) + } else { + warn "ERROR: filesystem corruption, $path is zero size\n"; + } $path = $tmp; } @@ -169,7 +166,7 @@ $pending->{$file}->{path} = $path; return 0; } else { - warn "ERROR: can't open $path : $!"; + warn "ERROR: can't open $path -- $!"; return -$!; } @@ -196,7 +193,7 @@ sub x_write { my ($file,$buf,$off) = @_; - $pending->{$file}->{write}++; + my $rv; my $path = fixup($file); @@ -204,21 +201,42 @@ return -ENOENT() unless -e $path; + $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending ); + confess "write into non-existant $path for $file: $!" unless -e $path; + my $fh = new IO::File; - return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path}); + return -ENOSYS() unless open($fh,'+<',$path); if($rv = seek( $fh ,$off,SEEK_SET)) { $rv = print( $fh $buf ); - warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; + warn "## write $path offset $off [",length( $buf ), "]\n" if $debug; + $pending->{$file}->{write}++; } $rv = -ENOSYS() unless $rv; - close($fh); + close($fh) || warn "can't close $path: $!"; return length($buf); } sub err { return (-shift || -$!) } sub x_readlink { return readlink(fixup(shift)); } -sub x_unlink { return unlink_all( shift ) ? 0 : -$! } +sub x_unlink { + my $file = shift; + my $path = fixup( $file ); + + if ( $file =~ m#\Q/.fuse_hidden\E# ) { + return unlink $path ? 0 : -$1; + } + + warn "# unlink( $file )\n"; + + unlink $path || return 0; + + my $tmp = $mount->{tmp} . '/' . $file; + unlink $tmp if ( -e $tmp ); + + delete( $pending->{$file} ); + return 0; +} sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } @@ -233,8 +251,12 @@ my $tmp = $mount->{tmp} . '/' . $old; if ( -e $tmp ) { - my $new_tmp = $mount->{tmp} . '/' . $new; - rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; + if ( $new =~ m#\Q/.fuse_hidden\E# ) { + unlink $tmp || confess "can't unlink $tmp for $new\n"; + } else { + my $new_tmp = $mount->{tmp} . '/' . $new; + rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; + } } if (defined( $pending->{$old} )) { @@ -245,6 +267,7 @@ $pending->{$new}->{path} = $path; delete( $pending->{$old} ); + warn "## tweaking pending to ", dump( $pending ) if $debug; } return $err; @@ -252,14 +275,18 @@ sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } sub x_chown { - my ($path) = fixup(shift); + my ($file,$uid,$gid) = @_; + my $path = fixup($file); print "nonexistent $path\n" unless -e $path; - my ($uid,$gid) = @_; # perl's chown() does not chown symlinks, it chowns the symlink's # target. it fails when the link's target doesn't exist, because # the stat64() syscall fails. # this causes error messages when unpacking symlinks in tarballs. my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0; + + my $tmp = $mount->{tmp} . '/' . $file; + syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp; + return $err; } @@ -300,6 +327,13 @@ sub x_release { my ( $file, $mode ) = @_; + + if ( $file =~ m#\Q/.fuse_hidden\E# ) { + warn "release internal $file\n" if $debug; + delete( $pending->{$file} ); + return 0; + } + if ( ! defined( $pending->{$file} ) ) { warn "release $file, NO PENDING DATA\n"; return 0; @@ -374,5 +408,5 @@ statfs =>"main::x_statfs", release =>"main::x_release", # threaded=>1, -# debug => 1, + debug => $fuse_debug, );