11 |
use PerlIO::gzip; |
use PerlIO::gzip; |
12 |
use File::Path; |
use File::Path; |
13 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
14 |
|
use Carp qw/confess/; |
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 = 256; |
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 |
|
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 { |
sub x_getattr { |
94 |
my ($file) = fixup(shift); |
my ($file) = fixup(shift); |
95 |
my (@list) = lstat($file); |
my (@list) = lstat($file); |
111 |
sub file_copy { |
sub file_copy { |
112 |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
my ( $s_opt, $s_path, $d_opt, $d_path ) = @_; |
113 |
warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n"; |
warn "## file_copy( $s_opt $s_path $d_opt $d_path )\n"; |
114 |
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 ); |
115 |
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: $!"; |
116 |
my $buff; |
my $buff; |
117 |
while( read( $s, $buff, 65535 ) ) { |
while( read( $s, $buff, 65535 ) ) { |
118 |
print $d $buff || die "can't write into $d_path: $!"; |
print $d $buff || confess "can't write into $d_path: $!"; |
119 |
warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug; |
warn ">> ", length($buff), " bytes, offset ", tell($s), " -> ", tell($d), "\n" if $debug; |
120 |
} |
} |
121 |
close($d) || warn "can't close $d_path: $!"; |
close($d) || warn "can't close $d_path: $!"; |
122 |
close($s) || warn "can't close $s_path: $!"; |
close($s) || warn "can't close $s_path: $!"; |
123 |
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; |
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 { |
sub x_open { |
132 |
my ($file) = shift; |
my ($file) = shift; |
133 |
my ($mode) = shift; |
my ($mode) = shift; |
134 |
$pending->{$file}->{open}++; |
$pending->{$file}->{open}++; |
135 |
|
warn "# open( $file, $mode ) pending: ", $pending->{$file}->{open}, "\n"; |
136 |
my $fh; |
my $fh; |
137 |
if ( $pending->{$file}->{open} == 1 ) { |
if ( $pending->{$file}->{open} == 1 ) { |
|
warn "# open( $file, $mode )\n"; |
|
138 |
my $path = fixup($file); |
my $path = fixup($file); |
139 |
my $tmp = $mount->{tmp} . '/' . $file; |
my $tmp = $mount->{tmp} . '/' . $file; |
140 |
|
warn ">>> open abs path: $path\n"; |
141 |
if ( -e $tmp ) { |
if ( -e $tmp ) { |
142 |
$path = $tmp; |
$path = $tmp; |
143 |
} elsif ( $path =~ m/\.gz$/ ) { |
} elsif ( $path =~ m/\.gz$/ ) { |
151 |
$pending->{$file}->{fh} = $fh; |
$pending->{$file}->{fh} = $fh; |
152 |
$pending->{$file}->{path} = $path; |
$pending->{$file}->{path} = $path; |
153 |
} elsif ( ! defined( $pending->{$file}->{fh} ) ) { |
} elsif ( ! defined( $pending->{$file}->{fh} ) ) { |
154 |
die "can't find fh for $file ", dump($pending); |
confess "can't find fh for $file ", dump($pending); |
155 |
} |
}; |
156 |
return 0; |
return 0; |
157 |
} |
} |
158 |
|
|
162 |
my $path = fixup( $file ); |
my $path = fixup( $file ); |
163 |
return -ENOENT() unless -e $path; |
return -ENOENT() unless -e $path; |
164 |
my ($fsize) = -s $path; |
my ($fsize) = -s $path; |
165 |
my $fh = $pending->{$file}->{fh} || die "no fh? ", dump( $pending ); |
my $fh = $pending->{$file}->{fh} || confess "no fh? ", dump( $pending ); |
166 |
if(seek($fh,$off,SEEK_SET)) { |
if(seek($fh,$off,SEEK_SET)) { |
167 |
read($fh,$rv,$bufsize); |
read($fh,$rv,$bufsize); |
168 |
} |
} |
188 |
sub err { return (-shift || -$!) } |
sub err { return (-shift || -$!) } |
189 |
|
|
190 |
sub x_readlink { return readlink(fixup(shift)); } |
sub x_readlink { return readlink(fixup(shift)); } |
191 |
sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; } |
sub x_unlink { return unlink_all( shift ) ? 0 : -$! } |
192 |
|
|
193 |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
sub x_symlink { return symlink(shift,fixup(shift)) ? 0 : -$!; } |
194 |
|
|
244 |
warn "release $file, not written into\n"; |
warn "release $file, not written into\n"; |
245 |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
} elsif ( defined( $pending->{$file}->{open} ) && $pending->{$file}->{open} == 1 ) { |
246 |
close( $pending->{$file}->{fh} ) || warn "can't close $file: $!"; |
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 ) { |
if ( $file =~ $skip_extensions_regex ) { |
258 |
warn "release $file $mode -- uncompressed\n"; |
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 { |
} else { |
264 |
warn "release $file $mode -- compressing\n"; |
warn "release $file $mode -- compressing\n"; |
265 |
|
|
|
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$//; |
|
|
} |
|
266 |
|
|
267 |
file_copy( '<', $path, '>:gzip', $dest . '.gz' ); |
file_copy( '<', $path, '>:gzip', $dest . '.gz' ); |
268 |
|
|
269 |
# FIXME add timeout to remove uncompressed version? |
# FIXME add timeout to remove uncompressed version? |
270 |
unlink $path || warn "can't remove $path: $!"; |
unlink_all( $file ) || warn "can't remove $path: $!"; |
271 |
} |
} |
272 |
} else { |
} else { |
273 |
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"; |