4 |
use threads::shared; |
use threads::shared; |
5 |
|
|
6 |
use Fuse; |
use Fuse; |
|
use IO::File; |
|
7 |
use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT); |
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); |
use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET); |
9 |
require 'syscall.ph'; # for SYS_mknod and SYS_lchown |
require 'syscall.ph'; # for SYS_mknod and SYS_lchown |
10 |
use PerlIO::gzip; |
use PerlIO::gzip; |
11 |
use File::Path; |
use File::Path; |
12 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
13 |
|
use Carp qw/confess/; |
14 |
|
use IO::File; |
15 |
|
use Getopt::Long; |
16 |
|
|
17 |
|
my $debug = 0; |
18 |
|
my $fuse_debug = 0; |
19 |
|
|
20 |
|
GetOptions( |
21 |
|
'debug+' => \$debug, |
22 |
|
'fuse-debug+' => \$fuse_debug, |
23 |
|
); |
24 |
|
|
25 |
my $mount = { |
my $mount = { |
26 |
from => '/tmp/comp', |
from => '/tmp/comp', |
28 |
tmp => '/dev/shm/comp', |
tmp => '/dev/shm/comp', |
29 |
}; |
}; |
30 |
|
|
31 |
my $debug = 1; |
my $debug = shift @ARGV; |
32 |
|
|
33 |
my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i; |
my $skip_extensions_regex = qr/\.(?:sw[a-z]|gif|png|jpeg|jpg|avi|rar|zip|bz2|gz|tgz|avi|mpeg|mpg|tmp|temp)$/i; |
34 |
|
|
35 |
|
# don't compress files smaller than this |
36 |
|
my $min_compress_size = 512; |
37 |
|
|
38 |
foreach my $dir ( keys %$mount ) { |
foreach my $dir ( keys %$mount ) { |
39 |
if ( ! -e $mount->{$dir} ) { |
if ( ! -e $mount->{$dir} ) { |
40 |
warn "created $mount->{$dir}\n"; |
warn "created $mount->{$dir}\n"; |
78 |
return unpack("L", $buff); |
return unpack("L", $buff); |
79 |
} |
} |
80 |
|
|
81 |
|
sub unlink_all { |
82 |
|
my $file = shift; |
83 |
|
warn "# unlink_all( $file )\n"; |
84 |
|
|
85 |
|
my $path = fixup( $file ); |
86 |
|
unlink $path || return 0; |
87 |
|
|
88 |
|
my $tmp = $mount->{tmp} . '/' . $file; |
89 |
|
unlink $tmp if ( -e $tmp ); |
90 |
|
|
91 |
|
delete( $pending->{$file} ); |
92 |
|
return 1; |
93 |
|
} |
94 |
|
|
95 |
sub x_getattr { |
sub x_getattr { |
96 |
my ($file) = fixup(shift); |
my ($file) = fixup(shift); |
97 |
my (@list) = lstat($file); |
my (@list) = lstat($file); |
112 |
|
|
113 |
sub file_copy { |
sub file_copy { |
114 |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
115 |
warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n"; |
warn "## file_copy( $s_opt $s_path [",-s $s_path,"] $d_opt $d_path [",-e $d_path ? -s $d_path : 'new',"])\n" if $debug; |
116 |
open(my $s, $s_opt, $s_path ) || die "can't open $s_path: $!"; |
open(my $s, $s_opt, $s_path ) || confess "can't open $s_path: $!\npending = ", dump( $pending ); |
117 |
open(my $d, $d_opt, $d_path ) || die "can't open $d_path: $!"; |
open(my $d, $d_opt, $d_path ) || confess "can't open $d_path: $!"; |
118 |
my $buff; |
my $buff; |
119 |
while( read( $s, $buff, 65535 ) ) { |
while( read( $s, $buff, 65535 ) ) { |
120 |
print $d $buff || die "can't write into $d_path: $!"; |
print $d $buff || confess "can't write into $d_path: $!"; |
121 |
warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug; |
warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug; |
122 |
} |
} |
123 |
close($d) || warn "can't close $d_path: $!"; |
close($d) || warn "can't close $d_path: $!"; |
124 |
close($s) || warn "can't close $s_path: $!"; |
close($s) || warn "can't close $s_path: $!"; |
125 |
warn "-- $s_path [", -s $s_path, "]\n >>> $d_path [", -s $d_path, "]\n" if $debug; |
warn "-- $s_path [", -s $s_path, "] >>> $d_path [", -s $d_path, "]\n" if $debug; |
126 |
|
my ($mode,$uid,$gid,$atime,$mtime) = (stat $s_path)[2,4,5,8,9]; |
127 |
|
|
128 |
|
chmod $mode, $d_path || warn "chmod( $mode $d_path ) failed: $!\n"; |
129 |
|
chown $uid,$gid,$d_path || warn "chown( $uid $gid $d_path ) failed: $!\n"; |
130 |
|
utime $atime,$mtime,$d_path || warn "utime( $atime $mtime $d_path ) failed: $!\n"; |
131 |
|
|
132 |
|
undef $d; |
133 |
|
undef $s; |
134 |
} |
} |
135 |
|
|
136 |
sub x_open { |
sub x_open { |
137 |
my ($file) = shift; |
my ($file) = shift; |
138 |
my ($mode) = shift; |
my ($mode) = shift; |
139 |
|
|
140 |
|
if ( $file eq '/.debug' ) { |
141 |
|
my $path = $mount->{from} . '/.debug'; |
142 |
|
open( my $debug, '>', $path ) || die "can't open $path: $!"; |
143 |
|
my $dump = dump( $pending ); |
144 |
|
print $debug "pending = $dump\n"; |
145 |
|
close($debug); |
146 |
|
$pending->{'/.debug'}->{path} = $path; |
147 |
|
warn "## created dump $path $dump\n"; |
148 |
|
return 0; |
149 |
|
} |
150 |
|
|
151 |
$pending->{$file}->{open}++; |
$pending->{$file}->{open}++; |
152 |
|
|
153 |
|
my $mode_desc = { |
154 |
|
rdonly => $mode && O_RDONLY, |
155 |
|
rdwr => $mode && O_RDWR, |
156 |
|
append => $mode && O_APPEND, |
157 |
|
create => $mode && O_CREAT, |
158 |
|
trunc => $mode && O_TRUNC, |
159 |
|
}; |
160 |
|
my $path = fixup($file); |
161 |
|
warn "## open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n" if $debug; |
162 |
my $fh; |
my $fh; |
163 |
if ( $pending->{$file}->{open} == 1 ) { |
|
164 |
warn "# open( $file, $mode )\n"; |
my $tmp = $mount->{tmp} . '/' . $file; |
165 |
my $path = fixup($file); |
if ( -e $tmp ) { |
166 |
my $tmp = $mount->{tmp} . '/' . $file; |
$path = $tmp; |
167 |
if ( -e $tmp ) { |
} elsif ( $path =~ m/\.gz$/ ) { |
168 |
$path = $tmp; |
my $dest_path = $tmp; |
169 |
} elsif ( $path =~ m/\.gz$/ ) { |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
170 |
my $dest_path = $tmp; |
mkpath $dest_path unless -e $dest_path; |
171 |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
if ( -s $path ) { |
172 |
mkpath $dest_path unless -e $dest_path; |
file_copy( '<:gzip', $path, '>', $tmp ) |
173 |
file_copy( '<:gzip', $path, '>', $tmp ); |
} else { |
174 |
$path = $tmp; |
warn "ERROR: filesystem corruption, $path is zero size\n"; |
175 |
} |
} |
176 |
return -$! unless sysopen($fh , $path, $mode); |
$path = $tmp; |
177 |
$pending->{$file}->{fh} = $fh; |
} |
178 |
|
|
179 |
|
if ( sysopen($fh , $path, $mode) ) { |
180 |
|
close($fh) || confess "can't close $path: $!"; |
181 |
|
warn "<<< open $path [", -e $path ? -s $path : 'new' , "]\n"; |
182 |
$pending->{$file}->{path} = $path; |
$pending->{$file}->{path} = $path; |
183 |
} elsif ( ! defined( $pending->{$file}->{fh} ) ) { |
return 0; |
184 |
die "can't find fh for $file ", dump($pending); |
} else { |
185 |
|
warn "ERROR: can't open $path : $!"; |
186 |
|
return -$!; |
187 |
} |
} |
188 |
return 0; |
|
189 |
} |
} |
190 |
|
|
191 |
sub x_read { |
sub x_read { |
192 |
my ($file,$bufsize,$off) = @_; |
my ($file,$bufsize,$off) = @_; |
193 |
my ($rv) = -ENOSYS(); |
my ($rv) = -ENOSYS(); |
194 |
my $path = fixup( $file ); |
my $path = fixup( $file ); |
195 |
|
|
196 |
|
confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} ); |
197 |
|
|
198 |
return -ENOENT() unless -e $path; |
return -ENOENT() unless -e $path; |
199 |
my ($fsize) = -s $path; |
|
200 |
my $fh = $pending->{$file}->{fh} || die "no fh? ", dump( $pending ); |
my $fh = new IO::File; |
201 |
|
return -ENOSYS() unless open($fh,$pending->{$file}->{path}); |
202 |
|
|
203 |
if(seek($fh,$off,SEEK_SET)) { |
if(seek($fh,$off,SEEK_SET)) { |
204 |
read($fh,$rv,$bufsize); |
read($fh,$rv,$bufsize); |
205 |
} |
} |
206 |
|
|
207 |
return $rv; |
return $rv; |
208 |
} |
} |
209 |
|
|
210 |
sub x_write { |
sub x_write { |
211 |
my ($file,$buf,$off) = @_; |
my ($file,$buf,$off) = @_; |
212 |
$pending->{$file}->{write}++; |
$pending->{$file}->{write}++; |
213 |
my ($rv); |
my $rv; |
214 |
my $path = fixup($file); |
my $path = fixup($file); |
215 |
|
|
216 |
|
confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} ); |
217 |
|
|
218 |
return -ENOENT() unless -e $path; |
return -ENOENT() unless -e $path; |
219 |
my ($fsize) = -s $path; |
|
220 |
my $fh = $pending->{$file}->{fh}; |
my $fh = new IO::File; |
221 |
return -ENOSYS() unless $fh; |
return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path}); |
222 |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
223 |
$rv = print( $fh $buf ); |
$rv = print( $fh $buf ); |
224 |
|
warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; |
225 |
} |
} |
226 |
$rv = -ENOSYS() unless $rv; |
$rv = -ENOSYS() unless $rv; |
227 |
|
close($fh); |
228 |
return length($buf); |
return length($buf); |
229 |
} |
} |
230 |
|
|
231 |
sub err { return (-shift || -$!) } |
sub err { return (-shift || -$!) } |
232 |
|
|
233 |
sub x_readlink { return readlink(fixup(shift)); } |
sub x_readlink { return readlink(fixup(shift)); } |
234 |
sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; } |
sub x_unlink { return unlink_all( shift ) ? 0 : -$! } |
235 |
|
|
236 |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
237 |
|
|
238 |
sub x_rename { |
sub x_rename { |
239 |
my ($old) = fixup(shift); |
my ($old,$new) = @_; |
240 |
my ($new) = fixup(shift); |
my $old_path = fixup($old); |
241 |
my ($err) = rename($old,$new) ? 0 : -ENOENT(); |
my $new_path = fixup($new); |
242 |
|
$new_path .= '.gz' if ( $old_path =~ m/\.gz$/ && $new_path !~ m/\.gz$/ ); |
243 |
|
|
244 |
|
my $err = rename($old_path,$new_path) ? 0 : -ENOENT(); |
245 |
|
warn "## rename( $old_path => $new_path ) = $err\n"; |
246 |
|
|
247 |
|
my $tmp = $mount->{tmp} . '/' . $old; |
248 |
|
if ( -e $tmp ) { |
249 |
|
my $new_tmp = $mount->{tmp} . '/' . $new; |
250 |
|
rename $tmp, $new_tmp || confess "can't rename $tmp -> $new_tmp : $!"; |
251 |
|
} |
252 |
|
|
253 |
|
if (defined( $pending->{$old} )) { |
254 |
|
$pending->{$new} = $pending->{$old}; |
255 |
|
|
256 |
|
my $path = $pending->{$old}->{path}; |
257 |
|
$path =~ s/\Q$old\E/$new/; |
258 |
|
$pending->{$new}->{path} = $path; |
259 |
|
$pending->{$old}->{path} = $path; |
260 |
|
|
261 |
|
#delete( $pending->{$old} ); |
262 |
|
} |
263 |
|
|
264 |
return $err; |
return $err; |
265 |
} |
} |
266 |
sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } |
sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } |
284 |
return $err; |
return $err; |
285 |
} |
} |
286 |
|
|
287 |
sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; } |
sub x_truncate { |
288 |
|
my ( $file,$size ) = @_; |
289 |
|
my $path = fixup($file); |
290 |
|
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
291 |
|
if ( $path =~ m/\.gz$/ ) { |
292 |
|
my $no_gz = $path; |
293 |
|
$no_gz =~ s/\.gz$//; |
294 |
|
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
295 |
|
} |
296 |
|
warn "## truncate( $file $size ) $path [", -s $path, "] = $rv\n" if $debug; |
297 |
|
$pending->{$file}->{write}++; |
298 |
|
return $rv; |
299 |
|
} |
300 |
sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } |
sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } |
301 |
|
|
302 |
sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; } |
sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; } |
320 |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
321 |
warn "release $file, not written into\n"; |
warn "release $file, not written into\n"; |
322 |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
323 |
close( $pending->{$file}->{fh} ) || warn "can't close $file: $!"; |
my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending ); |
324 |
|
my $dest = fixup( $file ); |
325 |
|
|
326 |
|
# cleanup old compressed copy |
327 |
|
if ( $dest =~ /\.gz$/ ) { |
328 |
|
warn "## remove old $dest\n"; |
329 |
|
unlink $dest || confess "can't remove $dest: $!"; |
330 |
|
$dest =~ s/\.gz$//; |
331 |
|
} |
332 |
|
|
333 |
if ( $file =~ $skip_extensions_regex ) { |
if ( $file =~ $skip_extensions_regex ) { |
334 |
warn "release $file $mode -- uncompressed\n"; |
warn "release $file [",-s $path,"] skipped compression\n"; |
335 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
336 |
|
} elsif ( -s $path < $min_compress_size ) { |
337 |
|
warn "release $file [",-s $path,"] uncompressed, too small\n"; |
338 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
339 |
} else { |
} else { |
340 |
warn "release $file $mode -- compressing\n"; |
warn "release $file [",-s $path,"] compressing\n"; |
|
|
|
|
my $path = $pending->{$file}->{path} || die "no path for $file ? ", dump( $pending ); |
|
|
my $dest = fixup( $file ); |
|
341 |
|
|
342 |
if ( $dest =~ /\.gz$/ ) { |
my $comp = $dest . '.gz'; |
343 |
warn "## remove old $dest\n"; |
file_copy( '<', $path, '>:gzip', $comp ); |
|
unlink $dest || die "can't remove $dest: $!"; |
|
|
$dest =~ s/\.gz$//; |
|
|
} |
|
344 |
|
|
345 |
file_copy( '<', $path, '>:gzip', $dest . '.gz' ); |
my ( $size_path, $size_comp ) = ( -s $path, -s $comp ); |
346 |
|
|
347 |
# FIXME add timeout to remove uncompressed version? |
if ( $size_path <= $size_comp ) { |
348 |
unlink $path || warn "can't remove $path: $!"; |
warn ">>> $size_path <= $size_comp leaving uncompressed\n"; |
349 |
|
unlink $comp || warn "can't reamove: $comp: $!"; |
350 |
|
} else { |
351 |
|
warn ">>> compressed $size_path -> $size_comp ",int(($size_comp * 100) / $size_path),"%\n"; |
352 |
|
# FIXME add timeout to remove uncompressed version? |
353 |
|
unlink $path || warn "can't remove $path: $!"; |
354 |
|
} |
355 |
} |
} |
356 |
} else { |
} else { |
357 |
warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; |
warn "release $file, but still used ", $pending->{$file}->{open} , " times, delaying compression\n"; |
|
$pending->{$file}->{open}--; |
|
|
return 0; |
|
358 |
} |
} |
359 |
delete( $pending->{$file} ); |
$pending->{$file}->{open}--; |
360 |
|
if ( $pending->{$file}->{open} == 0 ) { |
361 |
|
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
362 |
|
delete( $pending->{$file} ); |
363 |
|
} |
364 |
return 0; |
return 0; |
365 |
} |
} |
366 |
|
|
388 |
statfs =>"main::x_statfs", |
statfs =>"main::x_statfs", |
389 |
release =>"main::x_release", |
release =>"main::x_release", |
390 |
# threaded=>1, |
# threaded=>1, |
391 |
# debug => 1, |
debug => $fuse_debug, |
392 |
); |
); |