/[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 13 by dpavlin, Mon Jul 9 11:12:48 2007 UTC revision 29 by dpavlin, Tue Jul 10 02:57:03 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    my $stats = 1;
20    
21    GetOptions(
22            'debug+' => \$debug,
23            'fuse-debug+' => \$fuse_debug,
24            'stats!' => \$stats,
25    );
26    
27  my $mount = {  my $mount = {
28          from    => '/tmp/comp',          from    => '/tmp/comp',
# Line 19  my $mount = { Line 30  my $mount = {
30          tmp             => '/dev/shm/comp',          tmp             => '/dev/shm/comp',
31  };  };
32    
 my $debug = 1;  
   
33  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;
34    
35  # don't compress files smaller than this  # don't compress files smaller than this
# Line 35  foreach my $dir ( keys %$mount ) { Line 44  foreach my $dir ( keys %$mount ) {
44    
45  my $pending;  my $pending;
46    
47    sub real_name {
48            my ( $dir, $name ) = @_;
49            if ( -e "$dir/${name}.gz" ) {
50                    confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name";
51                    return "${name}.gz";
52            }
53            return $name;
54    }
55    
56  sub fixup {  sub fixup {
57          my ( $path ) = @_;          my ( $path ) = @_;
58          my $full = $mount->{from} . '/' . $path;          return $mount->{from} . '/' . real_name( $mount->{from}, $path );
         if ( -e $full . '.gz' ) {  
                 return $full . '.gz';  
         }  
         return $full;  
59  }  }
60    
61  sub original_name {  sub original_name {
# Line 69  sub gzip_original_size { Line 83  sub gzip_original_size {
83          return unpack("L", $buff);          return unpack("L", $buff);
84  }  }
85    
 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;  
 }  
   
86  sub x_getattr {  sub x_getattr {
87          my ($file) = fixup(shift);          my ($file) = fixup(shift);
88          my (@list) = lstat($file);          my (@list) = lstat($file);
# Line 103  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 [",-s $s_path,"] $d_opt $d_path [",-e $d_path ? -s $d_path : 'new',"])\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;
# Line 124  sub file_copy { Line 124  sub file_copy {
124          undef $s;          undef $s;
125  }  }
126    
127    sub tmp_path {
128            my $file = shift;
129    
130            my $path = fixup( $file );
131    
132            my $op = 'UNKNOWN';
133    
134            if (defined( $pending->{$file} )) {
135                    $path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending );
136                    $op = 'opened';
137            } else {
138                    my $tmp = $mount->{tmp} . '/' . $file;
139                    if ( -e $tmp ) {
140                            $path = $tmp;
141                            $op = 'existing';
142                    } elsif ( $path =~ m/\.gz$/ ) {
143                            my $dest_path = $tmp;
144                            $dest_path =~ s!/[^/]+$!!;      #!vim-fix
145                            mkpath $dest_path unless -e $dest_path;
146                            if ( -s $path ) {
147                                    file_copy( '<:gzip', $path, '>', $tmp )
148                            } else {
149                                    confess "ASSERT: filesystem corruption, $path is zero size\n";
150                            }
151                            $path = $tmp;
152                            $op = 'created';
153                    }
154                    confess "ASSERT: path shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{path} );
155                    confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} );
156                    $pending->{$file}->{path} = $path;
157                    $pending->{$file}->{open} = 0;  # not really opened, just uncompressed
158                    warn "## tmp_file( $file ) $op $path [", -s $path, "]\n";
159            }
160            return $path;
161    }
162    
163    sub compress_file2path {
164            my ( $file, $path ) = @_;
165    
166            my $dest = fixup( $file );
167    
168            if ( defined($pending->{$file}) ) {
169                    my $pending_path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending );
170    
171                    if ( $pending->{$file}->{open} > 1 ) {
172                            warn "$file used ", $pending->{$file}->{open}, " times, delaying compression\n";
173                            return;
174                    } elsif ( ! $path ) {
175                            $path = $pending_path;
176                    } elsif ( $pending_path ne $path ) {
177                            confess "ASSERT: compressing into $path instead of $pending_path\n";
178                    }
179            }
180    
181            confess "need path" unless $path;
182    
183            # cleanup old compressed copy
184            if ( $dest =~ /\.gz$/ ) {
185                    warn "## remove old $dest\n";
186                    unlink $dest || confess "can't remove $dest: $!";
187                    $dest =~ s/\.gz$//;
188                    confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest;
189            }
190    
191            if ( $path =~ $skip_extensions_regex ) {
192                    warn "$path [",-s $path,"] skipped compression\n";
193                    file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
194            } elsif ( -s $path < $min_compress_size ) {
195                    warn "$path [",-s $path,"] uncompressed, too small\n";
196                    file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
197            } else {
198                    warn "$path [",-s $path,"] compressing\n";
199    
200                    my $comp = $dest . '.gz';
201                    file_copy( '<', $path, '>:gzip', $comp );
202    
203                    my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
204    
205                    if ( $size_path <= $size_comp ) {
206                            warn ">>> $size_path <= $size_comp leaving uncompressed\n";
207                            unlink $comp || confess "can't remove: $comp: $!";
208                    } else {
209                            warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n";
210                            # FIXME add timeout to remove uncompressed version?
211                            unlink $path || confess "can't remove $path: $!";
212                    }
213            }
214    }
215    
216  sub x_open {  sub x_open {
217          my ($file) = shift;          my ($file) = shift;
218          my ($mode) = shift;          my ($mode) = shift;
# Line 139  sub x_open { Line 228  sub x_open {
228                  return 0;                  return 0;
229          }          }
230    
         $pending->{$file}->{open}++;  
   
