/[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 23 by dpavlin, Mon Jul 9 21:41:58 2007 UTC revision 38 by dpavlin, Sun Sep 2 11:38:59 2007 UTC
# Line 10  require 'syscall.ph'; # for SYS_mknod an Line 10  require 'syscall.ph'; # for SYS_mknod an
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/;  use Carp qw/confess cluck/;
14  use IO::File;  use IO::File;
15  use Getopt::Long;  use Getopt::Long;
16    
17  my $debug = 0;  my $debug = 0;
18  my $fuse_debug = 0;  my $fuse_debug = 0;
19    my $stats = 1;
20    
21  GetOptions(  GetOptions(
22          'debug+' => \$debug,          'debug+' => \$debug,
23          'fuse-debug+' => \$fuse_debug,          'fuse-debug+' => \$fuse_debug,
24            'stats!' => \$stats,
25  );  );
26    
27  my $mount = {  my $mount = {
28          from    => '/tmp/comp',          from    => shift @ARGV || '/tmp/comp',
29          to              => '/tmp/no-comp',          to              => shift @ARGV || '/tmp/no-comp',
30          tmp             => '/dev/shm/comp',          tmp             => shift @ARGV || '/dev/shm/comp',
31  };  };
32    
33  my $debug = shift @ARGV;  warn "mount $mount->{from} to $mount->{to} using $mount->{tmp} as cache\n";
34    
35  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/gz|gz%|\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|tgz|avi|mpeg|mpg|tmp|temp)$/i;
36    
37  # don't compress files smaller than this  # don't compress files smaller than this
38  my $min_compress_size = 512;  my $min_compress_size = 512;
# Line 44  foreach my $dir ( keys %$mount ) { Line 46  foreach my $dir ( keys %$mount ) {
46    
47  my $pending;  my $pending;
48    
49    sub real_name {
50            my ( $dir, $name ) = @_;
51            if ( -e "$dir/${name}.gz" ) {
52                    cluck "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name";
53                    return "${name}.gz";
54            }
55            if ( $name =~ m/\.gz$/ ) {
56                    return $name . '%';     # protect (mingle) compressed files
57            } else {
58                    return $name;
59            }
60    }
61    
62  sub fixup {  sub fixup {
63          my ( $path ) = @_;          my ( $path ) = @_;
64          my $full = $mount->{from} . '/' . $path;          return $mount->{from} . '/' . real_name( $mount->{from}, $path );
         if ( -e $full . '.gz' ) {  
                 return $full . '.gz';  
         }  
         return $full;  
65  }  }
66    
67  sub original_name {  sub original_name {
68          my $p = shift;          my $p = shift;
69          $p =~ s/\.gz$//;          $p =~ s/\.gz$//;
70            $p =~ s/\.gz%$/.gz/;    # demungle compressed .gz files
71          return $p;          return $p;
72  };  };
73    
# Line 78  sub gzip_original_size { Line 90  sub gzip_original_size {
90          return unpack("L", $buff);          return unpack("L", $buff);
91  }  }
92    
 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;  
 }  
   
