/[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 12 by dpavlin, Mon Jul 9 10:35:02 2007 UTC revision 25 by dpavlin, Mon Jul 9 22:44:20 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 146  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 158  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);  
         close($fh);  
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 190  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    
197          $pending->{$file}->{write}++;          $pending->{$file}->{write}++;
198          my $rv;          my $rv;
199          my $path = fixup($file);          my $path = fixup($file);
# Line 202  sub x_write { Line 206  sub x_write {
206          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});
207          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
208                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
209                  warn "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;                  warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
210          }          }
211          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
212          close($fh);          close($fh);
# Line 212  sub x_write { Line 216  sub x_write {
216  sub err { return (-shift || -$!) }  sub err { return (-shift || -$!) }
217    
218  sub x_readlink { return readlink(fixup(shift));         }  sub x_readlink { return readlink(fixup(shift));         }
219  sub x_unlink   { return unlink_all( shift ) ? 0 : -$! }  sub x_unlink   {
220            my $file = shift;
221            my $path = fixup( $file );
222    
223            if ( $file =~ m#\Q/.fuse_hidden\E# ) {
224                    return unlink $path ? 0 : -$1;
225            }
226    
227            warn "# unlink( $file )\n";
228    
229            unlink $path || return 0;
230    
231            my $tmp = $mount->{tmp} . '/' . $file;
232            unlink $tmp if ( -e $tmp );
233    
234            delete( $pending->{$file} );
235            return 0;
236    }
237    
238  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
239    
240  sub x_rename {  sub x_rename {
241          my ($old) = fixup(shift);          my ($old,$new) = @_;
242          my ($new) = fixup(shift);          my $old_path = fixup($old);
243          my ($err) = rename($old,$new) ? 0 : -ENOENT();          my $new_path = fixup($new);
244            $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
245    
246            my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
247            warn "## rename( $old_path => $new_path ) = $err\n";
248    
249            my $tmp = $mount->{tmp} . '/' . $old;
250            if ( -e $tmp ) {
251                    if ( $new =~ m#\Q/.fuse_hidden\E# ) {
252                            unlink $tmp || confess "can't unlink $tmp for $new\n";
253                    } else {
254                            my $new_tmp = $mount->{tmp} . '/' . $new;
255                            rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
256                    }
257            }
258    
259            if (defined( $pending->{$old} )) {
260                    $pending->{$new} = $pending->{$old};
261    
262                    my $path = $pending->{$old}->{path};
263                    $path =~ s/\Q$old\E/$new/;
264                    $pending->{$new}->{path} = $path;
265    
266                    delete( $pending->{$old} );
267                    warn "## tweaking pending to ", dump( $pending ) if $debug;
268            }
269    
270          return $err;          return $err;
271  }  }
272  sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }  sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
# Line 243  sub x_chmod { Line 290  sub x_chmod {
290          return $err;          return $err;
291  }  }
292    
293  sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }  sub x_truncate {
294            my ( $file,$size ) = @_;
295            my $path = fixup($file);
296            my $rv = truncate( $path, $size ) ? 0 : -$! ;
297            if ( $path =~ m/\.gz$/ ) {
298                    my $no_gz = $path;
299                    $no_gz =~ s/\.gz$//;
300                    rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";
301            }
302            warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
303            $pending->{$file}->{write}++;
304            return $rv;
305    }
306  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
307    
308  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 261  sub x_mknod { Line 320  sub x_mknod {
320    
321  sub x_release {  sub x_release {
322          my ( $file, $mode ) = @_;          my ( $file, $mode ) = @_;
323    
324            if ( $file =~ m#\Q/.fuse_hidden\E# ) {
325                    warn "release internal $file\n" if $debug;
326                    delete( $pending->{$file} );
327                    return 0;
328            }
329    
330          if ( ! defined( $pending->{$file} ) ) {          if ( ! defined( $pending->{$file} ) ) {
331                  warn "release $file, NO PENDING DATA\n";                  warn "release $file, NO PENDING DATA\n";
332                  return 0;                  return 0;
# Line 278  sub x_release { Line 344  sub x_release {
344                  }                  }
345    
346                  if ( $file =~ $skip_extensions_regex ) {                  if ( $file =~ $skip_extensions_regex ) {
347                          warn "release $file $mode -- uncompressed\n";                          warn "release $file [",-s $path,"] skipped compression\n";
348                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
349                  } elsif ( -s $path < $min_compress_size ) {                  } elsif ( -s $path < $min_compress_size ) {
350                          warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";                          warn "release $file [",-s $path,"] uncompressed, too small\n";
351                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
352                  } else {                  } else {
353                          warn "release $file $mode -- compressing\n";                          warn "release $file [",-s $path,"] compressing\n";
354    
355                            my $comp = $dest . '.gz';
356                            file_copy( '<', $path, '>:gzip', $comp );
357    
358                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                          my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
359    
360                          # FIXME add timeout to remove uncompressed version?                          if ( $size_path <= $size_comp ) {
361                          unlink $path || warn "can't remove $path: $!";                                  warn ">>> $size_path <= $size_comp leaving uncompressed\n";
362                                    unlink $comp || warn "can't reamove: $comp: $!";
363                            } else {
364                                    warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";
365                                    # FIXME add timeout to remove uncompressed version?
366                                    unlink $path || warn "can't remove $path: $!";
367                            }
368                  }                  }
369          } else {          } else {
370                  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;  
371          }          }
372          delete( $pending->{$file} );          $pending->{$file}->{open}--;
373            if ( $pending->{$file}->{open} == 0 ) {
374                    warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
375                    delete( $pending->{$file} );
376            }
377          return 0;          return 0;
378  }  }
379    
# Line 325  Fuse::main( Line 401  Fuse::main(
401          statfs  =>"main::x_statfs",          statfs  =>"main::x_statfs",
402          release =>"main::x_release",          release =>"main::x_release",
403  #       threaded=>1,  #       threaded=>1,
404  #       debug   => 1,          debug   => $fuse_debug,
405  );  );

Legend:
Removed from v.12  
changed lines
  Added in v.25

  ViewVC Help
Powered by ViewVC 1.1.26