* Added BackupPC::Xfer::Protocol as a common class for each Xfer
[BackupPC.git] / lib / BackupPC / Xfer.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::Xfer package
4 #
5 # DESCRIPTION
6 #
7 #   This library defines a Factory for invoking transfer protocols in
8 #   a polymorphic manner.  This libary allows for easier expansion of
9 #   supported protocols.
10 #
11 # AUTHOR
12 #   Paul Mantz  <pcmantz@zmanda.com>
13 #
14 # COPYRIGHT
15 #   Copyright (C) 2008  Zmanda
16 #
17 #   This program is free software; you can redistribute it and/or modify
18 #   it under the terms of the GNU General Public License as published by
19 #   the Free Software Foundation; either version 2 of the License, or
20 #   (at your option) any later version.
21 #
22 #   This program is distributed in the hope that it will be useful,
23 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
24 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 #   GNU General Public License for more details.
26 #
27 #   You should have received a copy of the GNU General Public License
28 #   along with this program; if not, write to the Free Software
29 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30 #
31 #========================================================================
32 #
33 # Version 3.1.0+
34 #
35 # See http://backuppc.sourceforge.net.
36 #
37 #========================================================================
38
39
40 package BackupPC::Xfer;
41
42 use strict;
43 use Encode qw/from_to encode/;
44
45 use BackupPC::Xfer::Archive;
46 use BackupPC::Xfer::BackupPCd;
47 use BackupPC::Xfer::Ftp;
48 use BackupPC::Xfer::Protocol;
49 use BackupPC::Xfer::Rsync;
50 use BackupPC::Xfer::Smb;
51 use BackupPC::Xfer::Tar;
52
53 use vars qw( $errStr );
54
55 sub create
56 {
57     my($protocol, $bpc, $args) = @_;
58     my $xfer;
59
60     $errStr = undef;
61
62     if ( $protocol eq 'archive' ) {
63
64         $xfer = BackupPC::Xfer::Archive->new( $bpc, $args );
65         $errStr = BackupPC::Xfer::Archive::errStr() if ( !defined($xfer) );
66         return $xfer;
67
68     } elsif ( $protocol eq 'backuppcd' ) {
69
70         $xfer = BackupPC::Xfer::BackupPCd->new( $bpc, $args );
71         $errStr = BackupPC::Xfer::BackupPCd::errStr() if ( !defined($xfer) );
72         return $xfer;
73
74     } elsif ( $protocol eq 'ftp' ) {
75
76         $xfer = BackupPC::Xfer::Ftp->new( $bpc, $args );
77         $errStr = BackupPC::Xfer::Ftp::errStr() if ( !defined($xfer) );
78         return $xfer;
79
80     } elsif ( $protocol eq 'rsync' || $protocol eq 'rsyncd' ) {
81
82         $xfer = BackupPC::Xfer::Rsync->new( $bpc, $args );
83         $errStr = BackupPC::Xfer::Rsync::errStr() if ( !defined($xfer) );
84         return $xfer;
85
86     } elsif ( $protocol eq 'smb' ) {
87
88         $xfer = BackupPC::Xfer::Smb->new( $bpc, $args );
89         $errStr = BackupPC::Xfer::Smb::errStr() if ( !defined($xfer) );
90         return $xfer;
91
92     } elsif ( $protocol eq 'tar' ) {
93
94         $xfer = BackupPC::Xfer::Tar->new( $bpc, $args );
95         $errStr = BackupPC::Xfer::Tar::errStr() if ( !defined($xfer) );
96         return $xfer;
97
98     } elsif ( $protocol eq 'protocol') {
99
100         $xfer = BackupPC::Xfer::Protocol->new( $bpc, $args );
101         $errStr = BackupPC::Xfer::Protocol::errStr() if ( !defined($xfer) );
102         return $xfer;
103
104     } else {
105
106         $xfer = undef;
107         $errStr = "$protocol is not a supported protocol.";
108         return $xfer;
109     }
110 }
111
112 #
113 # getShareNames() loads the correct shares dependent on the
114 # transfer type.
115 #
116 sub getShareNames
117 {
118     my($conf) = @_;
119     my $ShareNames;
120
121     if ( $conf->{XferMethod} eq "tar" ) {
122         $ShareNames = $conf->{TarShareName};
123
124     } elsif ( $conf->{XferMethod} eq "ftp" ) {
125         $ShareNames = $conf->{FtpShareName};
126
127     } elsif ( $conf->{XferMethod} eq "rsync" || $conf->{XferMethod} eq "rsyncd" ) {
128         $ShareNames = $conf->{RsyncShareName};
129
130     } elsif ( $conf->{XferMethod} eq "backuppcd" ) {
131         $ShareNames = $conf->{BackupPCdShareName};
132
133     } elsif ( $conf->{XferMethod} eq "smb" ) {
134         $ShareNames = $conf->{SmbShareName};
135
136     } else {
137         #
138         # default to smb shares
139         #
140         $ShareNames = $conf->{SmbShareName};
141     }
142
143     $ShareNames = [$ShareNames] unless ref($ShareNames) eq "ARRAY";
144     return $ShareNames;
145 }
146
147 sub errStr
148 {
149     return $errStr;
150 }
151
152 1;