231          my $mode_desc = {          my $mode_desc = {
232                  rdonly => $mode && O_RDONLY,                  rdonly => $mode && O_RDONLY,
233                  rdwr => $mode && O_RDWR,                  rdwr => $mode && O_RDWR,
234                  append => $mode && O_APPEND,                  append => $mode && O_APPEND,
235                  create => $mode && O_CREAT,                  create => $mode && O_CREAT,
236                    trunc => $mode && O_TRUNC,
237          };          };
238          warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n";  
239            my $path = tmp_path( $file );
240    
241            warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
242    
243          my $fh;          my $fh;
244    
245          my $path = fixup($file);          if ( sysopen($fh , $path, $mode) ) {
246          my $tmp = $mount->{tmp} . '/' . $file;                  close($fh) || confess "can't close $path: $!";
247          if ( -e $tmp ) {                  warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n";
248                  $path = $tmp;                  $pending->{$file}->{open}++;
249          } elsif ( $path =~ m/\.gz$/ ) {                  return 0;
250                  my $dest_path = $tmp;          } else {
251                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix                  warn "ERROR: can't open $path -- $!";
252                  mkpath $dest_path unless -e $dest_path;                  return -$!;
                 file_copy( '<:gzip', $path, '>', $tmp );  
                 $path = $tmp;  
253          }          }
         warn ">>> open abs path: $path [", -s $path, "]\n";  
         return -$! unless sysopen($fh , $path, $mode);  
         close($fh);  
254    
         $pending->{$file}->{path} = $path;  
         return 0;  
