/[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 34 - (hide annotations)
Mon Jul 16 07:51:20 2007 UTC (16 years, 9 months ago) by dpavlin
File MIME type: text/plain
File size: 13128 byte(s)
mungle compressed files (ending in .gz) by adding % to original .gz extension.
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 23 use Getopt::Long;
16 dpavlin 1
17 dpavlin 23 my $debug = 0;
18     my $fuse_debug = 0;
19 dpavlin 29 my $stats = 1;
20 dpavlin 23
21     GetOptions(
22     'debug+' => \$debug,
23     'fuse-debug+' => \$fuse_debug,
24 dpavlin 29 'stats!' => \$stats,
25 dpavlin 23 );
26    
27 dpavlin 1 my $mount = {
28     from => '/tmp/comp',
29     to => '/tmp/no-comp',
30     tmp => '/dev/shm/comp',
31     };
32    
33 dpavlin 34 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 dpavlin 1
35 dpavlin 5 # don't compress files smaller than this
36 dpavlin 8 my $min_compress_size = 512;
37 dpavlin 5
38 dpavlin 1 foreach my $dir ( keys %$mount ) {
39     if ( ! -e $mount->{$dir} ) {
40     warn "created $mount->{$dir}\n";
41     mkdir $mount->{$dir} || die "can't create $mount->{$dir}: $!";
42     }
43     }
44    
45     my $pending;
46    
47 dpavlin 27 sub real_name {
48     my ( $dir, $name ) = @_;
49     if ( -e "$dir/${name}.gz" ) {
50 dpavlin 28 confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name";
51 dpavlin 27 return "${name}.gz";
52     }
53 dpavlin 34 if ( $name =~ m/\.gz$/ ) {
54     return $name . '%'; # protect (mingle) compressed files
55     } else {
56     return $name;
57     }
58 dpavlin 27 }
59    
60 dpavlin 1 sub fixup {
61     my ( $path ) = @_;
62 dpavlin 27 return $mount->{from} . '/' . real_name( $mount->{from}, $path );
63 dpavlin 1 }
64    
65     sub original_name {
66     my $p = shift;
67     $p =~ s/\.gz$//;
68 dpavlin 34 $p =~ s/\.gz%$/.gz/; # demungle compressed .gz files
69 dpavlin 1 return $p;
70     };
71    
72     sub gzip_original_size {
73     my $file = shift;
74    
75     return unless -e $file;
76    
77     my $buff;
78    
79     open(my $fh, $file) || die "can't open $file: $!";
80    
81     # read($fh, $buff, 10 );
82     # print dump( unpack("nccVcc", $buff ) );
83    
84     seek($fh, -4, 2);
85     read($fh, $buff, 4 );
86     close($fh);
87    
88     return unpack("L", $buff);
89     }
90    
91     sub x_getattr {
92     my ($file) = fixup(shift);
93     my (@list) = lstat($file);
94     $list[7] = gzip_original_size( $file ) if ( $list[7] && $file =~ m/\.gz$/ );
95     return -$! unless @list;
96     return @list;
97     }
98    
99     sub x_getdir {
100     my ($dirname) = fixup(shift);
101     unless(opendir(DIRHANDLE,$dirname)) {
102     return -ENOENT();
103     }
104     my @files = map { original_name( $_ ) } readdir(DIRHANDLE);
105     closedir(DIRHANDLE);
106     return (@files, 0);
107     }
108    
109     sub file_copy {
110     my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
111 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;
112 dpavlin 5 open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending );
113     open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!";
114 dpavlin 1 my $buff;
115     while( read( $s, $buff, 65535 ) ) {
116 dpavlin 5 print $d $buff || confess "can't write into $d_path: $!";
117 dpavlin 13 warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
118 dpavlin 1 }
119     close($d) || warn "can't close $d_path: $!";
120     close($s) || warn "can't close $s_path: $!";
121 dpavlin 5 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 dpavlin 8
128     undef $d;
129     undef $s;
130 dpavlin 1 }
131    
132 dpavlin 29 sub tmp_path {
133 dpavlin 28 my $file = shift;
134    
135     my $path = fixup( $file );
136 dpavlin 29
137     my $op = 'UNKNOWN';
138    
139     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;
144     if ( -e $tmp ) {
145     $path = $tmp;
146     $op = 'existing';
147     } elsif ( $path =~ m/\.gz$/ ) {
148     my $dest_path = $tmp;
149     $dest_path =~ s!/[^/]+$!!; #!vim-fix
150     mkpath $dest_path unless -e $dest_path;
151     if ( -s $path ) {
152     file_copy( '<:gzip', $path, '>', $tmp )
153     } else {
154 dpavlin 33 confess "ASSERT: filesystem corruption, $path is zero size in ",dump( $pending );
155 dpavlin 29 }
156     $path = $tmp;
157     $op = 'created';
158 dpavlin 28 }
159 dpavlin 29 confess "ASSERT: path shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{path} );
160     confess "ASSERT: open shouldn't exist for $file in ", dump( $pending ) if defined( $pending->{$file}->{open} );
161     $pending->{$file}->{path} = $path;
162     $pending->{$file}->{open} = 0; # not really opened, just uncompressed
163 dpavlin 33 warn "## tmp_file( $file ) $op $path [", -s $path, "]\n" if $debug;
164 dpavlin 28 }
165     return $path;
166     }
167    
168 dpavlin 29 sub compress_file2path {
169     my ( $file, $path ) = @_;
170 dpavlin 28
171 dpavlin 29 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 dpavlin 33 confess "ASSERT: compressing into $path instead of $pending_path";
183 dpavlin 29 }
184     }
185    
186     confess "need path" unless $path;
187    
188 dpavlin 28 # 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 dpavlin 29 confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest;
194 dpavlin 28 }
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 dpavlin 33 warn ">>> $size_path <= $size_comp leaving uncompressed $dest\n";
212 dpavlin 28 unlink $comp || confess "can't remove: $comp: $!";
213 dpavlin 33 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
214 dpavlin 28 } else {
215 dpavlin 29 warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n";
216 dpavlin 30
217 dpavlin 28 # FIXME add timeout to remove uncompressed version?
218     unlink $path || confess "can't remove $path: $!";
219 dpavlin 30
220     if ( -e $dest ) {
221     warn "## cleanup uncompressed $dest\n" if $debug;
222     unlink $dest || confess "can't remove $dest: $!";
223     }
224 dpavlin 28 }
225 dpavlin 30
226 dpavlin 28 }
227     }
228    
229 dpavlin 1 sub x_open {
230     my ($file) = shift;
231     my ($mode) = shift;
232 dpavlin 12
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 dpavlin 8 my $mode_desc = {
245     rdonly => $mode && O_RDONLY,
246     rdwr => $mode && O_RDWR,
247     append => $mode && O_APPEND,
248     create => $mode && O_CREAT,
249 dpavlin 14 trunc => $mode && O_TRUNC,
250 dpavlin 8 };
251 dpavlin 28
252 dpavlin 29 my $path = tmp_path( $file );
253 dpavlin 28
254 dpavlin 19 warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
255 dpavlin 28
256 dpavlin 1 my $fh;
257 dpavlin 8
258 dpavlin 21 if ( sysopen($fh , $path, $mode) ) {
259     close($fh) || confess "can't close $path: $!";
260 dpavlin 29 warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n";
261     $pending->{$file}->{open}++;
262 dpavlin 21 return 0;
263     } else {
264 dpavlin 26 warn "ERROR: can't open $path -- $!";
265 dpavlin 21 return -$!;
266     }
267    
268 dpavlin 1 }
269    
270     sub x_read {
271     my ($file,$bufsize,$off) = @_;
272     my ($rv) = -ENOSYS();
273     my $path = fixup( $file );
274 dpavlin 10
275 dpavlin 12 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
276    
277 dpavlin 1 return -ENOENT() unless -e $path;
278 dpavlin 10
279     my $fh = new IO::File;
280     return -ENOSYS() unless open($fh,$pending->{$file}->{path});
281    
282 dpavlin 1 if(seek($fh,$off,SEEK_SET)) {
283     read($fh,$rv,$bufsize);
284 dpavlin 29 $pending->{$file}->{read} += length($rv) if $stats;
285 dpavlin 1 }
286 dpavlin 10
287 dpavlin 1 return $rv;
288     }
289    
290     sub x_write {
291     my ($file,$buf,$off) = @_;
292 dpavlin 25
293 dpavlin 10 my $rv;
294 dpavlin 1 my $path = fixup($file);
295 dpavlin 10
296 dpavlin 12 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
297    
298 dpavlin 1 return -ENOENT() unless -e $path;
299 dpavlin 10
300 dpavlin 26 $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
301 dpavlin 29 confess "write into non-existant $path for $file" unless -e $path;
302 dpavlin 26
303 dpavlin 10 my $fh = new IO::File;
304 dpavlin 26 return -ENOSYS() unless open($fh,'+<',$path);
305 dpavlin 1 if($rv = seek( $fh ,$off,SEEK_SET)) {
306     $rv = print( $fh $buf );
307 dpavlin 29 my $size = length($buf);
308     warn "## write $path offset $off [$size]\n" if $debug;
309     $pending->{$file}->{write} += $size;
310 dpavlin 1 }
311     $rv = -ENOSYS() unless $rv;
312 dpavlin 26 close($fh) || warn "can't close $path: $!";
313 dpavlin 1 return length($buf);
314     }
315    
316     sub err { return (-shift || -$!) }
317    
318 dpavlin 27 sub x_readlink { return readlink(fixup(shift)); }
319    
320 dpavlin 25 sub x_unlink {
321     my $file = shift;
322     my $path = fixup( $file );
323 dpavlin 1
324 dpavlin 25 if ( $file =~ m#\Q/.fuse_hidden\E# ) {
325     return unlink $path ? 0 : -$1;
326     }
327    
328     warn "# unlink( $file )\n";
329    
330     unlink $path || return 0;
331    
332     my $tmp = $mount->{tmp} . '/' . $file;
333     unlink $tmp if ( -e $tmp );
334    
335     delete( $pending->{$file} );
336     return 0;
337     }
338    
339 dpavlin 27 sub x_symlink {
340     my ($from,$to) = @_;
341 dpavlin 1
342 dpavlin 27 my $from_path = $from; #fixup( $from );
343     my $to_path = fixup( $to );
344    
345     my $rv = symlink( $from_path, $to_path ) ? 0 : -$!;
346     warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug;
347    
348     my $tmp = $mount->{tmp} . '/' . $from;
349     if ( -e $tmp ) {
350     my $tmp_to = $mount->{$tmp} . '/' . $to;
351     symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!";
352     }
353     return $rv;
354     }
355    
356     sub x_link {
357     my ($from,$to) = @_;
358    
359     my $from_path = fixup($from);
360     my $to_path = fixup($to);
361     $to_path .= '.gz' if ( $from_path =~ m/\.gz$/ && $to_path !~ m/\.gz$/ );
362    
363     my $rv = link( $from_path, $to_path ) ? 0 : -$!;
364    
365     warn "# link( $from_path -> $to_path ) = $rv\n" if $debug;
366    
367     return $rv;
368     }
369    
370 dpavlin 1 sub x_rename {
371 dpavlin 18 my ($old,$new) = @_;
372     my $old_path = fixup($old);
373     my $new_path = fixup($new);
374     $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
375    
376     my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
377     warn "## rename( $old_path => $new_path ) = $err\n";
378    
379     my $tmp = $mount->{tmp} . '/' . $old;
380     if ( -e $tmp ) {
381 dpavlin 25 if ( $new =~ m#\Q/.fuse_hidden\E# ) {
382 dpavlin 33 unlink $tmp || confess "can't unlink $tmp for $new";
383 dpavlin 25 } else {
384     my $new_tmp = $mount->{tmp} . '/' . $new;
385     rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
386     }
387 dpavlin 18 }
388    
389 dpavlin 20 if (defined( $pending->{$old} )) {
390     $pending->{$new} = $pending->{$old};
391    
392     my $path = $pending->{$old}->{path};
393     $path =~ s/\Q$old\E/$new/;
394     $pending->{$new}->{path} = $path;
395    
396 dpavlin 25 delete( $pending->{$old} );
397     warn "## tweaking pending to ", dump( $pending ) if $debug;
398 dpavlin 20 }
399    
400 dpavlin 1 return $err;
401     }
402    
403     sub x_chown {
404 dpavlin 26 my ($file,$uid,$gid) = @_;
405     my $path = fixup($file);
406 dpavlin 1 print "nonexistent $path\n" unless -e $path;
407     # perl's chown() does not chown symlinks, it chowns the symlink's
408     # target. it fails when the link's target doesn't exist, because
409     # the stat64() syscall fails.
410     # this causes error messages when unpacking symlinks in tarballs.
411     my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
412 dpavlin 26
413     my $tmp = $mount->{tmp} . '/' . $file;
414     syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp;
415    
416 dpavlin 1 return $err;
417     }
418    
419     sub x_chmod {
420     my ($path) = fixup(shift);
421     my ($mode) = shift;
422     my ($err) = chmod($mode,$path) ? 0 : -$!;
423     return $err;
424     }
425    
426 dpavlin 14 sub x_truncate {
427     my ( $file,$size ) = @_;
428 dpavlin 28
429     #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} );
430    
431 dpavlin 29 my $path = tmp_path( $file );
432 dpavlin 14 my $rv = truncate( $path, $size ) ? 0 : -$! ;
433 dpavlin 21 warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
434 dpavlin 29 compress_file2path( $file, $path );
435 dpavlin 27
436 dpavlin 14 return $rv;
437     }
438 dpavlin 28
439 dpavlin 1 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
440    
441     sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
442     sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
443    
444     sub x_mknod {
445     # since this is called for ALL files, not just devices, I'll do some checks
446     # and possibly run the real mknod command.
447     my ($file, $modes, $dev) = @_;
448     $file = fixup($file);
449     $! = 0;
450     syscall(&SYS_mknod,$file,$modes,$dev);
451     return -$!;
452     }
453    
454     sub x_release {
455     my ( $file, $mode ) = @_;
456 dpavlin 25
457 dpavlin 1 if ( ! defined( $pending->{$file} ) ) {
458     warn "release $file, NO PENDING DATA\n";
459     return 0;
460     } elsif ( ! defined( $pending->{$file}->{write} ) ) {
461     warn "release $file, not written into\n";
462 dpavlin 29 } elsif ( $file =~ m#\Q/.fuse_hidden\E# ) {
463     warn "release internal $file\n" if $debug;
464 dpavlin 1 } else {
465 dpavlin 29 compress_file2path( $file );
466 dpavlin 1 }
467 dpavlin 29
468 dpavlin 13 $pending->{$file}->{open}--;
469     if ( $pending->{$file}->{open} == 0 ) {
470     warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
471     delete( $pending->{$file} );
472     }
473 dpavlin 29
474 dpavlin 1 return 0;
475     }
476    
477     # kludge
478     sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
479     Fuse::main(
480     mountpoint=>$mount->{to},
481     getattr =>"main::x_getattr",
482     readlink=>"main::x_readlink",
483     getdir =>"main::x_getdir",
484     mknod =>"main::x_mknod",
485     mkdir =>"main::x_mkdir",
486     unlink =>"main::x_unlink",
487     rmdir =>"main::x_rmdir",
488     symlink =>"main::x_symlink",
489     rename =>"main::x_rename",
490     link =>"main::x_link",
491     chmod =>"main::x_chmod",
492     chown =>"main::x_chown",
493     truncate=>"main::x_truncate",
494     utime =>"main::x_utime",
495     open =>"main::x_open",
496     read =>"main::x_read",
497     write =>"main::x_write",
498     statfs =>"main::x_statfs",
499     release =>"main::x_release",
500     # threaded=>1,
501 dpavlin 23 debug => $fuse_debug,
502 dpavlin 1 );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26