* Added BackupPC::Xfer::Protocol as a common class for each Xfer
[BackupPC.git] / lib / BackupPC / Xfer.pm
diff --git a/lib/BackupPC/Xfer.pm b/lib/BackupPC/Xfer.pm
new file mode 100644 (file)
index 0000000..b585e25
--- /dev/null
@@ -0,0 +1,152 @@
+#============================================================= -*-perl-*-
+#
+# BackupPC::Xfer package
+#
+# DESCRIPTION
+#
+#   This library defines a Factory for invoking transfer protocols in
+#   a polymorphic manner.  This libary allows for easier expansion of
+#   supported protocols.
+#
+# AUTHOR
+#   Paul Mantz  <pcmantz@zmanda.com>
+#
+# COPYRIGHT
+#   Copyright (C) 2008  Zmanda
+#
+#   This program is free software; you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation; either version 2 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program; if not, write to the Free Software
+#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+#========================================================================
+#
+# Version 3.1.0+
+#
+# See http://backuppc.sourceforge.net.
+#
+#========================================================================
+
+
+package BackupPC::Xfer;
+
+use strict;
+use Encode qw/from_to encode/;
+
+use BackupPC::Xfer::Archive;
+use BackupPC::Xfer::BackupPCd;
+use BackupPC::Xfer::Ftp;
+use BackupPC::Xfer::Protocol;
+use BackupPC::Xfer::Rsync;
+use BackupPC::Xfer::Smb;
+use BackupPC::Xfer::Tar;
+
+use vars qw( $errStr );
+
+sub create
+{
+    my($protocol, $bpc, $args) = @_;
+    my $xfer;
+
+    $errStr = undef;
+
+    if ( $protocol eq 'archive' ) {
+
+        $xfer = BackupPC::Xfer::Archive->new( $bpc, $args );
+        $errStr = BackupPC::Xfer::Archive::errStr() if ( !defined($xfer) );
+        return $xfer;
+
+    } elsif ( $protocol eq 'backuppcd' ) {
+
+        $xfer = BackupPC::Xfer::BackupPCd->new( $bpc, $args );
+        $errStr = BackupPC::Xfer::BackupPCd::errStr() if ( !defined($xfer) );
+        return $xfer;
+
+    } elsif ( $protocol eq 'ftp' ) {
+
+        $xfer = BackupPC::Xfer::Ftp->new( $bpc, $args );
+        $errStr = BackupPC::Xfer::Ftp::errStr() if ( !defined($xfer) );
+        return $xfer;
+
+    } elsif ( $protocol eq 'rsync' || $protocol eq 'rsyncd' ) {
+
+        $xfer = BackupPC::Xfer::Rsync->new( $bpc, $args );
+        $errStr = BackupPC::Xfer::Rsync::errStr() if ( !defined($xfer) );
+        return $xfer;
+
+    } elsif ( $protocol eq 'smb' ) {
+
+        $xfer = BackupPC::Xfer::Smb->new( $bpc, $args );
+        $errStr = BackupPC::Xfer::Smb::errStr() if ( !defined($xfer) );
+        return $xfer;
+
+    } elsif ( $protocol eq 'tar' ) {
+
+        $xfer = BackupPC::Xfer::Tar->new( $bpc, $args );
+        $errStr = BackupPC::Xfer::Tar::errStr() if ( !defined($xfer) );
+        return $xfer;
+
+    } elsif ( $protocol eq 'protocol') {
+
+        $xfer = BackupPC::Xfer::Protocol->new( $bpc, $args );
+        $errStr = BackupPC::Xfer::Protocol::errStr() if ( !defined($xfer) );
+        return $xfer;
+
+    } else {
+
+       $xfer = undef;
+        $errStr = "$protocol is not a supported protocol.";
+       return $xfer;
+    }
+}
+
+#
+# getShareNames() loads the correct shares dependent on the
+# transfer type.
+#
+sub getShareNames
+{
+    my($conf) = @_;
+    my $ShareNames;
+
+    if ( $conf->{XferMethod} eq "tar" ) {
+        $ShareNames = $conf->{TarShareName};
+
+    } elsif ( $conf->{XferMethod} eq "ftp" ) {
+        $ShareNames = $conf->{FtpShareName};
+
+    } elsif ( $conf->{XferMethod} eq "rsync" || $conf->{XferMethod} eq "rsyncd" ) {
+        $ShareNames = $conf->{RsyncShareName};
+
+    } elsif ( $conf->{XferMethod} eq "backuppcd" ) {
+        $ShareNames = $conf->{BackupPCdShareName};
+
+    } elsif ( $conf->{XferMethod} eq "smb" ) {
+        $ShareNames = $conf->{SmbShareName};
+
+    } else {
+        #
+        # default to smb shares
+        #
+        $ShareNames = $conf->{SmbShareName};
+    }
+
+    $ShareNames = [$ShareNames] unless ref($ShareNames) eq "ARRAY";
+    return $ShareNames;
+}
+
+sub errStr
+{
+    return $errStr;
+}
+
+1;