255  }  }
256    
257  sub x_read {  sub x_read {
# Line 183  sub x_read { Line 268  sub x_read {
268    
269          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
270                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
271                    $pending->{$file}->{read} += length($rv) if $stats;
272          }          }
273    
274          return $rv;          return $rv;
# Line 190  sub x_read { Line 276  sub x_read {
276    
277  sub x_write {  sub x_write {
278          my ($file,$buf,$off) = @_;          my ($file,$buf,$off) = @_;
279          $pending->{$file}->{write}++;  
280          my $rv;          my $rv;
281          my $path = fixup($file);          my $path = fixup($file);
282    
# Line 198  sub x_write { Line 284  sub x_write {
284    
285          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
286    
287            $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
288            confess "write into non-existant $path for $file" unless -e $path;
289    
290          my $fh = new IO::File;          my $fh = new IO::File;
291          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});          return -ENOSYS() unless open($fh,'+<',$path);
292          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
293                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
294                  warn "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;                  my $size = length($buf);
295                    warn "## write $path offset $off [$size]\n" if $debug;
296                    $pending->{$file}->{write} += $size;
297          }          }
298          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
299          close($fh);          close($fh) || warn "can't close $path: $!";
300          return length($buf);          return length($buf);
301  }  }
302    
303  sub err { return (-shift || -$!) }  sub err { return (-shift || -$!) }
304    
305  sub x_readlink { return readlink(fixup(shift));         }  sub x_readlink { return readlink(fixup(shift)); }
 sub x_unlink   { return unlink_all( shift ) ? 0 : -$! }  
306    
307  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }  sub x_unlink   {
308            my $file = shift;
309            my $path = fixup( $file );
310    
311            if ( $file =~ m#\Q/.fuse_hidden\E# ) {
312                    return unlink $path ? 0 : -$1;
313            }
314    
315            warn "# unlink( $file )\n";
316    
317            unlink $path || return 0;
318    
319            my $tmp = $mount->{tmp} . '/' . $file;
320            unlink $tmp if ( -e $tmp );
321    
322            delete( $pending->{$file} );
323            return 0;
324    }
325    
326    sub x_symlink {
327            my ($from,$to) = @_;
328    
329            my $from_path = $from;  #fixup( $from );
330            my $to_path = fixup( $to );
331    
332            my $rv = symlink( $from_path, $to_path ) ? 0 : -$!;
333            warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug;
334    
335            my $tmp = $mount->{tmp} . '/' . $from;
336            if ( -e $tmp ) {
337                    my $tmp_to = $mount->{$tmp} . '/' . $to;
338                    symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!";
339            }
340            return $rv;
341    }
342    
343    sub x_link {
344            my ($from,$to) = @_;
345    
346            my $from_path = fixup($from);
347            my $to_path = fixup($to);
348            $to_path .= '.gz' if ( $from_path =~ m/\.gz$/ && $to_path !~ m/\.gz$/ );
349    
350            my $rv = link( $from_path, $to_path ) ? 0 : -$!;
351    
352            warn "# link( $from_path -> $to_path ) = $rv\n" if $debug;
353    
354            return $rv;
355    }
356    
357  sub x_rename {  sub x_rename {
358          my ($old) = fixup(shift);          my ($old,$new) = @_;
359          my ($new) = fixup(shift);          my $old_path = fixup($old);
360          my ($err) = rename($old,$new) ? 0 : -ENOENT();          my $new_path = fixup($new);
361            $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
362    
363            my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
364            warn "## rename( $old_path => $new_path ) = $err\n";
365    
366            my $tmp = $mount->{tmp} . '/' . $old;
367            if ( -e $tmp ) {
368                    if ( $new =~ m#\Q/.fuse_hidden\E# ) {
369                            unlink $tmp || confess "can't unlink $tmp for $new\n";
370                    } else {
371                            my $new_tmp = $mount->{tmp} . '/' . $new;
372                            rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
373                    }
374            }
375    
376            if (defined( $pending->{$old} )) {
377                    $pending->{$new} = $pending->{$old};
378    
379                    my $path = $pending->{$old}->{path};
380                    $path =~ s/\Q$old\E/$new/;
381                    $pending->{$new}->{path} = $path;
382    
383                    delete( $pending->{$old} );
384                    warn "## tweaking pending to ", dump( $pending ) if $debug;
385            }
386    
387          return $err;          return $err;
388  }  }
 sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }  