93  sub x_getattr {  sub x_getattr {
94          my ($file) = fixup(shift);          my ($file) = fixup(shift);
95          my (@list) = lstat($file);          my (@list) = lstat($file);
# Line 133  sub file_copy { Line 131  sub file_copy {
131          undef $s;          undef $s;
132  }  }
133    
134    sub tmp_path {
135            my $file = shift;
136    
137            my $path = fixup( $file );
138    
139            my $op = 'UNKNOWN';
140    
141            if (defined( $pending->{$file} )) {
142                    $path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending );
143                    $op = 'opened';
144            } else {
145                    my $tmp = $mount->{tmp} . '/' . $file;
146                    if ( -e $tmp ) {
147                            $path = $tmp;
148                            $op = 'existing';
149                    } elsif ( $path =~ m/\.gz$/ ) {
150                            my $dest_path = $tmp;
151                            $dest_path =~ s!/[^/]+$!!;      #!vim-fix
152                            mkpath $dest_path unless -e $dest_path;
153                            if ( -s $path ) {
154                                    file_copy( '<:gzip', $path, '>', $tmp )
155                            } else {
156                                    confess "ASSERT: filesystem corruption, $path is zero size in ",dump( $pending );
157                            }
158                            $path = $tmp;
159                            $op = 'created';
160                    }
161                    confess "ASSERT: path shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{path} );
162                    confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} );
163                    $pending->{$file}->{path} = $path;
164                    $pending->{$file}->{open} = 0;  # not really opened, just uncompressed
165                    warn "## tmp_file( $file ) $op $path [", -s $path, "]\n" if $debug;
166            }
167            return $path;
168    }
169    
170    sub compress_file2path {
171            my ( $file, $path ) = @_;
172    
173            my $dest = fixup( $file );
174    
175            if ( defined($pending->{$file}) ) {
176                    my $pending_path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending );
177    
178                    if ( $pending->{$file}->{open} > 1 ) {
179                            warn "$file used ", $pending->{$file}->{open}, " times, delaying compression\n";
180                            return;
181                    } elsif ( ! $path ) {
182                            $path = $pending_path;
183                    } elsif ( $pending_path ne $path ) {
184                            confess "ASSERT: compressing into $path instead of $pending_path";
185                    }
186            }
187    
188            confess "need path" unless $path;
189    
190            # cleanup old compressed copy
191            if ( $dest =~ /\.gz$/ ) {
192                    warn "## remove old $dest\n";
193                    unlink $dest || confess "can't remove $dest: $!";
194                    $dest =~ s/\.gz$//;
195                    confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest;
196            }
197    
198            if ( $path =~ $skip_extensions_regex ) {
199                    warn "$path [",-s $path,"] skipped compression\n";
200                    file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
201            } elsif ( -s $path < $min_compress_size ) {
202                    warn "$path [",-s $path,"] uncompressed, too small\n";
203                    file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
204            } else {
205                    warn "$path [",-s $path,"] compressing\n";
206    
207                    my $comp = $dest . '.gz';
208                    file_copy( '<', $path, '>:gzip', $comp );
209    
210                    my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
211    
212                    if ( $size_path <= $size_comp ) {
213                            warn ">>> $size_path <= $size_comp leaving uncompressed $dest\n";
214                            unlink $comp || confess "can't remove: $comp: $!";
215                            file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
216                    } else {
217                            warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n";
218    
219                            # FIXME add timeout to remove uncompressed version?
220                            unlink $path || confess "can't remove $path: $!";
221    
222                            if ( -e $dest ) {
223                                    warn "## cleanup uncompressed $dest\n" if $debug;
224                                    unlink $dest || confess "can't remove $dest: $!";
225                            }
226                    }
227    
228            }
229    }
230    
231  sub x_open {  sub x_open {
232          my ($file) = shift;          my ($file) = shift;
233          my ($mode) = shift;          my ($mode) = shift;
# Line 148  sub x_open { Line 243  sub x_open {
243                  return 0;                  return 0;
244          }          }
245    
         $pending->{$file}->{open}++;  
   
