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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26