--- fuse-comp.pl 2007/07/08 15:04:35 5 +++ fuse-comp.pl 2007/07/09 10:35:02 12 @@ -4,7 +4,6 @@ use threads::shared; use Fuse; -use IO::File; use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT); use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET); require 'syscall.ph'; # for SYS_mknod and SYS_lchown @@ -12,6 +11,7 @@ use File::Path; use Data::Dump qw/dump/; use Carp qw/confess/; +use IO::File; my $mount = { from => '/tmp/comp', @@ -24,7 +24,7 @@ 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 -my $min_compress_size = 256; +my $min_compress_size = 512; foreach my $dir ( keys %$mount ) { if ( ! -e $mount->{$dir} ) { @@ -71,21 +71,14 @@ sub unlink_all { my $file = shift; - foreach my $dir ( keys %$mount ) { - my $path = $mount->{$dir} . '/' . $file; - - map { - my $path = $_; - if ( -e $path ) { - if ( unlink $path ) { - warn "## unlink $path\n" if $debug; - } else { - warn "can't unlink $path: $!\n"; - return 0; - } - } - } [ $path, $path . '.gz' ]; - } + 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; } @@ -126,33 +119,53 @@ chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n"; chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n"; utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n"; + + undef $d; + undef $s; } sub x_open { my ($file) = shift; my ($mode) = shift; + + if ( $file eq '/.debug' ) { + my $path = $mount->{from} . '/.debug'; + open( my $debug, '>', $path ) || die "can't open $path: $!"; + my $dump = dump( $pending ); + print $debug "pending = $dump\n"; + close($debug); + $pending->{'/.debug'}->{path} = $path; + warn "## created dump $path $dump\n"; + return 0; + } + $pending->{$file}->{open}++; - warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, "\n"; - my $fh; - if ( $pending->{$file}->{open} == 1 ) { - my $path = fixup($file); - my $tmp = $mount->{tmp} . '/' . $file; - warn ">>> open abs path: $path\n"; - 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; - file_copy( '<:gzip', $path, '>', $tmp ); - $path = $tmp; - } - return -$! unless sysopen($fh , $path, $mode); - $pending->{$file}->{fh} = $fh; - $pending->{$file}->{path} = $path; - } elsif ( ! defined( $pending->{$file}->{fh} ) ) { - confess "can't find fh for $file ", dump($pending); + + my $mode_desc = { + rdonly => $mode && O_RDONLY, + rdwr => $mode && O_RDWR, + append => $mode && O_APPEND, + create => $mode && O_CREAT, }; + warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n"; + my $fh; + + my $path = fixup($file); + my $tmp = $mount->{tmp} . '/' . $file; + 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; + file_copy( '<:gzip', $path, '>', $tmp ); + $path = $tmp; + } + warn ">>> open abs path: $path ", -s $path, " bytes\n"; + return -$! unless sysopen($fh , $path, $mode); + close($fh); + + $pending->{$file}->{path} = $path; return 0; } @@ -160,28 +173,39 @@ my ($file,$bufsize,$off) = @_; my ($rv) = -ENOSYS(); my $path = fixup( $file ); + + confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} ); + return -ENOENT() unless -e $path; - my ($fsize) = -s $path; - my $fh = $pending->{$file}->{fh} || confess "no fh? ", dump( $pending ); + + my $fh = new IO::File; + return -ENOSYS() unless open($fh,$pending->{$file}->{path}); + if(seek($fh,$off,SEEK_SET)) { read($fh,$rv,$bufsize); } + return $rv; } sub x_write { my ($file,$buf,$off) = @_; $pending->{$file}->{write}++; - my ($rv); + my $rv; my $path = fixup($file); + + confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} ); + return -ENOENT() unless -e $path; - my ($fsize) = -s $path; - my $fh = $pending->{$file}->{fh}; - return -ENOSYS() unless $fh; + + my $fh = new IO::File; + return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path}); if($rv = seek( $fh ,$off,SEEK_SET)) { $rv = print( $fh $buf ); + warn "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; } $rv = -ENOSYS() unless $rv; + close($fh); return length($buf); } @@ -243,14 +267,13 @@ } elsif ( ! defined( $pending->{$file}->{write} ) ) { warn "release $file, not written into\n"; } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { - close( $pending->{$file}->{fh} ) || warn "can't close $file: $!"; my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending ); my $dest = fixup( $file ); # cleanup old compressed copy if ( $dest =~ /\.gz$/ ) { warn "## remove old $dest\n"; - unlink_all( $file ) || confess "can't remove $dest: $!"; + unlink $dest || confess "can't remove $dest: $!"; $dest =~ s/\.gz$//; } @@ -267,7 +290,7 @@ file_copy( '<', $path, '>:gzip', $dest . '.gz' ); # FIXME add timeout to remove uncompressed version? - unlink_all( $file ) || warn "can't remove $path: $!"; + unlink $path || warn "can't remove $path: $!"; } } else { warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";