lib: fixed timeout mismatch behaviour between USB and Router
[barry.git] / src / backup.cc
blob0b8ea4e3917240527fd6f4a3667f32e7e14a6c32
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 std::cout << "Closing Backup parser" << std::endl;
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());
65 //////////////////////////////////////////////////////////////////////////////
66 // Barry::Parser overrides
68 void Backup::Clear()
72 void Backup::SetIds(const std::string &DbName,
73 uint8_t RecType,
74 uint32_t UniqueId)
76 m_current_dbname = DbName;
78 std::ostringstream oss;
79 oss << std::hex << UniqueId << " " << (unsigned int)RecType;
80 m_tar_id_text = oss.str();
82 if( m_current_dbname.size() == 0 )
83 throw Barry::BackupError("Backup: No database name available");
84 if( m_tar_id_text.size() == 0 )
85 throw Barry::BackupError("Backup: No unique ID available!");
88 void Backup::ParseHeader(const Barry::Data &data, size_t &offset)
92 void Backup::ParseFields(const Barry::Data &data,
93 size_t &offset,
94 const Barry::IConverter *ic)
96 m_record_data.assign((const char*)data.GetData() + offset, data.GetSize() - offset);
99 void Backup::Store()
101 std::string tarname = m_current_dbname + "/" + m_tar_id_text;
102 std::cout << "Saving: " << tarname << std::endl;
103 m_tar->AppendFile(tarname.c_str(), m_record_data);
106 } // namespace Barry