menu: added new Keywords tag to .desktop files
[barry.git] / src / backup.cc
blobdb60b03b9424f1ec36642b77cc4397e2713cd68b
1 ///
2 /// \file backup.cc
3 /// Special parser class to support creation of Barry Backup files
4 ///
6 /*
7 Copyright (C) 2010-2013, 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 "i18n.h"
23 #include "backup.h"
24 #include "tarfile.h"
25 #include "error.h"
26 #include <sstream>
27 #include <iomanip>
28 #include <iostream>
30 namespace Barry {
32 Backup::Backup(const std::string &tarpath)
34 try {
35 m_tar.reset( new reuse::TarFile(tarpath.c_str(), true,
36 &reuse::gztar_ops_nonthread, true) );
38 catch( reuse::TarFile::TarError &te ) {
39 throw Barry::BackupError(te.what());
43 Backup::~Backup()
45 try {
46 Close();
48 catch( Barry::BackupError & ) {
49 // throw it away
53 void Backup::Close()
55 if( m_tar.get() ) try {
56 m_tar->Close();
57 m_tar.reset();
59 catch( reuse::TarFile::TarError &te ) {
60 throw Barry::BackupError(te.what());
64 void Backup::ClearStats()
66 m_stats.clear();
70 //////////////////////////////////////////////////////////////////////////////
71 // Barry::Parser overrides
73 void Backup::ParseRecord(const Barry::DBData &data,
74 const Barry::IConverter *ic)
76 m_current_dbname = data.GetDBName();
78 std::ostringstream oss;
79 oss << std::hex << data.GetUniqueId()
80 << " " << (unsigned int)data.GetRecType();
81 m_tar_id_text = oss.str();
83 if( m_current_dbname.size() == 0 )
84 throw Barry::BackupError(_("Backup: No database name available"));
85 if( m_tar_id_text.size() == 0 )
86 throw Barry::BackupError(_("Backup: No unique ID available!"));
88 m_record_data.assign(
89 (const char*)data.GetData().GetData() + data.GetOffset(),
90 data.GetData().GetSize() - data.GetOffset());
92 // save to tarball
93 std::string tarname = m_current_dbname + "/" + m_tar_id_text;
94 m_tar->AppendFile(tarname.c_str(), m_record_data);
96 // add stats
97 m_stats[m_current_dbname]++;
100 } // namespace Barry