/[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 39 - (show annotations)
Sun Sep 2 12:03:52 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 13741 byte(s)
fix symlink in tmp
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 cluck/;
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 => shift @ARGV || '/tmp/comp',
29 to => shift @ARGV || '/tmp/no-comp',
30 tmp => shift @ARGV || '/dev/shm/comp',
31 };
32
33 warn "mount $mount->{from} to $mount->{to} using $mount->{tmp} as cache\n";
34
35 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
37 # don't compress files smaller than this
38 my $min_compress_size = 512;
39
40 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 sub real_name {
50 my ( $dir, $name ) = @_;
51 if ( -e "$dir/${name}.gz" ) {
52 cluck "ASSERT: unexpected $dir/$name exists" if -e "$dir/$name";
53 return "${name}.gz";
54 }
55 if ( $name =~ m/\.gz$/ ) {
56 return $name . '%'; # protect (mingle) compressed files
57 } else {
58 return $name;
59 }
60 }
61
62 sub fixup {
63 my ( $path ) = @_;
64 return $mount->{from} . '/' . real_name( $mount->{from}, $path );
65 }
66
67 sub original_name {
68 my $p = shift;
69 $p =~ s/\.gz$//;
70 $p =~ s/\.gz%$/.gz/; # demungle compressed .gz files
71 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 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 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 my $buff;
117 while( read( $s, $buff, 65535 ) ) {
118 print $d $buff || confess "can't write into $d_path: $!";
119 warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
120 }
121 close($d) || warn "can't close $d_path: $!";
122 close($s) || warn "can't close $s_path: $!";
123 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
130 undef $d;
131 undef $s;
132 }
133
134 sub tmp_path {
135 my $file = shift;
136
137 my $path = fixup( $file );
138
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 confess "ASSERT: filesystem corruption, $path is zero size in ",dump( $pending );
157 }
158 $path = $tmp;
159 $op = 'created';
160 }
161 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 warn "## tmp_file( $file ) $op $path [", -s $path, "]\n" if $debug;
166 }
167 return $path;
168 }
169
170 sub compress_file2path {
171 my ( $file, $path ) = @_;
172
173 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 confess "ASSERT: compressing into $path instead of $pending_path";
185 }
186 }
187
188 confess "need path" unless $path;
189
190 # 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 confess "ASSERT: uncompressed $dest shouldn't exist!" if -e $dest;
196 }
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 warn ">>> $size_path <= $size_comp leaving uncompressed $dest\n";
214 unlink $comp || confess "can't remove: $comp: $!";
215 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
216 } else {
217 warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"% $comp\n";
218
219 # FIXME add timeout to remove uncompressed version?
220 unlink $path || confess "can't remove $path: $!";
221
222 if ( -e $dest ) {
223 warn "## cleanup uncompressed $dest\n" if $debug;
224 unlink $dest || confess "can't remove $dest: $!";
225 }
226 }
227
228 }
229 }
230
231 sub x_open {
232 my ($file) = shift;
233 my ($mode) = shift;
234
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 my $mode_desc = {
247 rdonly => $mode && O_RDONLY,
248 rdwr => $mode && O_RDWR,
249 append => $mode && O_APPEND,
250 create => $mode && O_CREAT,
251 trunc => $mode && O_TRUNC,
252 };
253
254 my $path = tmp_path( $file );
255
256 warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
257
258 my $fh;
259 my $rv = 0;
260
261 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 if ( sysopen($fh , $path, $mode) ) {
270 close($fh) || confess "can't close $path: $!";
271 warn "<<< sysopen $path [", -e $path ? -s $path : 'new' , "]\n";
272 $pending->{$file}->{open}++;
273 } else {
274 warn "ERROR: can't open $path -- $!";
275 $rv = -$!;
276 }
277
278 return $rv;
279
280 }
281
282 sub x_read {
283 my ($file,$bufsize,$off) = @_;
284 my ($rv) = -ENOSYS();
285 my $path = fixup( $file );
286
287 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
288
289 return -ENOENT() unless -e $path;
290
291 my $fh = new IO::File;
292 return -ENOSYS() unless open($fh,$pending->{$file}->{path});
293
294 if(seek($fh,$off,SEEK_SET)) {
295 read($fh,$rv,$bufsize);
296 $pending->{$file}->{read} += length($rv) if $stats;
297 }
298
299 return $rv;
300 }
301
302 sub x_write {
303 my ($file,$buf,$off) = @_;
304
305 my $rv;
306 my $path = fixup($file);
307
308 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
309
310 return -ENOENT() unless -e $path;
311
312 $path = $pending->{$file}->{path} || confess "no path for $file in ", dump( $pending );
313 confess "write into non-existant $path for $file" unless -e $path;
314
315 my $fh = new IO::File;
316 return -ENOSYS() unless open($fh,'+<',$path);
317 if($rv = seek( $fh ,$off,SEEK_SET)) {
318 $rv = print( $fh $buf );
319 my $size = length($buf);
320 warn "## write $path offset $off [$size]\n" if $debug;
321 $pending->{$file}->{write} += $size;
322 }
323 $rv = -ENOSYS() unless $rv;
324 close($fh) || warn "can't close $path: $!";
325 return length($buf);
326 }
327
328 sub err { return (-shift || -$!) }
329
330 sub x_readlink { return readlink(fixup(shift)); }
331
332 sub x_unlink {
333 my $file = shift;
334 my $path = fixup( $file );
335
336 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 sub x_symlink {
352 my ($from,$to) = @_;
353
354 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 my $tmp_to = $mount->{tmp} . '/' . $to;
362 if ( $rv == 0 && -e $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 sub x_rename {
383 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 if ( $new =~ m#\Q/.fuse_hidden\E# ) {
394 unlink $tmp || confess "can't unlink $tmp for $new";
395 } else {
396 my $new_tmp = $mount->{tmp} . '/' . $new;
397 rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
398 }
399 }
400
401 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 delete( $pending->{$old} );
409 warn "## tweaking pending to ", dump( $pending ) if $debug;
410 }
411
412 return $err;
413 }
414
415 sub x_chown {
416 my ($file,$uid,$gid) = @_;
417 my $path = fixup($file);
418 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
425 my $tmp = $mount->{tmp} . '/' . $file;
426 syscall(&SYS_lchown,$file,$uid,$gid,$path) if -e $tmp;
427
428 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 sub x_truncate {
439 my ( $file,$size ) = @_;
440
441 #confess "no pending file $file to truncate in ", dump( $pending ) unless defined( $pending->{$file} );
442
443 my $path = tmp_path( $file );
444 my $rv = truncate( $path, $size ) ? 0 : -$! ;
445 warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
446 compress_file2path( $file, $path );
447
448 return $rv;
449 }
450
451 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
469 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 } elsif ( $file =~ m#\Q/.fuse_hidden\E# ) {
475 warn "release internal $file\n" if $debug;
476 } else {
477 compress_file2path( $file );
478 }
479
480 $pending->{$file}->{open}--;
481 if ( $pending->{$file}->{open} == 0 ) {
482
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 delete( $pending->{$file} );
493 }
494
495 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 debug => $fuse_debug,
523 );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26