/[fuse-comp]/fuse-comp.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /fuse-comp.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5 by dpavlin, Sun Jul 8 15:04:35 2007 UTC revision 27 by dpavlin, Tue Jul 10 00:22:00 2007 UTC
# Line 4  use threads; Line 4  use threads;
4  use threads::shared;  use threads::shared;
5    
6  use Fuse;  use Fuse;
 use IO::File;  
7  use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);  use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
8  use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);  use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);
9  require 'syscall.ph'; # for SYS_mknod and SYS_lchown  require 'syscall.ph'; # for SYS_mknod and SYS_lchown
# Line 12  use PerlIO::gzip; Line 11  use PerlIO::gzip;
11  use File::Path;  use File::Path;
12  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
13  use Carp qw/confess/;  use Carp qw/confess/;
14    use IO::File;
15    use Getopt::Long;
16    
17    my $debug = 0;
18    my $fuse_debug = 0;
19    
20    GetOptions(
21            'debug+' => \$debug,
22            'fuse-debug+' => \$fuse_debug,
23    );
24    
25  my $mount = {  my $mount = {
26          from    => '/tmp/comp',          from    => '/tmp/comp',
# Line 19  my $mount = { Line 28  my $mount = {
28          tmp             => '/dev/shm/comp',          tmp             => '/dev/shm/comp',
29  };  };
30    
 my $debug = 1;  
   
