/[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 8 by dpavlin, Sun Jul 8 17:04:18 2007 UTC revision 21 by dpavlin, Mon Jul 9 17:35:26 2007 UTC
# Line 11  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    
16  my $mount = {  my $mount = {
17          from    => '/tmp/comp',          from    => '/tmp/comp',
# Line 18  my $mount = { Line 19  my $mount = {
19          tmp             => '/dev/shm/comp',          tmp             => '/dev/shm/comp',
20  };  };
21    
22  my $debug = 1;  my $debug = shift @ARGV;
23    
24  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;
25    
# Line 102  sub x_getdir { Line 103  sub x_getdir {
103    
104  sub file_copy {  sub file_copy {
105          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
106          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;
107          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 );
108          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: $!";
109          my $buff;          my $buff;
110          while( read( $s, $buff, 65535 ) ) {          while( read( $s, $buff, 65535 ) ) {
111                  print $d $buff || confess "can't write into $d_path: $!";                  print $d $buff || confess "can't write into $d_path: $!";
112                  warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;                  warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
113          }          }
114          close($d) || warn "can't close $d_path: $!";          close($d) || warn "can't close $d_path: $!";
115          close($s) || warn "can't close $s_path: $!";          close($s) || warn "can't close $s_path: $!";
# Line 126  sub file_copy { Line 127  sub file_copy {
127  sub x_open {  sub x_open {
128          my ($file) = shift;          my ($file) = shift;
129          my ($mode) = shift;          my ($mode) = shift;
130    
131            if ( $file eq '/.debug' ) {
132                    my $path = $mount->{from} . '/.debug';
133                    open( my $debug, '>', $path ) || die "can't open $path: $!";
134                    my $dump = dump( $pending );
135                    print $debug "pending = $dump\n";
136                    close($debug);
137                    $pending->{'/.debug'}->{path} = $path;
138                    warn "## created dump $path $dump\n";
139                    return 0;
140            }
141    
142          $pending->{$file}->{open}++;          $pending->{$file}->{open}++;
143    
144          my $mode_desc = {          my $mode_desc = {
# Line 133  sub x_open { Line 146  sub x_open {
146                  rdwr => $mode && O_RDWR,                  rdwr => $mode && O_RDWR,
147                  append => $mode && O_APPEND,                  append => $mode && O_APPEND,
148                  create => $mode && O_CREAT,                  create => $mode && O_CREAT,
149                    trunc => $mode && O_TRUNC,
150          };          };
151          warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n";          my $path = fixup($file);
152            warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
153          my $fh;          my $fh;
154    
         my $path = fixup($file);  
155          my $tmp = $mount->{tmp} . '/' . $file;          my $tmp = $mount->{tmp} . '/' . $file;
156          if ( -e $tmp ) {          if ( -e $tmp ) {
157                  $path = $tmp;                  $path = $tmp;
# Line 148  sub x_open { Line 162  sub x_open {
162                  file_copy( '<:gzip', $path, '>', $tmp );                  file_copy( '<:gzip', $path, '>', $tmp );
163                  $path = $tmp;                  $path = $tmp;
164          }          }
         warn ">>> open abs path: $path ", -s $path, " bytes\n";  
         return -$! unless sysopen($fh , $path, $mode);  
165    
166          $pending->{$file}->{fh} = $fh;          if ( sysopen($fh , $path, $mode) ) {
167          $pending->{$file}->{path} = $path;                  close($fh) || confess "can't close $path: $!";
168          $pending->{$file}->{mode} = {                  warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n";
169                  rdonly => $mode && O_RDONLY,                  $pending->{$file}->{path} = $path;
170                  rdwr => $mode && O_RDWR,                  return 0;
171                  append => $mode && O_APPEND,          } else {
172                  create => $mode && O_CREAT,                  warn "ERROR: can't open $path : $!";
173          };                  return -$!;
174          return 0;          }
175    
176  }  }
177    
178  sub x_read {  sub x_read {
179          my ($file,$bufsize,$off) = @_;          my ($file,$bufsize,$off) = @_;
180          my ($rv) = -ENOSYS();          my ($rv) = -ENOSYS();
181          my $path = fixup( $file );          my $path = fixup( $file );
182    
183            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
184    
185          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
186          my ($fsize) = -s $path;  
187          my $fh = $pending->{$file}->{fh} || confess "no fh? ", dump( $pending );          my $fh = new IO::File;
188            return -ENOSYS() unless open($fh,$pending->{$file}->{path});
189    
190          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
191                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
192          }          }
193    
194          return $rv;          return $rv;
195  }  }
196    
197  sub x_write {  sub x_write {
198          my ($file,$buf,$off) = @_;          my ($file,$buf,$off) = @_;
199          $pending->{$file}->{write}++;          $pending->{$file}->{write}++;
200          my ($rv);          my $rv;
201          my $path = fixup($file);          my $path = fixup($file);
202    
203            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
204    
205          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
206          my ($fsize) = -s $path;  
207          my $fh = $pending->{$file}->{fh};          my $fh = new IO::File;
208          return -ENOSYS() unless $fh;          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{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 ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
212          }          }
213          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
214            close($fh);
215          return length($buf);          return length($buf);
216  }  }
217    
# Line 199  sub x_unlink   { return unlink_all( shif Line 223  sub x_unlink   { return unlink_all( shif
223  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
224    
225  sub x_rename {  sub x_rename {
226          my ($old) = fixup(shift);          my ($old,$new) = @_;
227          my ($new) = fixup(shift);          my $old_path = fixup($old);
228          my ($err) = rename($old,$new) ? 0 : -ENOENT();          my $new_path = fixup($new);
229            $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
230    
231            my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
232            warn "## rename( $old_path => $new_path ) = $err\n";
233    
234            my $tmp = $mount->{tmp} . '/' . $old;
235            if ( -e $tmp ) {
236                    my $new_tmp = $mount->{tmp} . '/' . $new;
237                    rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
238            }
239    
240            if (defined( $pending->{$old} )) {
241                    $pending->{$new} = $pending->{$old};
242    
243                    my $path = $pending->{$old}->{path};
244                    $path =~ s/\Q$old\E/$new/;
245                    $pending->{$new}->{path} = $path;
246    
247                    delete( $pending->{$old} );
248            }
249    
250          return $err;          return $err;
251  }  }
252  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 270  sub x_chmod {
270          return $err;          return $err;
271  }  }
272    
273  sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }  sub x_truncate {
274            my ( $file,$size ) = @_;
275            my $path = fixup($file);
276            my $rv = truncate( $path, $size ) ? 0 : -$! ;
277            if ( $path =~ m/\.gz$/ ) {
278                    my $no_gz = $path;
279                    $no_gz =~ s/\.gz$//;
280                    rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";
281            }
282            warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
283            $pending->{$file}->{write}++;
284            return $rv;
285    }
286  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
287    
288  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 249  sub x_release { Line 306  sub x_release {
306          } elsif ( ! defined( $pending->{$file}->{write} ) ) {          } elsif ( ! defined( $pending->{$file}->{write} ) ) {
307                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
308          } 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: $!";  
309                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
310                  my $dest = fixup( $file );                  my $dest = fixup( $file );
311    
# Line 261  sub x_release { Line 317  sub x_release {
317                  }                  }
318    
319                  if ( $file =~ $skip_extensions_regex ) {                  if ( $file =~ $skip_extensions_regex ) {
320                          warn "release $file $mode -- uncompressed\n";                          warn "release $file [",-s $path,"] skipped compression\n";
321                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
322                  } elsif ( -s $path < $min_compress_size ) {                  } elsif ( -s $path < $min_compress_size ) {
323                          warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";                          warn "release $file [",-s $path,"] uncompressed, too small\n";
324                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
325                  } else {                  } else {
326                          warn "release $file $mode -- compressing\n";                          warn "release $file [",-s $path,"] compressing\n";
327    
328                            my $comp = $dest . '.gz';
329                            file_copy( '<', $path, '>:gzip', $comp );
330    
331                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                          my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
332    
333                          # FIXME add timeout to remove uncompressed version?                          if ( $size_path <= $size_comp ) {
334                          unlink $path || warn "can't remove $path: $!";                                  warn ">>> $size_path <= $size_comp leaving uncompressed\n";
335                                    unlink $comp || warn "can't reamove: $comp: $!";
336                            } else {
337                                    warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";
338                                    # FIXME add timeout to remove uncompressed version?
339                                    unlink $path || warn "can't remove $path: $!";
340                            }
341                  }                  }
342          } else {          } else {
343                  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;  
344          }          }
345          delete( $pending->{$file} );          $pending->{$file}->{open}--;
346            if ( $pending->{$file}->{open} == 0 ) {
347                    warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
348                    delete( $pending->{$file} );
349            }
350          return 0;          return 0;
351  }  }
352    

Legend:
Removed from v.8  
changed lines
  Added in v.21

  ViewVC Help
Powered by ViewVC 1.1.26