/[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 4 - (hide annotations)
Sun Jul 8 13:46:33 2007 UTC (12 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 7014 byte(s)
report size of files in file_copy, exclude all .sw*
1 dpavlin 1 #!/usr/bin/perl -w
2     use strict;
3     use threads;
4     use threads::shared;
5    
6     use Fuse;
7     use IO::File;
8     use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
9     use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);
10     require 'syscall.ph'; # for SYS_mknod and SYS_lchown
11     use PerlIO::gzip;
12     use File::Path;
13     use Data::Dump qw/dump/;
14    
15     my $mount = {
16     from => '/tmp/comp',
17     to => '/tmp/no-comp',
18     tmp => '/dev/shm/comp',
19     };
20    
21     my $debug = 1;
22    
23 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;
24 dpavlin 1
25     foreach my $dir ( keys %$mount ) {
26     if ( ! -e $mount->{$dir} ) {
27     warn "created $mount->{$dir}\n";
28     mkdir $mount->{$dir} || die "can't create $mount->{$dir}: $!";
29     }
30     }
31    
32     my $pending;
33    
34     sub fixup {
35     my ( $path ) = @_;
36     my $full = $mount->{from} . '/' . $path;
37     if ( -e $full . '.gz' ) {
38     return $full . '.gz';
39     }
40     return $full;
41     }
42    
43     sub original_name {
44     my $p = shift;
45     $p =~ s/\.gz$//;
46     return $p;
47     };
48    
49     sub gzip_original_size {
50     my $file = shift;
51    
52     return unless -e $file;
53    
54     my $buff;
55    
56     open(my $fh, $file) || die "can't open $file: $!";
57    
58     # read($fh, $buff, 10 );
59     # print dump( unpack("nccVcc", $buff ) );
60    
61     seek($fh, -4, 2);
62     read($fh, $buff, 4 );
63     close($fh);
64    
65     return unpack("L", $buff);
66     }
67    
68     sub x_getattr {
69     my ($file) = fixup(shift);
70     my (@list) = lstat($file);
71     $list[7] = gzip_original_size( $file ) if ( $list[7] && $file =~ m/\.gz$/ );
72     return -$! unless @list;
73     return @list;
74     }
75    
76     sub x_getdir {
77     my ($dirname) = fixup(shift);
78     unless(opendir(DIRHANDLE,$dirname)) {
79     return -ENOENT();
80     }
81     my @files = map { original_name( $_ ) } readdir(DIRHANDLE);
82     closedir(DIRHANDLE);
83     return (@files, 0);
84     }
85    
86     sub file_copy {
87     my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
88     warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n";
89     open(my $s, $s_opt, $s_path ) || die "can't open $s_path: $!";
90     open(my $d, $d_opt, $d_path ) || die "can't open $d_path: $!";
91     my $buff;
92     while( read( $s, $buff, 65535 ) ) {
93     print $d $buff || die "can't write into $d_path: $!";
94     warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug;
95     }
96     close($d) || warn "can't close $d_path: $!";
97     close($s) || warn "can't close $s_path: $!";
98 dpavlin 4 warn "-- $s_path [", -s $s_path, "]\n >>> $d_path [", -s $d_path, "]\n" if $debug;
99 dpavlin 1 }
100    
101     sub x_open {
102     my ($file) = shift;
103     my ($mode) = shift;
104     $pending->{$file}->{open}++;
105     my $fh;
106     if ( $pending->{$file}->{open} == 1 ) {
107     warn "# open( $file, $mode )\n";
108     my $path = fixup($file);
109     my $tmp = $mount->{tmp} . '/' . $file;
110     if ( -e $tmp ) {
111     $path = $tmp;
112     } elsif ( $path =~ m/\.gz$/ ) {
113     my $dest_path = $tmp;
114     $dest_path =~ s!/[^/]+$!!; #!vim-fix
115     mkpath $dest_path unless -e $dest_path;
116     file_copy( '<:gzip', $path, '>', $tmp );
117     $path = $tmp;
118     }
119     return -$! unless sysopen($fh , $path, $mode);
120     $pending->{$file}->{fh} = $fh;
121     $pending->{$file}->{path} = $path;
122     } elsif ( ! defined( $pending->{$file}->{fh} ) ) {
123     die "can't find fh for $file ", dump($pending);
124     }
125     return 0;
126     }
127    
128     sub x_read {
129     my ($file,$bufsize,$off) = @_;
130     my ($rv) = -ENOSYS();
131     my $path = fixup( $file );
132     return -ENOENT() unless -e $path;
133     my ($fsize) = -s $path;
134     my $fh = $pending->{$file}->{fh} || die "no fh? ", dump( $pending );
135     if(seek($fh,$off,SEEK_SET)) {
136     read($fh,$rv,$bufsize);
137     }
138     return $rv;
139     }
140    
141     sub x_write {
142     my ($file,$buf,$off) = @_;
143     $pending->{$file}->{write}++;
144     my ($rv);
145     my $path = fixup($file);
146     return -ENOENT() unless -e $path;
147     my ($fsize) = -s $path;
148     my $fh = $pending->{$file}->{fh};
149     return -ENOSYS() unless $fh;
150     if($rv = seek( $fh ,$off,SEEK_SET)) {
151     $rv = print( $fh $buf );
152     }
153     $rv = -ENOSYS() unless $rv;
154     return length($buf);
155     }
156    
157     sub err { return (-shift || -$!) }
158    
159     sub x_readlink { return readlink(fixup(shift)); }
160     sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; }
161    
162     sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
163    
164     sub x_rename {
165     my ($old) = fixup(shift);
166     my ($new) = fixup(shift);
167     my ($err) = rename($old,$new) ? 0 : -ENOENT();
168     return $err;
169     }
170     sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
171    
172     sub x_chown {
173     my ($path) = fixup(shift);
174     print "nonexistent $path\n" unless -e $path;
175     my ($uid,$gid) = @_;
176     # perl's chown() does not chown symlinks, it chowns the symlink's
177     # target. it fails when the link's target doesn't exist, because
178     # the stat64() syscall fails.
179     # this causes error messages when unpacking symlinks in tarballs.
180     my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
181     return $err;
182     }
183    
184     sub x_chmod {
185     my ($path) = fixup(shift);
186     my ($mode) = shift;
187     my ($err) = chmod($mode,$path) ? 0 : -$!;
188     return $err;
189     }
190    
191     sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }
192     sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
193    
194     sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
195     sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
196    
197     sub x_mknod {
198     # since this is called for ALL files, not just devices, I'll do some checks
199     # and possibly run the real mknod command.
200     my ($file, $modes, $dev) = @_;
201     $file = fixup($file);
202     $! = 0;
203     syscall(&SYS_mknod,$file,$modes,$dev);
204     return -$!;
205     }
206    
207     sub x_release {
208     my ( $file, $mode ) = @_;
209     if ( ! defined( $pending->{$file} ) ) {
210     warn "release $file, NO PENDING DATA\n";
211     return 0;
212     } elsif ( ! defined( $pending->{$file}->{write} ) ) {
213     warn "release $file, not written into\n";
214     } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
215     close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";
216     if ( $file =~ $skip_extensions_regex ) {
217     warn "release $file $mode -- uncompressed\n";
218     } else {
219     warn "release $file $mode -- compressing\n";
220    
221     my $path = $pending->{$file}->{path} || die "no path for $file ? ", dump( $pending );
222     my $dest = fixup( $file );
223    
224     if ( $dest =~ /\.gz$/ ) {
225     warn "## remove old $dest\n";
226     unlink $dest || die "can't remove $dest: $!";
227     $dest =~ s/\.gz$//;
228     }
229    
230     file_copy( '<', $path, '>:gzip', $dest . '.gz' );
231    
232     # FIXME add timeout to remove uncompressed version?
233     unlink $path || warn "can't remove $path: $!";
234     }
235     } else {
236     warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";
237     $pending->{$file}->{open}--;
238     return 0;
239     }
240     delete( $pending->{$file} );
241     return 0;
242     }
243    
244     # kludge
245     sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
246     Fuse::main(
247     mountpoint=>$mount->{to},
248     getattr =>"main::x_getattr",
249     readlink=>"main::x_readlink",
250     getdir =>"main::x_getdir",
251     mknod =>"main::x_mknod",
252     mkdir =>"main::x_mkdir",
253     unlink =>"main::x_unlink",
254     rmdir =>"main::x_rmdir",
255     symlink =>"main::x_symlink",
256     rename =>"main::x_rename",
257     link =>"main::x_link",
258     chmod =>"main::x_chmod",
259     chown =>"main::x_chown",
260     truncate=>"main::x_truncate",
261     utime =>"main::x_utime",
262     open =>"main::x_open",
263     read =>"main::x_read",
264     write =>"main::x_write",
265     statfs =>"main::x_statfs",
266     release =>"main::x_release",
267     # threaded=>1,
268     # debug => 1,
269     );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26