/[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 20 - (hide annotations)
Mon Jul 9 16:20:07 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 10074 byte(s)
add any flag to fuse-comp.pl or comp.sh to turn debug on,
repair pending data in rename, better output,
implemented check if compressed file is smaller than original
(no blacklisting for this yet)
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 dpavlin 20 my $debug = shift @ARGV;
23 dpavlin 1
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 dpavlin 19 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 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 13 warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
113 dpavlin 1 }
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 dpavlin 12
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 dpavlin 1 $pending->{$file}->{open}++;
143 dpavlin 8
144     my $mode_desc = {
145     rdonly => $mode && O_RDONLY,
146     rdwr => $mode && O_RDWR,
147     append => $mode && O_APPEND,
148     create => $mode && O_CREAT,
149 dpavlin 14 trunc => $mode && O_TRUNC,
150 dpavlin 8 };
151 dpavlin 14 my $path = fixup($file);
152 dpavlin 19 warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
153 dpavlin 1 my $fh;
154 dpavlin 8
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 dpavlin 19 warn "<<< open abs path: $path [", -e $path ? -s $path : 'new' , "]\n";
166 dpavlin 8 return -$! unless sysopen($fh , $path, $mode);
167 dpavlin 12 close($fh);
168 dpavlin 8
169     $pending->{$file}->{path} = $path;
170 dpavlin 1 return 0;
171     }
172    
173     sub x_read {
174     my ($file,$bufsize,$off) = @_;
175     my ($rv) = -ENOSYS();
176     my $path = fixup( $file );
177 dpavlin 10
178 dpavlin 12 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
179    
180 dpavlin 1 return -ENOENT() unless -e $path;
181 dpavlin 10
182     my $fh = new IO::File;
183     return -ENOSYS() unless open($fh,$pending->{$file}->{path});
184    
185 dpavlin 1 if(seek($fh,$off,SEEK_SET)) {
186     read($fh,$rv,$bufsize);
187     }
188 dpavlin 10
189 dpavlin 1 return $rv;
190     }
191    
192     sub x_write {
193     my ($file,$buf,$off) = @_;
194     $pending->{$file}->{write}++;
195 dpavlin 10 my $rv;
196 dpavlin 1 my $path = fixup($file);
197 dpavlin 10
198 dpavlin 12 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
199    
200 dpavlin 1 return -ENOENT() unless -e $path;
201 dpavlin 10
202     my $fh = new IO::File;
203     return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});
204 dpavlin 1 if($rv = seek( $fh ,$off,SEEK_SET)) {
205     $rv = print( $fh $buf );
206 dpavlin 17 warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
207 dpavlin 1 }
208     $rv = -ENOSYS() unless $rv;
209 dpavlin 12 close($fh);
210 dpavlin 1 return length($buf);
211     }
212    
213     sub err { return (-shift || -$!) }
214    
215     sub x_readlink { return readlink(fixup(shift)); }
216 dpavlin 5 sub x_unlink { return unlink_all( shift ) ? 0 : -$! }
217 dpavlin 1
218     sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
219    
220     sub x_rename {
221 dpavlin 18 my ($old,$new) = @_;
222     my $old_path = fixup($old);
223     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 dpavlin 20 if (defined( $pending->{$old} )) {
236     $pending->{$new} = $pending->{$old};
237    
238     my $path = $pending->{$old}->{path};
239     $path =~ s/\Q$old\E/$new/;
240     $pending->{$new}->{path} = $path;
241    
242     delete( $pending->{$old} );
243     }
244    
245 dpavlin 1 return $err;
246     }
247     sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
248    
249     sub x_chown {
250     my ($path) = fixup(shift);
251     print "nonexistent $path\n" unless -e $path;
252     my ($uid,$gid) = @_;
253     # perl's chown() does not chown symlinks, it chowns the symlink's
254     # target. it fails when the link's target doesn't exist, because
255     # the stat64() syscall fails.
256     # this causes error messages when unpacking symlinks in tarballs.
257     my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
258     return $err;
259     }
260    
261     sub x_chmod {
262     my ($path) = fixup(shift);
263     my ($mode) = shift;
264     my ($err) = chmod($mode,$path) ? 0 : -$!;
265     return $err;
266     }
267    
268 dpavlin 14 sub x_truncate {
269     my ( $file,$size ) = @_;
270     my $path = fixup($file);
271     my $rv = truncate( $path, $size ) ? 0 : -$! ;
272     if ( $path =~ m/\.gz$/ ) {
273     my $no_gz = $path;
274     $no_gz =~ s/\.gz$//;
275     rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";
276     }
277     warn "## truncate( $file $size ) $path [", -s $path, "]\n";
278 dpavlin 15 $pending->{$file}->{write}++;
279 dpavlin 14 return $rv;
280     }
281 dpavlin 1 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
282    
283     sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
284     sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
285    
286     sub x_mknod {
287     # since this is called for ALL files, not just devices, I'll do some checks
288     # and possibly run the real mknod command.
289     my ($file, $modes, $dev) = @_;
290     $file = fixup($file);
291     $! = 0;
292     syscall(&SYS_mknod,$file,$modes,$dev);
293     return -$!;
294     }
295    
296     sub x_release {
297     my ( $file, $mode ) = @_;
298     if ( ! defined( $pending->{$file} ) ) {
299     warn "release $file, NO PENDING DATA\n";
300     return 0;
301     } elsif ( ! defined( $pending->{$file}->{write} ) ) {
302     warn "release $file, not written into\n";
303     } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
304 dpavlin 5 my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
305     my $dest = fixup( $file );
306    
307     # cleanup old compressed copy
308     if ( $dest =~ /\.gz$/ ) {
309     warn "## remove old $dest\n";
310 dpavlin 8 unlink $dest || confess "can't remove $dest: $!";
311 dpavlin 5 $dest =~ s/\.gz$//;
312     }
313    
314 dpavlin 1 if ( $file =~ $skip_extensions_regex ) {
315 dpavlin 20 warn "release $file [",-s $path,"] skipped compression\n";
316 dpavlin 5 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
317     } elsif ( -s $path < $min_compress_size ) {
318 dpavlin 20 warn "release $file [",-s $path,"] uncompressed, too small\n";
319 dpavlin 5 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
320 dpavlin 1 } else {
321 dpavlin 20 warn "release $file [",-s $path,"] compressing\n";
322 dpavlin 1
323 dpavlin 20 my $comp = $dest . '.gz';
324     file_copy( '<', $path, '>:gzip', $comp );
325 dpavlin 1
326 dpavlin 20 my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
327 dpavlin 1
328 dpavlin 20 if ( $size_path <= $size_comp ) {
329     warn ">>> $size_path <= $size_comp leaving uncompressed\n";
330     unlink $comp || warn "can't reamove: $comp: $!";
331     } else {
332     warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";
333     # FIXME add timeout to remove uncompressed version?
334     unlink $path || warn "can't remove $path: $!";
335     }
336 dpavlin 1 }
337     } else {
338     warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";
339     }
340 dpavlin 13 $pending->{$file}->{open}--;
341     if ( $pending->{$file}->{open} == 0 ) {
342     warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
343     delete( $pending->{$file} );
344     }
345 dpavlin 1 return 0;
346     }
347    
348     # kludge
349     sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
350     Fuse::main(
351     mountpoint=>$mount->{to},
352     getattr =>"main::x_getattr",
353     readlink=>"main::x_readlink",
354     getdir =>"main::x_getdir",
355     mknod =>"main::x_mknod",
356     mkdir =>"main::x_mkdir",
357     unlink =>"main::x_unlink",
358     rmdir =>"main::x_rmdir",
359     symlink =>"main::x_symlink",
360     rename =>"main::x_rename",
361     link =>"main::x_link",
362     chmod =>"main::x_chmod",
363     chown =>"main::x_chown",
364     truncate=>"main::x_truncate",
365     utime =>"main::x_utime",
366     open =>"main::x_open",
367     read =>"main::x_read",
368     write =>"main::x_write",
369     statfs =>"main::x_statfs",
370     release =>"main::x_release",
371     # threaded=>1,
372     # debug => 1,
373     );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26