3 /// Builder class for restoring from Barry Backup files
7 Copyright (C) 2010-2012, 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.
37 int CountFiles(reuse::TarFile
&tar
,
38 const Barry::Restore::DBListType
&restoreList
,
39 Barry::Restore::DBListType
*available
,
43 std::string name
, last_name
;
46 while( tar
.ReadNextFilenameOnly(name
) ) {
47 std::string::size_type pos
= name
.rfind('/');
48 if( pos
== std::string::npos
)
50 std::string dbname
= name
.substr(0, pos
);
52 if( dbname
!= last_name
) {
54 good
= (default_all_db
&& restoreList
.size() == 0) ||
55 restoreList
.IsSelected(dbname
);
57 if( good
&& available
)
58 available
->push_back(dbname
);
68 //////////////////////////////////////////////////////////////////////////////
69 // Static Restore members
71 /// Splits a tarpath of the form "DBName/DBID" into separate string values.
72 /// Returns true if successful, false if tarpath is a bad name.
73 bool Restore::SplitTarPath(const std::string
&tarpath
,
75 std::string
&dbid_text
,
79 std::string::size_type pos
= tarpath
.rfind('/');
80 if( pos
== std::string::npos
)
81 return false; // bad name
83 dbname
= tarpath
.substr(0, pos
);
84 dbid_text
= tarpath
.substr(pos
+ 1);
85 if( dbname
.size() == 0 || dbid_text
.size() == 0 )
86 return false; // bad name
88 std::istringstream
iss(dbid_text
);
90 iss
>> std::hex
>> dbid
>> temp
;
91 dbrectype
= (uint8_t) temp
;
97 //////////////////////////////////////////////////////////////////////////////
98 // Restore - constructors
100 Restore::Restore(const std::string
&tarpath
, bool default_all_db
)
102 , m_default_all_db(default_all_db
)
103 , m_tar_record_state(RS_EMPTY
)
108 m_tar
.reset( new reuse::TarFile(tarpath
.c_str(), false,
109 &reuse::gztar_ops_nonthread
, true) );
111 catch( reuse::TarFile::TarError
&te
) {
112 throw Barry::RestoreError(te
.what());
121 //////////////////////////////////////////////////////////////////////////////
122 // Restore - Protected helpers
124 bool Restore::IsSelected(const std::string
&dbName
) const
126 // if in skip list, always return false,
127 // if nothing is in the main list, use default
128 // otherwise, only return true if specifically selected
129 if( m_dbSkipList
.IsSelected(dbName
) )
131 else if( m_dbList
.size() == 0 )
132 return m_default_all_db
;
134 return m_dbList
.IsSelected(dbName
);
138 //////////////////////////////////////////////////////////////////////////////
139 // Restore - Public API
141 void Restore::AddDB(const std::string
&dbName
)
143 if( find(m_dbList
.begin(), m_dbList
.end(), dbName
) == m_dbList
.end() ) {
144 // only add it if it is not already in the list
145 m_dbList
.push_back(dbName
);
149 void Restore::Add(const DBListType
&dbList
)
151 for( DBListType::const_iterator i
= dbList
.begin();
159 void Restore::Add(const DatabaseDatabase
&dbdb
)
161 for( DatabaseDatabase::DatabaseArrayType::const_iterator i
= dbdb
.Databases
.begin();
162 i
!= dbdb
.Databases
.end();
169 void Restore::AddSkipDB(const std::string
&dbName
)
171 if( find(m_dbSkipList
.begin(), m_dbSkipList
.end(), dbName
) == m_dbSkipList
.end() ) {
172 // only add it if it is not already in the list
173 m_dbSkipList
.push_back(dbName
);
177 void Restore::SkipCurrentDB()
179 // skip all records until next DB
181 Restore::RetrievalState state
;
182 while( (state
= Retrieve(m_record_data
)) == RS_NEXT
) {
183 std::cerr
<< "Skipping: "
184 << m_current_dbname
<< "/"
185 << m_tar_id_text
<< std::endl
;
186 m_tar_record_state
= RS_EMPTY
;
189 if( state
== RS_DBEND
) {
190 // process the end of database, so that user is free
191 // to call GetNextMeta() or Retrieve() or BuildRecord()
192 m_tar_record_state
= RS_NEXT
;
195 catch( reuse::TarFile::TarError
& ) {
196 m_tar_record_state
= RS_EOF
;
200 unsigned int Restore::GetRecordTotal() const
202 return GetRecordTotal(m_tarpath
, m_dbList
, m_default_all_db
);
205 unsigned int Restore::GetRecordTotal(const std::string
&tarpath
,
206 const DBListType
&dbList
,
209 unsigned int count
= 0;
211 std::auto_ptr
<reuse::TarFile
> tar
;
214 // do a scan through the tar file
215 tar
.reset( new reuse::TarFile(tarpath
.c_str(), false,
216 &reuse::gztar_ops_nonthread
, true) );
217 count
= CountFiles(*tar
, dbList
, 0, default_all_db
);
219 catch( reuse::TarFile::TarError
&te
) {
220 throw Barry::RestoreError(te
.what());
225 Barry::Restore::DBListType
Restore::GetDBList() const
227 return GetDBList(m_tarpath
);
230 Barry::Restore::DBListType
Restore::GetDBList(const std::string
&tarpath
)
232 std::auto_ptr
<reuse::TarFile
> tar
;
233 DBListType available
, empty
;
236 // do a scan through the tar file
237 tar
.reset( new reuse::TarFile(tarpath
.c_str(), false,
238 &reuse::gztar_ops_nonthread
, true) );
239 CountFiles(*tar
, empty
, &available
, true);
242 catch( reuse::TarFile::TarError
&te
) {
243 throw Barry::RestoreError(te
.what());
247 bool Restore::GetNextMeta(DBData
&data
)
249 // always use m_record_data here, so that we don't lose access
250 // to the actual record data for future calls to BuildRecord()
252 if( m_tar_record_state
== RS_EMPTY
) {
253 Retrieve(m_record_data
);
256 // fill in the meta data that will be returned in the next call
257 // to BuildRecord() or FetchRecord()... this is only valid if
258 // the state is RS_NEXT
259 switch( m_tar_record_state
)
262 data
.SetVersion(Barry::DBData::REC_VERSION_1
);
263 data
.SetDBName(m_current_dbname
);
264 data
.SetIds(m_rec_type
, m_unique_id
);
274 //////////////////////////////////////////////////////////////////////////////
275 // Barry::Builder overrides
277 Restore::RetrievalState
Restore::Retrieve(Data
&record_data
)
279 // don't do anything unless we're empty
280 if( m_tar_record_state
!= RS_EMPTY
)
281 return m_tar_record_state
;
283 // search for a valid record
285 // load record data from tar file
286 std::string filename
;
287 if( !m_tar
->ReadNextFile(filename
, record_data
) ) {
288 // assume end of file
289 return m_tar_record_state
= RS_EOF
;
291 m_tar_record_state
= RS_UNKNOWN
;
293 // split record filename into dbname and ID
295 if( !SplitTarPath(filename
, dbname
, m_tar_id_text
, m_rec_type
, m_unique_id
) ) {
296 // invalid filename, skip it
297 std::cerr
<< "Skipping invalid tar record: " << filename
<< std::endl
;
301 // are we working on the same dbname as last time?
303 if( m_current_dbname
== dbname
) {
304 return m_tar_record_state
= RS_NEXT
;
307 // DIFFERENT DBNAME from here on down!
308 m_tar_record_state
= RS_DBEND
;
310 // does the filter allow this record?
311 // if not, skip it and continue looking
312 if( !IsSelected(dbname
) ) {
316 // all checks pass, load the new dbname, and return DBEND
317 // if we are on a dbname boundary
318 if( m_current_dbname
.size() == 0 ) {
319 // this is the first time through Retrieve, so ok
320 m_tar_record_state
= RS_NEXT
;
323 m_current_dbname
= dbname
;
324 return m_tar_record_state
;
328 bool Restore::BuildRecord(Barry::DBData
&data
,
330 const Barry::IConverter
*ic
)
332 // in this case, we are loading into m_record_data anyway,
333 // so no special handling is needed, like FetchRecord() needs.
334 switch( Retrieve(m_record_data
) )
338 data
.SetVersion(Barry::DBData::REC_VERSION_1
);
339 data
.SetDBName(m_current_dbname
);
340 data
.SetIds(m_rec_type
, m_unique_id
);
341 data
.SetOffset(offset
);
343 int packet_size
= offset
+ m_record_data
.GetSize();
344 unsigned char *buf
= data
.UseData().GetBuffer(packet_size
);
345 memcpy(buf
+ offset
, m_record_data
.GetData(), m_record_data
.GetSize());
346 offset
+= m_record_data
.GetSize();
347 data
.UseData().ReleaseBuffer(packet_size
);
349 // clear loaded flag, as it has now been used
350 m_tar_record_state
= RS_EMPTY
;
357 throw std::logic_error("Invalid state in Restore::BuildRecord()");
360 // process the end of database by returning false
361 // the next round will be valid, so set to RS_NEXT
362 m_tar_record_state
= RS_NEXT
;
366 // always return false at end of file
371 bool Restore::FetchRecord(Barry::DBData
&data
, const Barry::IConverter
*ic
)
373 // if the record has not yet been loaded, we can optimize
374 // the buffer, and pass in our own... otherwise, just copy
375 // the current buffer from m_record_data
377 // it is assumed here that Builder users will not alternate
378 // between calls to BuildRecord() and FetchRecord()
380 if( m_tar_record_state
== RS_EMPTY
) {
381 // BUT, if RS_DBEND is the next value, then we need
382 // to save the data for the next round... this
383 // optimization is almost more bother than it's worth :-)
384 if( Retrieve(data
.UseData()) == RS_DBEND
) {
385 m_record_data
= data
.GetData();
386 m_tar_record_state
= RS_NEXT
;
391 data
.UseData() = m_record_data
;
394 switch( m_tar_record_state
)
397 data
.SetVersion(Barry::DBData::REC_VERSION_1
);
398 data
.SetDBName(m_current_dbname
);
399 data
.SetIds(m_rec_type
, m_unique_id
);
402 // clear loaded flag, as it has now been used
403 m_tar_record_state
= RS_EMPTY
;
409 throw std::logic_error("Invalid state in Restore::FetchRecord()");
412 // process the end of database by returning false
413 // the next round will be valid, so set to RS_NEXT
414 m_tar_record_state
= RS_NEXT
;
418 // always return false at end of file
423 bool Restore::EndOfFile() const
425 return m_tar_record_state
== RS_EOF
;