246          my $mode_desc = {          my $mode_desc = {
247                  rdonly => $mode && O_RDONLY,                  rdonly => $mode && O_RDONLY,
248                  rdwr => $mode && O_RDWR,                  rdwr => $mode && O_RDWR,
# Line 157  sub x_open { Line 250  sub x_open {
250                  create => $mode && O_CREAT,                  create => $mode && O_CREAT,
251                  trunc => $mode && O_TRUNC,                  trunc => $mode && O_TRUNC,
252          };          };
253          my $path = fixup($file);  
254            my $path = tmp_path( $file );
255    
256          warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;          warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
257    
258          my $fh;          my $fh;
259            my $rv = 0;
260    
261          my $tmp = $mount->{tmp} . '/' . $file;          if ( ! -w $path ) {
262          if ( -e $tmp ) {                  my $old_mode = (stat $path)[2];
263                  $path = $tmp;                  my $new_mode = $old_mode | 0600;
264          } elsif ( $path =~ m/\.gz$/ ) {                  chmod $new_mode, $path || confess "can't chmod $new_mode $path";
265                  my $dest_path = $tmp;                  warn "### modify mode $old_mode -> $new_mode for $path\n";
266                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix                  $pending->{$file}->{mode} = $old_mode;
                 mkpath $dest_path unless -e $dest_path;  
                 if ( -s $path ) {  
                         file_copy( '<:gzip', $path, '>', $tmp )  
                 } else {  
                         warn "ERROR: filesystem corruption, $path is zero size\n";  
                 }  
                 $path = $tmp;  
267          }          }
268    
269          if ( sysopen($fh , $path, $mode) ) {          if ( sysopen($fh , $path, $mode) ) {
270                  close($fh) || confess "can't close $path: $!";                  close($fh) || confess "can't close $path: $!";
271                  warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n";                  warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n";
272                  $pending->{$file}->{path} = $path;                  $pending->{$file}->{open}++;
                 return 0;  
273          } else {          } else {
274                  warn "ERROR: can't open $path : $!";                  warn "ERROR: can't open $path -- $!";
275                  return -$!;                  $rv = -$!;
276          }          }
277    
278            return $rv;
279    
280  }  }
281    
282  sub x_read {  sub x_read {
# Line 202  sub x_read { Line 293  sub x_read {
293    
294          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
295                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
296                    $pending->{$file}->{read} += length($rv) if $stats;
297          }          }
298    
299          return $rv;          return $rv;
# Line 209  sub x_read { Line 301  sub x_read {
301    
302  sub x_write {  sub x_write {
303          my ($file,$buf,$off) = @_;          my ($file,$buf,$off) = @_;
304          $pending->{$file}->{write}++;  
305          my $rv;          my $rv;
306          my $path = fixup($file);          my $path = fixup($file);
307    
# Line 217  sub x_write { Line 309  sub x_write {
309    
310          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
311    
312            $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
313            confess "write into non-existant $path for $file" unless -e $path;
314    
315          my $fh = new IO::File;          my $fh = new IO::File;
316          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});          return -ENOSYS() unless open($fh,'+<',$path);
317          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
318                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
319                  warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;                  my $size = length($buf);
320                    warn "## write $path offset $off [$size]\n" if $debug;
321                    $pending->{$file}->{write} += $size;
322          }          }
323          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
324          close($fh);          close($fh) || warn "can't close $path: $!";
325          return length($buf);          return length($buf);
326  }  }
327    
328  sub err { return (-shift || -$!) }  sub err { return (-shift || -$!) }
329    
330  sub x_readlink { return readlink(fixup(shift));         }  sub x_readlink { return readlink(fixup(shift)); }
331  sub x_unlink   { return unlink_all( shift ) ? 0 : -$! }  
332    sub x_unlink   {
333            my $file = shift;
334            my $path = fixup( $file );
335    
336            if ( $file =~ m#\Q/.fuse_hidden\E# ) {
337                    return unlink $path ? 0 : -$1;
338            }
339    
340            warn "# unlink( $file )\n";
341    
342            unlink $path || return 0;
343    
344            my $tmp = $mount->{tmp} . '/' . $file;
345            unlink $tmp if ( -e $tmp );
346    
347            delete( $pending->{$file} );
348            return 0;
349    }
350    
351    sub x_symlink {
352            my ($from,$to) = @_;
353    
354            my $from_path = $from;  #fixup( $from );
355            my $to_path = fixup( $to );
356    
357            my $rv = symlink( $from_path, $to_path ) ? 0 : -$!;
358            warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug;
359    
360            my $tmp = $mount->{tmp} . '/' . $from;
361            if ( -e $tmp ) {
362                    my $tmp_to = $mount->{$tmp} . '/' . $to;
363                    symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!";
364            }
365            return $rv;
366    }
367    
368    sub x_link {
369            my ($from,$to) = @_;
370    
371  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }          my $from_path = fixup($from);
372            my $to_path = fixup($to);
373            $to_path .= '.gz' if ( $from_path =~ m/\.gz$/ && $to_path !~ m/\.gz$/ );
374    
375            my $rv = link( $from_path, $to_path ) ? 0 : -$!;
376    
377            warn "# link( $from_path -> $to_path ) = $rv\n" if $debug;
378    
379            return $rv;
380    }
381    
382  sub x_rename {  sub x_rename {
383          my ($old,$new) = @_;          my ($old,$new) = @_;
# Line 246  sub x_rename { Line 390  sub x_rename {
390    
391          my $tmp = $mount->{tmp} . '/' . $old;          my $tmp = $mount->{tmp} . '/' . $old;
392          if ( -e $tmp ) {          if ( -e $tmp ) {
393                  my $new_tmp = $mount->{tmp} . '/' . $new;                  if ( $new =~ m#\Q/.fuse_hidden\E# ) {
394                  rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";                          unlink $tmp || confess "can't unlink $tmp for $new";
395                    } else {
396                            my $new_tmp = $mount->{tmp} . '/' . $new;
397                            rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
398                    }
399          }          }
400    
401          if (defined( $pending->{$old} )) {          if (defined( $pending->{$old} )) {
# Line 256  sub x_rename { Line 404  sub x_rename {
404                  my $path = $pending->{$old}->{path};                  my $path = $pending->{$old}->{path};
405                  $path =~ s/\Q$old\E/$new/;                  $path =~ s/\Q$old\E/$new/;
406                  $pending->{$new}->{path} = $path;                  $pending->{$new}->{path} = $path;
                 $pending->{$old}->{path} = $path;  
407    
408                  #delete( $pending->{$old} );                  delete( $pending->{$old} );
409                    warn "## tweaking pending to ", dump( $pending ) if $debug;
410          }          }
411    
412          return $err;          return $err;
413  }  }
 sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }  
