/[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 13 - (show annotations)
Mon Jul 9 11:12:48 2007 UTC (12 years, 4 months ago) by dpavlin
File MIME type: text/plain
File size: 8737 byte(s)
double rewrite (from compressed on-disk file) doesn't work
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
16 my $mount = {
17 from => '/tmp/comp',
18 to => '/tmp/no-comp',
19 tmp => '/dev/shm/comp',
20 };
21
22 my $debug = 1;
23
24 my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i;
25
26 # don't compress files smaller than this
27 my $min_compress_size = 512;
28
29 foreach my $dir ( keys %$mount ) {
30 if ( ! -e $mount->{$dir} ) {
31 warn "created $mount->{$dir}\n";
32 mkdir $mount->{$dir} || die "can't create $mount->{$dir}: $!";
33 }
34 }
35
36 my $pending;
37
38 sub fixup {
39 my ( $path ) = @_;
40 my $full = $mount->{from} . '/' . $path;
41 if ( -e $full . '.gz' ) {
42 return $full . '.gz';
43 }
44 return $full;
45 }
46
47 sub original_name {
48 my $p = shift;
49 $p =~ s/\.gz$//;
50 return $p;
51 };
52
53 sub gzip_original_size {
54 my $file = shift;
55
56 return unless -e $file;
57
58 my $buff;
59
60 open(my $fh, $file) || die "can't open $file: $!";
61
62 # read($fh, $buff, 10 );
63 # print dump( unpack("nccVcc", $buff ) );
64
65 seek($fh, -4, 2);
66 read($fh, $buff, 4 );
67 close($fh);
68
69 return unpack("L", $buff);
70 }
71
72 sub unlink_all {
73 my $file = shift;
74 warn "# unlink_all( $file )\n";
75
76 my $path = fixup( $file );
77 unlink $path || return 0;
78
79 my $tmp = $mount->{tmp} . '/' . $file;
80 unlink $tmp if ( -e $tmp );
81
82 delete( $pending->{$file} );
83 return 1;
84 }
85
86 sub x_getattr {
87 my ($file) = fixup(shift);
88 my (@list) = lstat($file);
89 $list[7] = gzip_original_size( $file ) if ( $list[7] && $file =~ m/\.gz$/ );
90 return -$! unless @list;
91 return @list;
92 }
93
94 sub x_getdir {
95 my ($dirname) = fixup(shift);
96 unless(opendir(DIRHANDLE,$dirname)) {
97 return -ENOENT();
98 }
99 my @files = map { original_name( $_ ) } readdir(DIRHANDLE);
100 closedir(DIRHANDLE);
101 return (@files, 0);
102 }
103
104 sub file_copy {
105 my ( $s_opt, $s_path, $d_opt, $d_path ) = @_;
106 warn "## file_copy( $s_opt $s_path [",-s $s_path,"] $d_opt $d_path [",-e $d_path ? -s $d_path : 'new',"])\n";
107 open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending );
108 open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!";
109 my $buff;
110 while( read( $s, $buff, 65535 ) ) {
111 print $d $buff || confess "can't write into $d_path: $!";
112 warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug;
113 }
114 close($d) || warn "can't close $d_path: $!";
115 close($s) || warn "can't close $s_path: $!";
116 warn "-- $s_path [", -s $s_path, "] >>> $d_path [", -s $d_path, "]\n" if $debug;
117 my ($mode,$uid,$gid,$atime,$mtime) = (stat $s_path)[2,4,5,8,9];
118
119 chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n";
120 chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n";
121 utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n";
122
123 undef $d;
124 undef $s;
125 }
126
127 sub x_open {
128 my ($file) = shift;
129 my ($mode) = shift;
130
131 if ( $file eq '/.debug' ) {
132 my $path = $mount->{from} . '/.debug';
133 open( my $debug, '>', $path ) || die "can't open $path: $!";
134 my $dump = dump( $pending );
135 print $debug "pending = $dump\n";
136 close($debug);
137 $pending->{'/.debug'}->{path} = $path;
138 warn "## created dump $path $dump\n";
139 return 0;
140 }
141
142 $pending->{$file}->{open}++;
143
144 my $mode_desc = {
145 rdonly => $mode && O_RDONLY,
146 rdwr => $mode && O_RDWR,
147 append => $mode && O_APPEND,
148 create => $mode && O_CREAT,
149 };
150 warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode: ", dump( $mode_desc ),"\n";
151 my $fh;
152
153 my $path = fixup($file);
154 my $tmp = $mount->{tmp} . '/' . $file;
155 if ( -e $tmp ) {
156 $path = $tmp;
157 } elsif ( $path =~ m/\.gz$/ ) {
158 my $dest_path = $tmp;
159 $dest_path =~ s!/[^/]+$!!; #!vim-fix
160 mkpath $dest_path unless -e $dest_path;
161 file_copy( '<:gzip', $path, '>', $tmp );
162 $path = $tmp;
163 }
164 warn ">>> open abs path: $path [", -s $path, "]\n";
165 return -$! unless sysopen($fh , $path, $mode);
166 close($fh);
167
168 $pending->{$file}->{path} = $path;
169 return 0;
170 }
171
172 sub x_read {
173 my ($file,$bufsize,$off) = @_;
174 my ($rv) = -ENOSYS();
175 my $path = fixup( $file );
176
177 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
178
179 return -ENOENT() unless -e $path;
180
181 my $fh = new IO::File;
182 return -ENOSYS() unless open($fh,$pending->{$file}->{path});
183
184 if(seek($fh,$off,SEEK_SET)) {
185 read($fh,$rv,$bufsize);
186 }
187
188 return $rv;
189 }
190
191 sub x_write {
192 my ($file,$buf,$off) = @_;
193 $pending->{$file}->{write}++;
194 my $rv;
195 my $path = fixup($file);
196
197 confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} );
198
199 return -ENOENT() unless -e $path;
200
201 my $fh = new IO::File;
202 return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path});
203 if($rv = seek( $fh ,$off,SEEK_SET)) {
204 $rv = print( $fh $buf );
205 warn "## ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug;
206 }
207 $rv = -ENOSYS() unless $rv;
208 close($fh);
209 return length($buf);
210 }
211
212 sub err { return (-shift || -$!) }
213
214 sub x_readlink { return readlink(fixup(shift)); }
215 sub x_unlink { return unlink_all( shift ) ? 0 : -$! }
216
217 sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
218
219 sub x_rename {
220 my ($old) = fixup(shift);
221 my ($new) = fixup(shift);
222 my ($err) = rename($old,$new) ? 0 : -ENOENT();
223 return $err;
224 }
225 sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
226
227 sub x_chown {
228 my ($path) = fixup(shift);
229 print "nonexistent $path\n" unless -e $path;
230 my ($uid,$gid) = @_;
231 # perl's chown() does not chown symlinks, it chowns the symlink's
232 # target. it fails when the link's target doesn't exist, because
233 # the stat64() syscall fails.
234 # this causes error messages when unpacking symlinks in tarballs.
235 my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
236 return $err;
237 }
238
239 sub x_chmod {
240 my ($path) = fixup(shift);
241 my ($mode) = shift;
242 my ($err) = chmod($mode,$path) ? 0 : -$!;
243 return $err;
244 }
245
246 sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }
247 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
248
249 sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
250 sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
251
252 sub x_mknod {
253 # since this is called for ALL files, not just devices, I'll do some checks
254 # and possibly run the real mknod command.
255 my ($file, $modes, $dev) = @_;
256 $file = fixup($file);
257 $! = 0;
258 syscall(&SYS_mknod,$file,$modes,$dev);
259 return -$!;
260 }
261
262 sub x_release {
263 my ( $file, $mode ) = @_;
264 if ( ! defined( $pending->{$file} ) ) {
265 warn "release $file, NO PENDING DATA\n";
266 return 0;
267 } elsif ( ! defined( $pending->{$file}->{write} ) ) {
268 warn "release $file, not written into\n";
269 } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
270 my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
271 my $dest = fixup( $file );
272
273 # cleanup old compressed copy
274 if ( $dest =~ /\.gz$/ ) {
275 warn "## remove old $dest\n";
276 unlink $dest || confess "can't remove $dest: $!";
277 $dest =~ s/\.gz$//;
278 }
279
280 if ( $file =~ $skip_extensions_regex ) {
281 warn "release $file $mode -- uncompressed\n";
282 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
283 } elsif ( -s $path < $min_compress_size ) {
284 warn "release $file -- uncompressed, too small [", -s $path, "]\n";
285 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
286 } else {
287 warn "release $file $mode -- compressing\n";
288
289
290 file_copy( '<', $path, '>:gzip', $dest . '.gz' );
291
292 # FIXME add timeout to remove uncompressed version?
293 unlink $path || warn "can't remove $path: $!";
294 }
295 } else {
296 warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";
297 }
298 $pending->{$file}->{open}--;
299 if ( $pending->{$file}->{open} == 0 ) {
300 warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug;
301 delete( $pending->{$file} );
302 }
303 return 0;
304 }
305
306 # kludge
307 sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
308 Fuse::main(
309 mountpoint=>$mount->{to},
310 getattr =>"main::x_getattr",
311 readlink=>"main::x_readlink",
312 getdir =>"main::x_getdir",
313 mknod =>"main::x_mknod",
314 mkdir =>"main::x_mkdir",
315 unlink =>"main::x_unlink",
316 rmdir =>"main::x_rmdir",
317 symlink =>"main::x_symlink",
318 rename =>"main::x_rename",
319 link =>"main::x_link",
320 chmod =>"main::x_chmod",
321 chown =>"main::x_chown",
322 truncate=>"main::x_truncate",
323 utime =>"main::x_utime",
324 open =>"main::x_open",
325 read =>"main::x_read",
326 write =>"main::x_write",
327 statfs =>"main::x_statfs",
328 release =>"main::x_release",
329 # threaded=>1,
330 # debug => 1,
331 );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26