* BackupPC_trashClean now logs an error if it can't remove all the
[BackupPC.git] / lib / BackupPC / Zip / FileMember.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::Zip::FileMember
4 #
5 # DESCRIPTION
6 #
7 #   This library defines a BackupPC::Zip::FileMember class that subclass
8 #   the Archive::Zip::FileMember class.  This allows BackupPC_zipCreate
9 #   to create zip files by reading and uncomressing BackupPC's pool
10 #   files on the fly.  This avoids the need to uncompress the files
11 #   ahead of time and either store them in memory or on disk.
12 #
13 # AUTHOR
14 #   Craig Barratt  <cbarratt@users.sourceforge.net>
15 #   Based on Archive::Zip::FileMember, Copyright (c) 2000 Ned Konz.
16 #
17 # COPYRIGHT
18 #   Copyright (C) 2002  Craig Barratt
19 #
20 #   This program is free software; you can redistribute it and/or modify
21 #   it under the terms of the GNU General Public License as published by
22 #   the Free Software Foundation; either version 2 of the License, or
23 #   (at your option) any later version.
24 #
25 #   This program is distributed in the hope that it will be useful,
26 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
27 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 #   GNU General Public License for more details.
29 #
30 #   You should have received a copy of the GNU General Public License
31 #   along with this program; if not, write to the Free Software
32 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33 #
34 #========================================================================
35 #
36 # Version 2.0.0beta2, released 13 Apr 2003.
37 #
38 # See http://backuppc.sourceforge.net.
39 #
40 #========================================================================
41
42 package BackupPC::Zip::FileMember;
43 use vars qw( @ISA );
44 @ISA = qw ( Archive::Zip::FileMember );
45
46 BEGIN { use Archive::Zip qw( :CONSTANTS :ERROR_CODES :UTILITY_METHODS ) }
47
48 # Given a file name, set up for eventual writing.
49 sub newFromFileNamed    # BackupPC::Zip::FileMember
50 {
51     my $class    = shift;
52     my $fileName = shift;
53     my $newName  = shift || $fileName;
54     my $size     = shift;
55     my $compress = shift;
56     return undef unless ( stat($fileName) && -r _ && !-d _ );
57     my $self = $class->new(@_);
58     $self->fileName($newName);
59     $self->{'externalFileName'}  = $fileName;
60     $self->{'compressionMethod'} = COMPRESSION_STORED;
61     $self->{'compressedSize'} = $self->{'uncompressedSize'} = $size;
62     $self->{'fileCompressLevel'} = $compress;
63     $self->desiredCompressionMethod( ( $self->compressedSize() > 0 ) 
64             ? COMPRESSION_DEFLATED
65             : COMPRESSION_STORED );
66     $self->isTextFile( -T _ );
67     return $self;
68 }
69
70 sub rewindData          # BackupPC::Zip::FileMember
71 {
72     my $self = shift;
73
74     my $status = $self->SUPER::rewindData(@_);
75     return $status unless $status == AZ_OK;
76
77     return AZ_IO_ERROR unless $self->fh();
78     $self->fh()->rewind();
79     return AZ_OK;
80 }
81
82 sub fh                  # BackupPC::Zip::FileMember
83 {
84     my $self = shift;
85     $self->_openFile() if !defined( $self->{'bpcfh'} );
86     return $self->{'bpcfh'};
87 }
88
89 # opens my file handle from my file name
90 sub _openFile           # BackupPC::Zip::FileMember
91 {
92     my $self = shift;
93     my ( $fh ) = BackupPC::FileZIO->open($self->externalFileName(), 0,
94                                          $self->{'fileCompressLevel'});
95     if ( !defined($fh) )
96     {
97         _ioError( "Can't open", $self->externalFileName() );
98         return undef;
99     }
100     $self->{'bpcfh'} = $fh;
101     return $fh;
102 }
103
104 # Closes my file handle
105 sub _closeFile          # BackupPC::Zip::FileMember
106 {
107     my $self = shift;
108     $self->{'bpcfh'}->close() if ( defined($self->{'bpcfh'}) );
109     $self->{'bpcfh'} = undef;
110 }
111
112 # Make sure I close my file handle
113 sub endRead             # BackupPC::Zip::FileMember
114 {
115     my $self = shift;
116     $self->_closeFile();
117     return $self->SUPER::endRead(@_);
118 }
119
120 # Return bytes read. Note that first parameter is a ref to a buffer.
121 # my $data;
122 # my ($bytesRead, $status) = $self->readRawChunk( \$data, $chunkSize );
123 sub _readRawChunk       # BackupPC::Zip::FileMember
124 {
125     my ( $self, $dataRef, $chunkSize ) = @_;
126     return ( 0, AZ_OK ) unless $chunkSize;
127     my $bytesRead = $self->fh()->read( $dataRef, $chunkSize )
128             or return ( 0, _ioError("reading data") );
129     return ( $bytesRead, AZ_OK );
130 }
131
132 sub extractToFileNamed  # BackupPC::Zip::FileMember
133 {
134     die("BackupPC::Zip::FileMember::extractToFileNamed not supported\n");
135 }