/[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 2 by dpavlin, Sun Jul 8 12:55:52 2007 UTC revision 8 by dpavlin, Sun Jul 8 17:04:18 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    
15  my $mount = {  my $mount = {
16          from    => '/tmp/comp',          from    => '/tmp/comp',
# Line 20  my $mount = { Line 20  my $mount = {
20    
21  my $debug = 1;  my $debug = 1;
22    
23  my $skip_extensions_regex = qr/\.(sw[px]|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;
24    
25    # don't compress files smaller than this
26    my $min_compress_size = 512;
27    
28  foreach my $dir ( keys %$mount ) {  foreach my $dir ( keys %$mount ) {
29          if ( ! -e $mount->{$dir} ) {          if ( ! -e $mount->{$dir} ) {
# Line 65  sub gzip_original_size { Line 68  sub gzip_original_size {
68          return unpack("L", $buff);          return unpack("L", $buff);
69  }  }
70    
71    sub unlink_all {
72            my $file = shift;
73            warn "# unlink_all( $file )\n";
74    
75            my $path = fixup( $file );
76            unlink $path || return 0;
77    
78            my $tmp = $mount->{tmp} . '/' . $file;
79            unlink $tmp if ( -e $tmp );
80    
81            delete( $pending->{$file} );
82            return 1;
83    }
84    
85  sub x_getattr {  sub x_getattr {
86          my ($file) = fixup(shift);          my ($file) = fixup(shift);
87          my (@list) = lstat($file);          my (@list) = lstat($file);
# Line 86  sub x_getdir { Line 103  sub x_getdir {
103  sub file_copy {  sub file_copy {
104          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;          my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
105          warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n";          warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n";
106          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 );
107          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: $!";
108          my $buff;          my $buff;
109          while( read( $s, $buff, 65535 ) ) {          while( read( $s, $buff, 65535 ) ) {
110                  print $d $buff || die "can't write into $d_path: $!";                  print $d $buff || confess "can't write into $d_path: $!";
111                  warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;                  warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;
112          }          }
113          close($d) || warn "can't close $d_path: $!";          close($d) || warn "can't close $d_path: $!";
114          close($s) || warn "can't close $s_path: $!";          close($s) || warn "can't close $s_path: $!";
115            warn "-- $s_path [", -s $s_path, "] >>> $d_path [", -s $d_path, "]\n" if $debug;
116            my ($mode,$uid,$gid,$atime,$mtime) = (stat $s_path)[2,4,5,8,9];
117    
118            chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";
119            chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";
120            utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
121    
122            undef $d;
123            undef $s;
124  }  }
125    
126  sub x_open {  sub x_open {
127          my ($file) = shift;          my ($file) = shift;
128          my ($mode) = shift;          my ($mode) = shift;
129          $pending->{$file}->{open}++;          $pending->{$file}->{open}++;
130    
131            my $mode_desc = {
132                    rdonly => $mode && O_RDONLY,
133                    rdwr => $mode && O_RDWR,
134                    append => $mode && O_APPEND,
135                    create => $mode && O_CREAT,
136            };
137            warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n";
138          my $fh;          my $fh;
139          if ( $pending->{$file}->{open} == 1 ) {  
140                  warn "# open( $file, $mode )\n";          my $path = fixup($file);
141                  my $path = fixup($file);          my $tmp = $mount->{tmp} . '/' . $file;
142                  my $tmp = $mount->{tmp} . '/' . $file;          if ( -e $tmp ) {
143                  if ( -e $tmp ) {                  $path = $tmp;
144                          $path = $tmp;          } elsif ( $path =~ m/\.gz$/ ) {
145                  } elsif ( $path =~ m/\.gz$/ ) {                  my $dest_path = $tmp;
146                          my $dest_path = $tmp;                  $dest_path =~ s!/[^/]+$!!;      #!vim-fix
147                          $dest_path =~ s!/[^/]+$!!;      #!vim-fix                  mkpath $dest_path unless -e $dest_path;
148                          mkpath $dest_path unless -e $dest_path;                  file_copy( '<:gzip', $path, '>', $tmp );
149                          file_copy( '<:gzip', $path, '>', $tmp );                  $path = $tmp;
                         $path = $tmp;  
                 }  
                 return -$! unless sysopen($fh , $path, $mode);  
                 $pending->{$file}->{fh} = $fh;  
                 $pending->{$file}->{path} = $path;  
         } elsif ( ! defined( $pending->{$file}->{fh} ) ) {  
                 die "can't find fh for $file ", dump($pending);  
150          }          }
151            warn ">>> open abs path: $path ", -s $path, " bytes\n";
152            return -$! unless sysopen($fh , $path, $mode);
153    
154            $pending->{$file}->{fh} = $fh;
155            $pending->{$file}->{path} = $path;
156            $pending->{$file}->{mode} = {
157                    rdonly => $mode && O_RDONLY,
158                    rdwr => $mode && O_RDWR,
159                    append => $mode && O_APPEND,
160                    create => $mode && O_CREAT,
161            };
162          return 0;          return 0;
163  }  }
164    
# Line 130  sub x_read { Line 168  sub x_read {
168          my $path = fixup( $file );          my $path = fixup( $file );
169          return -ENOENT() unless -e $path;          return -ENOENT() unless -e $path;
170          my ($fsize) = -s $path;          my ($fsize) = -s $path;
171          my $fh = $pending->{$file}->{fh} || die "no fh? ", dump( $pending );          my $fh = $pending->{$file}->{fh} || confess "no fh? ", dump( $pending );
172          if(seek($fh,$off,SEEK_SET)) {          if(seek($fh,$off,SEEK_SET)) {
173                  read($fh,$rv,$bufsize);                  read($fh,$rv,$bufsize);
174          }          }
# Line 156  sub x_write { Line 194  sub x_write {
194  sub err { return (-shift || -$!) }  sub err { return (-shift || -$!) }
195    
196  sub x_readlink { return readlink(fixup(shift));         }  sub x_readlink { return readlink(fixup(shift));         }
197  sub x_unlink   { return unlink(fixup(shift)) ? 0 : -$!; }  sub x_unlink   { return unlink_all( shift ) ? 0 : -$! }
198    
199  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }  sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
200    
# Line 212  sub x_release { Line 250  sub x_release {
250                  warn "release $file, not written into\n";                  warn "release $file, not written into\n";
251          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {          } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
252                  close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";                  close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";
253                    my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
254                    my $dest = fixup( $file );
255    
256                    # cleanup old compressed copy
257                    if ( $dest =~ /\.gz$/ ) {
258                            warn "## remove old $dest\n";
259                            unlink $dest || confess "can't remove $dest: $!";
260                            $dest =~ s/\.gz$//;
261                    }
262    
263                  if ( $file =~ $skip_extensions_regex ) {                  if ( $file =~ $skip_extensions_regex ) {
264                          warn "release $file $mode -- uncompressed\n";                          warn "release $file $mode -- uncompressed\n";
265                            file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
266                    } elsif ( -s $path < $min_compress_size ) {
267                            warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";
268                            file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
269                  } else {                  } else {
270                          warn "release $file $mode -- compressing\n";                          warn "release $file $mode -- compressing\n";
271    
                         my $path = $pending->{$file}->{path} || die "no path for $file ? ", dump( $pending );  
                         my $dest = fixup( $file );  
   
                         if ( $dest =~ /\.gz$/ ) {  
                                 warn "## remove old $dest\n";  
                                 unlink $dest || die "can't remove $dest: $!";  
                                 $dest =~ s/\.gz$//;  
                         }  
272    
273                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );                          file_copy( '<', $path, '>:gzip', $dest . '.gz' );
274    

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

  ViewVC Help
Powered by ViewVC 1.1.26