backup.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       backup.cc
00003 ///             Special parser class to support creation of Barry Backup files
00004 ///
00005 
00006 /*
00007     Copyright (C) 2010-2011, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include "backup.h"
00023 #include "tarfile.h"
00024 #include "error.h"
00025 #include <sstream>
00026 #include <iomanip>
00027 #include <iostream>
00028 
00029 namespace Barry {
00030 
00031 Backup::Backup(const std::string &tarpath)
00032 {
00033         try {
00034                 m_tar.reset( new reuse::TarFile(tarpath.c_str(), true,
00035                                 &reuse::gztar_ops_nonthread, true) );
00036         }
00037         catch( reuse::TarFile::TarError &te ) {
00038                 throw Barry::BackupError(te.what());
00039         }
00040 }
00041 
00042 Backup::~Backup()
00043 {
00044         try {
00045                 Close();
00046         }
00047         catch( Barry::BackupError & ) {
00048                 // throw it away
00049         }
00050 }
00051 
00052 void Backup::Close()
00053 {
00054         if( m_tar.get() ) try {
00055                 m_tar->Close();
00056                 m_tar.reset();
00057         }
00058         catch( reuse::TarFile::TarError &te ) {
00059                 throw Barry::BackupError(te.what());
00060         }
00061 }
00062 
00063 
00064 //////////////////////////////////////////////////////////////////////////////
00065 // Barry::Parser overrides
00066 
00067 void Backup::ParseRecord(const Barry::DBData &data,
00068                           const Barry::IConverter *ic)
00069 {
00070         m_current_dbname = data.GetDBName();
00071 
00072         std::ostringstream oss;
00073         oss << std::hex << data.GetUniqueId()
00074                 << " " << (unsigned int)data.GetRecType();
00075         m_tar_id_text = oss.str();
00076 
00077         if( m_current_dbname.size() == 0 )
00078                 throw Barry::BackupError("Backup: No database name available");
00079         if( m_tar_id_text.size() == 0 )
00080                 throw Barry::BackupError("Backup: No unique ID available!");
00081 
00082         m_record_data.assign(
00083                 (const char*)data.GetData().GetData() + data.GetOffset(),
00084                 data.GetData().GetSize() - data.GetOffset());
00085 
00086         // save to tarball
00087         std::string tarname = m_current_dbname + "/" + m_tar_id_text;
00088         m_tar->AppendFile(tarname.c_str(), m_record_data);
00089 }
00090 
00091 } // namespace Barry
00092 

Generated on Tue Mar 1 17:50:14 2011 for Barry by  doxygen 1.5.6