31  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[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i;
32    
33  # don't compress files smaller than this  # don't compress files smaller than this
34  my $min_compress_size = 256;  my $min_compress_size = 512;
35    
36  foreach my $dir ( keys %$mount ) {  foreach my $dir ( keys %$mount ) {
37          if ( ! -e $mount->{$dir} ) {          if ( ! -e $mount->{$dir} ) {
# Line 35  foreach my $dir ( keys %$mount ) { Line 42  foreach my $dir ( keys %$mount ) {
42    
43  my $pending;  my $pending;
44    
45    sub real_name {
46            my ( $dir, $name ) = @_;
47            if ( -e "$dir/${name}.gz" ) {
48                    return "${name}.gz";
49            }
50            return $name;
51    }
52    
53  sub fixup {  sub fixup {
54          my ( $path ) = @_;          my ( $path ) = @_;
55          my $full = $mount->{from} . '/' . $path;          return $mount->{from} . '/' . real_name( $mount->{from}, $path );
         if ( -e $full . '.gz' ) {  
                 return $full . '.gz';  
         }  
         return $full;  
56  }  }
57    
58  sub original_name {  sub original_name {
# Line 69  sub gzip_original_size { Line 80  sub gzip_original_size {
80          return unpack("L", $buff);          return unpack("L", $buff);
81  }  }
82    
 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' ];  
         }  
         delete( $pending->{$file} );  
         return 1;  
 }  
   
83  sub x_getattr {  sub x_getattr {
84          my ($file) = fixup(shift);          my ($file) = fixup(shift);
85          my (@list) = lstat($file);          my (@list) = lstat($file);
# Line 110  sub x_getdir { Line 100  sub x_getdir {
100    
101  sub file_copy {  sub file_copy {
102          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
103          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" if $debug;
104          open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending );          open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending );
105          open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!";          open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!";
106          my $buff;          my $buff;
107          while( read( $s, $buff, 65535 ) ) {          while( read( $s, $buff, 65535 ) ) {
108                  print $d $buff || confess "can't write into $d_path: $!";                  print $d $buff || confess "can't write into $d_path: $!";
109                  warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;                  warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
110          }          }
111          close($d) || warn "can't close $d_path: $!";          close($d) || warn "can't close $d_path: $!";
112          close($s) || warn "can't close $s_path: $!";          close($s) || warn "can't close $s_path: $!";
# Line 126  sub file_copy { Line 116  sub file_copy {
116          chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";          chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";
117          chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";          chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";
118          utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";          utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
119    
120            undef $d;
121            undef $s;
122  }  }
123    
124  sub x_open {  sub x_open {
125          my ($file) = shift;          my ($file) = shift;
126          my ($mode) = shift;          my ($mode) = shift;
127    
128            if ( $file eq '/.debug' ) {
129                    my $path = $mount->{from} . '/.debug';
130                    open( my $debug, '>', $path ) || die "can't open $path: $!";
131                    my $dump = dump( $pending );
132                    print $debug "pending = $dump\n";
133                    close($debug);
134                    $pending->{'/.debug'}->{path} = $path;
135                    warn "## created dump $path $dump\n";
136                    return 0;
137            }
138    
139          $pending->{$file}->{open}++;          $pending->{$file}->{open}++;
140          warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, "\n";  
141            my $mode_desc = {
142                    rdonly => $mode && O_RDONLY,
143                    rdwr => $mode && O_RDWR,
144                    append => $mode && O_APPEND,
145                    create => $mode && O_CREAT,
146                    trunc => $mode && O_TRUNC,
147            };
148            my $path = fixup($file);
149            warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
150          my $fh;          my $fh;
151          if ( $pending->{$file}->{open} == 1 ) {  
152                  my $path = fixup($file);          my $tmp = $mount->{tmp} . '/' . $file;
153                  my $tmp = $mount->{tmp} . '/' . $file;          if ( -e $tmp ) {
154                  warn ">>> open abs path: $path\n";                  $path = $tmp;
155                  if ( -e $tmp ) {          } elsif ( $path =~ m/\.gz$/ ) {
156                          $path = $tmp;                  my $dest_path = $tmp;
157                  } elsif ( $path =~ m/\.gz$/ ) {                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix
158                          my $dest_path = $tmp;                  mkpath $dest_path unless -e $dest_path;
159                          $dest_path =~ s!/[^/]+$!!;      #!vim-fix                  if ( -s $path ) {
160                          mkpath $dest_path unless -e $dest_path;                          file_copy( '<:gzip', $path, '>', $tmp )
161                          file_copy( '<:gzip', $path, '>', $tmp );                  } else {
162                          $path = $tmp;                          warn "ERROR: filesystem corruption, $path is zero size\n";
163                  }                  }
164                  return -$! unless sysopen($fh , $path, $mode);                  $path = $tmp;
165                  $pending->{$file}->{fh} = $fh;          }
166    
167            if ( sysopen($fh , $path, $mode) ) {
168                    close($fh) || confess "can't close $path: $!";
169                    warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n";
170                  $pending->{$file}->{path} = $path;                  $pending->{$file}->{path} = $path;
171          } elsif ( ! defined( $pending->{$file}->{fh} ) ) {                  return 0;
172                  confess "can't find fh for $file ", dump($pending);          } else {
173          };                  warn "ERROR: can't open $path -- $!";
174          return 0;                  return -$!;
175            }
176    
177  }  }
178    
179  sub x_read {  sub x_read {
180          my ($file,$bufsize,$off) = @_;          my ($file,$bufsize,$off) = @_;
181          my ($rv) = -ENOSYS();          my ($rv) = -ENOSYS();
182          my $path = fixup( $file );          my $path = fixup( $file );
183    
184            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
185    
186          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
187          my ($fsize) = -s $path;  
188          my $fh = $pending->{$file}->{fh} || confess "no fh? ", dump( $pending );          my $fh = new IO::File;
189            return -ENOSYS() unless open($fh,$pending->{$file}->{path});
190    
191          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
192                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
193          }          }
194    
195          return $rv;          return $rv;
196  }  }
197    
198  sub x_write {  sub x_write {
199          my ($file,$buf,$off) = @_;          my ($file,$buf,$off) = @_;
200          $pending->{$file}->{write}++;  
201          my ($rv);          my $rv;
202          my $path = fixup($file);          my $path = fixup($file);
203    
204            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
205    
206          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
207          my ($fsize) = -s $path;  
208          my $fh = $pending->{$file}->{fh};          $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
209          return -ENOSYS() unless $fh;          confess "write into non-existant $path for $file: $!" unless -e $path;
210    
211            my $fh = new IO::File;
212            return -ENOSYS() unless open($fh,'+<',$path);
213          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
214                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
215                    warn "## write $path offset $off [",length( $buf ), "]\n" if $debug;
216                    $pending->{$file}->{write}++;
217          }          }
218          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
219            close($fh) || warn "can't close $path: $!";
220          return length($buf);          return length($buf);
221  }  }
222    
223  sub err { return (-shift || -$!) }  sub err { return (-shift || -$!) }
224    
225  sub x_readlink { return readlink(fixup(shift));         }  sub x_readlink { return readlink(fixup(shift)); }
226  sub x_unlink   { return unlink_all( shift ) ? 0 : -$! }  
227    sub x_unlink   {
228            my $file = shift;
229            my $path = fixup( $file );
230    
231  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }          if ( $file =~ m#\Q/.fuse_hidden\E# ) {
232                    return unlink $path ? 0 : -$1;
233            }
234    
235            warn "# unlink( $file )\n";
236    
237            unlink $path || return 0;
238    
239            my $tmp = $mount->{tmp} . '/' . $file;
240            unlink $tmp if ( -e $tmp );
241    
242            delete( $pending->{$file} );
243            return 0;
244    }
245    
246    sub x_symlink {
247            my ($from,$to) = @_;
248    
249            my $from_path = $from;  #fixup( $from );
250            my $to_path = fixup( $to );
251    
252            my $rv = symlink( $from_path, $to_path ) ? 0 : -$!;
253            warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug;
254    
255            my $tmp = $mount->{tmp} . '/' . $from;
256            if ( -e $tmp ) {
257                    my $tmp_to = $mount->{$tmp} . '/' . $to;
258                    symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!";
259            }
260            return $rv;
261    }
262    
263    sub x_link {
264            my ($from,$to) = @_;
265    
266            my $from_path = fixup($from);
267            my $to_path = fixup($to);
268            $to_path .= '.gz' if ( $from_path =~ m/\.gz$/ && $to_path !~ m/\.gz$/ );
269    
270            my $rv = link( $from_path, $to_path ) ? 0 : -$!;
271    
272            warn "# link( $from_path -> $to_path ) = $rv\n" if $debug;
273    
274            return $rv;
275    }
276    
277  sub x_rename {  sub x_rename {
278          my ($old) = fixup(shift);          my ($old,$new) = @_;
279          my ($new) = fixup(shift);          my $old_path = fixup($old);
280          my ($err) = rename($old,$new) ? 0 : -ENOENT();          my $new_path = fixup($new);
281            $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
282    
283            my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
284            warn "## rename( $old_path => $new_path ) = $err\n";
285    
286            my $tmp = $mount->{tmp} . '/' . $old;
287            if ( -e $tmp ) {
288                    if ( $new =~ m#\Q/.fuse_hidden\E# ) {
289                            unlink $tmp || confess "can't unlink $tmp for $new\n";
290                    } else {
291                            my $new_tmp = $mount->{tmp} . '/' . $new;
292                            rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
293                    }
294            }
295    
296            if (defined( $pending->{$old} )) {
297                    $pending->{$new} = $pending->{$old};
298    
299                    my $path = $pending->{$old}->{path};
300                    $path =~ s/\Q$old\E/$new/;
301                    $pending->{$new}->{path} = $path;
302    
303                    delete( $pending->{$old} );
304                    warn "## tweaking pending to ", dump( $pending ) if $debug;
305            }
306    
307          return $err;          return $err;
308  }  }
 sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }  
