/[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 1 by dpavlin, Sun Jul 8 12:51:33 2007 UTC revision 26 by dpavlin, Mon Jul 9 23:28:58 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
10  use PerlIO::gzip;  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/;
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 18  my $mount = { Line 28  my $mount = {
28          tmp             => '/dev/shm/comp',          tmp             => '/dev/shm/comp',
29  };  };
30    
31  my $debug = 1;  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  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
34    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 85  sub x_getdir { Line 96  sub x_getdir {
96    
97  sub file_copy {  sub file_copy {
98          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
99          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;
100          open(my $s, $s_opt, $s_path ) || die "can't open $s_path: $!";          open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending );
101          open(my $d, $d_opt, $d_path ) || die "can't open $d_path: $!";          open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!";
102          my $buff;          my $buff;
103          while( read( $s, $buff, 65535 ) ) {          while( read( $s, $buff, 65535 ) ) {
104                  print $d $buff || die "can't write into $d_path: $!";                  print $d $buff || confess "can't write into $d_path: $!";
105                  warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;                  warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
106          }          }
107          close($d) || warn "can't close $d_path: $!";          close($d) || warn "can't close $d_path: $!";
108          close($s) || warn "can't close $s_path: $!";          close($s) || warn "can't close $s_path: $!";
109            warn "-- $s_path [", -s $s_path, "] >>> $d_path [", -s $d_path, "]\n" if $debug;
110            my ($mode,$uid,$gid,$atime,$mtime) = (stat $s_path)[2,4,5,8,9];
111    
112            chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";
113            chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";
114            utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
115    
116            undef $d;
117            undef $s;
118  }  }
119    
120  sub x_open {  sub x_open {
121          my ($file) = shift;          my ($file) = shift;
122          my ($mode) = shift;          my ($mode) = shift;
123    
124            if ( $file eq '/.debug' ) {
125                    my $path = $mount->{from} . '/.debug';
126                    open( my $debug, '>', $path ) || die "can't open $path: $!";
127                    my $dump = dump( $pending );
128                    print $debug "pending = $dump\n";
129                    close($debug);
130                    $pending->{'/.debug'}->{path} = $path;
131                    warn "## created dump $path $dump\n";
132                    return 0;
133            }
134    
135          $pending->{$file}->{open}++;          $pending->{$file}->{open}++;
136    
137            my $mode_desc = {
138                    rdonly => $mode && O_RDONLY,
139                    rdwr => $mode && O_RDWR,
140                    append => $mode && O_APPEND,
141                    create => $mode && O_CREAT,
142                    trunc => $mode && O_TRUNC,
143            };
144            my $path = fixup($file);
145            warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
146          my $fh;          my $fh;
147          if ( $pending->{$file}->{open} == 1 ) {  
148                  warn "# open( $file, $mode )\n";          my $tmp = $mount->{tmp} . '/' . $file;
149                  my $path = fixup($file);          if ( -e $tmp ) {
150                  my $tmp = $mount->{tmp} . '/' . $file;                  $path = $tmp;
151                  if ( -e $tmp ) {          } elsif ( $path =~ m/\.gz$/ ) {
152                          $path = $tmp;                  my $dest_path = $tmp;
153                  } elsif ( $path =~ m/\.gz$/ ) {                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix
154                          my $dest_path = $tmp;                  mkpath $dest_path unless -e $dest_path;
155                          $dest_path =~ s!/[^/]+$!!;      #!vim-fix                  if ( -s $path ) {
156                          mkpath $dest_path unless -e $dest_path;                          file_copy( '<:gzip', $path, '>', $tmp )
157                          file_copy( '<:gzip', $path, '>', $tmp );                  } else {
158                          $path = $tmp;                          warn "ERROR: filesystem corruption, $path is zero size\n";
159                  }                  }
160                  return -$! unless sysopen($fh , $path, $mode);                  $path = $tmp;
161                  $pending->{$file}->{fh} = $fh;          }
162    
163            if ( sysopen($fh , $path, $mode) ) {
164                    close($fh) || confess "can't close $path: $!";
165                    warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n";
166                  $pending->{$file}->{path} = $path;                  $pending->{$file}->{path} = $path;
167          } elsif ( ! defined( $pending->{$file}->{fh} ) ) {                  return 0;
168                  die "can't find fh for $file ", dump($pending);          } else {
169                    warn "ERROR: can't open $path -- $!";
170                    return -$!;
171          }          }
172          return 0;  
173  }  }
174    
175  sub x_read {  sub x_read {
176          my ($file,$bufsize,$off) = @_;          my ($file,$bufsize,$off) = @_;
177          my ($rv) = -ENOSYS();          my ($rv) = -ENOSYS();
178          my $path = fixup( $file );          my $path = fixup( $file );
179    
180            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
181    
182          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
183          my ($fsize) = -s $path;  
184          my $fh = $pending->{$file}->{fh} || die "no fh? ", dump( $pending );          my $fh = new IO::File;
185            return -ENOSYS() unless open($fh,$pending->{$file}->{path});
186    
187          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
188                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
189          }          }
190    
191          return $rv;          return $rv;
192  }  }
193    
194  sub x_write {  sub x_write {
195          my ($file,$buf,$off) = @_;          my ($file,$buf,$off) = @_;
196          $pending->{$file}->{write}++;  
197          my ($rv);          my $rv;
198          my $path = fixup($file);          my $path = fixup($file);
199    
200            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
201    
202          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
203          my ($fsize) = -s $path;  
204          my $fh = $pending->{$file}->{fh};          $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
205          return -ENOSYS() unless $fh;          confess "write into non-existant $path for $file: $!" unless -e $path;
206    
207            my $fh = new IO::File;
208            return -ENOSYS() unless open($fh,'+<',$path);
209          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
210                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
211                    warn "## write $path offset $off [",length( $buf ), "]\n" if $debug;
212                    $pending->{$file}->{write}++;
213          }          }
214          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
215            close($fh) || warn "can't close $path: $!";
216          return length($buf);          return length($buf);
217  }  }
218    
219  sub err { return (-shift || -$!) }  sub err { return (-shift || -$!) }
220    
221  sub x_readlink { return readlink(fixup(shift));         }  sub x_readlink { return readlink(fixup(shift));         }
222  sub x_unlink   { return unlink(fixup(shift)) ? 0 : -$!; }  sub x_unlink   {
223            my $file = shift;
224            my $path = fixup( $file );
225    
226            if ( $file =~ m#\Q/.fuse_hidden\E# ) {
227                    return unlink $path ? 0 : -$1;
228            }
229    
230            warn "# unlink( $file )\n";
231    
232            unlink $path || return 0;
233    
234            my $tmp = $mount->{tmp} . '/' . $file;
235            unlink $tmp if ( -e $tmp );
236    
237            delete( $pending->{$file} );
238            return 0;
239    }
240    
241  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
242    
243  sub x_rename {  sub x_rename {
244          my ($old) = fixup(shift);          my ($old,$new) = @_;
245          my ($new) = fixup(shift);          my $old_path = fixup($old);
246          my ($err) = rename($old,$new) ? 0 : -ENOENT();          my $new_path = fixup($new);
247            $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
248    
249            my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
250            warn "## rename( $old_path => $new_path ) = $err\n";
251    
252            my $tmp = $mount->{tmp} . '/' . $old;
253            if ( -e $tmp ) {
254                    if ( $new =~ m#\Q/.fuse_hidden\E# ) {
255                            unlink $tmp || confess "can't unlink $tmp for $new\n";
256                    } else {
257                            my $new_tmp = $mount->{tmp} . '/' . $new;
258                            rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
259                    }
260            }
261    
262            if (defined( $pending->{$old} )) {
263                    $pending->{$new} = $pending->{$old};
264    
265                    my $path = $pending->{$old}->{path};
266                    $path =~ s/\Q$old\E/$new/;
267                    $pending->{$new}->{path} = $path;
268    
269                    delete( $pending->{$old} );
270                    warn "## tweaking pending to ", dump( $pending ) if $debug;
271            }
272    
273          return $err;          return $err;
274  }  }
275  sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }  sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
276    
277  sub x_chown {  sub x_chown {
278          my ($path) = fixup(shift);          my ($file,$uid,$gid) = @_;
279            my $path = fixup($file);
280          print "nonexistent $path\n" unless -e $path;          print "nonexistent $path\n" unless -e $path;
         my ($uid,$gid) = @_;  
281          # perl's chown() does not chown symlinks, it chowns the symlink's          # perl's chown() does not chown symlinks, it chowns the symlink's
282          # target.  it fails when the link's target doesn't exist, because          # target.  it fails when the link's target doesn't exist, because
283          # the stat64() syscall fails.          # the stat64() syscall fails.
284          # this causes error messages when unpacking symlinks in tarballs.          # this causes error messages when unpacking symlinks in tarballs.
285          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
286    
287            my $tmp = $mount->{tmp} . '/' . $file;
288            syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp;
289    
290          return $err;          return $err;
291  }  }
292    
# Line 187  sub x_chmod { Line 297  sub x_chmod {
297          return $err;          return $err;
298  }  }
299    
300  sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }  sub x_truncate {
301            my ( $file,$size ) = @_;
302            my $path = fixup($file);
303            my $rv = truncate( $path, $size ) ? 0 : -$! ;
304            if ( $path =~ m/\.gz$/ ) {
305                    my $no_gz = $path;
306                    $no_gz =~ s/\.gz$//;
307                    rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";
308            }
309            warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
310            $pending->{$file}->{write}++;
311            return $rv;
312    }
313  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
314    
315  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 205  sub x_mknod { Line 327  sub x_mknod {
327    
328  sub x_release {  sub x_release {
329          my ( $file, $mode ) = @_;          my ( $file, $mode ) = @_;
330    
331            if ( $file =~ m#\Q/.fuse_hidden\E# ) {
332                    warn "release internal $file\n" if $debug;
333                    delete( $pending->{$file} );
334                    return 0;
335            }
336    
337          if ( ! defined( $pending->{$file} ) ) {          if ( ! defined( $pending->{$file} ) ) {
338                  warn "release $file, NO PENDING DATA\n";                  warn "release $file, NO PENDING DATA\n";
339                  return 0;                  return 0;
340          } elsif ( ! defined( $pending->{$file}->{write} ) ) {          } elsif ( ! defined( $pending->{$file}->{write} ) ) {
341                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
342          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
343                  close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
344                    my $dest = fixup( $file );
345    
346                    # cleanup old compressed copy
347                    if ( $dest =~ /\.gz$/ ) {
348                            warn "## remove old $dest\n";
349                            unlink $dest || confess "can't remove $dest: $!";
350                            $dest =~ s/\.gz$//;
351                    }
352    
353                  if ( $file =~ $skip_extensions_regex ) {                  if ( $file =~ $skip_extensions_regex ) {
354                          warn "release $file $mode -- uncompressed\n";                          warn "release $file [",-s $path,"] skipped compression\n";
355                            file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
356                    } elsif ( -s $path < $min_compress_size ) {
357                            warn "release $file [",-s $path,"] uncompressed, too small\n";
358                            file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
359                  } else {                  } else {
360                          warn "release $file $mode -- compressing\n";                          warn "release $file [",-s $path,"] compressing\n";
361    
362                          my $path = $pending->{$file}->{path} || die "no path for $file ? ", dump( $pending );                          my $comp = $dest . '.gz';
363                          my $dest = fixup( $file );                          file_copy( '<', $path, '>:gzip', $comp );
   
                         if ( $dest =~ /\.gz$/ ) {  
                                 warn "## remove old $dest\n";  
                                 unlink $dest || die "can't remove $dest: $!";  
                                 $dest =~ s/\.gz$//;  
                         }  
364    
365                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                          my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
366    
367                          # FIXME add timeout to remove uncompressed version?                          if ( $size_path <= $size_comp ) {
368                          unlink $path || warn "can't remove $path: $!";                                  warn ">>> $size_path <= $size_comp leaving uncompressed\n";
369                                    unlink $comp || warn "can't reamove: $comp: $!";
370                            } else {
371                                    warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";
372                                    # FIXME add timeout to remove uncompressed version?
373                                    unlink $path || warn "can't remove $path: $!";
374                            }
375                  }                  }
376          } else {          } else {
377                  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;  
378          }          }
379          delete( $pending->{$file} );          $pending->{$file}->{open}--;
380            if ( $pending->{$file}->{open} == 0 ) {
381                    warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
382                    delete( $pending->{$file} );
383            }
384          return 0;          return 0;
385  }  }
386    
# Line 264  Fuse::main( Line 408  Fuse::main(
408          statfs  =>"main::x_statfs",          statfs  =>"main::x_statfs",
409          release =>"main::x_release",          release =>"main::x_release",
410  #       threaded=>1,  #       threaded=>1,
411  #       debug   => 1,          debug   => $fuse_debug,
412  );  );

Legend:
Removed from v.1  
changed lines
  Added in v.26

  ViewVC Help
Powered by ViewVC 1.1.26