/[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 4 by dpavlin, Sun Jul 8 13:46:33 2007 UTC revision 35 by dpavlin, Mon Jul 16 08:24:08 2007 UTC
# Line 4  use threads; Line 4  use threads;
4  use threads::shared;  use threads::shared;
5    
6  use Fuse;  use Fuse;
 use IO::File;  
7  use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);  use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
8  use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);  use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);
9  require 'syscall.ph'; # for SYS_mknod and SYS_lchown  require 'syscall.ph'; # for SYS_mknod and SYS_lchown
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/;
14    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 18  my $mount = { Line 30  my $mount = {
30          tmp             => '/dev/shm/comp',          tmp             => '/dev/shm/comp',
31  };  };
32    
33  my $debug = 1;  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;
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;  # don't compress files smaller than this
36    my $min_compress_size = 512;
37    
38  foreach my $dir ( keys %$mount ) {  foreach my $dir ( keys %$mount ) {
39          if ( ! -e $mount->{$dir} ) {          if ( ! -e $mount->{$dir} ) {
# Line 31  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            if ( $name =~ m/\.gz$/ ) {
54                    return $name . '%';     # protect (mingle) compressed files
55            } else {
56                    return $name;
57            }
58    }
59    
60  sub fixup {  sub fixup {
61          my ( $path ) = @_;          my ( $path ) = @_;
62          my $full = $mount->{from} . '/' . $path;          return $mount->{from} . '/' . real_name( $mount->{from}, $path );
         if ( -e $full . '.gz' ) {  
                 return $full . '.gz';  
         }  
         return $full;  
63  }  }
64    
65  sub original_name {  sub original_name {
66          my $p = shift;          my $p = shift;
67          $p =~ s/\.gz$//;          $p =~ s/\.gz$//;
68            $p =~ s/\.gz%$/.gz/;    # demungle compressed .gz files
69          return $p;          return $p;
70  };  };
71    
# Line 85  sub x_getdir { Line 108  sub x_getdir {
108    
109  sub file_copy {  sub file_copy {
110          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
111          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;
112          open(my $s, $s_opt, $s_path ) || die "can't open $s_path: $!";          open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending );
113          open(my $d, $d_opt, $d_path ) || die "can't open $d_path: $!";          open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!";
114          my $buff;          my $buff;
115          while( read( $s, $buff, 65535 ) ) {          while( read( $s, $buff, 65535 ) ) {
116                  print $d $buff || die "can't write into $d_path: $!";                  print $d $buff || confess "can't write into $d_path: $!";
117                  warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;                  warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
118          }          }
119          close($d) || warn "can't close $d_path: $!";          close($d) || warn "can't close $d_path: $!";
120          close($s) || warn "can't close $s_path: $!";          close($s) || warn "can't close $s_path: $!";
121          warn "-- $s_path [", -s $s_path, "]\n >>> $d_path [", -s $d_path, "]\n" if $debug;          warn "-- $s_path [", -s $s_path, "] >>> $d_path [", -s $d_path, "]\n" if $debug;
122            my ($mode,$uid,$gid,$atime,$mtime) = (stat $s_path)[2,4,5,8,9];
123    
124            chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";
125            chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";
126            utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
127    
128            undef $d;
129            undef $s;
130  }  }
131    
132  sub x_open {  sub tmp_path {
133          my ($file) = shift;          my $file = shift;
134          my ($mode) = shift;  
135          $pending->{$file}->{open}++;          my $path = fixup( $file );
136          my $fh;  
137          if ( $pending->{$file}->{open} == 1 ) {          my $op = 'UNKNOWN';
138                  warn "# open( $file, $mode )\n";  
139                  my $path = fixup($file);          if (defined( $pending->{$file} )) {
140                    $path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending );
141                    $op = 'opened';
142            } else {
143                  my $tmp = $mount->{tmp} . '/' . $file;                  my $tmp = $mount->{tmp} . '/' . $file;
144                  if ( -e $tmp ) {                  if ( -e $tmp ) {
145                          $path = $tmp;                          $path = $tmp;
146                            $op = 'existing';
147                  } elsif ( $path =~ m/\.gz$/ ) {                  } elsif ( $path =~ m/\.gz$/ ) {
148                          my $dest_path = $tmp;                          my $dest_path = $tmp;
149                          $dest_path =~ s!/[^/]+$!!;      #!vim-fix                          $dest_path =~ s!/[^/]+$!!;      #!vim-fix
150                          mkpath $dest_path unless -e $dest_path;                          mkpath $dest_path unless -e $dest_path;
151                          file_copy( '<:gzip', $path, '>', $tmp );                          if ( -s $path ) {
152                                    file_copy( '<:gzip', $path, '>', $tmp )
153                            } else {
154                                    confess "ASSERT: filesystem corruption, $path is zero size in ",dump( $pending );
155                            }
156                          $path = $tmp;                          $path = $tmp;
157                            $op = 'created';
158                  }                  }
159                  return -$! unless sysopen($fh , $path, $mode);                  confess "ASSERT: path shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{path} );
160                  $pending->{$file}->{fh} = $fh;                  confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} );
161                  $pending->{$file}->{path} = $path;                  $pending->{$file}->{path} = $path;
162          } elsif ( ! defined( $pending->{$file}->{fh} ) ) {                  $pending->{$file}->{open} = 0;  # not really opened, just uncompressed
163                  die "can't find fh for $file ", dump($pending);                  warn "## tmp_file( $file ) $op $path [", -s $path, "]\n" if $debug;
164          }          }
165          return 0;          return $path;
166    }
167    
168    sub compress_file2path {
169            my ( $file, $path ) = @_;
170    
171            my $dest = fixup( $file );
172    
173            if ( defined($pending->{$file}) ) {
174                    my $pending_path = $pending->{$file}->{path} || confess "no path for $file in ",dump( $pending );
175    
176                    if ( $pending->{$file}->{open} > 1 ) {
177                            warn "$file used ", $pending->{$file}->{open}, " times, delaying compression\n";
178                            return;
179                    } elsif ( ! $path ) {
180                            $path = $pending_path;
181                    } elsif ( $pending_path ne $path ) {
182                            confess "ASSERT: compressing into $path instead of $pending_path";
183                    }
184            }
185    
186            confess "need path" unless $path;
187    
188            # cleanup old compressed copy
189            if ( $dest =~ /\.gz$/ ) {
190                    warn "## remove old $dest\n";
191                    unlink $dest || confess "can't remove $dest: $!";
192                    $dest =~ s/\.gz$//;
193                    confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest;
194            }
195    
196            if ( $path =~ $skip_extensions_regex ) {
197                    warn "$path [",-s $path,"] skipped compression\n";
198                    file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
199            } elsif ( -s $path < $min_compress_size ) {
200                    warn "$path [",-s $path,"] uncompressed, too small\n";
201                    file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
202            } else {
203                    warn "$path [",-s $path,"] compressing\n";
204    
205                    my $comp = $dest . '.gz';
206                    file_copy( '<', $path, '>:gzip', $comp );
207    
208                    my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
209    
210                    if ( $size_path <= $size_comp ) {
211                            warn ">>> $size_path <= $size_comp leaving uncompressed $dest\n";
212                            unlink $comp || confess "can't remove: $comp: $!";
213                            file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
214                    } else {
215                            warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n";
216    
217                            # FIXME add timeout to remove uncompressed version?
218                            unlink $path || confess "can't remove $path: $!";
219    
220                            if ( -e $dest ) {
221                                    warn "## cleanup uncompressed $dest\n" if $debug;
222                                    unlink $dest || confess "can't remove $dest: $!";
223                            }
224                    }
225    
226            }
227    }
228    
229    sub x_open {
230            my ($file) = shift;
231            my ($mode) = shift;
232    
233            if ( $file eq '/.debug' ) {
234                    my $path = $mount->{from} . '/.debug';
235                    open( my $debug, '>', $path ) || die "can't open $path: $!";
236                    my $dump = dump( $pending );
237                    print $debug "pending = $dump\n";
238                    close($debug);
239                    $pending->{'/.debug'}->{path} = $path;
240                    warn "## created dump $path $dump\n";
241                    return 0;
242            }
243    
244            my $mode_desc = {
245                    rdonly => $mode && O_RDONLY,
246                    rdwr => $mode && O_RDWR,
247                    append => $mode && O_APPEND,
248                    create => $mode && O_CREAT,
249                    trunc => $mode && O_TRUNC,
250            };
251    
252            my $path = tmp_path( $file );
253    
254            warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
255    
256            my $fh;
257            my $rv = 0;
258    
259            if ( ! -w $path ) {
260                    my $old_mode = (stat $path)[2];
261                    my $new_mode = $old_mode | 0600;
262                    chmod $new_mode, $path || confess "can't chmod $new_mode $path";
263                    warn "### modify mode $old_mode -> $new_mode for $path\n";
264                    $pending->{$file}->{mode} = $old_mode;
265            }
266    
267            if ( sysopen($fh , $path, $mode) ) {
268                    close($fh) || confess "can't close $path: $!";
269                    warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n";
270                    $pending->{$file}->{open}++;
271            } else {
272                    warn "ERROR: can't open $path -- $!";
273                    $rv = -$!;
274            }
275    
276            return $rv;
277    
278  }  }
279    
280  sub x_read {  sub x_read {
281          my ($file,$bufsize,$off) = @_;          my ($file,$bufsize,$off) = @_;
282          my ($rv) = -ENOSYS();          my ($rv) = -ENOSYS();
283          my $path = fixup( $file );          my $path = fixup( $file );
284    
285            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
286    
287          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
288          my ($fsize) = -s $path;  
289          my $fh = $pending->{$file}->{fh} || die "no fh? ", dump( $pending );          my $fh = new IO::File;
290            return -ENOSYS() unless open($fh,$pending->{$file}->{path});
291    
292          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
293                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
294                    $pending->{$file}->{read} += length($rv) if $stats;
295          }          }
296    
297          return $rv;          return $rv;
298  }  }
299    
300  sub x_write {  sub x_write {
301          my ($file,$buf,$off) = @_;          my ($file,$buf,$off) = @_;
302          $pending->{$file}->{write}++;  
303          my ($rv);          my $rv;
304          my $path = fixup($file);          my $path = fixup($file);
305    
306            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
307    
308          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
309          my ($fsize) = -s $path;  
310          my $fh = $pending->{$file}->{fh};          $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
311          return -ENOSYS() unless $fh;          confess "write into non-existant $path for $file" unless -e $path;
312    
313            my $fh = new IO::File;
314            return -ENOSYS() unless open($fh,'+<',$path);
315          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
316                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
317                    my $size = length($buf);
318                    warn "## write $path offset $off [$size]\n" if $debug;
319                    $pending->{$file}->{write} += $size;
320          }          }
321          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
322            close($fh) || warn "can't close $path: $!";
323          return length($buf);          return length($buf);
324  }  }
325    
326  sub err { return (-shift || -$!) }  sub err { return (-shift || -$!) }
327    
328  sub x_readlink { return readlink(fixup(shift));         }  sub x_readlink { return readlink(fixup(shift)); }
329  sub x_unlink   { return unlink(fixup(shift)) ? 0 : -$!; }  
330    sub x_unlink   {
331            my $file = shift;
332            my $path = fixup( $file );
333    
334            if ( $file =~ m#\Q/.fuse_hidden\E# ) {
335                    return unlink $path ? 0 : -$1;
336            }
337    
338            warn "# unlink( $file )\n";
339    
340            unlink $path || return 0;
341    
342            my $tmp = $mount->{tmp} . '/' . $file;
343            unlink $tmp if ( -e $tmp );
344    
345            delete( $pending->{$file} );
346            return 0;
347    }
348    
349    sub x_symlink {
350            my ($from,$to) = @_;
351    
352            my $from_path = $from;  #fixup( $from );
353            my $to_path = fixup( $to );
354    
355            my $rv = symlink( $from_path, $to_path ) ? 0 : -$!;
356            warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug;
357    
358            my $tmp = $mount->{tmp} . '/' . $from;
359            if ( -e $tmp ) {
360                    my $tmp_to = $mount->{$tmp} . '/' . $to;
361                    symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!";
362            }
363            return $rv;
364    }
365    
366    sub x_link {
367            my ($from,$to) = @_;
368    
369            my $from_path = fixup($from);
370            my $to_path = fixup($to);
371            $to_path .= '.gz' if ( $from_path =~ m/\.gz$/ && $to_path !~ m/\.gz$/ );
372    
373  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }          my $rv = link( $from_path, $to_path ) ? 0 : -$!;
374    
375            warn "# link( $from_path -> $to_path ) = $rv\n" if $debug;
376    
377            return $rv;
378    }
379    
380  sub x_rename {  sub x_rename {
381          my ($old) = fixup(shift);          my ($old,$new) = @_;
382          my ($new) = fixup(shift);          my $old_path = fixup($old);
383          my ($err) = rename($old,$new) ? 0 : -ENOENT();          my $new_path = fixup($new);
384            $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
385    
386            my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
387            warn "## rename( $old_path => $new_path ) = $err\n";
388    
389            my $tmp = $mount->{tmp} . '/' . $old;
390            if ( -e $tmp ) {
391                    if ( $new =~ m#\Q/.fuse_hidden\E# ) {
392                            unlink $tmp || confess "can't unlink $tmp for $new";
393                    } else {
394                            my $new_tmp = $mount->{tmp} . '/' . $new;
395                            rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
396                    }
397            }
398    
399            if (defined( $pending->{$old} )) {
400                    $pending->{$new} = $pending->{$old};
401    
402                    my $path = $pending->{$old}->{path};
403                    $path =~ s/\Q$old\E/$new/;
404                    $pending->{$new}->{path} = $path;
405    
406                    delete( $pending->{$old} );
407                    warn "## tweaking pending to ", dump( $pending ) if $debug;
408            }
409    
410          return $err;          return $err;
411  }  }
 sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }  
