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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26