/[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 10 by dpavlin, Sun Jul 8 21:03:26 2007 UTC revision 26 by dpavlin, Mon Jul 9 23:28:58 2007 UTC
# Line 12  use File::Path; Line 12  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;  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
# Line 69  sub gzip_original_size { Line 76  sub gzip_original_size {
76          return unpack("L", $buff);          return unpack("L", $buff);
77  }  }
78    
 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;  
 }  
   
79  sub x_getattr {  sub x_getattr {
80          my ($file) = fixup(shift);          my ($file) = fixup(shift);
81          my (@list) = lstat($file);          my (@list) = lstat($file);
# Line 103  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 ) || 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 );
101          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: $!";
102          my $buff;          my $buff;
103          while( read( $s, $buff, 65535 ) ) {          while( read( $s, $buff, 65535 ) ) {
104                  print $d $buff || confess "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: $!";
# Line 127  sub file_copy { Line 120  sub file_copy {
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 = {          my $mode_desc = {
# Line 134  sub x_open { Line 139  sub x_open {
139                  rdwr => $mode && O_RDWR,                  rdwr => $mode && O_RDWR,
140                  append => $mode && O_APPEND,                  append => $mode && O_APPEND,
141                  create => $mode && O_CREAT,                  create => $mode && O_CREAT,
142                    trunc => $mode && O_TRUNC,
143          };          };
144          warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n";          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    
         my $path = fixup($file);  
148          my $tmp = $mount->{tmp} . '/' . $file;          my $tmp = $mount->{tmp} . '/' . $file;
149          if ( -e $tmp ) {          if ( -e $tmp ) {
150                  $path = $tmp;                  $path = $tmp;
# Line 146  sub x_open { Line 152  sub x_open {
152                  my $dest_path = $tmp;                  my $dest_path = $tmp;
153                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix
154                  mkpath $dest_path unless -e $dest_path;                  mkpath $dest_path unless -e $dest_path;
155                  file_copy( '<:gzip', $path, '>', $tmp );                  if ( -s $path ) {
156                            file_copy( '<:gzip', $path, '>', $tmp )
157                    } else {
158                            warn "ERROR: filesystem corruption, $path is zero size\n";
159                    }
160                  $path = $tmp;                  $path = $tmp;
161          }          }
         warn ">>> open abs path: $path ", -s $path, " bytes\n";  
         return -$! unless sysopen($fh , $path, $mode);  
162    
163          $pending->{$file}->{path} = $path;          if ( sysopen($fh , $path, $mode) ) {
164          return 0;                  close($fh) || confess "can't close $path: $!";
165                    warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n";
166                    $pending->{$file}->{path} = $path;
167                    return 0;
168            } else {
169                    warn "ERROR: can't open $path -- $!";
170                    return -$!;
171            }
172    
173  }  }
174    
175  sub x_read {  sub x_read {
# Line 161  sub x_read { Line 177  sub x_read {
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    
184          my $fh = new IO::File;          my $fh = new IO::File;
# Line 175  sub x_read { Line 193  sub x_read {
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    
204            $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
205            confess "write into non-existant $path for $file: $!" unless -e $path;
206    
207          my $fh = new IO::File;          my $fh = new IO::File;
208          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});          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 "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;                  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_all( 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 225  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 243  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;
# Line 260  sub x_release { Line 351  sub x_release {
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 );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
356                  } elsif ( -s $path < $min_compress_size ) {                  } elsif ( -s $path < $min_compress_size ) {
357                          warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";                          warn "release $file [",-s $path,"] uncompressed, too small\n";
358                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          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 $comp = $dest . '.gz';
363                            file_copy( '<', $path, '>:gzip', $comp );
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 307  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.10  
changed lines
  Added in v.26

  ViewVC Help
Powered by ViewVC 1.1.26