412    
413  sub x_chown {  sub x_chown {
414          my ($path) = fixup(shift);          my ($file,$uid,$gid) = @_;
415            my $path = fixup($file);
416          print "nonexistent $path\n" unless -e $path;          print "nonexistent $path\n" unless -e $path;
         my ($uid,$gid) = @_;  
417          # perl's chown() does not chown symlinks, it chowns the symlink's          # perl's chown() does not chown symlinks, it chowns the symlink's
418          # target.  it fails when the link's target doesn't exist, because          # target.  it fails when the link's target doesn't exist, because
419          # the stat64() syscall fails.          # the stat64() syscall fails.
420          # this causes error messages when unpacking symlinks in tarballs.          # this causes error messages when unpacking symlinks in tarballs.
421          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;          my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
422    
423            my $tmp = $mount->{tmp} . '/' . $file;
424            syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp;
425    
426          return $err;          return $err;
427  }  }
428    
# Line 188  sub x_chmod { Line 433  sub x_chmod {
433          return $err;          return $err;
434  }  }
435    
436  sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }  sub x_truncate {
437            my ( $file,$size ) = @_;
438    
439            #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} );
440    
441            my $path = tmp_path( $file );
442            my $rv = truncate( $path, $size ) ? 0 : -$! ;
443            warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
444            compress_file2path( $file, $path );
445    
446            return $rv;
447    }
448    
449  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
450    
451  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 206  sub x_mknod { Line 463  sub x_mknod {
463    
464  sub x_release {  sub x_release {
465          my ( $file, $mode ) = @_;          my ( $file, $mode ) = @_;
466    
467          if ( ! defined( $pending->{$file} ) ) {          if ( ! defined( $pending->{$file} ) ) {
468                  warn "release $file, NO PENDING DATA\n";                  warn "release $file, NO PENDING DATA\n";
469                  return 0;                  return 0;
470          } elsif ( ! defined( $pending->{$file}->{write} ) ) {          } elsif ( ! defined( $pending->{$file}->{write} ) ) {
471                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
472          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {          } elsif ( $file =~ m#\Q/.fuse_hidden\E# ) {
473                  close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";                  warn "release internal $file\n" if $debug;
474                  if ( $file =~ $skip_extensions_regex ) {          } else {
475                          warn "release $file $mode -- uncompressed\n";                  compress_file2path( $file );
476                  } else {          }
                         warn "release $file $mode -- compressing\n";  
477    
478                          my $path = $pending->{$file}->{path} || die "no path for $file ? ", dump( $pending );          $pending->{$file}->{open}--;
479                          my $dest = fixup( $file );          if ( $pending->{$file}->{open} == 0 ) {
480    
481                          if ( $dest =~ /\.gz$/ ) {                  my $path = fixup( $file );
                                 warn "## remove old $dest\n";  
                                 unlink $dest || die "can't remove $dest: $!";  
                                 $dest =~ s/\.gz$//;  
                         }  
482    
483                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                  if ( my $old_mode = $pending->{$file}->{mode} ) {
484                            chmod $old_mode, $path || confess "can't chmod $old_mode $path";
485                            warn "### restored mode $old_mode $path\n";
486    
                         # FIXME add timeout to remove uncompressed version?  
                         unlink $path || warn "can't remove $path: $!";  
487                  }                  }
488          } else {  
489                  warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";                  warn "## cleanup pending $file [", -s $path, "]\n" if $debug;
490                  $pending->{$file}->{open}--;                  delete( $pending->{$file} );
                 return 0;  
491          }          }
492          delete( $pending->{$file} );  
493          return 0;          return 0;
494  }  }
495    
# Line 265  Fuse::main( Line 517  Fuse::main(
517          statfs  =>"main::x_statfs",          statfs  =>"main::x_statfs",
518          release =>"main::x_release",          release =>"main::x_release",
519  #       threaded=>1,  #       threaded=>1,
520  #       debug   => 1,          debug   => $fuse_debug,
521  );  );

Legend:
Removed from v.4  
changed lines
  Added in v.35

  ViewVC Help
Powered by ViewVC 1.1.26