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 |
|
|
16 |
my $mount = { |
my $mount = { |
17 |
from => '/tmp/comp', |
from => '/tmp/comp', |
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; |
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 ) { |
foreach my $dir ( keys %$mount ) { |
30 |
if ( ! -e $mount->{$dir} ) { |
if ( ! -e $mount->{$dir} ) { |
31 |
warn "created $mount->{$dir}\n"; |
warn "created $mount->{$dir}\n"; |
69 |
return unpack("L", $buff); |
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 { |
sub x_getattr { |
87 |
my ($file) = fixup(shift); |
my ($file) = fixup(shift); |
88 |
my (@list) = lstat($file); |
my (@list) = lstat($file); |
103 |
|
|
104 |
sub file_copy { |
sub file_copy { |
105 |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
106 |
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"; |
107 |
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 ); |
108 |
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: $!"; |
109 |
my $buff; |
my $buff; |
110 |
while( read( $s, $buff, 65535 ) ) { |
while( read( $s, $buff, 65535 ) ) { |
111 |
print $d $buff || die "can't write into $d_path: $!"; |
print $d $buff || confess "can't write into $d_path: $!"; |
112 |
warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug; |
warn ">> [", length($buff), "] offset ", tell($s), " -> ", tell($d), "\n" if $debug; |
113 |
} |
} |
114 |
close($d) || warn "can't close $d_path: $!"; |
close($d) || warn "can't close $d_path: $!"; |
115 |
close($s) || warn "can't close $s_path: $!"; |
close($s) || warn "can't close $s_path: $!"; |
116 |
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; |
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 { |
sub x_open { |
128 |
my ($file) = shift; |
my ($file) = shift; |
129 |
my ($mode) = shift; |
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}++; |
$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 |
|
trunc => $mode && O_TRUNC, |
150 |
|
}; |
151 |
|
my $path = fixup($file); |
152 |
|
warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, " mode $mode: ", dump( $mode_desc )," $path [", -s $path, "]\n"; |
153 |
my $fh; |
my $fh; |
154 |
if ( $pending->{$file}->{open} == 1 ) { |
|
155 |
warn "# open( $file, $mode )\n"; |
my $tmp = $mount->{tmp} . '/' . $file; |
156 |
my $path = fixup($file); |
if ( -e $tmp ) { |
157 |
my $tmp = $mount->{tmp} . '/' . $file; |
$path = $tmp; |
158 |
if ( -e $tmp ) { |
} elsif ( $path =~ m/\.gz$/ ) { |
159 |
$path = $tmp; |
my $dest_path = $tmp; |
160 |
} elsif ( $path =~ m/\.gz$/ ) { |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
161 |
my $dest_path = $tmp; |
mkpath $dest_path unless -e $dest_path; |
162 |
$dest_path =~ s!/[^/]+$!!; #!vim-fix |
file_copy( '<:gzip', $path, '>', $tmp ); |
163 |
mkpath $dest_path unless -e $dest_path; |
$path = $tmp; |
|
file_copy( '<:gzip', $path, '>', $tmp ); |
|
|
$path = $tmp; |
|
|
} |
|
|
return -$! unless sysopen($fh , $path, $mode); |
|
|
$pending->{$file}->{fh} = $fh; |
|
|
$pending->{$file}->{path} = $path; |
|
|
} elsif ( ! defined( $pending->{$file}->{fh} ) ) { |
|
|
die "can't find fh for $file ", dump($pending); |
|
164 |
} |
} |
165 |
|
warn ">>> open abs path: $path [", -e $path ? -s $path : 'new' , "]\n"; |
166 |
|
return -$! unless sysopen($fh , $path, $mode); |
167 |
|
warn ">>> after open [",-s $path, "]\n"; |
168 |
|
close($fh); |
169 |
|
|
170 |
|
$pending->{$file}->{path} = $path; |
171 |
return 0; |
return 0; |
172 |
} |
} |
173 |
|
|
175 |
my ($file,$bufsize,$off) = @_; |
my ($file,$bufsize,$off) = @_; |
176 |
my ($rv) = -ENOSYS(); |
my ($rv) = -ENOSYS(); |
177 |
my $path = fixup( $file ); |
my $path = fixup( $file ); |
178 |
|
|
179 |
|
confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} ); |
180 |
|
|
181 |
return -ENOENT() unless -e $path; |
return -ENOENT() unless -e $path; |
182 |
my ($fsize) = -s $path; |
|
183 |
my $fh = $pending->{$file}->{fh} || die "no fh? ", dump( $pending ); |
my $fh = new IO::File; |
184 |
|
return -ENOSYS() unless open($fh,$pending->{$file}->{path}); |
185 |
|
|
186 |
if(seek($fh,$off,SEEK_SET)) { |
if(seek($fh,$off,SEEK_SET)) { |
187 |
read($fh,$rv,$bufsize); |
read($fh,$rv,$bufsize); |
188 |
} |
} |
189 |
|
|
190 |
return $rv; |
return $rv; |
191 |
} |
} |
192 |
|
|
193 |
sub x_write { |
sub x_write { |
194 |
my ($file,$buf,$off) = @_; |
my ($file,$buf,$off) = @_; |
195 |
$pending->{$file}->{write}++; |
$pending->{$file}->{write}++; |
196 |
my ($rv); |
my $rv; |
197 |
my $path = fixup($file); |
my $path = fixup($file); |
198 |
|
|
199 |
|
confess "no pending file $file ", dump( $pending ) unless defined( $pending->{$file} ); |
200 |
|
|
201 |
return -ENOENT() unless -e $path; |
return -ENOENT() unless -e $path; |
202 |
my ($fsize) = -s $path; |
|
203 |
my $fh = $pending->{$file}->{fh}; |
my $fh = new IO::File; |
204 |
return -ENOSYS() unless $fh; |
return -ENOSYS() unless open($fh,'+<',$pending->{$file}->{path}); |
205 |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
if($rv = seek( $fh ,$off,SEEK_SET)) { |
206 |
$rv = print( $fh $buf ); |
$rv = print( $fh $buf ); |
207 |
|
warn "## write ", $pending->{$file}->{path}, " $off ",length( $buf ), "\n" if $debug; |
208 |
} |
} |
209 |
$rv = -ENOSYS() unless $rv; |
$rv = -ENOSYS() unless $rv; |
210 |
|
close($fh); |
211 |
return length($buf); |
return length($buf); |
212 |
} |
} |
213 |
|
|
214 |
sub err { return (-shift || -$!) } |
sub err { return (-shift || -$!) } |
215 |
|
|
216 |
sub x_readlink { return readlink(fixup(shift)); } |
sub x_readlink { return readlink(fixup(shift)); } |
217 |
sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; } |
sub x_unlink { return unlink_all( shift ) ? 0 : -$! } |
218 |
|
|
219 |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
220 |
|
|
245 |
return $err; |
return $err; |
246 |
} |
} |
247 |
|
|
248 |
sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; } |
sub x_truncate { |
249 |
|
my ( $file,$size ) = @_; |
250 |
|
my $path = fixup($file); |
251 |
|
my $rv = truncate( $path, $size ) ? 0 : -$! ; |
252 |
|
if ( $path =~ m/\.gz$/ ) { |
253 |
|
my $no_gz = $path; |
254 |
|
$no_gz =~ s/\.gz$//; |
255 |
|
rename $path, $no_gz || confess "can't rename $path -> $no_gz: $!"; |
256 |
|
} |
257 |
|
warn "## truncate( $file $size ) $path [", -s $path, "]\n"; |
258 |
|
$pending->{$file}->{write}++; |
259 |
|
return $rv; |
260 |
|
} |
261 |
sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } |
sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } |
262 |
|
|
263 |
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 -$!; } |
281 |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
} elsif ( ! defined( $pending->{$file}->{write} ) ) { |
282 |
warn "release $file, not written into\n"; |
warn "release $file, not written into\n"; |
283 |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
284 |
close( $pending->{$file}->{fh} ) || warn "can't close $file: $!"; |
my $path = $pending->{$file}->{path} || confess "no path for $file ? ", dump( $pending ); |
285 |
|
my $dest = fixup( $file ); |
286 |
|
|
287 |
|
# cleanup old compressed copy |
288 |
|
if ( $dest =~ /\.gz$/ ) { |
289 |
|
warn "## remove old $dest\n"; |
290 |
|
unlink $dest || confess "can't remove $dest: $!"; |
291 |
|
$dest =~ s/\.gz$//; |
292 |
|
} |
293 |
|
|
294 |
if ( $file =~ $skip_extensions_regex ) { |
if ( $file =~ $skip_extensions_regex ) { |
295 |
warn "release $file $mode -- uncompressed\n"; |
warn "release $file $mode -- uncompressed\n"; |
296 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
297 |
|
} elsif ( -s $path < $min_compress_size ) { |
298 |
|
warn "release $file -- uncompressed, too small [", -s $path, "]\n"; |
299 |
|
file_copy( '<', $path, '>', $dest ) if ( $path ne $dest ); |
300 |
} else { |
} else { |
301 |
warn "release $file $mode -- compressing\n"; |
warn "release $file $mode -- compressing\n"; |
302 |
|
|
|
my $path = $pending->{$file}->{path} || die "no path for $file ? ", dump( $pending ); |
|
|
my $dest = fixup( $file ); |
|
|
|
|
|
if ( $dest =~ /\.gz$/ ) { |
|
|
warn "## remove old $dest\n"; |
|
|
unlink $dest || die "can't remove $dest: $!"; |
|
|
$dest =~ s/\.gz$//; |
|
|
} |
|
303 |
|
|
304 |
file_copy( '<', $path, '>:gzip', $dest . '.gz' ); |
file_copy( '<', $path, '>:gzip', $dest . '.gz' ); |
305 |
|
|
306 |
# FIXME add timeout to remove uncompressed version? |
# FIXME add timeout to remove uncompressed version? |
307 |
|
# FIXME leave uncompressed file if smaller than compressed |
308 |
unlink $path || warn "can't remove $path: $!"; |
unlink $path || warn "can't remove $path: $!"; |
309 |
} |
} |
310 |
} else { |
} else { |
311 |
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; |
|
312 |
} |
} |
313 |
delete( $pending->{$file} ); |
$pending->{$file}->{open}--; |
314 |
|
if ( $pending->{$file}->{open} == 0 ) { |
315 |
|
warn "## cleanup pending $file [", -s fixup($file), "]\n" if $debug; |
316 |
|
delete( $pending->{$file} ); |
317 |
|
} |
318 |
return 0; |
return 0; |
319 |
} |
} |
320 |
|
|