/[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 1 - (hide annotations)
Sun Jul 8 12:51:33 2007 UTC (12 years, 4 months ago) by dpavlin
File MIME type: text/plain
File size: 6918 byte(s)
first code drop of gzip compressing filesystem
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     my $skip_extensions_regex = qr/\.(sw[px]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg)$/i;
24    
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     }
99    
100     sub x_open {
101     my ($file) = shift;
102     my ($mode) = shift;
103     $pending->{$file}->{open}++;
104     my $fh;
105     if ( $pending->{$file}->{open} == 1 ) {
106     warn "# open( $file, $mode )\n";
107     my $path = fixup($file);
108     my $tmp = $mount->{tmp} . '/' . $file;
109     if ( -e $tmp ) {
110     $path = $tmp;
111     } elsif ( $path =~ m/\.gz$/ ) {
112     my $dest_path = $tmp;
113     $dest_path =~ s!/[^/]+$!!; #!vim-fix
114     mkpath $dest_path unless -e $dest_path;
115     file_copy( '<:gzip', $path, '>', $tmp );
116     $path = $tmp;
117     }
118     return -$! unless sysopen($fh , $path, $mode);
119     $pending->{$file}->{fh} = $fh;
120     $pending->{$file}->{path} = $path;
121     } elsif ( ! defined( $pending->{$file}->{fh} ) ) {
122     die "can't find fh for $file ", dump($pending);
123     }
124     return 0;
125     }
126    
127     sub x_read {
128     my ($file,$bufsize,$off) = @_;
129     my ($rv) = -ENOSYS();
130     my $path = fixup( $file );
131     return -ENOENT() unless -e $path;
132     my ($fsize) = -s $path;
133     my $fh = $pending->{$file}->{fh} || die "no fh? ", dump( $pending );
134     if(seek($fh,$off,SEEK_SET)) {
135     read($fh,$rv,$bufsize);
136     }
137     return $rv;
138     }
139    
140     sub x_write {
141     my ($file,$buf,$off) = @_;
142     $pending->{$file}->{write}++;
143     my ($rv);
144     my $path = fixup($file);
145     return -ENOENT() unless -e $path;
146     my ($fsize) = -s $path;
147     my $fh = $pending->{$file}->{fh};
148     return -ENOSYS() unless $fh;
149     if($rv = seek( $fh ,$off,SEEK_SET)) {
150     $rv = print( $fh $buf );
151     }
152     $rv = -ENOSYS() unless $rv;
153     return length($buf);
154     }
155    
156     sub err { return (-shift || -$!) }
157    
158     sub x_readlink { return readlink(fixup(shift)); }
159     sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; }
160    
161     sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
162    
163     sub x_rename {
164     my ($old) = fixup(shift);
165     my ($new) = fixup(shift);
166     my ($err) = rename($old,$new) ? 0 : -ENOENT();
167     return $err;
168     }
169     sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
170    
171     sub x_chown {
172     my ($path) = fixup(shift);
173     print "nonexistent $path\n" unless -e $path;
174     my ($uid,$gid) = @_;
175     # perl's chown() does not chown symlinks, it chowns the symlink's
176     # target. it fails when the link's target doesn't exist, because
177     # the stat64() syscall fails.
178     # this causes error messages when unpacking symlinks in tarballs.
179     my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
180     return $err;
181     }
182    
183     sub x_chmod {
184     my ($path) = fixup(shift);
185     my ($mode) = shift;
186     my ($err) = chmod($mode,$path) ? 0 : -$!;
187     return $err;
188     }
189    
190     sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }
191     sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
192    
193     sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
194     sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
195    
196     sub x_mknod {
197     # since this is called for ALL files, not just devices, I'll do some checks
198     # and possibly run the real mknod command.
199     my ($file, $modes, $dev) = @_;
200     $file = fixup($file);
201     $! = 0;
202     syscall(&SYS_mknod,$file,$modes,$dev);
203     return -$!;
204     }
205    
206     sub x_release {
207     my ( $file, $mode ) = @_;
208     if ( ! defined( $pending->{$file} ) ) {
209     warn "release $file, NO PENDING DATA\n";
210     return 0;
211     } elsif ( ! defined( $pending->{$file}->{write} ) ) {
212     warn "release $file, not written into\n";
213     } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
214     close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";
215     if ( $file =~ $skip_extensions_regex ) {
216     warn "release $file $mode -- uncompressed\n";
217     } else {
218     warn "release $file $mode -- compressing\n";
219    
220     my $path = $pending->{$file}->{path} || die "no path for $file ? ", dump( $pending );
221     my $dest = fixup( $file );
222    
223     if ( $dest =~ /\.gz$/ ) {
224     warn "## remove old $dest\n";
225     unlink $dest || die "can't remove $dest: $!";
226     $dest =~ s/\.gz$//;
227     }
228    
229     file_copy( '<', $path, '>:gzip', $dest . '.gz' );
230    
231     # FIXME add timeout to remove uncompressed version?
232     unlink $path || warn "can't remove $path: $!";
233     }
234     } else {
235     warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";
236     $pending->{$file}->{open}--;
237     return 0;
238     }
239     delete( $pending->{$file} );
240     return 0;
241     }
242    
243     # kludge
244     sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
245     Fuse::main(
246     mountpoint=>$mount->{to},
247     getattr =>"main::x_getattr",
248     readlink=>"main::x_readlink",
249     getdir =>"main::x_getdir",
250     mknod =>"main::x_mknod",
251     mkdir =>"main::x_mkdir",
252     unlink =>"main::x_unlink",
253     rmdir =>"main::x_rmdir",
254     symlink =>"main::x_symlink",
255     rename =>"main::x_rename",
256     link =>"main::x_link",
257     chmod =>"main::x_chmod",
258     chown =>"main::x_chown",
259     truncate=>"main::x_truncate",
260     utime =>"main::x_utime",
261     open =>"main::x_open",
262     read =>"main::x_read",
263     write =>"main::x_write",
264     statfs =>"main::x_statfs",
265     release =>"main::x_release",
266     # threaded=>1,
267     # debug => 1,
268     );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26