--- fuse-comp.pl 2007/07/08 12:51:33 1 +++ fuse-comp.pl 2007/07/09 15:59:57 19 @@ -4,13 +4,14 @@ 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 use PerlIO::gzip; use File::Path; use Data::Dump qw/dump/; +use Carp qw/confess/; +use IO::File; my $mount = { from => '/tmp/comp', @@ -18,9 +19,12 @@ tmp => '/dev/shm/comp', }; -my $debug = 1; +my $debug = 0; + +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[px]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg)$/i; +# don't compress files smaller than this +my $min_compress_size = 512; foreach my $dir ( keys %$mount ) { if ( ! -e $mount->{$dir} ) { @@ -65,6 +69,20 @@ 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); @@ -85,42 +103,70 @@ sub file_copy { my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; - warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n"; - open(my $s, $s_opt, $s_path ) || die "can't open $s_path: $!"; - open(my $d, $d_opt, $d_path ) || die "can't open $d_path: $!"; + warn "## file_copy( $s_opt $s_path [",-s $s_path,"] $d_opt $d_path [",-e $d_path ? -s $d_path : 'new',"])\n" if $debug; + 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 || die "can't write into $d_path: $!"; - warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug; + print $d $buff || confess "can't write into $d_path: $!"; + 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: $!"; + warn "-- $s_path [", -s $s_path, "] >>> $d_path [", -s $d_path, "]\n" if $debug; + my ($mode,$uid,$gid,$atime,$mtime) = (stat $s_path)[2,4,5,8,9]; + + 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}++; + + my $mode_desc = { + rdonly => $mode && O_RDONLY, + rdwr => $mode && O_RDWR, + append => $mode && O_APPEND, + create => $mode && O_CREAT, + trunc => $mode && O_TRUNC, + }; + my $path = fixup($file); + warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug; my $fh; - if ( $pending->{$file}->{open} == 1 ) { - warn "# open( $file, $mode )\n"; - 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; - } - return -$! unless sysopen($fh , $path, $mode); - $pending->{$file}->{fh} = $fh; - $pending->{$file}->{path} = $path; - } elsif ( ! defined( $pending->{$file}->{fh} ) ) { - die "can't find fh for $file ", dump($pending); + + 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 [", -e $path ? -s $path : 'new' , "]\n"; + return -$! unless sysopen($fh , $path, $mode); + close($fh); + + $pending->{$file}->{path} = $path; return 0; } @@ -128,42 +174,64 @@ 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} || die "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 "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; } $rv = -ENOSYS() unless $rv; + close($fh); return length($buf); } sub err { return (-shift || -$!) } sub x_readlink { return readlink(fixup(shift)); } -sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; } +sub x_unlink { return unlink_all( shift ) ? 0 : -$! } sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } sub x_rename { - my ($old) = fixup(shift); - my ($new) = fixup(shift); - my ($err) = rename($old,$new) ? 0 : -ENOENT(); + my ($old,$new) = @_; + my $old_path = fixup($old); + my $new_path = fixup($new); + $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ ); + + my $err = rename($old_path,$new_path) ? 0 : -ENOENT(); + warn "## rename( $old_path => $new_path ) = $err\n"; + + 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 : $!"; + } + return $err; } sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } @@ -187,7 +255,19 @@ 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"; + $pending->{$file}->{write}++; + 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 -$!; } @@ -211,32 +291,40 @@ } 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 $dest || confess "can't remove $dest: $!"; + $dest =~ s/\.gz$//; + } + if ( $file =~ $skip_extensions_regex ) { 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, "]\n"; + file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); } else { warn "release $file $mode -- compressing\n"; - my $path = $pending->{$file}->{path} || die "no path for $file ? ", dump( $pending ); - my $dest = fixup( $file ); - - if ( $dest =~ /\.gz$/ ) { - warn "## remove old $dest\n"; - unlink $dest || die "can't remove $dest: $!"; - $dest =~ s/\.gz$//; - } 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; }