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