/[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 5 - (show annotations)
Sun Jul 8 15:04:35 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 8204 byte(s)
confess all errors, added minimum site for files which will be compressed
(by default, 256 bytes), unlinking should now cleanup everything,
preserve file attributes when (un)compressing
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 use Carp qw/confess/;
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 = 256;
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 foreach my $dir ( keys %$mount ) {
75 my $path = $mount->{$dir} . '/' . $file;
76
77 map {
78 my $path = $_;
79 if ( -e $path ) {
80 if ( unlink $path ) {
81 warn "## unlink $path\n" if $debug;
82 } else {
83 warn "can't unlink $path: $!\n";
84 return 0;
85 }
86 }
87 } [ $path, $path . '.gz' ];
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 $d_opt $d_path )\n";
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), " bytes, 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
131 sub x_open {
132 my ($file) = shift;
133 my ($mode) = shift;
134 $pending->{$file}->{open}++;
135 warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, "\n";
136 my $fh;
137 if ( $pending->{$file}->{open} == 1 ) {
138 my $path = fixup($file);
139 my $tmp = $mount->{tmp} . '/' . $file;
140 warn ">>> open abs path: $path\n";
141 if ( -e $tmp ) {
142 $path = $tmp;
143 } elsif ( $path =~ m/\.gz$/ ) {
144 my $dest_path = $tmp;
145 $dest_path =~ s!/[^/]+$!!; #!vim-fix
146 mkpath $dest_path unless -e $dest_path;
147 file_copy( '<:gzip', $path, '>', $tmp );
148 $path = $tmp;
149 }
150 return -$! unless sysopen($fh , $path, $mode);
151 $pending->{$file}->{fh} = $fh;
152 $pending->{$file}->{path} = $path;
153 } elsif ( ! defined( $pending->{$file}->{fh} ) ) {
154 confess "can't find fh for $file ", dump($pending);
155 };
156 return 0;
157 }
158
159 sub x_read {
160 my ($file,$bufsize,$off) = @_;
161 my ($rv) = -ENOSYS();
162 my $path = fixup( $file );
163 return -ENOENT() unless -e $path;
164 my ($fsize) = -s $path;
165 my $fh = $pending->{$file}->{fh} || confess "no fh? ", dump( $pending );
166 if(seek($fh,$off,SEEK_SET)) {
167 read($fh,$rv,$bufsize);
168 }
169 return $rv;
170 }
171
172 sub x_write {
173 my ($file,$buf,$off) = @_;
174 $pending->{$file}->{write}++;
175 my ($rv);
176 my $path = fixup($file);
177 return -ENOENT() unless -e $path;
178 my ($fsize) = -s $path;
179 my $fh = $pending->{$file}->{fh};
180 return -ENOSYS() unless $fh;
181 if($rv = seek( $fh ,$off,SEEK_SET)) {
182 $rv = print( $fh $buf );
183 }
184 $rv = -ENOSYS() unless $rv;
185 return length($buf);
186 }
187
188 sub err { return (-shift || -$!) }
189
190 sub x_readlink { return readlink(fixup(shift)); }
191 sub x_unlink { return unlink_all( shift ) ? 0 : -$! }
192
193 sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; }
194
195 sub x_rename {
196 my ($old) = fixup(shift);
197 my ($new) = fixup(shift);
198 my ($err) = rename($old,$new) ? 0 : -ENOENT();
199 return $err;
200 }
201 sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
202
203 sub x_chown {
204 my ($path) = fixup(shift);
205 print "nonexistent $path\n" unless -e $path;
206 my ($uid,$gid) = @_;
207 # perl's chown() does not chown symlinks, it chowns the symlink's
208 # target. it fails when the link's target doesn't exist, because
209 # the stat64() syscall fails.
210 # this causes error messages when unpacking symlinks in tarballs.
211 my ($err) = syscall(&SYS_lchown,$path,$uid,$gid,$path) ? -$! : 0;
212 return $err;
213 }
214
215 sub x_chmod {
216 my ($path) = fixup(shift);
217 my ($mode) = shift;
218 my ($err) = chmod($mode,$path) ? 0 : -$!;
219 return $err;
220 }
221
222 sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }
223 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
224
225 sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
226 sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
227
228 sub x_mknod {
229 # since this is called for ALL files, not just devices, I'll do some checks
230 # and possibly run the real mknod command.
231 my ($file, $modes, $dev) = @_;
232 $file = fixup($file);
233 $! = 0;
234 syscall(&SYS_mknod,$file,$modes,$dev);
235 return -$!;
236 }
237
238 sub x_release {
239 my ( $file, $mode ) = @_;
240 if ( ! defined( $pending->{$file} ) ) {
241 warn "release $file, NO PENDING DATA\n";
242 return 0;
243 } elsif ( ! defined( $pending->{$file}->{write} ) ) {
244 warn "release $file, not written into\n";
245 } elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) {
246 close( $pending->{$file}->{fh} ) || warn "can't close $file: $!";
247 my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending );
248 my $dest = fixup( $file );
249
250 # cleanup old compressed copy
251 if ( $dest =~ /\.gz$/ ) {
252 warn "## remove old $dest\n";
253 unlink_all( $file ) || confess "can't remove $dest: $!";
254 $dest =~ s/\.gz$//;
255 }
256
257 if ( $file =~ $skip_extensions_regex ) {
258 warn "release $file $mode -- uncompressed\n";
259 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
260 } elsif ( -s $path < $min_compress_size ) {
261 warn "release $file -- uncompressed, too small ", -s $path, " bytes\n";
262 file_copy( '<', $path, '>', $dest ) if ( $path ne $dest );
263 } else {
264 warn "release $file $mode -- compressing\n";
265
266
267 file_copy( '<', $path, '>:gzip', $dest . '.gz' );
268
269 # FIXME add timeout to remove uncompressed version?
270 unlink_all( $file ) || warn "can't remove $path: $!";
271 }
272 } else {
273 warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n";
274 $pending->{$file}->{open}--;
275 return 0;
276 }
277 delete( $pending->{$file} );
278 return 0;
279 }
280
281 # kludge
282 sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
283 Fuse::main(
284 mountpoint=>$mount->{to},
285 getattr =>"main::x_getattr",
286 readlink=>"main::x_readlink",
287 getdir =>"main::x_getdir",
288 mknod =>"main::x_mknod",
289 mkdir =>"main::x_mkdir",
290 unlink =>"main::x_unlink",
291 rmdir =>"main::x_rmdir",
292 symlink =>"main::x_symlink",
293 rename =>"main::x_rename",
294 link =>"main::x_link",
295 chmod =>"main::x_chmod",
296 chown =>"main::x_chown",
297 truncate=>"main::x_truncate",
298 utime =>"main::x_utime",
299 open =>"main::x_open",
300 read =>"main::x_read",
301 write =>"main::x_write",
302 statfs =>"main::x_statfs",
303 release =>"main::x_release",
304 # threaded=>1,
305 # debug => 1,
306 );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26