/[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 24 - (show annotations)
Mon Jul 9 21:57:11 2007 UTC (12 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 10417 byte(s)
fix
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 fixup {
46 my ( $path ) = @_;
47 my $full = $mount->{from} . '/' . $path;
48 if ( -e $full . '.gz' ) {
49 return $full . '.gz';
50 }
51 return $full;
52 }
53
54 sub original_name {
55 my $p = shift;
56 $p =~ s/\.gz$//;
57 return $p;
58 };
59
60 sub gzip_original_size {
61 my $file = shift;
62
63 return unless -e $file;
64
65 my $buff;
66
67 open(my $fh, $file) || die "can't open $file: $!";
68
69 # read($fh, $buff, 10 );
70 # print dump( unpack("nccVcc", $buff ) );
71
72 seek($fh, -4, 2);
73 read($fh, $buff, 4 );
74 close($fh);
75
76 return unpack("L", $buff);
77 }
78
79 sub unlink_all {
80 my $file = shift;
81 warn "# unlink_all( $file )\n";
82
83 my $path = fixup( $file );
84 unlink $path || return 0;
85
86 my $tmp = $mount->{tmp} . '/' . $file;
87 unlink $tmp if ( -e $tmp );
88
89 delete( $pending->{$file} );
90 return 1;
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 x_open {
135 my ($file) = shift;
136 my ($mode) = shift;
137
138 if ( $file eq '/.debug' ) {
139 my $path = $mount->{from} . '/.debug';
140 open( my $debug, '>', $path ) || die "can't open $path: $!";
141 my $dump = dump( $pending );
142 print $debug "pending = $dump\n";
143 close($debug);
144 $pending->{'/.debug'}->{path} = $path;
145 warn "## created dump $path $dump\n";
146 return 0;
147 }
148
149 $pending->{$file}->{open}++;
150
151 my $mode_desc = {
152 rdonly => $mode && O_RDONLY,
153 rdwr => $mode && O_RDWR,
154 append => $mode && O_APPEND,
155 create => $mode && O_CREAT,
156 trunc => $mode && O_TRUNC,
157 };
158 my $path = fixup($file);
159 warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
160 my $fh;
161
162 my $tmp = $mount->{tmp} . '/' . $file;
163 if ( -e $tmp ) {
164 $path = $tmp;
165 } elsif ( $path =~ m/\.gz$/ ) {
166 my $dest_path = $tmp;
167 $dest_path =~ s!/[^/]+$!!; #!vim-fix
168 mkpath $dest_path unless -e $dest_path;
169 if ( -s $path ) {
170 file_copy( '<:gzip', $path, '>', $tmp )
171 } else {
172 warn "ERROR: filesystem corruption, $path is zero size\n";
173 }
174 $path = $tmp;
175 }
176
177 if ( sysopen($fh , $path, $mode) ) {
178 close($fh) || confess "can't close $path: $!";
179 warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n";
180 $pending->{$file}->{path} = $path;
181 return 0;
182 } else {
183 warn "ERROR: can't open $path : $!";
184 return -$!;
185 }
186
187 }
188
189 sub x_read {
190 my ($file,$bufsize,$off) = @_;
191 my ($rv) = -ENOSYS();
192 my $path = fixup( $file );
193
194 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
195
196 return -ENOENT() unless -e $path;
197
198 my $fh = new IO::File;
199 return -ENOSYS() unless open($fh,$pending->{$file}->{path});
200
201 if(seek($fh,$off,SEEK_SET)) {
202 read($fh,$rv,$bufsize);
203 }
204
205 return $rv;
206 }
207
208 sub x_write {
209 my ($file,$buf,$off) = @_;
210 $pending->{$file}->{write}++;
211 my $rv;
212 my $path = fixup($file);
213
214 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
215
216 return -ENOENT() unless -e $path;
217
218 my $fh = new IO::File;
219 return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});
220 if($rv = seek( $fh ,$off,SEEK_SET)) {
221 $rv = print( $fh $buf );
222 warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
223 }
224 $rv = -ENOSYS() unless $rv;
225 close($fh);
226 return length($buf);
227 }
228
229 sub err { return (-shift || -$!) }
230
231 sub x_readlink { return readlink(fixup(shift)); }
232 sub x_unlink { return unlink_all( shift ) ? 0 : -$! }
233
234 sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
235
236 sub x_rename {
237 my ($old,$new) = @_;
238 my $old_path = fixup($old);
239 my $new_path = fixup($new);
240 $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
241
242 my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
243 warn "## rename( $old_path => $new_path ) = $err\n";
244
245 my $tmp = $mount->{tmp} . '/' . $old;
246 if ( -e $tmp ) {
247 my $new_tmp = $mount->{tmp} . '/' . $new;
248 rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
249 }
250
251 if (defined( $pending->{$old} )) {
252 $pending->{$new} = $pending->{$old};
253
254 my $path = $pending->{$old}->{path};
255 $path =~ s/\Q$old\E/$new/;
256 $pending->{$new}->{path} = $path;
257 $pending->{$old}->{path} = $path;
258
259 #delete( $pending->{$old} );
260 }
261
262 return $err;
263 }
264 sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
265
266 sub x_chown {
267 my ($path) = fixup(shift);
268 print "nonexistent $path\n" unless -e $path;
269 my ($uid,$gid) = @_;
270 # perl's chown() does not chown symlinks, it chowns the symlink's
271 # target. it fails when the link's target doesn't exist, because
272 # the stat64() syscall fails.
273 # this causes error messages when unpacking symlinks in tarballs.
274 my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
275 return $err;
276 }
277
278 sub x_chmod {
279 my ($path) = fixup(shift);
280 my ($mode) = shift;
281 my ($err) = chmod($mode,$path) ? 0 : -$!;
282 return $err;
283 }
284
285 sub x_truncate {
286 my ( $file,$size ) = @_;
287 my $path = fixup($file);
288 my $rv = truncate( $path, $size ) ? 0 : -$! ;
289 if ( $path =~ m/\.gz$/ ) {
290 my $no_gz = $path;
291 $no_gz =~ s/\.gz$//;
292 rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";
293 }
294 warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
295 $pending->{$file}->{write}++;
296 return $rv;
297 }
298 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
299
300 sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
301 sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
302
303 sub x_mknod {
304 # since this is called for ALL files, not just devices, I'll do some checks
305 # and possibly run the real mknod command.
306 my ($file, $modes, $dev) = @_;
307 $file = fixup($file);
308 $! = 0;
309 syscall(&SYS_mknod,$file,$modes,$dev);
310 return -$!;
311 }
312
313 sub x_release {
314 my ( $file, $mode ) = @_;
315 if ( ! defined( $pending->{$file} ) ) {
316 warn "release $file, NO PENDING DATA\n";
317 return 0;
318 } elsif ( ! defined( $pending->{$file}->{write} ) ) {
319 warn "release $file, not written into\n";
320 } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
321 my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
322 my $dest = fixup( $file );
323
324 # cleanup old compressed copy
325 if ( $dest =~ /\.gz$/ ) {
326 warn "## remove old $dest\n";
327 unlink $dest || confess "can't remove $dest: $!";
328 $dest =~ s/\.gz$//;
329 }
330
331 if ( $file =~ $skip_extensions_regex ) {
332 warn "release $file [",-s $path,"] skipped compression\n";
333 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
334 } elsif ( -s $path < $min_compress_size ) {
335 warn "release $file [",-s $path,"] uncompressed, too small\n";
336 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
337 } else {
338 warn "release $file [",-s $path,"] compressing\n";
339
340 my $comp = $dest . '.gz';
341 file_copy( '<', $path, '>:gzip', $comp );
342
343 my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
344
345 if ( $size_path <= $size_comp ) {
346 warn ">>> $size_path <= $size_comp leaving uncompressed\n";
347 unlink $comp || warn "can't reamove: $comp: $!";
348 } else {
349 warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";
350 # FIXME add timeout to remove uncompressed version?
351 unlink $path || warn "can't remove $path: $!";
352 }
353 }
354 } else {
355 warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";
356 }
357 $pending->{$file}->{open}--;
358 if ( $pending->{$file}->{open} == 0 ) {
359 warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
360 delete( $pending->{$file} );
361 }
362 return 0;
363 }
364
365 # kludge
366 sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
367 Fuse::main(
368 mountpoint=>$mount->{to},
369 getattr =>"main::x_getattr",
370 readlink=>"main::x_readlink",
371 getdir =>"main::x_getdir",
372 mknod =>"main::x_mknod",
373 mkdir =>"main::x_mkdir",
374 unlink =>"main::x_unlink",
375 rmdir =>"main::x_rmdir",
376 symlink =>"main::x_symlink",
377 rename =>"main::x_rename",
378 link =>"main::x_link",
379 chmod =>"main::x_chmod",
380 chown =>"main::x_chown",
381 truncate=>"main::x_truncate",
382 utime =>"main::x_utime",
383 open =>"main::x_open",
384 read =>"main::x_read",
385 write =>"main::x_write",
386 statfs =>"main::x_statfs",
387 release =>"main::x_release",
388 # threaded=>1,
389 debug => $fuse_debug,
390 );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26