/[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

Annotation of /fuse-comp.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (hide annotations)
Sun Jul 8 21:03:26 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 8072 byte(s)
cleanups and more strict tests
1 dpavlin 1 #!/usr/bin/perl -w
2     use strict;
3     use threads;
4     use threads::shared;
5    
6     use Fuse;
7     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);
9     require 'syscall.ph'; # for SYS_mknod and SYS_lchown
10     use PerlIO::gzip;
11     use File::Path;
12     use Data::Dump qw/dump/;
13 dpavlin 5 use Carp qw/confess/;
14 dpavlin 10 use IO::File;
15 dpavlin 1
16     my $mount = {
17     from => '/tmp/comp',
18     to => '/tmp/no-comp',
19     tmp => '/dev/shm/comp',
20     };
21    
22     my $debug = 1;
23    
24 dpavlin 4 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 dpavlin 1
26 dpavlin 5 # don't compress files smaller than this
27 dpavlin 8 my $min_compress_size = 512;
28 dpavlin 5
29 dpavlin 1 foreach my $dir ( keys %$mount ) {
30     if ( ! -e $mount->{$dir} ) {
31     warn "created $mount->{$dir}\n";
32     mkdir $mount->{$dir} || die "can't create $mount->{$dir}: $!";
33     }
34     }
35    
36     my $pending;
37    
38     sub fixup {
39     my ( $path ) = @_;
40     my $full = $mount->{from} . '/' . $path;
41     if ( -e $full . '.gz' ) {
42     return $full . '.gz';
43     }
44     return $full;
45     }
46    
47     sub original_name {
48     my $p = shift;
49     $p =~ s/\.gz$//;
50     return $p;
51     };
52    
53     sub gzip_original_size {
54     my $file = shift;
55    
56     return unless -e $file;
57    
58     my $buff;
59    
60     open(my $fh, $file) || die "can't open $file: $!";
61    
62     # read($fh, $buff, 10 );
63     # print dump( unpack("nccVcc", $buff ) );
64    
65     seek($fh, -4, 2);
66     read($fh, $buff, 4 );
67     close($fh);
68    
69     return unpack("L", $buff);
70     }
71    
72 dpavlin 5 sub unlink_all {
73     my $file = shift;
74 dpavlin 8 warn "# unlink_all( $file )\n";
75    
76     my $path = fixup( $file );
77     unlink $path || return 0;
78    
79     my $tmp = $mount->{tmp} . '/' . $file;
80     unlink $tmp if ( -e $tmp );
81    
82 dpavlin 5 delete( $pending->{$file} );
83     return 1;
84     }
85    
86 dpavlin 1 sub x_getattr {
87     my ($file) = fixup(shift);
88     my (@list) = lstat($file);
89     $list[7] = gzip_original_size( $file ) if ( $list[7] && $file =~ m/\.gz$/ );
90     return -$! unless @list;
91     return @list;
92     }
93    
94     sub x_getdir {
95     my ($dirname) = fixup(shift);
96     unless(opendir(DIRHANDLE,$dirname)) {
97     return -ENOENT();
98     }
99     my @files = map { original_name( $_ ) } readdir(DIRHANDLE);
100     closedir(DIRHANDLE);
101     return (@files, 0);
102     }
103    
104     sub file_copy {
105     my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
106     warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n";
107 dpavlin 5 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: $!";
109 dpavlin 1 my $buff;
110     while( read( $s, $buff, 65535 ) ) {
111 dpavlin 5 print $d $buff || confess "can't write into $d_path: $!";
112 dpavlin 1 warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;
113     }
114     close($d) || warn "can't close $d_path: $!";
115     close($s) || warn "can't close $s_path: $!";
116 dpavlin 5 warn "-- $s_path [", -s $s_path, "] >>> $d_path [", -s $d_path, "]\n" if $debug;
117     my ($mode,$uid,$gid,$atime,$mtime) = (stat $s_path)[2,4,5,8,9];
118    
119     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";
121     utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
122 dpavlin 8
123     undef $d;
124     undef $s;
125 dpavlin 1 }
126    
127     sub x_open {
128     my ($file) = shift;
129     my ($mode) = shift;
130     $pending->{$file}->{open}++;
131 dpavlin 8
132     my $mode_desc = {
133     rdonly => $mode && O_RDONLY,
134     rdwr => $mode && O_RDWR,
135     append => $mode && O_APPEND,
136     create => $mode && O_CREAT,
137     };
138     warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n";
139 dpavlin 1 my $fh;
140 dpavlin 8
141     my $path = fixup($file);
142     my $tmp = $mount->{tmp} . '/' . $file;
143     if ( -e $tmp ) {
144     $path = $tmp;
145     } elsif ( $path =~ m/\.gz$/ ) {
146     my $dest_path = $tmp;
147     $dest_path =~ s!/[^/]+$!!; #!vim-fix
148     mkpath $dest_path unless -e $dest_path;
149     file_copy( '<:gzip', $path, '>', $tmp );
150     $path = $tmp;
151     }
152     warn ">>> open abs path: $path ", -s $path, " bytes\n";
153     return -$! unless sysopen($fh , $path, $mode);
154    
155     $pending->{$file}->{path} = $path;
156 dpavlin 1 return 0;
157     }
158    
159     sub x_read {
160     my ($file,$bufsize,$off) = @_;
161     my ($rv) = -ENOSYS();
162     my $path = fixup( $file );
163 dpavlin 10
164 dpavlin 1 return -ENOENT() unless -e $path;
165 dpavlin 10
166     my $fh = new IO::File;
167     return -ENOSYS() unless open($fh,$pending->{$file}->{path});
168    
169 dpavlin 1 if(seek($fh,$off,SEEK_SET)) {
170     read($fh,$rv,$bufsize);
171     }
172 dpavlin 10
173 dpavlin 1 return $rv;
174     }
175    
176     sub x_write {
177     my ($file,$buf,$off) = @_;
178     $pending->{$file}->{write}++;
179 dpavlin 10 my $rv;
180 dpavlin 1 my $path = fixup($file);
181 dpavlin 10
182 dpavlin 1 return -ENOENT() unless -e $path;
183 dpavlin 10
184     my $fh = new IO::File;
185     return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});
186 dpavlin 1 if($rv = seek( $fh ,$off,SEEK_SET)) {
187     $rv = print( $fh $buf );
188 dpavlin 10 warn "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
189 dpavlin 1 }
190     $rv = -ENOSYS() unless $rv;
191     return length($buf);
192     }
193    
194     sub err { return (-shift || -$!) }
195    
196     sub x_readlink { return readlink(fixup(shift)); }
197 dpavlin 5 sub x_unlink { return unlink_all( shift ) ? 0 : -$! }
198 dpavlin 1
199     sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
200    
201     sub x_rename {
202     my ($old) = fixup(shift);
203     my ($new) = fixup(shift);
204     my ($err) = rename($old,$new) ? 0 : -ENOENT();
205     return $err;
206     }
207     sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
208    
209     sub x_chown {
210     my ($path) = fixup(shift);
211     print "nonexistent $path\n" unless -e $path;
212     my ($uid,$gid) = @_;
213     # perl's chown() does not chown symlinks, it chowns the symlink's
214     # target. it fails when the link's target doesn't exist, because
215     # the stat64() syscall fails.
216     # this causes error messages when unpacking symlinks in tarballs.
217     my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
218     return $err;
219     }
220    
221     sub x_chmod {
222     my ($path) = fixup(shift);
223     my ($mode) = shift;
224     my ($err) = chmod($mode,$path) ? 0 : -$!;
225     return $err;
226     }
227    
228     sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }
229     sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
230    
231     sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
232     sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
233    
234     sub x_mknod {
235     # since this is called for ALL files, not just devices, I'll do some checks
236     # and possibly run the real mknod command.
237     my ($file, $modes, $dev) = @_;
238     $file = fixup($file);
239     $! = 0;
240     syscall(&SYS_mknod,$file,$modes,$dev);
241     return -$!;
242     }
243    
244     sub x_release {
245     my ( $file, $mode ) = @_;
246     if ( ! defined( $pending->{$file} ) ) {
247     warn "release $file, NO PENDING DATA\n";
248     return 0;
249     } elsif ( ! defined( $pending->{$file}->{write} ) ) {
250     warn "release $file, not written into\n";
251     } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
252 dpavlin 5 my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
253     my $dest = fixup( $file );
254    
255     # cleanup old compressed copy
256     if ( $dest =~ /\.gz$/ ) {
257     warn "## remove old $dest\n";
258 dpavlin 8 unlink $dest || confess "can't remove $dest: $!";
259 dpavlin 5 $dest =~ s/\.gz$//;
260     }
261    
262 dpavlin 1 if ( $file =~ $skip_extensions_regex ) {
263     warn "release $file $mode -- uncompressed\n";
264 dpavlin 5 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
265     } elsif ( -s $path < $min_compress_size ) {
266     warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";
267     file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
268 dpavlin 1 } else {
269     warn "release $file $mode -- compressing\n";
270    
271    
272     file_copy( '<', $path, '>:gzip', $dest . '.gz' );
273    
274     # FIXME add timeout to remove uncompressed version?
275 dpavlin 8 unlink $path || warn "can't remove $path: $!";
276 dpavlin 1 }
277     } else {
278     warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";
279     $pending->{$file}->{open}--;
280     return 0;
281     }
282     delete( $pending->{$file} );
283     return 0;
284     }
285    
286     # kludge
287     sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
288     Fuse::main(
289     mountpoint=>$mount->{to},
290     getattr =>"main::x_getattr",
291     readlink=>"main::x_readlink",
292     getdir =>"main::x_getdir",
293     mknod =>"main::x_mknod",
294     mkdir =>"main::x_mkdir",
295     unlink =>"main::x_unlink",
296     rmdir =>"main::x_rmdir",
297     symlink =>"main::x_symlink",
298     rename =>"main::x_rename",
299     link =>"main::x_link",
300     chmod =>"main::x_chmod",
301     chown =>"main::x_chown",
302     truncate=>"main::x_truncate",
303     utime =>"main::x_utime",
304     open =>"main::x_open",
305     read =>"main::x_read",
306     write =>"main::x_write",
307     statfs =>"main::x_statfs",
308     release =>"main::x_release",
309     # threaded=>1,
310     # debug => 1,
311     );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26