309    
310  sub x_chown {  sub x_chown {
311          my ($path) = fixup(shift);          my ($file,$uid,$gid) = @_;
312            my $path = fixup($file);
313          print "nonexistent $path\n" unless -e $path;          print "nonexistent $path\n" unless -e $path;
         my ($uid,$gid) = @_;  
314          # perl's chown() does not chown symlinks, it chowns the symlink's          # perl's chown() does not chown symlinks, it chowns the symlink's
315          # target.  it fails when the link's target doesn't exist, because          # target.  it fails when the link's target doesn't exist, because
316          # the stat64() syscall fails.          # the stat64() syscall fails.
317          # this causes error messages when unpacking symlinks in tarballs.          # this causes error messages when unpacking symlinks in tarballs.
318          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
319    
320            my $tmp = $mount->{tmp} . '/' . $file;
321            syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp;
322    
323          return $err;          return $err;
324  }  }
325    
# Line 219  sub x_chmod { Line 330  sub x_chmod {
330          return $err;          return $err;
331  }  }
332    
333  sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }  sub x_truncate {
334            my ( $file,$size ) = @_;
335            my $path = fixup($file);
336            my $rv = truncate( $path, $size ) ? 0 : -$! ;
337            if ( $path =~ m/\.gz$/ ) {
338                    my $no_gz = $path;
339                    $no_gz =~ s/\.gz$//;
340                    rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";
341            }
342            warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
343            $pending->{$file}->{write}++;
344    
345            my $tmp = $mount->{tmp} . '/' . $file;
346            truncate( $tmp, $size ) if -e $tmp;
347    
348            return $rv;
349    }
350  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
351    
352  sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }  sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
# Line 237  sub x_mknod { Line 364  sub x_mknod {
364    
365  sub x_release {  sub x_release {
366          my ( $file, $mode ) = @_;          my ( $file, $mode ) = @_;
367    
368            if ( $file =~ m#\Q/.fuse_hidden\E# ) {
369                    warn "release internal $file\n" if $debug;
370                    delete( $pending->{$file} );
371                    return 0;
372            }
373    
374          if ( ! defined( $pending->{$file} ) ) {          if ( ! defined( $pending->{$file} ) ) {
375                  warn "release $file, NO PENDING DATA\n";                  warn "release $file, NO PENDING DATA\n";
376                  return 0;                  return 0;
377          } elsif ( ! defined( $pending->{$file}->{write} ) ) {          } elsif ( ! defined( $pending->{$file}->{write} ) ) {
378                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
379          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
                 close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";  
380                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
381                  my $dest = fixup( $file );                  my $dest = fixup( $file );
382    
383                  # cleanup old compressed copy                  # cleanup old compressed copy
384                  if ( $dest =~ /\.gz$/ ) {                  if ( $dest =~ /\.gz$/ ) {
385                          warn "## remove old $dest\n";                          warn "## remove old $dest\n";
386                          unlink_all( $file ) || confess "can't remove $dest: $!";                          unlink $dest || confess "can't remove $dest: $!";
387                          $dest =~ s/\.gz$//;                          $dest =~ s/\.gz$//;
388                  }                  }
389    
390                  if ( $file =~ $skip_extensions_regex ) {                  if ( $file =~ $skip_extensions_regex ) {
391                          warn "release $file $mode -- uncompressed\n";                          warn "release $path [",-s $path,"] skipped compression\n";
392                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
393                  } elsif ( -s $path < $min_compress_size ) {                  } elsif ( -s $path < $min_compress_size ) {
394                          warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";                          warn "release $path [",-s $path,"] uncompressed, too small\n";
395                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
396                  } else {                  } else {
397                          warn "release $file $mode -- compressing\n";                          warn "release $path [",-s $path,"] compressing\n";
398    
399                            my $comp = $dest . '.gz';
400                            file_copy( '<', $path, '>:gzip', $comp );
401    
402                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                          my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
403    
404                          # FIXME add timeout to remove uncompressed version?                          if ( $size_path <= $size_comp ) {
405                          unlink_all( $file ) || warn "can't remove $path: $!";                                  warn ">>> $size_path <= $size_comp leaving uncompressed\n";
406                                    unlink $comp || confess "can't remove: $comp: $!";
407                            } else {
408                                    warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";
409                                    # FIXME add timeout to remove uncompressed version?
410                                    unlink $path || confess "can't remove $path: $!";
411                            }
412                  }                  }
413          } else {          } else {
414                  warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";                  warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";
                 $pending->{$file}->{open}--;  
                 return 0;  
415          }          }
416          delete( $pending->{$file} );          $pending->{$file}->{open}--;
417            if ( $pending->{$file}->{open} == 0 ) {
418                    warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
419                    delete( $pending->{$file} );
420            }
421          return 0;          return 0;
422  }  }
423    
# Line 302  Fuse::main( Line 445  Fuse::main(
445          statfs  =>"main::x_statfs",          statfs  =>"main::x_statfs",
446          release =>"main::x_release",          release =>"main::x_release",
447  #       threaded=>1,  #       threaded=>1,
448  #       debug   => 1,          debug   => $fuse_debug,
449  );  );

Legend:
Removed from v.5  
changed lines
  Added in v.27

  ViewVC Help
Powered by ViewVC 1.1.26