--- fuse-comp.pl 2007/07/08 15:04:35 5 +++ fuse-comp.pl 2007/07/08 17:04:18 8 @@ -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 @@ -24,7 +23,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 +70,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,32 +118,46 @@ 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; $pending->{$file}->{open}++; - warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, "\n"; + + 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; - 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 $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); + + $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; } @@ -250,7 +256,7 @@ # 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 +273,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";