/[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 24 by dpavlin, Mon Jul 9 21:57:11 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 103  sub x_getdir { Line 110  sub x_getdir {
110    
111  sub file_copy {  sub file_copy {
112          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
113          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;
114          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 );
115          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: $!";
116          my $buff;          my $buff;
117          while( read( $s, $buff, 65535 ) ) {          while( read( $s, $buff, 65535 ) ) {
118                  print $d $buff || confess "can't write into $d_path: $!";                  print $d $buff || confess "can't write into $d_path: $!";
119                  warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;                  warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
120          }          }
121          close($d) || warn "can't close $d_path: $!";          close($d) || warn "can't close $d_path: $!";
122          close($s) || warn "can't close $s_path: $!";          close($s) || warn "can't close $s_path: $!";
# Line 127  sub file_copy { Line 134  sub file_copy {
134  sub x_open {  sub x_open {
135          my ($file) = shift;          my ($file) = shift;
136          my ($mode) = shift;          my ($mode) = shift;
137    
138            if ( $file eq '/.debug' ) {
139                    my $path = $mount->{from} . '/.debug';
140                    open( my $debug, '>', $path ) || die "can't open $path: $!";
141                    my $dump = dump( $pending );
142                    print $debug "pending = $dump\n";
143                    close($debug);
144                    $pending->{'/.debug'}->{path} = $path;
145                    warn "## created dump $path $dump\n";
146                    return 0;
147            }
148    
149          $pending->{$file}->{open}++;          $pending->{$file}->{open}++;
150    
151          my $mode_desc = {          my $mode_desc = {
# Line 134  sub x_open { Line 153  sub x_open {
153                  rdwr => $mode && O_RDWR,                  rdwr => $mode && O_RDWR,
154                  append => $mode && O_APPEND,                  append => $mode && O_APPEND,
155                  create => $mode && O_CREAT,                  create => $mode && O_CREAT,
156                    trunc => $mode && O_TRUNC,
157          };          };
158          warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n";          my $path = fixup($file);
159            warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
160          my $fh;          my $fh;
161    
         my $path = fixup($file);  
162          my $tmp = $mount->{tmp} . '/' . $file;          my $tmp = $mount->{tmp} . '/' . $file;
163          if ( -e $tmp ) {          if ( -e $tmp ) {
164                  $path = $tmp;                  $path = $tmp;
# Line 146  sub x_open { Line 166  sub x_open {
166                  my $dest_path = $tmp;                  my $dest_path = $tmp;
167                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix
168                  mkpath $dest_path unless -e $dest_path;                  mkpath $dest_path unless -e $dest_path;
169                  file_copy( '<:gzip', $path, '>', $tmp );                  if ( -s $path ) {
170                            file_copy( '<:gzip', $path, '>', $tmp )
171                    } else {
172                            warn "ERROR: filesystem corruption, $path is zero size\n";
173                    }
174                  $path = $tmp;                  $path = $tmp;
175          }          }
         warn ">>> open abs path: $path ", -s $path, " bytes\n";  
         return -$! unless sysopen($fh , $path, $mode);  
176    
177          $pending->{$file}->{path} = $path;          if ( sysopen($fh , $path, $mode) ) {
178          return 0;                  close($fh) || confess "can't close $path: $!";
179                    warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n";
180                    $pending->{$file}->{path} = $path;
181                    return 0;
182            } else {
183                    warn "ERROR: can't open $path : $!";
184                    return -$!;
185            }
186    
187  }  }
188    
189  sub x_read {  sub x_read {
# Line 161  sub x_read { Line 191  sub x_read {
191          my ($rv) = -ENOSYS();          my ($rv) = -ENOSYS();
192          my $path = fixup( $file );          my $path = fixup( $file );
193    
194            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
195    
196          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
197    
198          my $fh = new IO::File;          my $fh = new IO::File;
# Line 179  sub x_write { Line 211  sub x_write {
211          my $rv;          my $rv;
212          my $path = fixup($file);          my $path = fixup($file);
213    
214            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
215    
216          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
217    
218          my $fh = new IO::File;          my $fh = new IO::File;
219          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});
220          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
221                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
222                  warn "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;                  warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
223          }          }
224          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
225            close($fh);
226          return length($buf);          return length($buf);
227  }  }
228    
# Line 199  sub x_unlink   { return unlink_all( shif Line 234  sub x_unlink   { return unlink_all( shif
234  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
235    
236  sub x_rename {  sub x_rename {
237          my ($old) = fixup(shift);          my ($old,$new) = @_;
238          my ($new) = fixup(shift);          my $old_path = fixup($old);
239          my ($err) = rename($old,$new) ? 0 : -ENOENT();          my $new_path = fixup($new);
240            $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
241    
242            my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
243            warn "## rename( $old_path => $new_path ) = $err\n";
244    
245            my $tmp = $mount->{tmp} . '/' . $old;
246            if ( -e $tmp ) {
247                    my $new_tmp = $mount->{tmp} . '/' . $new;
248                    rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
249            }
250    
251            if (defined( $pending->{$old} )) {
252                    $pending->{$new} = $pending->{$old};
253    
254                    my $path = $pending->{$old}->{path};
255                    $path =~ s/\Q$old\E/$new/;
256                    $pending->{$new}->{path} = $path;
257                    $pending->{$old}->{path} = $path;
258    
259                    #delete( $pending->{$old} );
260            }
261    
262          return $err;          return $err;
263  }  }
264  sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }  sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
# Line 225  sub x_chmod { Line 282  sub x_chmod {
282          return $err;          return $err;
283  }  }
284    
285  sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }  sub x_truncate {
286            my ( $file,$size ) = @_;
287            my $path = fixup($file);
288            my $rv = truncate( $path, $size ) ? 0 : -$! ;
289            if ( $path =~ m/\.gz$/ ) {
290                    my $no_gz = $path;
291                    $no_gz =~ s/\.gz$//;
292                    rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";
293            }
294            warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
295            $pending->{$file}->{write}++;
296            return $rv;
297    }
298  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
299    
300  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 260  sub x_release { Line 329  sub x_release {
329                  }                  }
330    
331                  if ( $file =~ $skip_extensions_regex ) {                  if ( $file =~ $skip_extensions_regex ) {
332                          warn "release $file $mode -- uncompressed\n";                          warn "release $file [",-s $path,"] skipped compression\n";
333                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
334                  } elsif ( -s $path < $min_compress_size ) {                  } elsif ( -s $path < $min_compress_size ) {
335                          warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";                          warn "release $file [",-s $path,"] uncompressed, too small\n";
336                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
337                  } else {                  } else {
338                          warn "release $file $mode -- compressing\n";                          warn "release $file [",-s $path,"] compressing\n";
339    
340                            my $comp = $dest . '.gz';
341                            file_copy( '<', $path, '>:gzip', $comp );
342    
343                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                          my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
344    
345                          # FIXME add timeout to remove uncompressed version?                          if ( $size_path <= $size_comp ) {
346                          unlink $path || warn "can't remove $path: $!";                                  warn ">>> $size_path <= $size_comp leaving uncompressed\n";
347                                    unlink $comp || warn "can't reamove: $comp: $!";
348                            } else {
349                                    warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";
350                                    # FIXME add timeout to remove uncompressed version?
351                                    unlink $path || warn "can't remove $path: $!";
352                            }
353                  }                  }
354          } else {          } else {
355                  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;  
356          }          }
357          delete( $pending->{$file} );          $pending->{$file}->{open}--;
358            if ( $pending->{$file}->{open} == 0 ) {
359                    warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
360                    delete( $pending->{$file} );
361            }
362          return 0;          return 0;
363  }  }
364    
# Line 307  Fuse::main( Line 386  Fuse::main(
386          statfs  =>"main::x_statfs",          statfs  =>"main::x_statfs",
387          release =>"main::x_release",          release =>"main::x_release",
388  #       threaded=>1,  #       threaded=>1,
389  #       debug   => 1,          debug   => $fuse_debug,
390  );  );

Legend:
Removed from v.10  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26