/[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 5 by dpavlin, Sun Jul 8 15:04:35 2007 UTC revision 19 by dpavlin, Mon Jul 9 15:59:57 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
# Line 12  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 19  my $mount = { Line 19  my $mount = {
19          tmp             => '/dev/shm/comp',          tmp             => '/dev/shm/comp',
20  };  };
21    
22  my $debug = 1;  my $debug = 0;
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    
26  # don't compress files smaller than this  # don't compress files smaller than this
27  my $min_compress_size = 256;  my $min_compress_size = 512;
28    
29  foreach my $dir ( keys %$mount ) {  foreach my $dir ( keys %$mount ) {
30          if ( ! -e $mount->{$dir} ) {          if ( ! -e $mount->{$dir} ) {
# Line 71  sub gzip_original_size { Line 71  sub gzip_original_size {
71    
72  sub unlink_all {  sub unlink_all {
73          my $file = shift;          my $file = shift;
74          foreach my $dir ( keys %$mount ) {          warn "# unlink_all( $file )\n";
75                  my $path = $mount->{$dir} . '/' . $file;  
76                    my $path = fixup( $file );
77                  map {          unlink $path || return 0;
78                          my $path = $_;  
79                          if ( -e $path ) {          my $tmp = $mount->{tmp} . '/' . $file;
80                                  if ( unlink $path ) {          unlink $tmp if ( -e $tmp );
81                                          warn "## unlink $path\n" if $debug;  
                                 } else {  
                                         warn "can't unlink $path: $!\n";  
                                         return 0;  
                                 }  
                         }  
                 } [ $path, $path . '.gz' ];  
         }  
82          delete( $pending->{$file} );          delete( $pending->{$file} );
83          return 1;          return 1;
84  }  }
# Line 110  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 119  sub file_copy {
119          chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";          chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";
120          chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";          chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";
121          utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";          utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
122    
123            undef $d;
124            undef $s;
125  }  }
126    
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          warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, "\n";  
144          my $fh;          my $mode_desc = {
145          if ( $pending->{$file}->{open} == 1 ) {                  rdonly => $mode && O_RDONLY,
146                  my $path = fixup($file);                  rdwr => $mode && O_RDWR,
147                  my $tmp = $mount->{tmp} . '/' . $file;                  append => $mode && O_APPEND,
148                  warn ">>> open abs path: $path\n";                  create => $mode && O_CREAT,
149                  if ( -e $tmp ) {                  trunc => $mode && O_TRUNC,
                         $path = $tmp;  
                 } elsif ( $path =~ m/\.gz$/ ) {  
                         my $dest_path = $tmp;  
                         $dest_path =~ s!/[^/]+$!!;      #!vim-fix  
                         mkpath $dest_path unless -e $dest_path;  
                         file_copy( '<:gzip', $path, '>', $tmp );  
                         $path = $tmp;  
                 }  
                 return -$! unless sysopen($fh , $path, $mode);  
                 $pending->{$file}->{fh} = $fh;  
                 $pending->{$file}->{path} = $path;  
         } elsif ( ! defined( $pending->{$file}->{fh} ) ) {  
                 confess "can't find fh for $file ", dump($pending);  
150          };          };
151            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;
154    
155            my $tmp = $mount->{tmp} . '/' . $file;
156            if ( -e $tmp ) {
157                    $path = $tmp;
158            } elsif ( $path =~ m/\.gz$/ ) {
159                    my $dest_path = $tmp;
160                    $dest_path =~ s!/[^/]+$!!;      #!vim-fix
161                    mkpath $dest_path unless -e $dest_path;
162                    file_copy( '<:gzip', $path, '>', $tmp );
163                    $path = $tmp;
164            }
165            warn "<<< open abs path: $path [", -e $path ? -s $path : 'new' , "]\n";
166            return -$! unless sysopen($fh , $path, $mode);
167            close($fh);
168    
169            $pending->{$file}->{path} = $path;
170          return 0;          return 0;
171  }  }
172    
# Line 160  sub x_read { Line 174  sub x_read {
174          my ($file,$bufsize,$off) = @_;          my ($file,$bufsize,$off) = @_;
175          my ($rv) = -ENOSYS();          my ($rv) = -ENOSYS();
176          my $path = fixup( $file );          my $path = fixup( $file );
177    
178            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
179    
180          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
181          my ($fsize) = -s $path;  
182          my $fh = $pending->{$file}->{fh} || confess "no fh? ", dump( $pending );          my $fh = new IO::File;
183            return -ENOSYS() unless open($fh,$pending->{$file}->{path});
184    
185          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
186                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
187          }          }
188    
189          return $rv;          return $rv;
190  }  }
191    
192  sub x_write {  sub x_write {
193          my ($file,$buf,$off) = @_;          my ($file,$buf,$off) = @_;
194          $pending->{$file}->{write}++;          $pending->{$file}->{write}++;
195          my ($rv);          my $rv;
196          my $path = fixup($file);          my $path = fixup($file);
197    
198            confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
199    
200          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
201          my ($fsize) = -s $path;  
202          my $fh = $pending->{$file}->{fh};          my $fh = new IO::File;
203          return -ENOSYS() unless $fh;          return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});
204          if($rv = seek( $fh ,$off,SEEK_SET)) {          if($rv = seek( $fh ,$off,SEEK_SET)) {
205                  $rv = print( $fh $buf );                  $rv = print( $fh $buf );
206                    warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
207          }          }
208          $rv = -ENOSYS() unless $rv;          $rv = -ENOSYS() unless $rv;
209            close($fh);
210          return length($buf);          return length($buf);
211  }  }
212    
# Line 193  sub x_unlink   { return unlink_all( shif Line 218  sub x_unlink   { return unlink_all( shif
218  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
219    
220  sub x_rename {  sub x_rename {
221          my ($old) = fixup(shift);          my ($old,$new) = @_;
222          my ($new) = fixup(shift);          my $old_path = fixup($old);
223          my ($err) = rename($old,$new) ? 0 : -ENOENT();          my $new_path = fixup($new);
224            $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
225    
226            my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
227            warn "## rename( $old_path => $new_path ) = $err\n";
228    
229            my $tmp = $mount->{tmp} . '/' . $old;
230            if ( -e $tmp ) {
231                    my $new_tmp = $mount->{tmp} . '/' . $new;
232                    rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
233            }
234    
235          return $err;          return $err;
236  }  }
237  sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }  sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
# Line 219  sub x_chmod { Line 255  sub x_chmod {
255          return $err;          return $err;
256  }  }
257    
258  sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }  sub x_truncate {
259            my ( $file,$size ) = @_;
260            my $path = fixup($file);
261            my $rv = truncate( $path, $size ) ? 0 : -$! ;
262            if ( $path =~ m/\.gz$/ ) {
263                    my $no_gz = $path;
264                    $no_gz =~ s/\.gz$//;
265                    rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";
266            }
267            warn "## truncate( $file $size ) $path [", -s $path, "]\n";
268            $pending->{$file}->{write}++;
269            return $rv;
270    }
271  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }  sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
272    
273  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 243  sub x_release { Line 291  sub x_release {
291          } elsif ( ! defined( $pending->{$file}->{write} ) ) {          } elsif ( ! defined( $pending->{$file}->{write} ) ) {
292                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
293          } 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: $!";  
294                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );                  my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
295                  my $dest = fixup( $file );                  my $dest = fixup( $file );
296    
297                  # cleanup old compressed copy                  # cleanup old compressed copy
298                  if ( $dest =~ /\.gz$/ ) {                  if ( $dest =~ /\.gz$/ ) {
299                          warn "## remove old $dest\n";                          warn "## remove old $dest\n";
300                          unlink_all( $file ) || confess "can't remove $dest: $!";                          unlink $dest || confess "can't remove $dest: $!";
301                          $dest =~ s/\.gz$//;                          $dest =~ s/\.gz$//;
302                  }                  }
303    
# Line 258  sub x_release { Line 305  sub x_release {
305                          warn "release $file $mode -- uncompressed\n";                          warn "release $file $mode -- uncompressed\n";
306                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
307                  } elsif ( -s $path < $min_compress_size ) {                  } elsif ( -s $path < $min_compress_size ) {
308                          warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";                          warn "release $file -- uncompressed, too small [", -s $path, "]\n";
309                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );                          file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
310                  } else {                  } else {
311                          warn "release $file $mode -- compressing\n";                          warn "release $file $mode -- compressing\n";
# Line 267  sub x_release { Line 314  sub x_release {
314                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );
315    
316                          # FIXME add timeout to remove uncompressed version?                          # FIXME add timeout to remove uncompressed version?
317                          unlink_all( $file ) || warn "can't remove $path: $!";                          # FIXME leave uncompressed file if smaller than compressed
318                            unlink $path || warn "can't remove $path: $!";
319                  }                  }
320          } else {          } else {
321                  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;  
322          }          }
323          delete( $pending->{$file} );          $pending->{$file}->{open}--;
324            if ( $pending->{$file}->{open} == 0 ) {
325                    warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
326                    delete( $pending->{$file} );
327            }
328          return 0;          return 0;
329  }  }
330    

Legend:
Removed from v.5  
changed lines
  Added in v.19

  ViewVC Help
Powered by ViewVC 1.1.26