lib: cleaned up Clear() functions in all record classes
[barry.git] / src / backup.cc
blob5548503a51e01960187f1f88651cb5329339ed5f
1 ///
2 /// \file backup.cc
3 /// Special parser class to support creation of Barry Backup files
4 ///
6 /*
7 Copyright (C) 2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "backup.h"
23 #include "tarfile.h"
24 #include "error.h"
25 #include <sstream>
26 #include <iomanip>
27 #include <iostream>
29 namespace Barry {
31 Backup::Backup(const std::string &tarpath)
33 try {
34 m_tar.reset( new reuse::TarFile(tarpath.c_str(), true,
35 &reuse::gztar_ops_nonthread, true) );
37 catch( reuse::TarFile::TarError &te ) {
38 throw Barry::BackupError(te.what());
42 Backup::~Backup()
44 try {
45 Close();
47 catch( Barry::BackupError & ) {
48 // throw it away
52 void Backup::Close()
54 if( m_tar.get() ) try {
55 m_tar->Close();
56 m_tar.reset();
58 catch( reuse::TarFile::TarError &te ) {
59 throw Barry::BackupError(te.what());
64 //////////////////////////////////////////////////////////////////////////////
65 // Barry::Parser overrides
67 void Backup::ParseRecord(const Barry::DBData &data,
68 const Barry::IConverter *ic)
70 m_current_dbname = data.GetDBName();
72 std::ostringstream oss;
73 oss << std::hex << data.GetUniqueId()
74 << " " << (unsigned int)data.GetRecType();
75 m_tar_id_text = oss.str();
77 if( m_current_dbname.size() == 0 )
78 throw Barry::BackupError("Backup: No database name available");
79 if( m_tar_id_text.size() == 0 )
80 throw Barry::BackupError("Backup: No unique ID available!");
82 m_record_data.assign(
83 (const char*)data.GetData().GetData() + data.GetOffset(),
84 data.GetData().GetSize() - data.GetOffset());
86 // save to tarball
87 std::string tarname = m_current_dbname + "/" + m_tar_id_text;
88 m_tar->AppendFile(tarname.c_str(), m_record_data);
91 } // namespace Barry