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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26