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

Contents of /fuse-comp.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations)
Mon Jul 16 07:41:55 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 12970 byte(s)
logging tweaks
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 use Carp qw/confess/;
14 use IO::File;
15 use Getopt::Long;
16
17 my $debug = 0;
18 my $fuse_debug = 0;
19 my $stats = 1;
20
21 GetOptions(
22 'debug+' => \$debug,
23 'fuse-debug+' => \$fuse_debug,
24 'stats!' => \$stats,
25 );
26
27 my $mount = {
28 from => '/tmp/comp',
29 to => '/tmp/no-comp',
30 tmp => '/dev/shm/comp',
31 };
32
33 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
35 # don't compress files smaller than this
36 my $min_compress_size = 512;
37
38 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 sub real_name {
48 my ( $dir, $name ) = @_;
49 if ( -e "$dir/${name}.gz" ) {
50 confess "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name";
51 return "${name}.gz";
52 }
53 return $name;
54 }
55
56 sub fixup {
57 my ( $path ) = @_;
58 return $mount->{from} . '/' . real_name( $mount->{from}, $path );
59 }
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 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 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 my $buff;
110 while( read( $s, $buff, 65535 ) ) {
111 print $d $buff || confess "can't write into $d_path: $!";
112 warn ">> [", length($buff), "] 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 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
123 undef $d;
124 undef $s;
125 }
126
127 sub tmp_path {
128 my $file = shift;
129
130 my $path = fixup( $file );
131
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 in ",dump( $pending );
150 }
151 $path = $tmp;
152 $op = 'created';
153 }
154 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" if $debug;
159 }
160 return $path;
161 }
162
163 sub compress_file2path {
164 my ( $file, $path ) = @_;
165
166 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";
178 }
179 }
180
181 confess "need path" unless $path;
182
183 # 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 confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest;
189 }
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 $dest\n";
207 unlink $comp || confess "can't remove: $comp: $!";
208 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
209 } else {
210 warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n";
211
212 # FIXME add timeout to remove uncompressed version?
213 unlink $path || confess "can't remove $path: $!";
214
215 if ( -e $dest ) {
216 warn "## cleanup uncompressed $dest\n" if $debug;
217 unlink $dest || confess "can't remove $dest: $!";
218 }
219 }
220
221 }
222 }
223
224 sub x_open {
225 my ($file) = shift;
226 my ($mode) = shift;
227
228 if ( $file eq '/.debug' ) {
229 my $path = $mount->{from} . '/.debug';
230 open( my $debug, '>', $path ) || die "can't open $path: $!";
231 my $dump = dump( $pending );
232 print $debug "pending = $dump\n";
233 close($debug);
234 $pending->{'/.debug'}->{path} = $path;
235 warn "## created dump $path $dump\n";
236 return 0;
237 }
238
239 my $mode_desc = {
240 rdonly => $mode && O_RDONLY,
241 rdwr => $mode && O_RDWR,
242 append => $mode && O_APPEND,
243 create => $mode && O_CREAT,
244 trunc => $mode && O_TRUNC,
245 };
246
247 my $path = tmp_path( $file );
248
249 warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
250
251 my $fh;
252
253 if ( sysopen($fh , $path, $mode) ) {
254 close($fh) || confess "can't close $path: $!";
255 warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n";
256 $pending->{$file}->{open}++;
257 return 0;
258 } else {
259 warn "ERROR: can't open $path -- $!";
260 return -$!;
261 }
262
263 }
264
265 sub x_read {
266 my ($file,$bufsize,$off) = @_;
267 my ($rv) = -ENOSYS();
268 my $path = fixup( $file );
269
270 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
271
272 return -ENOENT() unless -e $path;
273
274 my $fh = new IO::File;
275 return -ENOSYS() unless open($fh,$pending->{$file}->{path});
276
277 if(seek($fh,$off,SEEK_SET)) {
278 read($fh,$rv,$bufsize);
279 $pending->{$file}->{read} += length($rv) if $stats;
280 }
281
282 return $rv;
283 }
284
285 sub x_write {
286 my ($file,$buf,$off) = @_;
287
288 my $rv;
289 my $path = fixup($file);
290
291 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
292
293 return -ENOENT() unless -e $path;
294
295 $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
296 confess "write into non-existant $path for $file" unless -e $path;
297
298 my $fh = new IO::File;
299 return -ENOSYS() unless open($fh,'+<',$path);
300 if($rv = seek( $fh ,$off,SEEK_SET)) {
301 $rv = print( $fh $buf );
302 my $size = length($buf);
303 warn "## write $path offset $off [$size]\n" if $debug;
304 $pending->{$file}->{write} += $size;
305 }
306 $rv = -ENOSYS() unless $rv;
307 close($fh) || warn "can't close $path: $!";
308 return length($buf);
309 }
310
311 sub err { return (-shift || -$!) }
312
313 sub x_readlink { return readlink(fixup(shift)); }
314
315 sub x_unlink {
316 my $file = shift;
317 my $path = fixup( $file );
318
319 if ( $file =~ m#\Q/.fuse_hidden\E# ) {
320 return unlink $path ? 0 : -$1;
321 }
322
323 warn "# unlink( $file )\n";
324
325 unlink $path || return 0;
326
327 my $tmp = $mount->{tmp} . '/' . $file;
328 unlink $tmp if ( -e $tmp );
329
330 delete( $pending->{$file} );
331 return 0;
332 }
333
334 sub x_symlink {
335 my ($from,$to) = @_;
336
337 my $from_path = $from; #fixup( $from );
338 my $to_path = fixup( $to );
339
340 my $rv = symlink( $from_path, $to_path ) ? 0 : -$!;
341 warn "# symlink( $from_path -> $to_path ) = $rv\n" if $debug;
342
343 my $tmp = $mount->{tmp} . '/' . $from;
344 if ( -e $tmp ) {
345 my $tmp_to = $mount->{$tmp} . '/' . $to;
346 symlink( $tmp, $tmp_to ) || confess "can't symlink $tmp -> $tmp_to: $!";
347 }
348 return $rv;
349 }
350
351 sub x_link {
352 my ($from,$to) = @_;
353
354 my $from_path = fixup($from);
355 my $to_path = fixup($to);
356 $to_path .= '.gz' if ( $from_path =~ m/\.gz$/ && $to_path !~ m/\.gz$/ );
357
358 my $rv = link( $from_path, $to_path ) ? 0 : -$!;
359
360 warn "# link( $from_path -> $to_path ) = $rv\n" if $debug;
361
362 return $rv;
363 }
364
365 sub x_rename {
366 my ($old,$new) = @_;
367 my $old_path = fixup($old);
368 my $new_path = fixup($new);
369 $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
370
371 my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
372 warn "## rename( $old_path => $new_path ) = $err\n";
373
374 my $tmp = $mount->{tmp} . '/' . $old;
375 if ( -e $tmp ) {
376 if ( $new =~ m#\Q/.fuse_hidden\E# ) {
377 unlink $tmp || confess "can't unlink $tmp for $new";
378 } else {
379 my $new_tmp = $mount->{tmp} . '/' . $new;
380 rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
381 }
382 }
383
384 if (defined( $pending->{$old} )) {
385 $pending->{$new} = $pending->{$old};
386
387 my $path = $pending->{$old}->{path};
388 $path =~ s/\Q$old\E/$new/;
389 $pending->{$new}->{path} = $path;
390
391 delete( $pending->{$old} );
392 warn "## tweaking pending to ", dump( $pending ) if $debug;
393 }
394
395 return $err;
396 }
397
398 sub x_chown {
399 my ($file,$uid,$gid) = @_;
400 my $path = fixup($file);
401 print "nonexistent $path\n" unless -e $path;
402 # perl's chown() does not chown symlinks, it chowns the symlink's
403 # target. it fails when the link's target doesn't exist, because
404 # the stat64() syscall fails.
405 # this causes error messages when unpacking symlinks in tarballs.
406 my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
407
408 my $tmp = $mount->{tmp} . '/' . $file;
409 syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp;
410
411 return $err;
412 }
413
414 sub x_chmod {
415 my ($path) = fixup(shift);
416 my ($mode) = shift;
417 my ($err) = chmod($mode,$path) ? 0 : -$!;
418 return $err;
419 }
420
421 sub x_truncate {
422 my ( $file,$size ) = @_;
423
424 #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} );
425
426 my $path = tmp_path( $file );
427 my $rv = truncate( $path, $size ) ? 0 : -$! ;
428 warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
429 compress_file2path( $file, $path );
430
431 return $rv;
432 }
433
434 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
435
436 sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
437 sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
438
439 sub x_mknod {
440 # since this is called for ALL files, not just devices, I'll do some checks
441 # and possibly run the real mknod command.
442 my ($file, $modes, $dev) = @_;
443 $file = fixup($file);
444 $! = 0;
445 syscall(&SYS_mknod,$file,$modes,$dev);
446 return -$!;
447 }
448
449 sub x_release {
450 my ( $file, $mode ) = @_;
451
452 if ( ! defined( $pending->{$file} ) ) {
453 warn "release $file, NO PENDING DATA\n";
454 return 0;
455 } elsif ( ! defined( $pending->{$file}->{write} ) ) {
456 warn "release $file, not written into\n";
457 } elsif ( $file =~ m#\Q/.fuse_hidden\E# ) {
458 warn "release internal $file\n" if $debug;
459 } else {
460 compress_file2path( $file );
461 }
462
463 $pending->{$file}->{open}--;
464 if ( $pending->{$file}->{open} == 0 ) {
465 warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
466 delete( $pending->{$file} );
467 }
468
469 return 0;
470 }
471
472 # kludge
473 sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
474 Fuse::main(
475 mountpoint=>$mount->{to},
476 getattr =>"main::x_getattr",
477 readlink=>"main::x_readlink",
478 getdir =>"main::x_getdir",
479 mknod =>"main::x_mknod",
480 mkdir =>"main::x_mkdir",
481 unlink =>"main::x_unlink",
482 rmdir =>"main::x_rmdir",
483 symlink =>"main::x_symlink",
484 rename =>"main::x_rename",
485 link =>"main::x_link",
486 chmod =>"main::x_chmod",
487 chown =>"main::x_chown",
488 truncate=>"main::x_truncate",
489 utime =>"main::x_utime",
490 open =>"main::x_open",
491 read =>"main::x_read",
492 write =>"main::x_write",
493 statfs =>"main::x_statfs",
494 release =>"main::x_release",
495 # threaded=>1,
496 debug => $fuse_debug,
497 );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26