1 #============================================================= -*-perl-*-
3 # BackupPC::FileZIO package
7 # This library defines a BackupPC::FileZIO class for doing
8 # compressed or normal file I/O.
11 # Craig Barratt <cbarratt@users.sourceforge.net>
14 # Copyright (C) 2001-2003 Craig Barratt
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #========================================================================
32 # Version 2.1.0beta2, released 23 May 2004.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
38 package BackupPC::FileZIO;
42 use vars qw( $CompZlibOK );
48 # For compressed files we have a to careful about running out of memory
49 # when we inflate a deflated file. For example, if a 500MB file of all
50 # zero-bytes is compressed, it will only occupy a few tens of kbytes. If
51 # we read the compressed file in decent-size chunks, a single inflate
52 # will try to allocate 500MB. Not a good idea.
54 # Instead, we compress the file in chunks of $CompMaxWrite. If a
55 # deflated chunk produces less than $CompMaxRead bytes, then we flush
56 # and continue. This adds a few bytes to the compressed output file, but
57 # only in extreme cases where the compression ratio is very close to
58 # 100%. The result is that, provided we read the compressed file in
59 # chunks of $CompMaxRead or less, the biggest inflated data will be
62 my $CompMaxRead = 131072; # 128K
63 my $CompMaxWrite = 6291456; # 6MB
66 # We maintain a write buffer for small writes for both compressed and
67 # uncompressed files. This is the size of the write buffer.
69 my $WriteBufSize = 65536;
72 eval "use Compress::Zlib;";
75 # Compress::Zlib doesn't exist. Define some dummy constant
76 # subs so that the code below doesn't barf.
79 sub Z_OK { return 0; }
80 sub Z_STREAM_END { return 1; }
90 my($class, $fileName, $write, $compLevel) = @_;
94 if ( ref(\$fileName) eq "GLOB" ) {
98 open(FH, ">", $fileName) || return;
100 open(FH, "<", $fileName) || return;
105 $compLevel = 0 if ( !$CompZlibOK );
111 compress => $compLevel,
115 $self->{deflate} = $self->myDeflateInit;
117 $self->{inflate} = $self->myInflateInit;
118 $self->{inflateStart} = 1;
135 -Level => $self->{compress},
150 my($self, $dataRef, $nRead) = @_;
153 return if ( $self->{write} );
154 return sysread($self->{fh}, $$dataRef, $nRead) if ( !$self->{compress} );
155 while ( !$self->{eof} && $nRead > length($self->{dataOut}) ) {
156 if ( !length($self->{dataIn}) ) {
157 $n = sysread($self->{fh}, $self->{dataIn}, $CompMaxRead);
158 return $n if ( $n < 0 );
159 $self->{eof} = 1 if ( $n == 0 );
161 if ( $self->{inflateStart} && $self->{dataIn} ne "" ) {
162 my $chr = substr($self->{dataIn}, 0, 1);
164 $self->{inflateStart} = 0;
165 if ( $chr eq chr(0xd6) ) {
167 # Flag 0xd6 means this is a compressed file with
168 # appended md4 block checksums for rsync. Change
169 # the first byte back to 0x78 and proceed.
171 ##print("Got 0xd6 block: normal\n");
172 substr($self->{dataIn}, 0, 1) = chr(0x78);
173 } elsif ( $chr eq chr(0xb3) ) {
175 # Flag 0xb3 means this is the start of the rsync
176 # block checksums, so consider this as EOF for
177 # the compressed file. Also seek the file so
178 # it is positioned at the 0xb3.
180 seek($self->{fh}, -length($self->{dataIn}), 1);
182 $self->{dataIn} = "";
183 ##print("Got 0xb3 block: considering eof\n");
187 # normal case: nothing to do
191 my($data, $err) = $self->{inflate}->inflate($self->{dataIn});
192 $self->{dataOut} .= $data;
193 if ( $err == Z_STREAM_END ) {
195 $self->{inflate} = $self->myInflateInit;
196 $self->{inflateStart} = 1;
197 } elsif ( $err != Z_OK ) {
202 if ( $nRead >= length($self->{dataOut}) ) {
203 $n = length($self->{dataOut});
204 $$dataRef = $self->{dataOut};
205 $self->{dataOut} = '';
208 $$dataRef = substr($self->{dataOut}, 0, $nRead);
209 $self->{dataOut} = substr($self->{dataOut}, $nRead);
215 # Provide a line-at-a-time interface. This splits and buffers the
216 # lines, you cannot mix calls to read() and readLine().
223 $self->{readLineBuf} = [] if ( !defined($self->{readLineBuf}) );
224 while ( !@{$self->{readLineBuf}} ) {
225 $self->read(\$str, $CompMaxRead);
227 $str = $self->{readLineFrag};
228 $self->{readLineFrag} = "";
231 @{$self->{readLineBuf}} = split(/\n/, $self->{readLineFrag} . $str);
232 if ( substr($str, -1, 1) ne "\n" ) {
233 $self->{readLineFrag} = pop(@{$self->{readLineBuf}});
235 $self->{readLineFrag} = "";
238 return shift(@{$self->{readLineBuf}}) . "\n";
245 return if ( $self->{write} );
246 return sysseek($self->{fh}, 0, 0) if ( !$self->{compress} );
247 $self->{dataOut} = '';
248 $self->{dataIn} = '';
250 $self->{inflate} = $self->myInflateInit;
251 $self->{inflateStart} = 1;
252 return sysseek($self->{fh}, 0, 0);
258 my($data, $force) = @_;
261 # Buffer small writes using a buffer size of up to $WriteBufSize.
263 if ( $force || length($self->{writeBuf}) + length($data) > $WriteBufSize ) {
264 if ( length($self->{writeBuf}) ) {
265 my $wrData = $self->{writeBuf} . $data;
266 return -1 if ( syswrite($self->{fh}, $wrData) != length($wrData) );
267 $self->{writeBuf} = undef;
269 return if ( length($data) == 0 );
270 return -1 if ( syswrite($self->{fh}, $data) != length($data) );
273 $self->{writeBuf} .= $data;
280 my($self, $dataRef) = @_;
281 my $n = length($$dataRef);
283 return if ( !$self->{write} );
284 print(STDERR $$dataRef) if ( $self->{writeTeeStderr} );
285 return 0 if ( $n == 0 );
286 if ( !$self->{compress} ) {
288 # If smbclient gets a read error on the client (due to a file lock)
289 # it will write a dummy file of zeros. We detect this so we can
290 # store the file efficiently as a sparse file. writeZeroCnt is
291 # the number of consecutive 0 bytes at the start of the file.
294 if ( $self->{writeZeroCnt} >= 0 && $$dataRef =~ /^(\0+)/s ) {
296 $self->{writeZeroCnt} += $skip;
297 return $n if ( $skip == $n );
300 # We now have some non-zero bytes, so time to seek to the right
301 # place and turn off zero-byte detection.
303 if ( $self->{writeZeroCnt} > 0 ) {
304 sysseek($self->{fh}, $self->{writeZeroCnt}, 0);
305 $self->{writeZeroCnt} = -1;
306 } elsif ( $self->{writeZeroCnt} == 0 ) {
307 $self->{writeZeroCnt} = -1;
309 return -1 if ( $self->writeBuffered(substr($$dataRef, $skip)) < 0 );
312 for ( my $i = 0 ; $i < $n ; $i += $CompMaxWrite ) {
313 my $dataIn = substr($$dataRef, $i, $CompMaxWrite);
314 my $dataOut = $self->{deflate}->deflate($dataIn);
315 return -1 if ( $self->writeBuffered($dataOut) < 0 );
316 $self->{deflateIn} += length($dataIn);
317 $self->{deflateOut} += length($dataOut);
318 if ( $self->{deflateIn} >= $CompMaxWrite ) {
319 if ( $self->{deflateOut} < $CompMaxRead ) {
321 # Compression is too high: to avoid huge memory requirements
322 # on read we need to flush().
324 $dataOut = $self->{deflate}->flush();
326 $self->{deflate} = $self->myDeflateInit;
327 return -1 if ( $self->writeBuffered($dataOut) < 0 );
329 $self->{deflateIn} = $self->{deflateOut} = 0;
339 return $self->{name};
344 my($self, $param) = @_;
346 $self->{writeTeeStderr} = $param if ( defined($param) );
347 return $self->{writeTeeStderr};
355 if ( $self->{write} && $self->{compress} ) {
356 my $data = $self->{deflate}->flush();
357 $err = 1 if ( $self->writeBuffered($data) < 0 );
358 } elsif ( $self->{write} && !$self->{compress} ) {
359 if ( $self->{writeZeroCnt} > 0 ) {
361 # We got a file of all zero bytes. Write a single zero byte
362 # at the end of the file. On most file systems this is an
363 # efficient way to store the file.
365 $err = 1 if ( sysseek($self->{fh}, $self->{writeZeroCnt} - 1, 0)
366 != $self->{writeZeroCnt} - 1
367 || syswrite($self->{fh}, "\0") != 1 );
370 $self->writeBuffered(undef, 1);
372 return $err ? -1 : 0;
376 # If $compress is >0, copy and compress $srcFile putting the output
377 # in $destFileZ. Otherwise, copy the file to $destFileNoZ, or do
378 # nothing if $destFileNoZ is undef. Finally, if rename is set, then
379 # the source file is removed.
383 my($class, $srcFile, $destFileZ, $destFileNoZ, $compress, $rmSrc) = @_;
384 my(@s) = stat($srcFile);
385 my $atime = $s[8] =~ /(.*)/ && $1;
386 my $mtime = $s[9] =~ /(.*)/ && $1;
387 if ( $CompZlibOK && $compress > 0 ) {
388 my $fh = BackupPC::FileZIO->open($destFileZ, 1, $compress);
390 if ( defined($fh) && open(LOG, "<", $srcFile) ) {
392 while ( sysread(LOG, $data, 65536) > 0 ) {
397 unlink($srcFile) if ( $rmSrc );
398 utime($atime, $mtime, $destFileZ);
401 $fh->close() if ( defined($fh) );
405 return 0 if ( !defined($destFileNoZ) );
407 return rename($srcFile, $destFileNoZ);
409 return 0 if ( !copy($srcFile, $destFileNoZ) );
410 utime($atime, $mtime, $destFileNoZ);