--- fuse-comp.pl 2007/07/08 17:04:18 8 +++ fuse-comp.pl 2007/07/09 13:04:25 14 @@ -11,6 +11,7 @@ use File::Path; use Data::Dump qw/dump/; use Carp qw/confess/; +use IO::File; my $mount = { from => '/tmp/comp', @@ -102,13 +103,13 @@ sub file_copy { my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; - warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n"; + warn "## file_copy( $s_opt $s_path [",-s $s_path,"] $d_opt $d_path [",-e $d_path ? -s $d_path : 'new',"])\n"; open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending ); open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!"; my $buff; while( read( $s, $buff, 65535 ) ) { print $d $buff || confess "can't write into $d_path: $!"; - warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug; + warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug; } close($d) || warn "can't close $d_path: $!"; close($s) || warn "can't close $s_path: $!"; @@ -126,6 +127,18 @@ 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}++; my $mode_desc = { @@ -133,11 +146,12 @@ rdwr => $mode && O_RDWR, append => $mode && O_APPEND, create => $mode && O_CREAT, + trunc => $mode && O_TRUNC, }; - warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n"; + my $path = fixup($file); + warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n"; my $fh; - my $path = fixup($file); my $tmp = $mount->{tmp} . '/' . $file; if ( -e $tmp ) { $path = $tmp; @@ -148,17 +162,12 @@ file_copy( '<:gzip', $path, '>', $tmp ); $path = $tmp; } - warn ">>> open abs path: $path ", -s $path, " bytes\n"; + 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); - $pending->{$file}->{fh} = $fh; $pending->{$file}->{path} = $path; - $pending->{$file}->{mode} = { - rdonly => $mode && O_RDONLY, - rdwr => $mode && O_RDWR, - append => $mode && O_APPEND, - create => $mode && O_CREAT, - }; return 0; } @@ -166,28 +175,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); } @@ -225,7 +245,18 @@ return $err; } -sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; } +sub x_truncate { + my ( $file,$size ) = @_; + my $path = fixup($file); + 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: $!"; + } + warn "## truncate( $file $size ) $path [", -s $path, "]\n"; + return $rv; +} sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; } @@ -249,7 +280,6 @@ } 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 ); @@ -264,7 +294,7 @@ warn "release $file $mode -- uncompressed\n"; file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); } elsif ( -s $path < $min_compress_size ) { - warn "release $file -- uncompressed, too small ", -s $path, " bytes\n"; + warn "release $file -- uncompressed, too small [", -s $path, "]\n"; file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); } else { warn "release $file $mode -- compressing\n"; @@ -273,14 +303,17 @@ file_copy( '<', $path, '>:gzip', $dest . '.gz' ); # FIXME add timeout to remove uncompressed version? + # FIXME leave uncompressed file if smaller than compressed unlink $path || warn "can't remove $path: $!"; } } else { warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; - $pending->{$file}->{open}--; - return 0; } - delete( $pending->{$file} ); + $pending->{$file}->{open}--; + if ( $pending->{$file}->{open} == 0 ) { + warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; + delete( $pending->{$file} ); + } return 0; }