/[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 25 - (hide annotations)
Mon Jul 9 22:44:20 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 10708 byte(s)
hopefually correct handling of .fuse_hidden files which are created
when open file is unlinked.

unlink cleanup, final rename tweaking and tests
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    
20     GetOptions(
21     'debug+' => \$debug,
22     'fuse-debug+' => \$fuse_debug,
23     );
24    
25 dpavlin 1 my $mount = {
26     from => '/tmp/comp',
27     to => '/tmp/no-comp',
28     tmp => '/dev/shm/comp',
29     };
30    
31 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;
32 dpavlin 1
33 dpavlin 5 # don't compress files smaller than this
34 dpavlin 8 my $min_compress_size = 512;
35 dpavlin 5
36 dpavlin 1 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 x_getattr {
80     my ($file) = fixup(shift);
81     my (@list) = lstat($file);
82     $list[7] = gzip_original_size( $file ) if ( $list[7] && $file =~ m/\.gz$/ );
83     return -$! unless @list;
84     return @list;
85     }
86    
87     sub x_getdir {
88     my ($dirname) = fixup(shift);
89     unless(opendir(DIRHANDLE,$dirname)) {
90     return -ENOENT();
91     }
92     my @files = map { original_name( $_ ) } readdir(DIRHANDLE);
93     closedir(DIRHANDLE);
94     return (@files, 0);
95     }
96    
97     sub file_copy {
98     my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
99 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;
100 dpavlin 5 open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending );
101     open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!";
102 dpavlin 1 my $buff;
103     while( read( $s, $buff, 65535 ) ) {
104 dpavlin 5 print $d $buff || confess "can't write into $d_path: $!";
105 dpavlin 13 warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
106 dpavlin 1 }
107     close($d) || warn "can't close $d_path: $!";
108     close($s) || warn "can't close $s_path: $!";
109 dpavlin 5 warn "-- $s_path [", -s $s_path, "] >>> $d_path [", -s $d_path, "]\n" if $debug;
110     my ($mode,$uid,$gid,$atime,$mtime) = (stat $s_path)[2,4,5,8,9];
111    
112     chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";
113     chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";
114     utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
115 dpavlin 8
116     undef $d;
117     undef $s;
118 dpavlin 1 }
119    
120     sub x_open {
121     my ($file) = shift;
122     my ($mode) = shift;
123 dpavlin 12
124     if ( $file eq '/.debug' ) {
125     my $path = $mount->{from} . '/.debug';
126     open( my $debug, '>', $path ) || die "can't open $path: $!";
127     my $dump = dump( $pending );
128     print $debug "pending = $dump\n";
129     close($debug);
130     $pending->{'/.debug'}->{path} = $path;
131     warn "## created dump $path $dump\n";
132     return 0;
133     }
134    
135 dpavlin 1 $pending->{$file}->{open}++;
136 dpavlin 8
137     my $mode_desc = {
138     rdonly => $mode && O_RDONLY,
139     rdwr => $mode && O_RDWR,
140     append => $mode && O_APPEND,
141     create => $mode && O_CREAT,
142 dpavlin 14 trunc => $mode && O_TRUNC,
143 dpavlin 8 };
144 dpavlin 14 my $path = fixup($file);
145 dpavlin 19 warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug;
146 dpavlin 1 my $fh;
147 dpavlin 8
148     my $tmp = $mount->{tmp} . '/' . $file;
149     if ( -e $tmp ) {
150     $path = $tmp;
151     } elsif ( $path =~ m/\.gz$/ ) {
152     my $dest_path = $tmp;
153     $dest_path =~ s!/[^/]+$!!; #!vim-fix
154     mkpath $dest_path unless -e $dest_path;
155 dpavlin 23 if ( -s $path ) {
156     file_copy( '<:gzip', $path, '>', $tmp )
157     } else {
158     warn "ERROR: filesystem corruption, $path is zero size\n";
159     }
160 dpavlin 8 $path = $tmp;
161     }
162    
163 dpavlin 21 if ( sysopen($fh , $path, $mode) ) {
164     close($fh) || confess "can't close $path: $!";
165     warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n";
166     $pending->{$file}->{path} = $path;
167     return 0;
168     } else {
169     warn "ERROR: can't open $path : $!";
170     return -$!;
171     }
172    
173 dpavlin 1 }
174    
175     sub x_read {
176     my ($file,$bufsize,$off) = @_;
177     my ($rv) = -ENOSYS();
178     my $path = fixup( $file );
179 dpavlin 10
180 dpavlin 12 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
181    
182 dpavlin 1 return -ENOENT() unless -e $path;
183 dpavlin 10
184     my $fh = new IO::File;
185     return -ENOSYS() unless open($fh,$pending->{$file}->{path});
186    
187 dpavlin 1 if(seek($fh,$off,SEEK_SET)) {
188     read($fh,$rv,$bufsize);
189     }
190 dpavlin 10
191 dpavlin 1 return $rv;
192     }
193    
194     sub x_write {
195     my ($file,$buf,$off) = @_;
196 dpavlin 25
197 dpavlin 1 $pending->{$file}->{write}++;
198 dpavlin 10 my $rv;
199 dpavlin 1 my $path = fixup($file);
200 dpavlin 10
201 dpavlin 12 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
202    
203 dpavlin 1 return -ENOENT() unless -e $path;
204 dpavlin 10
205     my $fh = new IO::File;
206     return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});
207 dpavlin 1 if($rv = seek( $fh ,$off,SEEK_SET)) {
208     $rv = print( $fh $buf );
209 dpavlin 17 warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
210 dpavlin 1 }
211     $rv = -ENOSYS() unless $rv;
212 dpavlin 12 close($fh);
213 dpavlin 1 return length($buf);
214     }
215    
216     sub err { return (-shift || -$!) }
217    
218     sub x_readlink { return readlink(fixup(shift)); }
219 dpavlin 25 sub x_unlink {
220     my $file = shift;
221     my $path = fixup( $file );
222 dpavlin 1
223 dpavlin 25 if ( $file =~ m#\Q/.fuse_hidden\E# ) {
224     return unlink $path ? 0 : -$1;
225     }
226    
227     warn "# unlink( $file )\n";
228    
229     unlink $path || return 0;
230    
231     my $tmp = $mount->{tmp} . '/' . $file;
232     unlink $tmp if ( -e $tmp );
233    
234     delete( $pending->{$file} );
235     return 0;
236     }
237    
238 dpavlin 1 sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
239    
240     sub x_rename {
241 dpavlin 18 my ($old,$new) = @_;
242     my $old_path = fixup($old);
243     my $new_path = fixup($new);
244     $new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ );
245    
246     my $err = rename($old_path,$new_path) ? 0 : -ENOENT();
247     warn "## rename( $old_path => $new_path ) = $err\n";
248    
249     my $tmp = $mount->{tmp} . '/' . $old;
250     if ( -e $tmp ) {
251 dpavlin 25 if ( $new =~ m#\Q/.fuse_hidden\E# ) {
252     unlink $tmp || confess "can't unlink $tmp for $new\n";
253     } else {
254     my $new_tmp = $mount->{tmp} . '/' . $new;
255     rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!";
256     }
257 dpavlin 18 }
258    
259 dpavlin 20 if (defined( $pending->{$old} )) {
260     $pending->{$new} = $pending->{$old};
261    
262     my $path = $pending->{$old}->{path};
263     $path =~ s/\Q$old\E/$new/;
264     $pending->{$new}->{path} = $path;
265    
266 dpavlin 25 delete( $pending->{$old} );
267     warn "## tweaking pending to ", dump( $pending ) if $debug;
268 dpavlin 20 }
269    
270 dpavlin 1 return $err;
271     }
272     sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
273    
274     sub x_chown {
275     my ($path) = fixup(shift);
276     print "nonexistent $path\n" unless -e $path;
277     my ($uid,$gid) = @_;
278     # perl's chown() does not chown symlinks, it chowns the symlink's
279     # target. it fails when the link's target doesn't exist, because
280     # the stat64() syscall fails.
281     # this causes error messages when unpacking symlinks in tarballs.
282     my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
283     return $err;
284     }
285    
286     sub x_chmod {
287     my ($path) = fixup(shift);
288     my ($mode) = shift;
289     my ($err) = chmod($mode,$path) ? 0 : -$!;
290     return $err;
291     }
292    
293 dpavlin 14 sub x_truncate {
294     my ( $file,$size ) = @_;
295     my $path = fixup($file);
296     my $rv = truncate( $path, $size ) ? 0 : -$! ;
297     if ( $path =~ m/\.gz$/ ) {
298     my $no_gz = $path;
299     $no_gz =~ s/\.gz$//;
300     rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!";
301     }
302 dpavlin 21 warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug;
303 dpavlin 15 $pending->{$file}->{write}++;
304 dpavlin 14 return $rv;
305     }
306 dpavlin 1 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
307    
308     sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
309     sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
310    
311     sub x_mknod {
312     # since this is called for ALL files, not just devices, I'll do some checks
313     # and possibly run the real mknod command.
314     my ($file, $modes, $dev) = @_;
315     $file = fixup($file);
316     $! = 0;
317     syscall(&SYS_mknod,$file,$modes,$dev);
318     return -$!;
319     }
320    
321     sub x_release {
322     my ( $file, $mode ) = @_;
323 dpavlin 25
324     if ( $file =~ m#\Q/.fuse_hidden\E# ) {
325     warn "release internal $file\n" if $debug;
326     delete( $pending->{$file} );
327     return 0;
328     }
329    
330 dpavlin 1 if ( ! defined( $pending->{$file} ) ) {
331     warn "release $file, NO PENDING DATA\n";
332     return 0;
333     } elsif ( ! defined( $pending->{$file}->{write} ) ) {
334     warn "release $file, not written into\n";
335     } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
336 dpavlin 5 my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
337     my $dest = fixup( $file );
338    
339     # cleanup old compressed copy
340     if ( $dest =~ /\.gz$/ ) {
341     warn "## remove old $dest\n";
342 dpavlin 8 unlink $dest || confess "can't remove $dest: $!";
343 dpavlin 5 $dest =~ s/\.gz$//;
344     }
345    
346 dpavlin 1 if ( $file =~ $skip_extensions_regex ) {
347 dpavlin 20 warn "release $file [",-s $path,"] skipped compression\n";
348 dpavlin 5 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
349     } elsif ( -s $path < $min_compress_size ) {
350 dpavlin 20 warn "release $file [",-s $path,"] uncompressed, too small\n";
351 dpavlin 5 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
352 dpavlin 1 } else {
353 dpavlin 20 warn "release $file [",-s $path,"] compressing\n";
354 dpavlin 1
355 dpavlin 20 my $comp = $dest . '.gz';
356     file_copy( '<', $path, '>:gzip', $comp );
357 dpavlin 1
358 dpavlin 20 my ( $size_path, $size_comp ) = ( -s $path, -s $comp );
359 dpavlin 1
360 dpavlin 20 if ( $size_path <= $size_comp ) {
361     warn ">>> $size_path <= $size_comp leaving uncompressed\n";
362     unlink $comp || warn "can't reamove: $comp: $!";
363     } else {
364     warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n";
365     # FIXME add timeout to remove uncompressed version?
366     unlink $path || warn "can't remove $path: $!";
367     }
368 dpavlin 1 }
369     } else {
370     warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";
371     }
372 dpavlin 13 $pending->{$file}->{open}--;
373     if ( $pending->{$file}->{open} == 0 ) {
374     warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
375     delete( $pending->{$file} );
376     }
377 dpavlin 1 return 0;
378     }
379    
380     # kludge
381     sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
382     Fuse::main(
383     mountpoint=>$mount->{to},
384     getattr =>"main::x_getattr",
385     readlink=>"main::x_readlink",
386     getdir =>"main::x_getdir",
387     mknod =>"main::x_mknod",
388     mkdir =>"main::x_mkdir",
389     unlink =>"main::x_unlink",
390     rmdir =>"main::x_rmdir",
391     symlink =>"main::x_symlink",
392     rename =>"main::x_rename",
393     link =>"main::x_link",
394     chmod =>"main::x_chmod",
395     chown =>"main::x_chown",
396     truncate=>"main::x_truncate",
397     utime =>"main::x_utime",
398     open =>"main::x_open",
399     read =>"main::x_read",
400     write =>"main::x_write",
401     statfs =>"main::x_statfs",
402     release =>"main::x_release",
403     # threaded=>1,
404 dpavlin 23 debug => $fuse_debug,
405 dpavlin 1 );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26