389    
390  sub x_chown {  sub x_chown {
391          my ($path) = fixup(shift);          my ($file,$uid,$gid) = @_;
392            my $path = fixup($file);
393          print "nonexistent $path\n" unless -e $path;          print "nonexistent $path\n" unless -e $path;
         my ($uid,$gid) = @_;  
394          # perl's chown() does not chown symlinks, it chowns the symlink's          # perl's chown() does not chown symlinks, it chowns the symlink's
395          # target.  it fails when the link's target doesn't exist, because          # target.  it fails when the link's target doesn't exist, because
396          # the stat64() syscall fails.          # the stat64() syscall fails.
397          # this causes error messages when unpacking symlinks in tarballs.          # this causes error messages when unpacking symlinks in tarballs.
398          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
399    
400            my $tmp = $mount->{tmp} . '/' . $file;
401            syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp;
402    
403          return $err;          return $err;
404  }  }
405    
# Line 243  sub x_chmod { Line 410  sub x_chmod {
410          return $err;          return $err;
411  }  }
412    
413  sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }  sub x_truncate {
414            my ( $file,$size ) = @_;
415    
416            #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} );
417    
418            my $path = tmp_path( $file );
419            my $rv = truncate( $path, $size ) ? 0 : -$! ;
420            warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
421            compress_file2path( $file, $path );
422    
423            return $rv;
424    }
425    
426  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
427    
428  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 440  sub x_mknod {
440    
441  sub x_release {  sub x_release {
442          my ( $file, $mode ) = @_;          my ( $file, $mode ) = @_;
443    
444          if ( ! defined( $pending->{$file} ) ) {          if ( ! defined( $pending->{$file} ) ) {
445                  warn "release $file, NO PENDING DATA\n";                  warn "release $file, NO PENDING DATA\n";
446                  return 0;                  return 0;
447          } elsif ( ! defined( $pending->{$file}->{write} ) ) {          } elsif ( ! defined( $pending->{$file}->{write} ) ) {
448                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
449          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {          } elsif ( $file =~ m#\Q/.fuse_hidden\E# ) {
450                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );                  warn "release internal $file\n" if $debug;
                 my $dest = fixup( $file );  
   
                 # cleanup old compressed copy  
                 if ( $dest =~ /\.gz$/ ) {  
                         warn "## remove old $dest\n";  
                         unlink $dest || confess "can't remove $dest: $!";  
                         $dest =~ s/\.gz$//;  
                 }  
   
                 if ( $file =~ $skip_extensions_regex ) {  
                         warn "release $file $mode -- uncompressed\n";  
                         file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );  
                 } elsif ( -s $path < $min_compress_size ) {  
                         warn "release $file -- uncompressed, too small [", -s $path, "]\n";  
                         file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );  
                 } else {  
                         warn "release $file $mode -- compressing\n";  
   
   
                         file_copy( '<', $path, '>:gzip', $dest . '.gz' );  
   
                         # FIXME add timeout to remove uncompressed version?  
                         unlink $path || warn "can't remove $path: $!";  
                 }  
451          } else {          } else {
452                  warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";                  compress_file2path( $file );
453          }          }
454    
455          $pending->{$file}->{open}--;          $pending->{$file}->{open}--;
456          if ( $pending->{$file}->{open} == 0 ) {          if ( $pending->{$file}->{open} == 0 ) {
457                  warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;                  warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
458                  delete( $pending->{$file} );                  delete( $pending->{$file} );
459          }          }
460    
461          return 0;          return 0;
462  }  }
463    
# Line 327  Fuse::main( Line 485  Fuse::main(
485          statfs  =>"main::x_statfs",          statfs  =>"main::x_statfs",
486          release =>"main::x_release",          release =>"main::x_release",
487  #       threaded=>1,  #       threaded=>1,
488  #       debug   => 1,          debug   => $fuse_debug,
489  );  );

Legend:
Removed from v.13  
changed lines
  Added in v.29

  ViewVC Help
Powered by ViewVC 1.1.26