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-2012, 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 void Backup::ClearStats()
00064 {
00065         m_stats.clear();
00066 }
00067 
00068 
00069 //////////////////////////////////////////////////////////////////////////////
00070 // Barry::Parser overrides
00071 
00072 void Backup::ParseRecord(const Barry::DBData &data,
00073                           const Barry::IConverter *ic)
00074 {
00075         m_current_dbname = data.GetDBName();
00076 
00077         std::ostringstream oss;
00078         oss << std::hex << data.GetUniqueId()
00079                 << " " << (unsigned int)data.GetRecType();
00080         m_tar_id_text = oss.str();
00081 
00082         if( m_current_dbname.size() == 0 )
00083                 throw Barry::BackupError("Backup: No database name available");
00084         if( m_tar_id_text.size() == 0 )
00085                 throw Barry::BackupError("Backup: No unique ID available!");
00086 
00087         m_record_data.assign(
00088                 (const char*)data.GetData().GetData() + data.GetOffset(),
00089                 data.GetData().GetSize() - data.GetOffset());
00090 
00091         // save to tarball
00092         std::string tarname = m_current_dbname + "/" + m_tar_id_text;
00093         m_tar->AppendFile(tarname.c_str(), m_record_data);
00094 
00095         // add stats
00096         m_stats[m_current_dbname]++;
00097 }
00098 
00099 } // namespace Barry
00100