414    
415  sub x_chown {  sub x_chown {
416          my ($path) = fixup(shift);          my ($file,$uid,$gid) = @_;
417            my $path = fixup($file);
418          print "nonexistent $path\n" unless -e $path;          print "nonexistent $path\n" unless -e $path;
         my ($uid,$gid) = @_;  
419          # perl's chown() does not chown symlinks, it chowns the symlink's          # perl's chown() does not chown symlinks, it chowns the symlink's
420          # target.  it fails when the link's target doesn't exist, because          # target.  it fails when the link's target doesn't exist, because
421          # the stat64() syscall fails.          # the stat64() syscall fails.
422          # this causes error messages when unpacking symlinks in tarballs.          # this causes error messages when unpacking symlinks in tarballs.
423          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
424    
425            my $tmp = $mount->{tmp} . '/' . $file;
426            syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp;
427    
428          return $err;          return $err;
429  }  }
430    
# Line 286  sub x_chmod { Line 437  sub x_chmod {
437    
438  sub x_truncate {  sub x_truncate {
439          my ( $file,$size ) = @_;          my ( $file,$size ) = @_;
440          my $path = fixup($file);  
441            #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} );
442    
443            my $path = tmp_path( $file );
444          my $rv = truncate( $path, $size ) ? 0 : -$! ;          my $rv = truncate( $path, $size ) ? 0 : -$! ;
         if ( $path =~ m/\.gz$/ ) {  
                 my $no_gz = $path;  
                 $no_gz =~ s/\.gz$//;  
                 rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";  
         }  
445          warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;          warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
446          $pending->{$file}->{write}++;          compress_file2path( $file, $path );
447    
448          return $rv;          return $rv;
449  }  }
450    
451  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
452    
453  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 314  sub x_mknod { Line 465  sub x_mknod {
465    
466  sub x_release {  sub x_release {
467          my ( $file, $mode ) = @_;          my ( $file, $mode ) = @_;
468    
469          if ( ! defined( $pending->{$file} ) ) {          if ( ! defined( $pending->{$file} ) ) {
470                  warn "release $file, NO PENDING DATA\n";                  warn "release $file, NO PENDING DATA\n";
471                  return 0;                  return 0;
472          } elsif ( ! defined( $pending->{$file}->{write} ) ) {          } elsif ( ! defined( $pending->{$file}->{write} ) ) {
473                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
474          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {          } elsif ( $file =~ m#\Q/.fuse_hidden\E# ) {
475                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );                  warn "release internal $file\n" if $debug;
476                  my $dest = fixup( $file );          } else {
477                    compress_file2path( $file );
478                  # cleanup old compressed copy          }
                 if ( $dest =~ /\.gz$/ ) {  
                         warn "## remove old $dest\n";  
                         unlink $dest || confess "can't remove $dest: $!";  
                         $dest =~ s/\.gz$//;  
                 }  
479    
480                  if ( $file =~ $skip_extensions_regex ) {          $pending->{$file}->{open}--;
481                          warn "release $file [",-s $path,"] skipped compression\n";          if ( $pending->{$file}->{open} == 0 ) {
                         file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );  
                 } elsif ( -s $path < $min_compress_size ) {  
                         warn "release $file [",-s $path,"] uncompressed, too small\n";  
                         file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );  
                 } else {  
                         warn "release $file [",-s $path,"] compressing\n";  
482    
483                          my $comp = $dest . '.gz';                  my $path = fixup( $file );
                         file_copy( '<', $path, '>:gzip', $comp );  
484    
485                          my ( $size_path, $size_comp ) = ( -s $path, -s $comp );                  if ( my $old_mode = $pending->{$file}->{mode} ) {
486                            chmod $old_mode, $path || confess "can't chmod $old_mode $path";
487                            warn "### restored mode $old_mode $path\n";
488    
                         if ( $size_path <= $size_comp ) {  
                                 warn ">>> $size_path <= $size_comp leaving uncompressed\n";  
                                 unlink $comp || warn "can't reamove: $comp: $!";  
                         } else {  
                                 warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";  
                                 # FIXME add timeout to remove uncompressed version?  
                                 unlink $path || warn "can't remove $path: $!";  
                         }  
489                  }                  }
490          } else {  
491                  warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";                  warn "## cleanup pending $file [", -s $path, "]\n" if $debug;
         }  
         $pending->{$file}->{open}--;  
         if ( $pending->{$file}->{open} == 0 ) {  
                 warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;  
492                  delete( $pending->{$file} );                  delete( $pending->{$file} );
493          }          }
494    
495          return 0;          return 0;
496  }  }
497    

Legend:
Removed from v.23  
changed lines
  Added in v.38

  ViewVC Help
Powered by ViewVC 1.1.26